Skip to content

Commit

Permalink
Add a class to preprocess <partitioning/> section
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Nov 27, 2017
1 parent affc852 commit 0cf22bd
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/lib/autoinstall/partitioning_preprocessor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# encoding: utf-8

# Copyright (c) [2017] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "y2storage"
require "autoinstall/dialogs/disk_selector"

module Y2Autoinstallation
# This class is responsible for preprocessing the +<partitioning/>+ section
# of an AutoYaST profile.
#
# At this time, the only preprocessing is replacing +device=ask+ with a real
# device name.
class PartitioningPreprocessor
include Yast

# Preprocesses the partitioning section
#
# It returns a new object so the original section is not modified.
#
# @param original_drives [Array<Hash>] List of drives according to an AutoYaST
# +partitioning+ section.
def run(drives)
return if drives.nil?
replace_ask(deep_copy(drives))
end

protected

# Replaces +ask+ for user selected values
#
# When +device+ is set to +ask+, ask the user about which device to use.
#
# @param [Array<Hash>] Drives definition from an AutoYaST profile
# @return [Array<Hash>] Drives definition replacing +ask+ for user selected values
def replace_ask(drives)
blacklist = []
drives.each do |drive|
next unless drive["device"] == "ask"
selection = select_disk(blacklist)
return nil if selection == :abort
drive["device"] = selection
blacklist << drive["device"]
end
end

# Asks the user about which device should be used
#
# @param blacklist [Array<String>] List of device names that were already used
# @return [String,Symbol] Selected device name
def select_disk(blacklist)
Y2Autoinstallation::Dialogs::DiskSelector.new(blacklist: blacklist).run
end
end
end
63 changes: 63 additions & 0 deletions test/lib/partitioning_preprocessor_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env rspec
# encoding: utf-8

# Copyright (c) [2017] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../test_helper"
require "autoinstall/partitioning_preprocessor"

describe Y2Autoinstallation::PartitioningPreprocessor do
subject(:preprocessor) { described_class.new }

let(:profile) do
[sda, undefined]
end

let(:sda) { { "device" => "/dev/sda" } }
let(:selected) { "/dev/sdb" }
let(:undefined) { { "device" => "ask" } }

let(:disk_selector) do
instance_double(Y2Autoinstallation::Dialogs::DiskSelector, run: selected)
end

before do
allow(Y2Autoinstallation::Dialogs::DiskSelector).to receive(:new).and_return(disk_selector)
end

describe "#run" do
it "sets disk devices when device=ask" do
expect(preprocessor.run(profile)).to eq([sda, { "device" => selected }])
end

it "asks the user about which device to use" do
expect(disk_selector).to receive(:run).and_return(selected)
preprocessor.run(profile)
end

context "when the user aborts" do
let(:selected) { :abort }

it "returns nil" do
expect(preprocessor.run(profile)).to be_nil
end
end
end
end

0 comments on commit 0cf22bd

Please sign in to comment.