Skip to content

Commit

Permalink
Test settings conversion from D-Bus
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Jul 27, 2023
1 parent bdd1c1a commit aca34ed
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def initialize(dbus_settings, config:)
#
# @return [Agama::Storage::ProposalSettings]
def convert
settings = ProposalSettingsReader.new(config).read
settings = Agama::Storage::ProposalSettingsReader.new(config).read

settings.tap do |target|
dbus_settings.each do |dbus_property, dbus_value|
Expand Down Expand Up @@ -133,7 +133,7 @@ def volumes_conversion(target, value)
# Keep default volumes if no volumes are given
return if value.empty?

required_volumes = target.volumes.select { |v| v.ouline.required? }
required_volumes = target.volumes.select { |v| v.outline.required? }
volumes = value.map { |v| VolumeConversion.from_dbus(v, config: config) }

target.volumes = volumes + missing_volumes(required_volumes, volumes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def initialize(dbus_volume, config:)
#
# @return [Agama::Storage::Volume]
def convert
volume = VolumeTemplatesBuilder.new_from_config(config).for(dbus_volume["MountPath"])
builder = Agama::Storage::VolumeTemplatesBuilder.new_from_config(config)
volume = builder.for(dbus_volume["MountPath"])

volume.tap do |target|
dbus_volume.each do |dbus_property, dbus_value|
Expand Down
13 changes: 7 additions & 6 deletions service/lib/agama/storage/proposal_settings_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ def initialize(config)
#
# @return [ProposalSettings]
def read
settings = ProposalSettings.new
config.data.fetch("storage", {}).each do |key, value|
send(READERS[key], settings, value)
ProposalSettings.new.tap do |settings|
config.data.fetch("storage", {}).each do |key, value|
reader = READERS[key]
send(reader, settings, value) if reader
end
end
settings
end

private
Expand All @@ -51,7 +52,7 @@ def read
# Settings from control file and their readers.
READERS = {
"lvm" => :lvm_reader,
"encrypttion" => :encryption_reader,
"encryption" => :encryption_reader,
"space_policy" => :space_policy_reader,
"volumes" => :volumes_reader
}.freeze
Expand Down Expand Up @@ -86,7 +87,7 @@ def volumes_reader(settings, volumes)
builder = VolumeTemplatesBuilder.new_from_config(config)
mount_paths = volumes.map { |v| v["mount_path"] }.compact

settings.volumes = mount_paths.map { |mp| buider.for(mp) }
settings.volumes = mount_paths.map { |mp| builder.for(mp) }
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# frozen_string_literal: true

# Copyright (c) [2023] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../../../../test_helper"
require "y2storage/encryption_method"
require "y2storage/pbkd_function"
require "agama/config"
require "agama/storage/proposal_settings"
require "agama/dbus/storage/proposal_settings_conversion/from_dbus"

describe Agama::DBus::Storage::ProposalSettingsConversion::FromDBus do
subject { described_class.new(dbus_settings, config: config) }

let(:config) { Agama::Config.new(config_data) }

let(:config_data) do
{
"storage" => {
"lvm" => true,
"encryption" => {
"method" => "luks2",
"pbkd_function" => "argon2id"
},
"space_policy" => "delete",
"volumes" => [
{ "mount_path" => "/" }
],
"volume_templates" => [
{
"mount_path" => "/",
"outline" => {
"required" => true
}
},
{
"mount_path" => "swap",
"outline" => {
"required" => false
}
}
]
}
}
end

describe "#convert" do
let(:dbus_settings) do
{
"BootDevice" => "/dev/sda",
"LVM" => true,
"SystemVGDevices" => ["/dev/sda", "/dev/sdb"],
"EncryptionPassword" => "notsecret",
"EncryptionMethod" => "luks1",
"EncryptionPBKDFunction" => "pbkdf2",
"SpacePolicy" => "custom",
"SpaceActions" => { "/dev/sda" => "force_delete" },
"Volumes" => [
{ "MountPath" => "/test" }
]
}
end

it "generates proposal settings from D-Bus values" do
settings = subject.convert

expect(settings).to be_a(Agama::Storage::ProposalSettings)
expect(settings.boot_device).to eq("/dev/sda")
expect(settings.lvm.enabled).to eq(true)
expect(settings.lvm.system_vg_devices).to contain_exactly("/dev/sda", "/dev/sdb")
expect(settings.encryption.method).to eq(Y2Storage::EncryptionMethod::LUKS1)
expect(settings.encryption.pbkd_function).to eq(Y2Storage::PbkdFunction::PBKDF2)
expect(settings.space.policy).to eq(:custom)
expect(settings.space.actions).to eq({ "/dev/sda" => "force_delete" })
expect(settings.volumes.map(&:mount_path)).to include("/test")
end

it "adds missing required volumes" do
settings = subject.convert

expect(settings.volumes.map(&:mount_path)).to contain_exactly("/", "/test")
end

context "when a value is not provided from D-Bus" do
let(:dbus_settings) { {} }

it "generates proposal settings with default values from config" do
settings = subject.convert

expect(settings).to be_a(Agama::Storage::ProposalSettings)
expect(settings.boot_device).to be_nil
expect(settings.lvm.enabled).to eq(true)
expect(settings.encryption.method).to eq(Y2Storage::EncryptionMethod::LUKS2)
expect(settings.encryption.pbkd_function).to eq(Y2Storage::PbkdFunction::ARGON2ID)
expect(settings.space.policy).to eq(:delete)
expect(settings.volumes.map(&:mount_path)).to contain_exactly("/")
end
end
end
end

0 comments on commit aca34ed

Please sign in to comment.