diff --git a/service/test/agama/storage/config_conversions/from_json_test.rb b/service/test/agama/storage/config_conversions/from_json_test.rb index cfe4f508bc..b75af5aa5b 100644 --- a/service/test/agama/storage/config_conversions/from_json_test.rb +++ b/service/test/agama/storage/config_conversions/from_json_test.rb @@ -26,6 +26,9 @@ require "y2storage/filesystems/mount_by_type" require "y2storage/filesystems/type" require "y2storage/pbkd_function" +require "y2storage/refinements" + +using Y2Storage::Refinements::SizeCasts describe Agama::Storage::ConfigConversions::FromJSON do subject { described_class.new(config_json, product_config: product_config) } @@ -73,6 +76,82 @@ } end + shared_examples "omitting sizes" do |result| + let(:example_configs) do + [ + { filesystem: { path: "/", type: { btrfs: { snapshots: false } } } }, + { filesystem: { path: "/home" } }, + { filesystem: { path: "/opt" } }, + { filesystem: { path: "swap" } } + ] + end + + it "uses default sizes" do + config = subject.convert + devices = result.call(config) + expect(devices).to contain_exactly( + an_object_having_attributes( + filesystem: have_attributes(path: "/"), + size: have_attributes(default: true, min: be_nil, max: be_nil) + ), + an_object_having_attributes( + filesystem: have_attributes(path: "/home"), + size: have_attributes(default: true, min: be_nil, max: be_nil) + ), + an_object_having_attributes( + filesystem: have_attributes(path: "/opt"), + size: have_attributes(default: true, min: be_nil, max: be_nil) + ), + an_object_having_attributes( + filesystem: have_attributes(path: "swap"), + size: have_attributes(default: true, min: be_nil, max: be_nil) + ) + ) + end + end + + shared_examples "fixed sizes" do |result| + let(:example_configs) do + [ + { filesystem: { path: "/" }, size: "10 GiB" }, + { filesystem: { path: "/home" }, size: "6Gb" }, + { filesystem: { path: "/opt" }, size: 3221225472 }, + { filesystem: { path: "swap" }, size: "6 Gib" } + ] + end + + it "sets both min and max to the same value if a string is used" do + config = subject.convert + devices = result.call(config) + expect(devices).to include( + an_object_having_attributes( + filesystem: have_attributes(path: "/"), + size: have_attributes(default: false, min: 10.GiB, max: 10.GiB) + ) + ) + end + + it "sets both min and max to the same value if an integer is used" do + config = subject.convert + devices = result.call(config) + expect(devices).to include( + an_object_having_attributes( + filesystem: have_attributes(path: "/opt"), + size: have_attributes(default: false, min: 3.GiB, max: 3.GiB) + ) + ) + end + + it "makes a difference between SI units and binary units" do + config = subject.convert + devices = result.call(config) + home_size = devices.find { |d| d.filesystem.path == "/home" }.size + swap_size = devices.find { |d| d.filesystem.path == "swap" }.size + expect(swap_size.min.to_i).to eq 6 * 1024 * 1024 * 1024 + expect(home_size.max.to_i).to eq 6 * 1000 * 1000 * 1000 + end + end + before do # Speed up tests by avoding real check of TPM presence. allow(Y2Storage::EncryptionMethod::TPM_FDE).to receive(:possible?).and_return(true) @@ -439,43 +518,15 @@ { drives: [ { - partitions: [ - { filesystem: { path: "/", type: { btrfs: { snapshots: false } } } }, - { filesystem: { path: "/home" } }, - { filesystem: { path: "/opt" } }, - { filesystem: { path: "swap" } } - ] + partitions: example_configs } ] } end - it "uses default sizes" do - config = subject.convert - partitions = config.drives.first.partitions - expect(partitions).to contain_exactly( - an_object_having_attributes( - filesystem: have_attributes(path: "/"), - size: have_attributes(default: true, min: 5.GiB, max: 10.GiB) - ), - an_object_having_attributes( - filesystem: have_attributes(path: "/home"), - size: have_attributes(default: true, min: 5.GiB, - max: Y2Storage::DiskSize.unlimited) - ), - an_object_having_attributes( - filesystem: have_attributes(path: "/opt"), - size: have_attributes(default: true, min: 100.MiB, - max: Y2Storage::DiskSize.unlimited) - ), - an_object_having_attributes( - filesystem: have_attributes(path: "swap"), - size: have_attributes( - default: true, min: Y2Storage::DiskSize.zero, max: Y2Storage::DiskSize.unlimited - ) - ) - ) - end + result = proc { |config| config.drives.first.partitions } + + include_examples "omitting sizes", result end context "setting fixed sizes for the partitions" do @@ -483,47 +534,15 @@ { drives: [ { - partitions: [ - { filesystem: { path: "/" }, size: "10 GiB" }, - { filesystem: { path: "/home" }, size: "6Gb" }, - { filesystem: { path: "/opt" }, size: 3221225472 }, - { filesystem: { path: "swap" }, size: "6 Gib" } - ] + partitions: example_configs } ] } end - it "sets both min and max to the same value if a string is used" do - config = subject.convert - partitions = config.drives.first.partitions - expect(partitions).to include( - an_object_having_attributes( - filesystem: have_attributes(path: "/"), - size: have_attributes(default: false, min: 10.GiB, max: 10.GiB) - ) - ) - end - - it "sets both min and max to the same value if an integer is used" do - config = subject.convert - partitions = config.drives.first.partitions - expect(partitions).to include( - an_object_having_attributes( - filesystem: have_attributes(path: "/opt"), - size: have_attributes(default: false, min: 3.GiB, max: 3.GiB) - ) - ) - end + result = proc { |config| config.drives.first.partitions } - it "makes a difference between SI units and binary units" do - config = subject.convert - partitions = config.drives.first.partitions - home_size = partitions.find { |p| p.filesystem.path == "/home" }.size - swap_size = partitions.find { |p| p.filesystem.path == "swap" }.size - expect(swap_size.min.to_i).to eq 6 * 1024 * 1024 * 1024 - expect(home_size.max.to_i).to eq 6 * 1000 * 1000 * 1000 - end + include_examples "fixed sizes", result end # Note the min is mandatory @@ -575,6 +594,29 @@ ) ) end + + it "uses nil for min size as current" do + config = subject.convert + partitions = config.drives.first.partitions + expect(partitions).to include( + an_object_having_attributes( + filesystem: have_attributes(path: "/data1"), + size: have_attributes(default: false, min: be_nil, + max: Y2Storage::DiskSize.unlimited) + ) + ) + end + + it "uses nil for max size as current" do + config = subject.convert + partitions = config.drives.first.partitions + expect(partitions).to include( + an_object_having_attributes( + filesystem: have_attributes(path: "/data2"), + size: have_attributes(default: false, min: 10.GiB, max: be_nil) + ) + ) + end end context "using a hash" do @@ -598,6 +640,14 @@ { filesystem: { path: "/opt" }, size: { min: "1073741824", max: 3221225472 } + }, + { + filesystem: { path: "/data1" }, + size: { min: "current" } + }, + { + filesystem: { path: "/data2" }, + size: { min: "10 GiB", max: "current" } } ] } @@ -629,6 +679,14 @@ { filesystem: { path: "/opt" }, size: ["1073741824", 3221225472] + }, + { + filesystem: { path: "/data1" }, + size: ["current"] + }, + { + filesystem: { path: "/data2" }, + size: ["10 GiB", "current"] } ] } @@ -958,8 +1016,8 @@ ), size: have_attributes( default: true, - min: 40.GiB, - max: Y2Storage::DiskSize.unlimited + min: be_nil, + max: be_nil ), stripes: be_nil, stripe_size: be_nil, @@ -1004,5 +1062,21 @@ ) end end + + context "omitting sizes for the logical volumes" do + let(:config_json) do + { + volumeGroups: [ + { + logicalVolumes: example_configs + } + ] + } + end + + result = proc { |config| config.volume_groups.first.logical_volumes } + + include_examples "omitting sizes", result + end end end