Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Sep 20, 2024
1 parent d3ee397 commit 11a6078
Showing 1 changed file with 143 additions and 69 deletions.
212 changes: 143 additions & 69 deletions service/test/agama/storage/config_conversions/from_json_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -439,91 +518,31 @@
{
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
let(:config_json) do
{
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
Expand Down Expand Up @@ -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
Expand All @@ -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" }
}
]
}
Expand Down Expand Up @@ -629,6 +679,14 @@
{
filesystem: { path: "/opt" },
size: ["1073741824", 3221225472]
},
{
filesystem: { path: "/data1" },
size: ["current"]
},
{
filesystem: { path: "/data2" },
size: ["10 GiB", "current"]
}
]
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

0 comments on commit 11a6078

Please sign in to comment.