Skip to content

Commit

Permalink
Override storage settings with information from the profile
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed May 12, 2017
1 parent aef552d commit ce1ad92
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 32 deletions.
6 changes: 5 additions & 1 deletion src/clients/inst_autosetup.rb
Expand Up @@ -272,7 +272,11 @@ def main
if Profile.current["partitioning_advanced"] && !Profile.current["partitioning_advanced"].empty?
write_storage = AutoinstStorage.ImportAdvanced(Profile.current["partitioning_advanced"])
else
write_storage = AutoinstStorage.Import(Profile.current["partitioning"])
storage_settings = {
"partitioning" => Profile.current.fetch("partitioning", []),
"storage" => Profile.current.fetch("general", {}).fetch("storage", {})
}
write_storage = AutoinstStorage.Import(storage_settings)
end

semiauto_partitions = general_section["semi-automatic"] &&
Expand Down
68 changes: 37 additions & 31 deletions src/modules/AutoinstStorage.rb
Expand Up @@ -28,7 +28,7 @@ def main
# All shared data are in yast2.rpm to break cyclic dependencies
Yast.import "AutoinstData"

# Read existing fstab and format partitions, but dont create anything
# Read existing fstab and flords of blackormat partitions, but dont create anything
# Use same mountpoints etc.
@read_fstab = false
@ZeroNewPartitions = true
Expand Down Expand Up @@ -126,41 +126,28 @@ def find_next_disk( tm, after, ctype )
end

# Get all the configuration from a map.
#
# When called by inst_auto<module name> (preparing autoinstallation data)
# the list may be empty.
# @param [Array<Hash>] settings a list [...]
#
# @param settings [Hash] Profile settings
# @option settings [Hash] "storage" Settings to override the control.xml
# @option settings [Array<Hash>] "partitioning" List of drives (custom partitioning)
# @return [Boolean] success
def Import(settings)
settings = deep_copy(settings)
Builtins.y2milestone("entering Import with %1", settings)

if !settings || settings.empty? || settings["proposal"]
# Storage proposal will be taken for
set = Y2Storage::ProposalSettings.new
if settings.is_a?(Hash) && settings["proposal"]
p = settings["proposal"]
set.use_lvm = p["use_lvm"] if p["use_lvm"]
set.root_filesystem_type = p["root_filesystem_type"] if p["root_filesystem_type"]
set.use_snapshots = p["use_snapshots"] if p["use_snapshots"]
set.use_separate_home = p["use_separate_home"] if p["use_separate_home"]
set.home_filesystem_type = p["home_filesystem_type"] if p["home_filesystem_type"]
set.enlarge_swap_for_suspend = p["enlarge_swap_for_suspend"] if p["enlarge_swap_for_suspend"]
set.root_device = p["root_device"] if p["root_device"]
set.candidate_devices = p["candidate_devices"] if p["candidate_devices"]
set.encryption_password = p["encryption_password"] if p["encryption_password"]
end
log.info "Calling storage proposal with #{set}"
proposal = Y2Storage::Proposal.new(settings: set)
proposal.propose
if proposal.proposed?
# Save to storage manager
log.info "Storing accepted proposal"
Y2Storage::StorageManager.instance.proposal = proposal
return true
end
return false
log.info "entering Import with #{settings.inspect}"

# Overlay storage configuration
storage_settings = settings.fetch("storage", nil)
Yast::ProductFeatures.SetOverlay("partitioning" => storage_settings) if storage_settings

# Initialize proposal if needed
if settings.fetch("partitioning", []).empty?
initialize_proposal
else
# TODO: AutoYaST customized partitioning
false
end
false
end

# Import Fstab data
Expand Down Expand Up @@ -359,6 +346,25 @@ def build_id(disk_dev_name, nr)
publish :function => :ImportAdvanced, :type => "boolean (map)"
publish :function => :Summary, :type => "string ()"
publish :function => :Write, :type => "boolean ()"

private

# Initialize partition proposal
#
# @return [Boolean] true if proposal was successfully created; false otherwise.
def initialize_proposal
log.info "Initializing a proposal"
proposal_settings = Y2Storage::ProposalSettings.new_for_current_product
proposal = Y2Storage::Proposal.new(settings: proposal_settings)
proposal.propose
if proposal.proposed?
log.info "Storing accepted proposal"
Y2Storage::StorageManager.instance.proposal = proposal
return true
end
false
end

end

AutoinstStorage = AutoinstStorageClass.new
Expand Down
1 change: 1 addition & 0 deletions test/Makefile.am
Expand Up @@ -11,6 +11,7 @@ TESTS = \
AutoinstGeneral_test.rb \
AutoinstPartPlan_test.rb \
AutoinstSoftware_test.rb \
autoinst_storage_test.rb \
profile_test.rb \
Y2ModuleConfig_test.rb \
include/autopart_test.rb \
Expand Down
89 changes: 89 additions & 0 deletions test/autoinst_storage_test.rb
@@ -0,0 +1,89 @@
#!/usr/bin/env rspec

require_relative "test_helper"
Yast.import "AutoinstStorage"

describe Yast::AutoinstStorage do
subject { Yast::AutoinstStorage }

describe "#Import" do
let(:proposal_settings) { double("proposal_settings") }
let(:proposal) { double("proposal", propose: nil, proposed?: true) }
let(:storage_manager) { double("storage_manager") }

before do
allow(Y2Storage::StorageManager).to receive(:instance)
.and_return(storage_manager)
allow(Y2Storage::Proposal).to receive(:new)
.and_return(proposal)
allow(storage_manager).to receive(:proposal=)
end

context "when storage settings are specified" do
let(:profile) do
{ "storage" => { "proposal_lvm" => true } }
end

it "overrides control file values" do
expect(Yast::ProductFeatures).to receive(:SetOverlay)
.with("partitioning" => profile["storage"])
subject.Import(profile)
end
end

context "when no partitioning plan is given" do
let(:profile) do
{ "storage" => { "proposal_lvm" => true } }
end

it "sets a proposal" do
expect(storage_manager).to receive(:proposal=).with(proposal)
subject.Import(profile)
end

it "returns true" do
expect(subject.Import(profile)).to eq(true)
end

context "if proposal fails" do
before do
allow(proposal).to receive(:proposed?).and_return(false)
end

it "does not set the proposal" do
expect(storage_manager).to_not receive(:proposal=)
subject.Import(profile)
end

it "returns false" do
expect(storage_manager).to_not receive(:proposal=)
subject.Import(profile)
end
end
end

context "when profile contains an empty set of partitions" do
let(:profile) do
{ "partitions" => [] }
end

it "sets a proposal" do
expect(storage_manager).to receive(:proposal=).with(proposal)
subject.Import(profile)
end
end

context "when a partition plan is given" do
let(:profile) { { "partitioning" => [{"device" => "/dev/sda"}] } }

it "does not build a proposal" do
expect(Y2Storage::Proposal).to_not receive(:new)
subject.Import(profile)
end

it "returns false" do
expect(subject.Import(profile)).to eq(false)
end
end
end
end

0 comments on commit ce1ad92

Please sign in to comment.