From 0cf22bd93f8ee44f66ff6973103255d0008bc415 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Mon, 27 Nov 2017 14:49:53 +0000 Subject: [PATCH] Add a class to preprocess section --- .../autoinstall/partitioning_preprocessor.rb | 72 +++++++++++++++++++ test/lib/partitioning_preprocessor_test.rb | 63 ++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 src/lib/autoinstall/partitioning_preprocessor.rb create mode 100644 test/lib/partitioning_preprocessor_test.rb diff --git a/src/lib/autoinstall/partitioning_preprocessor.rb b/src/lib/autoinstall/partitioning_preprocessor.rb new file mode 100644 index 000000000..577187e38 --- /dev/null +++ b/src/lib/autoinstall/partitioning_preprocessor.rb @@ -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 ++ 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] 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] Drives definition from an AutoYaST profile + # @return [Array] 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] 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 diff --git a/test/lib/partitioning_preprocessor_test.rb b/test/lib/partitioning_preprocessor_test.rb new file mode 100644 index 000000000..d6f222b15 --- /dev/null +++ b/test/lib/partitioning_preprocessor_test.rb @@ -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