From a616f6d8bd06586527a1ae0bddc0f71797586f67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Iv=C3=A1n=20L=C3=B3pez=20Gonz=C3=A1lez?= Date: Mon, 2 Dec 2024 07:34:50 +0000 Subject: [PATCH] wip --- .../from_model_conversions/with_partitions.rb | 30 +++-- .../config_conversions/from_model_test.rb | 123 +++++++++++++++++- 2 files changed, 137 insertions(+), 16 deletions(-) diff --git a/service/lib/agama/storage/config_conversions/from_model_conversions/with_partitions.rb b/service/lib/agama/storage/config_conversions/from_model_conversions/with_partitions.rb index 01fd890da2..6108e9a81b 100644 --- a/service/lib/agama/storage/config_conversions/from_model_conversions/with_partitions.rb +++ b/service/lib/agama/storage/config_conversions/from_model_conversions/with_partitions.rb @@ -67,26 +67,32 @@ def partitions # @return [Array] def used_partitions - new_partitions + reused_partitions + partitions.select { |p| used_partition?(p) } + end + + def used_partition?(partition_model) + new_partition?(partition_model) || reused_partition?(partition_model) end # @return [Array] - def new_partitions - partitions - .reject { |p| p[:name] } - .reject { |p| p[:delete] } - .reject { |p| p[:deleteIfNeeded] } + def new_partition?(partition_model) + partition_model[:name].nil? && + !partition_model[:delete] && + !partition_model[:deleteIfNeeded] end # TODO: improve check by ensuring the alias is referenced. # # @return [Array] - def reused_partitions - partitions - .select { |p| p[:name] } - .reject { |p| p[:delete] } - .reject { |p| p[:deleteIfNeeded] } - .select { |p| p[:mountPath] || p[:filesystem] || p[:alias] } + def reused_partition?(partition_model) + any_usage = partition_model[:mountPath] || + partition_model[:filesystem] || + partition_model[:alias] + + any_usage && + partition_model[:name] && + !partition_model[:delete] && + !partition_model[:deleteIfNeeded] end # @return [Configs::Partition] diff --git a/service/test/agama/storage/config_conversions/from_model_test.rb b/service/test/agama/storage/config_conversions/from_model_test.rb index 47969f79ec..31ec4226e5 100644 --- a/service/test/agama/storage/config_conversions/from_model_test.rb +++ b/service/test/agama/storage/config_conversions/from_model_test.rb @@ -648,15 +648,130 @@ end shared_examples "with spacePolicy and partitions" do |config_proc| - let(:partitions) { [] } + let(:partitions) do + [ + { + name: "/dev/vda1", + mountPath: "/data" + }, + { + name: "/dev/vda2", + mountPath: "swap", + filesystem: { type: "swap" } + }, + { + name: "/dev/vda3", + mountPath: "/home", + size: { default: false, min: 1.GiB.to_i, max: 10.GiB.to_i } + }, + { + name: "/dev/vda4", + resizeIfNeeded: true + }, + { + name: "/dev/vda5", + deleteIfNeeded: true + }, + { + name: "/dev/vda6", + size: { default: false, min: 5.GiB } + }, + { + name: "/dev/vda7", + delete: true + }, + { + mountPath: "/", + filesystem: { type: "btrfs" } + } + ] + end - xcontext "if space policy is 'keep'" do + context "if space policy is 'keep'" do let(:spacePolicy) { "keep" } it "sets #partitions to the expected value" do config = config_proc.call(subject.convert) partitions = config.partitions - expect(partitions).to be_empty + expect(partitions.size).to eq(4) + expect(partitions[0].search.name).to eq("/dev/vda1") + expect(partitions[1].search.name).to eq("/dev/vda2") + expect(partitions[2].search.name).to eq("/dev/vda3") + expect(partitions[2].size.default?).to eq(false) + expect(partitions[2].size.min).to eq(1.GiB) + expect(partitions[2].size.max).to eq(10.GiB) + expect(partitions[3].filesystem.path).to eq("/") + end + end + + context "if space policy is 'delete'" do + let(:spacePolicy) { "delete" } + + it "sets #partitions to the expected value" do + config = config_proc.call(subject.convert) + partitions = config.partitions + expect(partitions.size).to eq(5) + expect(partitions[0].search.name).to eq("/dev/vda1") + expect(partitions[1].search.name).to eq("/dev/vda2") + expect(partitions[2].search.name).to eq("/dev/vda3") + expect(partitions[2].size.default?).to eq(false) + expect(partitions[2].size.min).to eq(1.GiB) + expect(partitions[2].size.max).to eq(10.GiB) + expect(partitions[3].filesystem.path).to eq("/") + expect(partitions[4].search.name).to be_nil + expect(partitions[4].search.max).to be_nil + expect(partitions[4].delete).to eq(true) + end + end + + context "if space policy is 'resize'" do + let(:spacePolicy) { "resize" } + + it "sets #partitions to the expected value" do + config = config_proc.call(subject.convert) + partitions = config.partitions + expect(partitions.size).to eq(5) + expect(partitions[0].search.name).to eq("/dev/vda1") + expect(partitions[1].search.name).to eq("/dev/vda2") + expect(partitions[2].search.name).to eq("/dev/vda3") + expect(partitions[2].size.default?).to eq(false) + expect(partitions[2].size.min).to eq(1.GiB) + expect(partitions[2].size.max).to eq(10.GiB) + expect(partitions[3].filesystem.path).to eq("/") + expect(partitions[4].search.name).to be_nil + expect(partitions[4].search.max).to be_nil + expect(partitions[4].size.default?).to eq(false) + expect(partitions[4].size.min).to eq(Y2Storage::DiskSize.zero) + expect(partitions[4].size.max).to be_nil + end + end + + context "if space policy is 'custom'" do + let(:spacePolicy) { "custom" } + + it "sets #partitions to the expected value" do + config = config_proc.call(subject.convert) + partitions = config.partitions + expect(partitions.size).to eq(8) + expect(partitions[0].search.name).to eq("/dev/vda1") + expect(partitions[1].search.name).to eq("/dev/vda2") + expect(partitions[2].search.name).to eq("/dev/vda3") + expect(partitions[2].size.default?).to eq(false) + expect(partitions[2].size.min).to eq(1.GiB) + expect(partitions[2].size.max).to eq(10.GiB) + expect(partitions[3].search.name).to eq("/dev/vda4") + expect(partitions[3].size.default?).to eq(false) + expect(partitions[3].size.min).to eq(Y2Storage::DiskSize.zero) + expect(partitions[3].size.max).to be_nil + expect(partitions[4].search.name).to eq("/dev/vda5") + expect(partitions[4].delete_if_needed?).to eq(true) + expect(partitions[5].search.name).to eq("/dev/vda6") + expect(partitions[5].size.default?).to eq(false) + expect(partitions[5].size.min).to eq(5.GiB) + expect(partitions[5].size.max).to eq(Y2Storage::DiskSize.unlimited) + expect(partitions[6].search.name).to eq("/dev/vda7") + expect(partitions[6].delete?).to eq(true) + expect(partitions[7].filesystem.path).to eq("/") end end end @@ -801,7 +916,7 @@ end context "if a drive specifies both 'spacePolicy' and 'partitions'" do - let(:drive) { { partitions: partitions } } + let(:drive) { { spacePolicy: spacePolicy, partitions: partitions } } include_examples "with spacePolicy and partitions", drive_proc end end