Skip to content

Commit

Permalink
Fix conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Aug 17, 2023
1 parent 02fa693 commit 5db39de
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ def target_vg_conversion(target, value)
# @param target [Agama::Storage::Volume]
# @param value [String]
def fs_type_conversion(target, value)
fs_type = Y2Storage::Filesystems::Type.all.find { |t| t.to_human_string == value }
fs_type = target.outline.filesystems.find { |t| t.to_human_string == value }
return unless fs_type

target.fs_type = fs_type
end

Expand All @@ -123,12 +125,16 @@ def max_size_conversion(target, value)
# @param target [Agama::Storage::Volume]
# @param value [Boolean]
def auto_size_conversion(target, value)
return unless target.auto_size_supported?

target.auto_size = value
end

# @param target [Agama::Storage::Volume]
# @param value [Booelan]
def snapshots_conversion(target, value)
return unless target.outline.snapshots_configurable?

target.btrfs.snapshots = value
end
end
Expand Down
2 changes: 1 addition & 1 deletion service/lib/agama/storage/proposal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def proposal
# @return [Y2Storage::GuidedProposal]
def calculate_proposal(settings)
proposal = Y2Storage::MinGuidedProposal.new(
settings: ProposalSettingsConversion.to_y2storage(settings),
settings: ProposalSettingsConversion.to_y2storage(settings, config: config),
devicegraph: probed_devicegraph,
disk_analyzer: disk_analyzer
)
Expand Down
6 changes: 4 additions & 2 deletions service/lib/agama/storage/proposal_settings_conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ def self.from_y2storage(settings, config:)
# Performs conversion to Y2Storage format.
#
# @param settings [Agama::Storage::ProposalSettings]
# @param config [Agama::Config]
#
# @return [Y2Storage::ProposalSettings]
def self.to_y2storage(settings)
ToY2Storage.new(settings).convert
def self.to_y2storage(settings, config:)
ToY2Storage.new(settings, config: config).convert
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def space_policy_conversion(target)

# @param target [Agama::Storage::ProposalSettings]
def volumes_conversion(target)
target.volumes = settings.volumes.map do |spec|
target.volumes = settings.volumes.select(&:proposed?).map do |spec|
VolumeConversion.from_y2storage(spec, config: config)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@

require "y2storage"
require "agama/storage/volume_conversion"
require "agama/storage/volume_templates_builder"

module Agama
module Storage
module ProposalSettingsConversion
# Proposal settings conversion to Y2Storage format.
class ToY2Storage
# @param settings [Agama::Storage::ProposalSettings]
def initialize(settings)
# @param config [Agama::Config]
def initialize(settings, config:)
@settings = settings
@config = config
end

# Performs the conversion to Y2Storage format.
Expand All @@ -55,6 +58,9 @@ def convert
# @return [Agama::Storage::ProposalSettings]
attr_reader :settings

# @return [Agama::Config]
attr_reader :config

# @param target [Y2Storage::ProposalSettings]
def boot_device_conversion(target)
target.root_device = settings.boot_device
Expand Down Expand Up @@ -95,10 +101,25 @@ def space_policy_conversion(target)

# @param target [Y2Storage::ProposalSettings]
def volumes_conversion(target)
target.volumes = settings.volumes.map { |v| VolumeConversion.to_y2storage(v) }
volumes = settings.volumes.map { |v| VolumeConversion.to_y2storage(v) }
disabled_volumes = missing_volumes.map do |volume|
VolumeConversion.to_y2storage(volume).tap { |v| v.proposed = false }
end

target.volumes = volumes + disabled_volumes

fallbacks_conversion(target)
end

# @return [Array<Agama::Storage::Volume>]
def missing_volumes
mount_paths = settings.volumes.map(&:mount_path)

VolumeTemplatesBuilder.new_from_config(config).all
.reject { |t| mount_paths.include?(t.mount_path) }
.reject { |t| t.mount_path.empty? }
end

# @param target [Y2Storage::ProposalSettings]
def fallbacks_conversion(target)
target.volumes.each do |spec|
Expand Down
7 changes: 7 additions & 0 deletions service/lib/agama/storage/volume_templates_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def initialize(config_data)
end
end

# Generates all volumes from templates
#
# @return [Array<Agama::Storage::Volume>]
def all
@data.keys.map { |k| self.for(k) }
end

# Generates a default volume for the given path
#
# @param path [String]
Expand Down
2 changes: 1 addition & 1 deletion service/test/agama/dbus/storage/manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
it "returns the same generic default volume for any path" do
generic = {
"FsType" => "Ext4", "MountOptions" => [],
"MinSize" => 0, "MaxSize" => 0, "AutoSize" => false
"MinSize" => 0, "AutoSize" => false
}
generic_outline = { "Required" => false, "FsTypes" => [], "SupportAutoSize" => false }

Expand Down
85 changes: 77 additions & 8 deletions service/test/agama/dbus/storage/manager_volumes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"mount_options" => ["whatever=foo"],
"outline" => {
"required" => true,
"snapshots_configurable" => snapshots_configurable,
"filesystems" => ["btrfs"],
"auto_size" => {
"base_min" => "5 GiB", "base_max" => "20 GiB", "min_fallback_for" => ["/home"]
Expand Down Expand Up @@ -100,6 +101,8 @@
]
end

let(:snapshots_configurable) { true }

let(:proposal) do
instance_double(Agama::Storage::Proposal, on_calculate: nil, settings: settings)
end
Expand Down Expand Up @@ -161,7 +164,7 @@
subject.calculate_proposal(dbus_settings)
end

it "calculates a proposal ignoring ommitted default values that are not mandatory" do
it "calculates a proposal ignoring ommitted default volumes that are not mandatory" do
expect(proposal).to receive(:calculate) do |settings|
expect(settings.volumes.map(&:mount_path)).to_not include("/home")
end
Expand Down Expand Up @@ -212,21 +215,87 @@
end
end

xcontext "when the D-Bus settings include changes in the volume outline" do
# TODO
context "when the D-Bus settings include changes in the volume outline" do
let(:dbus_settings) { { "Volumes" => [dbus_root_vol] } }
let(:dbus_root_vol) do
{
"MountPath" => "/",
"AutoSize" => true,
"Outline" => {
"Required" => false
}
}
end

it "ignores the given outline values" do
expect(proposal).to receive(:calculate) do |settings|
root = settings.volumes.find { |v| v.mount_path == "/" }
expect(root.outline.required?).to eq(true)
end

subject.calculate_proposal(dbus_settings)
end
end

xcontext "when the D-Bus settings specify auto_size for an unsupported volume" do
# TODO
context "when the D-Bus settings specify auto_size for an unsupported volume" do
let(:dbus_settings) { { "Volumes" => [dbus_home_vol] } }
let(:dbus_home_vol) do
{
"MountPath" => "/home",
"AutoSize" => true
}
end

it "does not set auto size" do
expect(proposal).to receive(:calculate) do |settings|
home = settings.volumes.find { |v| v.mount_path == "/home" }
expect(home.auto_size?).to eq(false)
end

subject.calculate_proposal(dbus_settings)
end
end

xcontext "when the D-Bus settings specify a filesystem type not listed in the outline" do
context "when the D-Bus settings specify a filesystem type not listed in the outline" do
# NOTE: do we have some mechanism to specify that any type is allowed (for example,
# empty or omitted #filesystems in an outline
let(:dbus_settings) { { "Volumes" => [dbus_root_vol] } }
let(:dbus_root_vol) do
{
"MountPath" => "/",
"FsType" => "Ext3"
}
end

it "does not set the given filesystem type" do
expect(proposal).to receive(:calculate) do |settings|
root = settings.volumes.find { |v| v.mount_path == "/" }
expect(root.fs_type).to eq(Y2Storage::Filesystems::Type::BTRFS)
end

subject.calculate_proposal(dbus_settings)
end
end

xcontext "when the D-Bus settings specify a forbidden configuration for snapshots" do
# TODO
context "when the D-Bus settings specify a forbidden configuration for snapshots" do
let(:dbus_settings) { { "Volumes" => [dbus_home_vol] } }
let(:dbus_home_vol) do
{
"MountPath" => "/home",
"Snapshots" => true
}
end

let(:snapshots_configurable) { false }

it "does not set snapshots" do
expect(proposal).to receive(:calculate) do |settings|
root = settings.volumes.find { |v| v.mount_path == "/" }
expect(root.btrfs.snapshots?).to eq(false)
end

subject.calculate_proposal(dbus_settings)
end
end
end
end
Loading

0 comments on commit 5db39de

Please sign in to comment.