From ad6106da8d744ef04f0848537cb8daccc16b9b9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Iv=C3=A1n=20L=C3=B3pez=20Gonz=C3=A1lez?= Date: Fri, 28 Jun 2024 06:52:55 +0100 Subject: [PATCH 1/2] storage: rename JSON conversions --- service/lib/agama/dbus/storage/manager.rb | 40 +++++------ .../lib/agama/storage/proposal_settings.rb | 18 +++++ .../storage/proposal_settings_conversion.rb | 20 ------ .../storage/proposal_settings_conversions.rb | 31 ++++++++ .../from_json.rb} | 64 ++++++++--------- .../to_json.rb} | 25 ++++--- service/lib/agama/storage/volume.rb | 23 +++++- .../lib/agama/storage/volume_conversion.rb | 20 ------ .../lib/agama/storage/volume_conversions.rb | 31 ++++++++ .../from_json.rb} | 71 ++++++++++--------- .../to_json.rb} | 14 ++-- .../test/agama/dbus/storage/manager_test.rb | 7 +- .../proposal_settings_conversion_test.rb | 27 ------- .../from_json_test.rb} | 42 +++++------ .../from_y2storage_test.rb | 0 .../to_json_test.rb} | 8 +-- .../to_y2storage_test.rb | 0 .../agama/storage/proposal_settings_test.rb | 27 +++++++ .../agama/storage/volume_conversion_test.rb | 28 -------- .../from_json_test.rb} | 44 ++++++------ .../from_y2storage_test.rb | 0 .../to_json_test.rb} | 6 +- .../to_y2storage_test.rb | 0 service/test/agama/storage/volume_test.rb | 53 ++++++++++++++ 24 files changed, 339 insertions(+), 260 deletions(-) create mode 100644 service/lib/agama/storage/proposal_settings_conversions.rb rename service/lib/agama/storage/{proposal_settings_conversion/from_schema.rb => proposal_settings_conversions/from_json.rb} (71%) rename service/lib/agama/storage/{proposal_settings_conversion/to_schema.rb => proposal_settings_conversions/to_json.rb} (80%) create mode 100644 service/lib/agama/storage/volume_conversions.rb rename service/lib/agama/storage/{volume_conversion/from_schema.rb => volume_conversions/from_json.rb} (65%) rename service/lib/agama/storage/{volume_conversion/to_schema.rb => volume_conversions/to_json.rb} (88%) rename service/test/agama/storage/{proposal_settings_conversion/from_schema_test.rb => proposal_settings_conversions/from_json_test.rb} (86%) rename service/test/agama/storage/{proposal_settings_conversion => proposal_settings_conversions}/from_y2storage_test.rb (100%) rename service/test/agama/storage/{proposal_settings_conversion/to_schema_test.rb => proposal_settings_conversions/to_json_test.rb} (91%) rename service/test/agama/storage/{proposal_settings_conversion => proposal_settings_conversions}/to_y2storage_test.rb (100%) rename service/test/agama/storage/{volume_conversion/from_schema_test.rb => volume_conversions/from_json_test.rb} (83%) rename service/test/agama/storage/{volume_conversion => volume_conversions}/from_y2storage_test.rb (100%) rename service/test/agama/storage/{volume_conversion/to_schema_test.rb => volume_conversions/to_json_test.rb} (93%) rename service/test/agama/storage/{volume_conversion => volume_conversions}/to_y2storage_test.rb (100%) create mode 100644 service/test/agama/storage/volume_test.rb diff --git a/service/lib/agama/dbus/storage/manager.rb b/service/lib/agama/dbus/storage/manager.rb index 6019320ef5..70fd4cf970 100644 --- a/service/lib/agama/dbus/storage/manager.rb +++ b/service/lib/agama/dbus/storage/manager.rb @@ -30,13 +30,13 @@ require "agama/dbus/interfaces/service_status" require "agama/dbus/storage/devices_tree" require "agama/dbus/storage/iscsi_nodes_tree" -require "agama/dbus/storage/proposal_settings_conversion" require "agama/dbus/storage/proposal" +require "agama/dbus/storage/proposal_settings_conversion" require "agama/dbus/storage/volume_conversion" require "agama/dbus/storage/with_iscsi_auth" require "agama/dbus/with_service_status" require "agama/storage/encryption_settings" -require "agama/storage/proposal_settings_conversion" +require "agama/storage/proposal_settings" require "agama/storage/volume_templates_builder" Yast.import "Arch" @@ -99,12 +99,12 @@ def probe # AutoYaST settings: { "storage": ... } vs { "legacyAutoyastStorage": ... }. def apply_storage_config(serialized_config) @serialized_storage_config = serialized_config - storage_config = JSON.parse(serialized_config, symbolize_names: true) + config_json = JSON.parse(serialized_config, symbolize_names: true) - if (guided_settings = storage_config.dig(:storage, :guided)) - calculate_guided_proposal(guided_settings) - elsif (autoyast_settings = storage_config[:legacyAutoyastStorage]) - calculate_autoyast_proposal(autoyast_settings) + if (settings_json = config_json.dig(:storage, :guided)) + calculate_guided_proposal(settings_json) + elsif (settings_json = config_json[:legacyAutoyastStorage]) + calculate_autoyast_proposal(settings_json) else raise "Invalid config: #{serialized_config}" end @@ -259,11 +259,10 @@ def proposal_result return {} unless proposal.calculated? if proposal.strategy?(ProposalStrategy::GUIDED) - settings = Agama::Storage::ProposalSettingsConversion.to_schema(proposal.settings) { "success" => proposal.success?, "strategy" => ProposalStrategy::GUIDED, - "settings" => settings.to_json + "settings" => proposal.settings.to_json_settings.to_json } else { @@ -400,16 +399,15 @@ def proposal # Calculates a guided proposal. # - # @param settings [Hash] Settings according to the JSON schema. + # @param settings_json [Hash] JSON settings according to schema. # @return [Integer] 0 success; 1 error - def calculate_guided_proposal(settings) - proposal_settings = Agama::Storage::ProposalSettingsConversion.from_schema( - settings, config: config - ) + def calculate_guided_proposal(settings_json) + proposal_settings = Agama::Storage::ProposalSettings + .new_from_json(settings_json, config: config) logger.info( "Calculating guided storage proposal from D-Bus.\n" \ - "Input settings: #{settings}\n" \ + "Input settings: #{settings_json}\n" \ "Agama settings: #{proposal_settings.inspect}" ) @@ -419,15 +417,15 @@ def calculate_guided_proposal(settings) # Calculates an AutoYaST proposal. # - # @param settings [Hash] AutoYaST settings. + # @param settings_json [Hash] AutoYaST settings. # @return [Integer] 0 success; 1 error - def calculate_autoyast_proposal(settings) + def calculate_autoyast_proposal(settings_json) # Ensures keys are strings. - autoyast_settings = JSON.parse(settings.to_json) + autoyast_settings = JSON.parse(settings_json.to_json) logger.info( "Calculating AutoYaST storage proposal from D-Bus.\n" \ - "Input settings: #{settings}\n" \ + "Input settings: #{settings_json}\n" \ "AutoYaST settings: #{autoyast_settings}" ) @@ -437,12 +435,12 @@ def calculate_autoyast_proposal(settings) # Generates the storage config from the current proposal, if any. # - # @return [Hash] Storage config according to the JSON schema. + # @return [Hash] Storage config according to JSON schema. def generate_storage_config if proposal.strategy?(ProposalStrategy::GUIDED) { storage: { - guided: Agama::Storage::ProposalSettingsConversion.to_schema(proposal.settings) + guided: proposal.settings.to_json_settings } } elsif proposal.strategy?(ProposalStrategy::AUTOYAST) diff --git a/service/lib/agama/storage/proposal_settings.rb b/service/lib/agama/storage/proposal_settings.rb index 77e142dda7..cbf3d297a8 100644 --- a/service/lib/agama/storage/proposal_settings.rb +++ b/service/lib/agama/storage/proposal_settings.rb @@ -22,6 +22,7 @@ require "agama/storage/boot_settings" require "agama/storage/device_settings" require "agama/storage/encryption_settings" +require "agama/storage/proposal_settings_conversions" require "agama/storage/space_settings" module Agama @@ -85,6 +86,23 @@ def default_boot_device end end + # Creates a new proposal settings object from JSON hash according to schema. + # + # @param settings_json [Hash] + # @param config [Config] + # + # @return [ProposalSettings] + def self.new_from_json(settings_json, config:) + Storage::ProposalSettingsConversions::FromJSON.new(settings_json, config: config).convert + end + + # Generates a JSON hash according to schema. + # + # @return [Hash] + def to_json_settings + Storage::ProposalSettingsConversions::ToJSON.new(self).convert + end + private # Device used for booting. diff --git a/service/lib/agama/storage/proposal_settings_conversion.rb b/service/lib/agama/storage/proposal_settings_conversion.rb index ea0f28f161..f8439c7531 100644 --- a/service/lib/agama/storage/proposal_settings_conversion.rb +++ b/service/lib/agama/storage/proposal_settings_conversion.rb @@ -19,9 +19,7 @@ # To contact SUSE LLC about this file by physical or electronic mail, you may # find current contact information at www.suse.com. -require "agama/storage/proposal_settings_conversion/from_schema" require "agama/storage/proposal_settings_conversion/from_y2storage" -require "agama/storage/proposal_settings_conversion/to_schema" require "agama/storage/proposal_settings_conversion/to_y2storage" module Agama @@ -47,24 +45,6 @@ def self.from_y2storage(y2storage_settings, settings) def self.to_y2storage(settings, config:) ToY2Storage.new(settings, config: config).convert end - - # Performs conversion from Hash according to the JSON schema. - # - # @param schema_settings [Hash] - # @param config [Agama::Config] - # - # @return [Agama::Storage::ProposalSettings] - def self.from_schema(schema_settings, config:) - FromSchema.new(schema_settings, config: config).convert - end - - # Performs conversion according to the JSON schema. - # - # @param settings [Agama::Storage::ProposalSettings] - # @return [Hash] - def self.to_schema(settings) - ToSchema.new(settings).convert - end end end end diff --git a/service/lib/agama/storage/proposal_settings_conversions.rb b/service/lib/agama/storage/proposal_settings_conversions.rb new file mode 100644 index 0000000000..6ad03db45f --- /dev/null +++ b/service/lib/agama/storage/proposal_settings_conversions.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +# Copyright (c) [2024] 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 "agama/storage/proposal_settings_conversions/from_json" +require "agama/storage/proposal_settings_conversions/to_json" + +module Agama + module Storage + # Conversions for the proposal settings. + module ProposalSettingsConversions + end + end +end diff --git a/service/lib/agama/storage/proposal_settings_conversion/from_schema.rb b/service/lib/agama/storage/proposal_settings_conversions/from_json.rb similarity index 71% rename from service/lib/agama/storage/proposal_settings_conversion/from_schema.rb rename to service/lib/agama/storage/proposal_settings_conversions/from_json.rb index 6db366f4d7..6cd1a0b872 100644 --- a/service/lib/agama/storage/proposal_settings_conversion/from_schema.rb +++ b/service/lib/agama/storage/proposal_settings_conversions/from_json.rb @@ -24,20 +24,19 @@ require "agama/storage/encryption_settings" require "agama/storage/proposal_settings_reader" require "agama/storage/space_settings" -require "agama/storage/volume_conversion" +require "agama/storage/volume" require "y2storage/encryption_method" require "y2storage/pbkd_function" module Agama module Storage - module ProposalSettingsConversion - # Proposal settings conversion from Hash according to the JSON schema. - class FromSchema - # @param schema_settings [Hash] + module ProposalSettingsConversions + # Proposal settings conversion from JSON hash according to schema. + class FromJSON + # @param settings_json [Hash] # @param config [Config] - def initialize(schema_settings, config:) - # @todo Raise error if schema_settings does not match the JSON schema. - @schema_settings = schema_settings + def initialize(settings_json, config:) + @settings_json = settings_json @config = config end @@ -45,6 +44,7 @@ def initialize(schema_settings, config:) # # @return [ProposalSettings] def convert + # @todo Raise error if settings_json does not match the JSON schema. device_settings = target_conversion boot_settings = boot_conversion encryption_settings = encryption_conversion @@ -63,49 +63,49 @@ def convert private # @return [Hash] - attr_reader :schema_settings + attr_reader :settings_json # @return [Config] attr_reader :config def target_conversion - target_schema = schema_settings[:target] - return unless target_schema + target_json = settings_json[:target] + return unless target_json - if target_schema == "disk" + if target_json == "disk" Agama::Storage::DeviceSettings::Disk.new - elsif target_schema == "newLvmVg" + elsif target_json == "newLvmVg" Agama::Storage::DeviceSettings::NewLvmVg.new - elsif (device = target_schema[:disk]) + elsif (device = target_json[:disk]) Agama::Storage::DeviceSettings::Disk.new(device) - elsif (devices = target_schema[:newLvmVg]) + elsif (devices = target_json[:newLvmVg]) Agama::Storage::DeviceSettings::NewLvmVg.new(devices) end end def boot_conversion - boot_schema = schema_settings[:boot] - return unless boot_schema + boot_json = settings_json[:boot] + return unless boot_json Agama::Storage::BootSettings.new.tap do |boot_settings| - boot_settings.configure = boot_schema[:configure] - boot_settings.device = boot_schema[:device] + boot_settings.configure = boot_json[:configure] + boot_settings.device = boot_json[:device] end end def encryption_conversion - encryption_schema = schema_settings[:encryption] - return unless encryption_schema + encryption_json = settings_json[:encryption] + return unless encryption_json Agama::Storage::EncryptionSettings.new.tap do |encryption_settings| - encryption_settings.password = encryption_schema[:password] + encryption_settings.password = encryption_json[:password] - if (method_value = encryption_schema[:method]) + if (method_value = encryption_json[:method]) method = Y2Storage::EncryptionMethod.find(method_value.to_sym) encryption_settings.method = method end - if (function_value = encryption_schema[:pbkdFunction]) + if (function_value = encryption_json[:pbkdFunction]) function = Y2Storage::PbkdFunction.find(function_value) encryption_settings.pbkd_function = function end @@ -113,13 +113,13 @@ def encryption_conversion end def space_conversion - space_schema = schema_settings[:space] - return unless space_schema + space_json = settings_json[:space] + return unless space_json Agama::Storage::SpaceSettings.new.tap do |space_settings| - space_settings.policy = space_schema[:policy].to_sym + space_settings.policy = space_json[:policy].to_sym - actions_value = space_schema[:actions] || [] + actions_value = space_json[:actions] || [] space_settings.actions = actions_value.map { |a| action_conversion(a) }.inject(:merge) end end @@ -132,11 +132,11 @@ def action_conversion(action) end def volumes_conversion - volumes_schema = schema_settings[:volumes] - return [] unless volumes_schema + volumes_json = settings_json[:volumes] + return [] unless volumes_json - volumes_schema.map do |volume_schema| - VolumeConversion.from_schema(volume_schema, config: config) + volumes_json.map do |volume_json| + Volume.new_from_json(volume_json, config: config) end end diff --git a/service/lib/agama/storage/proposal_settings_conversion/to_schema.rb b/service/lib/agama/storage/proposal_settings_conversions/to_json.rb similarity index 80% rename from service/lib/agama/storage/proposal_settings_conversion/to_schema.rb rename to service/lib/agama/storage/proposal_settings_conversions/to_json.rb index 071b1e424a..9eee973d39 100644 --- a/service/lib/agama/storage/proposal_settings_conversion/to_schema.rb +++ b/service/lib/agama/storage/proposal_settings_conversions/to_json.rb @@ -20,19 +20,18 @@ # find current contact information at www.suse.com. require "agama/storage/device_settings" -require "agama/storage/volume_conversion" module Agama module Storage - module ProposalSettingsConversion - # Proposal settings conversion according to the JSON schema. - class ToSchema + module ProposalSettingsConversions + # Proposal settings conversion to JSON hash according to schema. + class ToJSON # @param settings [ProposalSettings] def initialize(settings) @settings = settings end - # Performs the conversion according to the JSON schema. + # Performs the conversion to JSON. # # @return [Hash] def convert @@ -41,9 +40,9 @@ def convert boot: boot_conversion, space: space_conversion, volumes: volumes_conversion - }.tap do |schema| - encryption_schema = encryption_conversion - schema[:encryption] = encryption_schema if encryption_schema + }.tap do |settings_json| + encryption_json = encryption_conversion + settings_json[:encryption] = encryption_json if encryption_json end end @@ -68,9 +67,9 @@ def target_conversion def boot_conversion { configure: settings.boot.configure? - }.tap do |schema| + }.tap do |boot_json| device = settings.boot.device - schema[:device] = device if device + boot_json[:device] = device if device end end @@ -80,9 +79,9 @@ def encryption_conversion { password: settings.encryption.password, method: settings.encryption.method.id.to_s - }.tap do |schema| + }.tap do |encryption_json| function = settings.encryption.pbkd_function - schema[:pbkdFunction] = function.value if function + encryption_json[:pbkdFunction] = function.value if function end end @@ -100,7 +99,7 @@ def action_key(action) end def volumes_conversion - settings.volumes.map { |v| VolumeConversion.to_schema(v) } + settings.volumes.map(&:to_json_settings) end end end diff --git a/service/lib/agama/storage/volume.rb b/service/lib/agama/storage/volume.rb index 2ff73300d7..76b4a39417 100644 --- a/service/lib/agama/storage/volume.rb +++ b/service/lib/agama/storage/volume.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -# Copyright (c) [2022-2023] SUSE LLC +# Copyright (c) [2022-2024] SUSE LLC # # All Rights Reserved. # @@ -20,10 +20,12 @@ # find current contact information at www.suse.com. require "forwardable" +require "json" require "y2storage/disk_size" require "agama/storage/btrfs_settings" -require "agama/storage/volume_outline" +require "agama/storage/volume_conversions" require "agama/storage/volume_location" +require "agama/storage/volume_outline" module Agama module Storage @@ -111,6 +113,23 @@ def auto_size_supported? # for the snapshots but does not allow to enable them). outline.adaptive_sizes? end + + # Creates a new volume object from a JSON hash according to schema. + # + # @param volume_json [Hash] + # @param config [Config] + # + # @return [Volume] + def self.new_from_json(volume_json, config:) + Storage::VolumeConversions::FromJSON.new(volume_json, config: config).convert + end + + # Generates a JSON hash according to schema. + # + # @return [Hash] + def to_json_settings + Storage::VolumeConversions::ToJSON.new(self).convert + end end end end diff --git a/service/lib/agama/storage/volume_conversion.rb b/service/lib/agama/storage/volume_conversion.rb index 9b393fa4e8..432f39034f 100644 --- a/service/lib/agama/storage/volume_conversion.rb +++ b/service/lib/agama/storage/volume_conversion.rb @@ -19,9 +19,7 @@ # To contact SUSE LLC about this file by physical or electronic mail, you may # find current contact information at www.suse.com. -require "agama/storage/volume_conversion/from_schema" require "agama/storage/volume_conversion/from_y2storage" -require "agama/storage/volume_conversion/to_schema" require "agama/storage/volume_conversion/to_y2storage" module Agama @@ -43,24 +41,6 @@ def self.from_y2storage(volume) def self.to_y2storage(volume) ToY2Storage.new(volume).convert end - - # Performs conversion from Hash according to the JSON schema. - # - # @param volume_schema [Hash] - # @param config [Agama::Config] - # - # @return [Agama::Storage::Volume] - def self.from_schema(volume_schema, config:) - FromSchema.new(volume_schema, config: config).convert - end - - # Performs conversion according to the JSON schema. - # - # @param volume [Agama::Storage::Volume] - # @return [Hash] - def self.to_schema(volume) - ToSchema.new(volume).convert - end end end end diff --git a/service/lib/agama/storage/volume_conversions.rb b/service/lib/agama/storage/volume_conversions.rb new file mode 100644 index 0000000000..3c37c4d329 --- /dev/null +++ b/service/lib/agama/storage/volume_conversions.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +# Copyright (c) [2024] 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 "agama/storage/volume_conversions/from_json" +require "agama/storage/volume_conversions/to_json" + +module Agama + module Storage + # Conversions for a volume + module VolumeConversions + end + end +end diff --git a/service/lib/agama/storage/volume_conversion/from_schema.rb b/service/lib/agama/storage/volume_conversions/from_json.rb similarity index 65% rename from service/lib/agama/storage/volume_conversion/from_schema.rb rename to service/lib/agama/storage/volume_conversions/from_json.rb index f792314576..a400d250a2 100644 --- a/service/lib/agama/storage/volume_conversion/from_schema.rb +++ b/service/lib/agama/storage/volume_conversions/from_json.rb @@ -26,21 +26,22 @@ module Agama module Storage - module VolumeConversion - # Volume conversion from Hash according to the JSON schema. - class FromSchema - # @param volume_schema [Hash] + module VolumeConversions + # Volume conversion from JSON hash according schema. + class FromJSON + # @param volume_json [Hash] # @param config [Config] - def initialize(volume_schema, config:) - # @todo Raise error if volume_schema does not match the JSON schema. - @volume_schema = volume_schema + def initialize(volume_json, config:) + @volume_json = volume_json @config = config end - # Performs the conversion from Hash according to the JSON schema. + # Performs the conversion from JSON Hash according to schema. # # @return [Volume] def convert + # @todo Raise error if volume_json does not match the JSON schema. + default_volume.tap do |volume| mount_conversion(volume) filesystem_conversion(volume) @@ -52,15 +53,15 @@ def convert private # @return [Hash] - attr_reader :volume_schema + attr_reader :volume_json # @return [Agama::Config] attr_reader :config # @param volume [Volume] def mount_conversion(volume) - path_value = volume_schema.dig(:mount, :path) - options_value = volume_schema.dig(:mount, :options) + path_value = volume_json.dig(:mount, :path) + options_value = volume_json.dig(:mount, :options) volume.mount_path = path_value volume.mount_options = options_value if options_value @@ -68,31 +69,31 @@ def mount_conversion(volume) # @param volume [Volume] def filesystem_conversion(volume) - filesystem_schema = volume_schema[:filesystem] - return unless filesystem_schema + filesystem_json = volume_json[:filesystem] + return unless filesystem_json - if filesystem_schema.is_a?(String) - filesystem_string_conversion(volume, filesystem_schema) + if filesystem_json.is_a?(String) + filesystem_string_conversion(volume, filesystem_json) else - filesystem_hash_conversion(volume, filesystem_schema) + filesystem_hash_conversion(volume, filesystem_json) end end # @param volume [Volume] - # @param filesystem [String] - def filesystem_string_conversion(volume, filesystem) + # @param filesystem_json [String] + def filesystem_string_conversion(volume, filesystem_json) filesystems = volume.outline.filesystems - fs_type = filesystems.find { |t| t.to_s == filesystem } + fs_type = filesystems.find { |t| t.to_s == filesystem_json } volume.fs_type = fs_type if fs_type end # @param volume [Volume] - # @param filesystem [Hash] - def filesystem_hash_conversion(volume, filesystem) + # @param filesystem_json [Hash] + def filesystem_hash_conversion(volume, filesystem_json) filesystem_string_conversion(volume, "btrfs") - snapshots_value = filesystem.dig(:btrfs, :snapshots) + snapshots_value = filesystem_json.dig(:btrfs, :snapshots) return if !volume.outline.snapshots_configurable? || snapshots_value.nil? volume.btrfs.snapshots = snapshots_value @@ -101,16 +102,16 @@ def filesystem_hash_conversion(volume, filesystem) # @todo Support array format ([min, max]) and string format ("2 GiB") # @param volume [Volume] def size_conversion(volume) - size_schema = volume_schema[:size] - return unless size_schema + size_json = volume_json[:size] + return unless size_json - if size_schema == "auto" + if size_json == "auto" volume.auto_size = true if volume.auto_size_supported? else volume.auto_size = false - min_value = size_schema[:min] - max_value = size_schema[:max] + min_value = size_json[:min] + max_value = size_json[:max] volume.min_size = Y2Storage::DiskSize.new(min_value) volume.max_size = if max_value @@ -122,22 +123,22 @@ def size_conversion(volume) end def target_conversion(volume) - target_schema = volume_schema[:target] - return unless target_schema + target_json = volume_json[:target] + return unless target_json - if target_schema == "default" + if target_json == "default" volume.location.target = :default volume.location.device = nil - elsif (device = target_schema[:newPartition]) + elsif (device = target_json[:newPartition]) volume.location.target = :new_partition volume.location.device = device - elsif (device = target_schema[:newVg]) + elsif (device = target_json[:newVg]) volume.location.target = :new_vg volume.location.device = device - elsif (device = target_schema[:device]) + elsif (device = target_json[:device]) volume.location.target = :device volume.location.device = device - elsif (device = target_schema[:filesystem]) + elsif (device = target_json[:filesystem]) volume.location.target = :filesystem volume.location.device = device end @@ -146,7 +147,7 @@ def target_conversion(volume) def default_volume Agama::Storage::VolumeTemplatesBuilder .new_from_config(config) - .for(volume_schema.dig(:mount, :path)) + .for(volume_json.dig(:mount, :path)) end end end diff --git a/service/lib/agama/storage/volume_conversion/to_schema.rb b/service/lib/agama/storage/volume_conversions/to_json.rb similarity index 88% rename from service/lib/agama/storage/volume_conversion/to_schema.rb rename to service/lib/agama/storage/volume_conversions/to_json.rb index 0ba7e8b1ff..4915ae68c0 100644 --- a/service/lib/agama/storage/volume_conversion/to_schema.rb +++ b/service/lib/agama/storage/volume_conversions/to_json.rb @@ -25,15 +25,15 @@ module Agama module Storage - module VolumeConversion - # Volume conversion according to the JSON schema. - class ToSchema + module VolumeConversions + # Volume conversion to JSON hash according to schema. + class ToJSON # @param volume [Volume] def initialize(volume) @volume = volume end - # Performs the conversion according to the JSON schema. + # Performs the conversion to JSON. # # @return [Hash] def convert @@ -41,9 +41,9 @@ def convert mount: mount_conversion, size: size_conversion, target: target_conversion - }.tap do |schema| - filesystem_schema = filesystem_conversion - schema[:filesystem] = filesystem_schema if filesystem_schema + }.tap do |volume_json| + filesystem_json = filesystem_conversion + volume_json[:filesystem] = filesystem_json if filesystem_json end end diff --git a/service/test/agama/dbus/storage/manager_test.rb b/service/test/agama/dbus/storage/manager_test.rb index 16a8896b03..cc81b29000 100644 --- a/service/test/agama/dbus/storage/manager_test.rb +++ b/service/test/agama/dbus/storage/manager_test.rb @@ -27,7 +27,6 @@ require "agama/storage/manager" require "agama/storage/proposal" require "agama/storage/proposal_settings" -require "agama/storage/proposal_settings_conversion" require "agama/storage/volume" require "agama/storage/iscsi/manager" require "agama/storage/dasd/manager" @@ -547,7 +546,7 @@ def pretty_json(value) it "returns serialized storage config including guided proposal settings" do expected_config = { storage: { - guided: Agama::Storage::ProposalSettingsConversion.to_schema(settings) + guided: settings.to_json_settings } } @@ -626,9 +625,7 @@ def pretty_json(value) it "returns a Hash with success, strategy and settings" do result = subject.proposal_result - serialized_settings = Agama::Storage::ProposalSettingsConversion - .to_schema(proposal.settings) - .to_json + serialized_settings = proposal.settings.to_json_settings.to_json expect(result.keys).to contain_exactly("success", "strategy", "settings") expect(result["success"]).to eq(true) diff --git a/service/test/agama/storage/proposal_settings_conversion_test.rb b/service/test/agama/storage/proposal_settings_conversion_test.rb index e57a03e257..dfb1db457a 100644 --- a/service/test/agama/storage/proposal_settings_conversion_test.rb +++ b/service/test/agama/storage/proposal_settings_conversion_test.rb @@ -20,7 +20,6 @@ # find current contact information at www.suse.com. require_relative "../../test_helper" -require "agama/config" require "agama/storage/proposal_settings" require "agama/storage/proposal_settings_conversion" require "y2storage" @@ -47,30 +46,4 @@ expect(result).to be_a(Y2Storage::ProposalSettings) end end - - describe "#from_schema" do - let(:config) { Agama::Config.new } - - let(:schema_settings) do - { - target: { - disk: "/dev/vda" - } - } - end - - it "generates proposal settings from settings according to the JSON schema" do - result = described_class.from_schema(schema_settings, config: config) - expect(result).to be_a(Agama::Storage::ProposalSettings) - end - end - - describe "#to_schema" do - let(:proposal_settings) { Agama::Storage::ProposalSettings.new } - - it "generates settings according to the JSON schema from the proposal settings" do - result = described_class.to_schema(proposal_settings) - expect(result).to be_a(Hash) - end - end end diff --git a/service/test/agama/storage/proposal_settings_conversion/from_schema_test.rb b/service/test/agama/storage/proposal_settings_conversions/from_json_test.rb similarity index 86% rename from service/test/agama/storage/proposal_settings_conversion/from_schema_test.rb rename to service/test/agama/storage/proposal_settings_conversions/from_json_test.rb index 14d7a89d91..3426264077 100644 --- a/service/test/agama/storage/proposal_settings_conversion/from_schema_test.rb +++ b/service/test/agama/storage/proposal_settings_conversions/from_json_test.rb @@ -20,15 +20,15 @@ # find current contact information at www.suse.com. require_relative "../../../test_helper" -require "agama/storage/proposal_settings_conversion/from_schema" +require "agama/storage/proposal_settings_conversions/from_json" require "agama/config" require "agama/storage/device_settings" require "agama/storage/proposal_settings" require "y2storage/encryption_method" require "y2storage/pbkd_function" -describe Agama::Storage::ProposalSettingsConversion::FromSchema do - subject { described_class.new(settings_schema, config: config) } +describe Agama::Storage::ProposalSettingsConversions::FromJSON do + subject { described_class.new(settings_json, config: config) } let(:config) { Agama::Config.new(config_data) } @@ -71,7 +71,7 @@ end describe "#convert" do - let(:settings_schema) do + let(:settings_json) do { target: { disk: "/dev/sda" @@ -107,7 +107,7 @@ } end - it "generates settings with the values provided from hash according to the JSON schema" do + it "generates settings with the values provided from JSON" do settings = subject.convert expect(settings).to be_a(Agama::Storage::ProposalSettings) @@ -127,8 +127,8 @@ ) end - context "when the hash settings is missing some values" do - let(:settings_schema) { {} } + context "when the JSON is missing some values" do + let(:settings_json) { {} } it "completes the missing values with default values from the config" do settings = subject.convert @@ -148,8 +148,8 @@ end end - context "when the hash settings does not indicate the target" do - let(:settings_schema) { {} } + context "when the JSON does not indicate the target" do + let(:settings_json) { {} } it "generates settings with disk target and without specific device" do settings = subject.convert @@ -159,8 +159,8 @@ end end - context "when the hash settings indicates disk target without device" do - let(:settings_schema) do + context "when the JSON indicates disk target without device" do + let(:settings_json) do { target: "disk" } @@ -174,8 +174,8 @@ end end - context "when the hash settings indicates disk target with a device" do - let(:settings_schema) do + context "when the JSON indicates disk target with a device" do + let(:settings_json) do { target: { disk: "/dev/vda" @@ -191,8 +191,8 @@ end end - context "when the hash settings indicates newLvmVg target without devices" do - let(:settings_schema) do + context "when the JSON indicates newLvmVg target without devices" do + let(:settings_json) do { target: "newLvmVg" } @@ -206,8 +206,8 @@ end end - context "when the hash settings indicates newLvmVg target with devices" do - let(:settings_schema) do + context "when the JSON indicates newLvmVg target with devices" do + let(:settings_json) do { target: { newLvmVg: ["/dev/vda", "/dev/vdb"] @@ -223,8 +223,8 @@ end end - context "when the hash settings does not indicate volumes" do - let(:settings_schema) { { volumes: [] } } + context "when the JSON does not indicate volumes" do + let(:settings_json) { { volumes: [] } } it "generates settings with the default volumes from config" do settings = subject.convert @@ -244,8 +244,8 @@ end end - context "when the hash settings does not contain a required volume" do - let(:settings_schema) do + context "when the JSON does not contain a required volume" do + let(:settings_json) do { volumes: [ { diff --git a/service/test/agama/storage/proposal_settings_conversion/from_y2storage_test.rb b/service/test/agama/storage/proposal_settings_conversions/from_y2storage_test.rb similarity index 100% rename from service/test/agama/storage/proposal_settings_conversion/from_y2storage_test.rb rename to service/test/agama/storage/proposal_settings_conversions/from_y2storage_test.rb diff --git a/service/test/agama/storage/proposal_settings_conversion/to_schema_test.rb b/service/test/agama/storage/proposal_settings_conversions/to_json_test.rb similarity index 91% rename from service/test/agama/storage/proposal_settings_conversion/to_schema_test.rb rename to service/test/agama/storage/proposal_settings_conversions/to_json_test.rb index 8eba7d5a45..fad93ce2d5 100644 --- a/service/test/agama/storage/proposal_settings_conversion/to_schema_test.rb +++ b/service/test/agama/storage/proposal_settings_conversions/to_json_test.rb @@ -20,14 +20,14 @@ # find current contact information at www.suse.com. require_relative "../../../test_helper" -require "agama/storage/proposal_settings_conversion/to_schema" +require "agama/storage/proposal_settings_conversions/to_json" require "agama/storage/device_settings" require "agama/storage/proposal_settings" require "agama/storage/volume" require "y2storage/encryption_method" require "y2storage/pbkd_function" -describe Agama::Storage::ProposalSettingsConversion::ToSchema do +describe Agama::Storage::ProposalSettingsConversions::ToJSON do let(:default_settings) { Agama::Storage::ProposalSettings.new } let(:custom_settings) do @@ -44,7 +44,7 @@ end describe "#convert" do - it "converts the settings to the proper hash according to the JSON schema" do + it "converts the settings to a JSON hash according to schema" do # @todo Check whether the result matches the JSON schema. expect(described_class.new(default_settings).convert).to eq( @@ -101,7 +101,7 @@ end end - it "converts the settings to the proper hash according to the JSON schema" do + it "converts the settings to a JSON hash according to schema" do expect(described_class.new(settings).convert).to eq( target: { newLvmVg: ["/dev/vda"] diff --git a/service/test/agama/storage/proposal_settings_conversion/to_y2storage_test.rb b/service/test/agama/storage/proposal_settings_conversions/to_y2storage_test.rb similarity index 100% rename from service/test/agama/storage/proposal_settings_conversion/to_y2storage_test.rb rename to service/test/agama/storage/proposal_settings_conversions/to_y2storage_test.rb diff --git a/service/test/agama/storage/proposal_settings_test.rb b/service/test/agama/storage/proposal_settings_test.rb index 82fcc58d8b..0e2aafcdad 100644 --- a/service/test/agama/storage/proposal_settings_test.rb +++ b/service/test/agama/storage/proposal_settings_test.rb @@ -20,6 +20,7 @@ # find current contact information at www.suse.com. require_relative "../../test_helper" +require "agama/config" require "agama/storage/device_settings" require "agama/storage/proposal_settings" require "agama/storage/volume" @@ -170,4 +171,30 @@ include_examples "volume devices" end end + + describe ".new_from_json" do + let(:config) { Agama::Config.new } + + let(:settings_json) do + { + target: { + disk: "/dev/vda" + } + } + end + + it "generates a proposal settings from JSON according to schema" do + result = described_class.new_from_json(settings_json, config: config) + expect(result).to be_a(Agama::Storage::ProposalSettings) + end + end + + describe "#to_json_settings" do + let(:proposal_settings) { Agama::Storage::ProposalSettings.new } + + it "generates a JSON hash according to schema" do + result = proposal_settings.to_json_settings + expect(result).to be_a(Hash) + end + end end diff --git a/service/test/agama/storage/volume_conversion_test.rb b/service/test/agama/storage/volume_conversion_test.rb index af2d9175bf..b63866edf5 100644 --- a/service/test/agama/storage/volume_conversion_test.rb +++ b/service/test/agama/storage/volume_conversion_test.rb @@ -20,8 +20,6 @@ # find current contact information at www.suse.com. require_relative "../../test_helper" -require "agama/config" -require "agama/storage/proposal_settings" require "agama/storage/volume" require "agama/storage/volume_conversion" require "y2storage" @@ -44,30 +42,4 @@ expect(result).to be_a(Y2Storage::VolumeSpecification) end end - - describe "#from_schema" do - let(:config) { Agama::Config.new } - - let(:volume_schema) do - { - mount: { - path: "/test" - } - } - end - - it "generates a volume from settings according to the JSON schema" do - result = described_class.from_schema(volume_schema, config: config) - expect(result).to be_a(Agama::Storage::Volume) - end - end - - describe "#to_schema" do - let(:volume) { Agama::Storage::Volume.new("/test") } - - it "generates volume settings according to the JSON schema from a volume" do - result = described_class.to_schema(volume) - expect(result).to be_a(Hash) - end - end end diff --git a/service/test/agama/storage/volume_conversion/from_schema_test.rb b/service/test/agama/storage/volume_conversions/from_json_test.rb similarity index 83% rename from service/test/agama/storage/volume_conversion/from_schema_test.rb rename to service/test/agama/storage/volume_conversions/from_json_test.rb index 0a009e1257..579da8505b 100644 --- a/service/test/agama/storage/volume_conversion/from_schema_test.rb +++ b/service/test/agama/storage/volume_conversions/from_json_test.rb @@ -24,15 +24,15 @@ require "agama/config" require "agama/storage/volume" require "agama/storage/volume_templates_builder" -require "agama/storage/volume_conversion/from_schema" +require "agama/storage/volume_conversions/from_json" require "y2storage/disk_size" def default_volume(mount_path) Agama::Storage::VolumeTemplatesBuilder.new_from_config(config).for(mount_path) end -describe Agama::Storage::VolumeConversion::FromSchema do - subject { described_class.new(volume_schema, config: config) } +describe Agama::Storage::VolumeConversions::FromJSON do + subject { described_class.new(volume_json, config: config) } let(:config) { Agama::Config.new(config_data) } @@ -67,7 +67,7 @@ def default_volume(mount_path) end describe "#convert" do - let(:volume_schema) do + let(:volume_json) do { mount: { path: "/test", @@ -84,13 +84,13 @@ def default_volume(mount_path) } end - it "generates a volume with the expected outline from the config" do + it "generates a volume with the expected outline from JSON" do volume = subject.convert expect(volume.outline).to eq_outline(default_volume("/test").outline) end - it "generates a volume with the values provided from hash according to the JSON schema" do + it "generates a volume with the values provided from JSON" do volume = subject.convert expect(volume).to be_a(Agama::Storage::Volume) @@ -105,8 +105,8 @@ def default_volume(mount_path) expect(volume.btrfs.snapshots).to eq(false) end - context "when the hash settings is missing some values" do - let(:volume_schema) do + context "when the JSON is missing some values" do + let(:volume_json) do { mount: { path: "/test" @@ -129,8 +129,8 @@ def default_volume(mount_path) end end - context "when the hash settings does not indicate max size" do - let(:volume_schema) do + context "when the JSON does not indicate max size" do + let(:volume_json) do { mount: { path: "/test" @@ -148,7 +148,7 @@ def default_volume(mount_path) end end - context "when the hash settings indicates auto size for a supported volume" do + context "when the JSON indicates auto size for a supported volume" do let(:outline) do { "auto_size" => { @@ -157,7 +157,7 @@ def default_volume(mount_path) } end - let(:volume_schema) do + let(:volume_json) do { mount: { path: "/test" @@ -173,10 +173,10 @@ def default_volume(mount_path) end end - context "when the hash settings indicates auto size for an unsupported volume" do + context "when the JSON indicates auto size for an unsupported volume" do let(:outline) { {} } - let(:volume_schema) do + let(:volume_json) do { mount: { path: "/test" @@ -192,10 +192,10 @@ def default_volume(mount_path) end end - context "when the hash settings indicates a filesystem included in the outline" do + context "when the JSON indicates a filesystem included in the outline" do let(:outline) { { "filesystems" => ["btrfs", "ext4"] } } - let(:volume_schema) do + let(:volume_json) do { mount: { path: "/test" @@ -211,10 +211,10 @@ def default_volume(mount_path) end end - context "when the hash settings indicates a filesystem not included in the outline" do + context "when the JSON indicates a filesystem not included in the outline" do let(:outline) { { "filesystems" => ["btrfs"] } } - let(:volume_schema) do + let(:volume_json) do { mount: { path: "/test" @@ -230,10 +230,10 @@ def default_volume(mount_path) end end - context "when the hash settings indicates snapshots for a supported volume" do + context "when the JSON indicates snapshots for a supported volume" do let(:outline) { { "snapshots_configurable" => true } } - let(:volume_schema) do + let(:volume_json) do { mount: { path: "/test" @@ -253,10 +253,10 @@ def default_volume(mount_path) end end - context "when the D-Bus settings provide Snapshots for an unsupported volume" do + context "when the JSON indicates snapshots for an unsupported volume" do let(:outline) { { "snapshots_configurable" => false } } - let(:volume_schema) do + let(:volume_json) do { mount: { path: "/test" diff --git a/service/test/agama/storage/volume_conversion/from_y2storage_test.rb b/service/test/agama/storage/volume_conversions/from_y2storage_test.rb similarity index 100% rename from service/test/agama/storage/volume_conversion/from_y2storage_test.rb rename to service/test/agama/storage/volume_conversions/from_y2storage_test.rb diff --git a/service/test/agama/storage/volume_conversion/to_schema_test.rb b/service/test/agama/storage/volume_conversions/to_json_test.rb similarity index 93% rename from service/test/agama/storage/volume_conversion/to_schema_test.rb rename to service/test/agama/storage/volume_conversions/to_json_test.rb index a3e757678e..11cb600328 100644 --- a/service/test/agama/storage/volume_conversion/to_schema_test.rb +++ b/service/test/agama/storage/volume_conversions/to_json_test.rb @@ -20,12 +20,12 @@ # find current contact information at www.suse.com. require_relative "../../../test_helper" -require "agama/storage/volume_conversion/to_schema" +require "agama/storage/volume_conversions/to_json" require "agama/storage/volume" require "y2storage/filesystems/type" require "y2storage/disk_size" -describe Agama::Storage::VolumeConversion::ToSchema do +describe Agama::Storage::VolumeConversions::ToJSON do let(:default_volume) { Agama::Storage::Volume.new("/test") } let(:custom_volume1) do @@ -48,7 +48,7 @@ end describe "#convert" do - it "converts the volume to the proper hash according to the JSON schema" do + it "converts the volume to a JSON hash according to schema" do # @todo Check whether the result matches the JSON schema. expect(described_class.new(default_volume).convert).to eq( diff --git a/service/test/agama/storage/volume_conversion/to_y2storage_test.rb b/service/test/agama/storage/volume_conversions/to_y2storage_test.rb similarity index 100% rename from service/test/agama/storage/volume_conversion/to_y2storage_test.rb rename to service/test/agama/storage/volume_conversions/to_y2storage_test.rb diff --git a/service/test/agama/storage/volume_test.rb b/service/test/agama/storage/volume_test.rb new file mode 100644 index 0000000000..f0e4ae8a7d --- /dev/null +++ b/service/test/agama/storage/volume_test.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +# Copyright (c) [2024] 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 "agama/config" +require "agama/storage/volume" + +describe Agama::Storage::Volume do + describe ".new_from_json" do + let(:config) { Agama::Config.new } + + let(:volume_json) do + { + mount: { + path: "/test" + } + } + end + + it "generates a volume from JSON according to schema" do + result = described_class.new_from_json(volume_json, config: config) + expect(result).to be_a(Agama::Storage::Volume) + expect(result.mount_path).to eq("/test") + end + end + + describe "#to_json_settngs" do + let(:volume) { Agama::Storage::Volume.new("/test") } + + it "generates a JSON hash according to schema" do + result = volume.to_json_settings + expect(result).to be_a(Hash) + end + end +end From a3bc41ec2380883b2fef2f0d88661ab1bcc0d2ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Iv=C3=A1n=20L=C3=B3pez=20Gonz=C3=A1lez?= Date: Fri, 28 Jun 2024 07:28:33 +0100 Subject: [PATCH 2/2] storage: move Y2Storage conversions --- .../lib/agama/storage/proposal_settings.rb | 8 +++ .../storage/proposal_settings_conversion.rb | 50 ------------------- .../storage/proposal_settings_conversions.rb | 2 + .../from_y2storage.rb | 8 +-- .../to_y2storage.rb | 7 ++- .../storage/proposal_strategies/autoyast.rb | 3 +- .../storage/proposal_strategies/guided.rb | 10 ++-- service/lib/agama/storage/volume.rb | 10 ++-- .../lib/agama/storage/volume_conversion.rb | 46 ----------------- .../lib/agama/storage/volume_conversions.rb | 2 + .../from_y2storage.rb | 2 +- .../to_y2storage.rb | 2 +- .../proposal_settings_conversion_test.rb | 49 ------------------ .../from_y2storage_test.rb | 4 +- .../to_y2storage_test.rb | 4 +- .../agama/storage/proposal_settings_test.rb | 12 +++++ .../agama/storage/volume_conversion_test.rb | 45 ----------------- .../volume_conversions/from_y2storage_test.rb | 4 +- .../volume_conversions/to_y2storage_test.rb | 4 +- service/test/agama/storage/volume_test.rb | 10 ++++ 20 files changed, 65 insertions(+), 217 deletions(-) delete mode 100644 service/lib/agama/storage/proposal_settings_conversion.rb rename service/lib/agama/storage/{proposal_settings_conversion => proposal_settings_conversions}/from_y2storage.rb (93%) rename service/lib/agama/storage/{proposal_settings_conversion => proposal_settings_conversions}/to_y2storage.rb (96%) delete mode 100644 service/lib/agama/storage/volume_conversion.rb rename service/lib/agama/storage/{volume_conversion => volume_conversions}/from_y2storage.rb (99%) rename service/lib/agama/storage/{volume_conversion => volume_conversions}/to_y2storage.rb (99%) delete mode 100644 service/test/agama/storage/proposal_settings_conversion_test.rb delete mode 100644 service/test/agama/storage/volume_conversion_test.rb diff --git a/service/lib/agama/storage/proposal_settings.rb b/service/lib/agama/storage/proposal_settings.rb index cbf3d297a8..8ec12201ae 100644 --- a/service/lib/agama/storage/proposal_settings.rb +++ b/service/lib/agama/storage/proposal_settings.rb @@ -103,6 +103,14 @@ def to_json_settings Storage::ProposalSettingsConversions::ToJSON.new(self).convert end + # Generates Y2Storage proposal settings. + # + # @param config [Config] + # @return [Y2Storage::ProposalSettings] + def to_y2storage(config:) + Storage::ProposalSettingsConversions::ToY2Storage.new(self, config: config).convert + end + private # Device used for booting. diff --git a/service/lib/agama/storage/proposal_settings_conversion.rb b/service/lib/agama/storage/proposal_settings_conversion.rb deleted file mode 100644 index f8439c7531..0000000000 --- a/service/lib/agama/storage/proposal_settings_conversion.rb +++ /dev/null @@ -1,50 +0,0 @@ -# frozen_string_literal: true - -# Copyright (c) [2023-2024] 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 "agama/storage/proposal_settings_conversion/from_y2storage" -require "agama/storage/proposal_settings_conversion/to_y2storage" - -module Agama - module Storage - # Conversions for the proposal settings. - module ProposalSettingsConversion - # Performs conversion from Y2Storage. - # - # @param y2storage_settings [Y2Storage::ProposalSettings] - # @param settings [Agama::Storage::ProposalSettings] - # - # @return [Agama::Storage::ProposalSettings] - def self.from_y2storage(y2storage_settings, settings) - FromY2Storage.new(y2storage_settings, settings).convert - end - - # Performs conversion to Y2Storage. - # - # @param settings [Agama::Storage::ProposalSettings] - # @param config [Agama::Config] - # - # @return [Y2Storage::ProposalSettings] - def self.to_y2storage(settings, config:) - ToY2Storage.new(settings, config: config).convert - end - end - end -end diff --git a/service/lib/agama/storage/proposal_settings_conversions.rb b/service/lib/agama/storage/proposal_settings_conversions.rb index 6ad03db45f..11517c4b58 100644 --- a/service/lib/agama/storage/proposal_settings_conversions.rb +++ b/service/lib/agama/storage/proposal_settings_conversions.rb @@ -20,7 +20,9 @@ # find current contact information at www.suse.com. require "agama/storage/proposal_settings_conversions/from_json" +require "agama/storage/proposal_settings_conversions/from_y2storage" require "agama/storage/proposal_settings_conversions/to_json" +require "agama/storage/proposal_settings_conversions/to_y2storage" module Agama module Storage diff --git a/service/lib/agama/storage/proposal_settings_conversion/from_y2storage.rb b/service/lib/agama/storage/proposal_settings_conversions/from_y2storage.rb similarity index 93% rename from service/lib/agama/storage/proposal_settings_conversion/from_y2storage.rb rename to service/lib/agama/storage/proposal_settings_conversions/from_y2storage.rb index 475e53e25c..4a5f26983f 100644 --- a/service/lib/agama/storage/proposal_settings_conversion/from_y2storage.rb +++ b/service/lib/agama/storage/proposal_settings_conversions/from_y2storage.rb @@ -19,11 +19,11 @@ # To contact SUSE LLC about this file by physical or electronic mail, you may # find current contact information at www.suse.com. -require "agama/storage/volume_conversion" +require "agama/storage/volume_conversions/from_y2storage" module Agama module Storage - module ProposalSettingsConversion + module ProposalSettingsConversions # Proposal settings conversion from Y2Storage. # # @note This class does not perform a real conversion from Y2Storage settings. Instead of @@ -60,7 +60,7 @@ def convert # Recovers space actions. # # @note Space actions are generated in the conversion of the settings to Y2Storage format, - # see {ProposalSettingsConversion::ToY2Storage}. + # see {ProposalSettingsConversions::ToY2Storage}. # # @param target [Agama::Storage::ProposalSettings] def space_actions_conversion(target) @@ -77,7 +77,7 @@ def volumes_conversion(target) # @param volume [Agama::Storage::Volume] # @return [Agama::Storage::Volume] def volume_conversion(volume) - VolumeConversion.from_y2storage(volume) + VolumeConversions::FromY2Storage.new(volume).convert end end end diff --git a/service/lib/agama/storage/proposal_settings_conversion/to_y2storage.rb b/service/lib/agama/storage/proposal_settings_conversions/to_y2storage.rb similarity index 96% rename from service/lib/agama/storage/proposal_settings_conversion/to_y2storage.rb rename to service/lib/agama/storage/proposal_settings_conversions/to_y2storage.rb index eacee97742..72572e491a 100644 --- a/service/lib/agama/storage/proposal_settings_conversion/to_y2storage.rb +++ b/service/lib/agama/storage/proposal_settings_conversions/to_y2storage.rb @@ -21,12 +21,11 @@ require "y2storage" require "agama/storage/device_settings" -require "agama/storage/volume_conversion" require "agama/storage/volume_templates_builder" module Agama module Storage - module ProposalSettingsConversion + module ProposalSettingsConversions # Proposal settings conversion to Y2Storage. class ToY2Storage # @param settings [Agama::Storage::ProposalSettings] @@ -142,10 +141,10 @@ def space_policy_conversion(target) def volumes_conversion(target) target.swap_reuse = :none - volumes = settings.volumes.map { |v| VolumeConversion.to_y2storage(v) } + volumes = settings.volumes.map(&:to_y2storage) disabled_volumes = missing_volumes.map do |volume| - VolumeConversion.to_y2storage(volume).tap { |v| v.proposed = false } + volume.to_y2storage.tap { |v| v.proposed = false } end target.volumes = volumes + disabled_volumes diff --git a/service/lib/agama/storage/proposal_strategies/autoyast.rb b/service/lib/agama/storage/proposal_strategies/autoyast.rb index 832e1b1eb2..e076672095 100644 --- a/service/lib/agama/storage/proposal_strategies/autoyast.rb +++ b/service/lib/agama/storage/proposal_strategies/autoyast.rb @@ -22,7 +22,6 @@ require "agama/storage/proposal_strategies/base" require "agama/storage/proposal_settings" require "agama/storage/proposal_settings_reader" -require "agama/storage/proposal_settings_conversion" module Agama module Storage @@ -78,7 +77,7 @@ def issues # @return [Y2Storage::ProposalSettings] def proposal_settings agama_default = ProposalSettingsReader.new(config).read - ProposalSettingsConversion.to_y2storage(agama_default, config: config) + agama_default.to_y2storage(config: config) end # Agama issue equivalent to the given AutoYaST issue diff --git a/service/lib/agama/storage/proposal_strategies/guided.rb b/service/lib/agama/storage/proposal_strategies/guided.rb index 105b86d46d..2137b033ea 100644 --- a/service/lib/agama/storage/proposal_strategies/guided.rb +++ b/service/lib/agama/storage/proposal_strategies/guided.rb @@ -21,7 +21,7 @@ require "agama/storage/proposal_strategies/base" require "agama/storage/device_settings" -require "agama/storage/proposal_settings_conversion" +require "agama/storage/proposal_settings_conversions/from_y2storage" module Agama module Storage @@ -43,7 +43,7 @@ def initialize(config, logger, input_settings) # Settings used for calculating the proposal. # # @note Some values are recoverd from Y2Storage, see - # {ProposalSettingsConversion::FromY2Storage} + # {ProposalSettingsConversions::FromY2Storage} # # @return [ProposalSettings] attr_reader :settings @@ -55,7 +55,9 @@ def calculate proposal.propose ensure storage_manager.proposal = proposal - @settings = ProposalSettingsConversion.from_y2storage(proposal.settings, input_settings) + @settings = ProposalSettingsConversions::FromY2Storage + .new(proposal.settings, input_settings) + .convert end # @see Base#issues @@ -107,7 +109,7 @@ def missing_target_device?(settings) # @return [Y2Storage::GuidedProposal] def guided_proposal(settings) Y2Storage::MinGuidedProposal.new( - settings: ProposalSettingsConversion.to_y2storage(settings, config: config), + settings: settings.to_y2storage(config: config), devicegraph: probed_devicegraph, disk_analyzer: disk_analyzer ) diff --git a/service/lib/agama/storage/volume.rb b/service/lib/agama/storage/volume.rb index 76b4a39417..023c4df78a 100644 --- a/service/lib/agama/storage/volume.rb +++ b/service/lib/agama/storage/volume.rb @@ -30,9 +30,6 @@ module Agama module Storage # A volume represents the characteristics of a file system to create in the system. - # - # A volume is converted to D-Bus and to Y2Storage formats in order to provide the volume - # information with the expected representation, see {VolumeConversion}. class Volume extend Forwardable @@ -130,6 +127,13 @@ def self.new_from_json(volume_json, config:) def to_json_settings Storage::VolumeConversions::ToJSON.new(self).convert end + + # Generates a Y2Storage volume. + # + # @return [Y2Storage::VolumeSpecification] + def to_y2storage + Storage::VolumeConversions::ToY2Storage.new(self).convert + end end end end diff --git a/service/lib/agama/storage/volume_conversion.rb b/service/lib/agama/storage/volume_conversion.rb deleted file mode 100644 index 432f39034f..0000000000 --- a/service/lib/agama/storage/volume_conversion.rb +++ /dev/null @@ -1,46 +0,0 @@ -# frozen_string_literal: true - -# Copyright (c) [2023-2024] 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 "agama/storage/volume_conversion/from_y2storage" -require "agama/storage/volume_conversion/to_y2storage" - -module Agama - module Storage - # Conversions for a volume - module VolumeConversion - # Performs conversion from Y2Storage. - # - # @param volume [Agama::Storage::Volume] - # @return [Agama::Storage::Volume] - def self.from_y2storage(volume) - FromY2Storage.new(volume).convert - end - - # Performs conversion to Y2Storage. - # - # @param volume [Agama::Storage::Volume] - # @return [Y2Storage::VolumeSpecification] - def self.to_y2storage(volume) - ToY2Storage.new(volume).convert - end - end - end -end diff --git a/service/lib/agama/storage/volume_conversions.rb b/service/lib/agama/storage/volume_conversions.rb index 3c37c4d329..e8a2518a78 100644 --- a/service/lib/agama/storage/volume_conversions.rb +++ b/service/lib/agama/storage/volume_conversions.rb @@ -20,7 +20,9 @@ # find current contact information at www.suse.com. require "agama/storage/volume_conversions/from_json" +require "agama/storage/volume_conversions/from_y2storage" require "agama/storage/volume_conversions/to_json" +require "agama/storage/volume_conversions/to_y2storage" module Agama module Storage diff --git a/service/lib/agama/storage/volume_conversion/from_y2storage.rb b/service/lib/agama/storage/volume_conversions/from_y2storage.rb similarity index 99% rename from service/lib/agama/storage/volume_conversion/from_y2storage.rb rename to service/lib/agama/storage/volume_conversions/from_y2storage.rb index 0c493fe707..0ad025eb1b 100644 --- a/service/lib/agama/storage/volume_conversion/from_y2storage.rb +++ b/service/lib/agama/storage/volume_conversions/from_y2storage.rb @@ -23,7 +23,7 @@ module Agama module Storage - module VolumeConversion + module VolumeConversions # Volume conversion from Y2Storage. # # @note This class does not perform a real conversion from Y2Storage. Instead of that, it diff --git a/service/lib/agama/storage/volume_conversion/to_y2storage.rb b/service/lib/agama/storage/volume_conversions/to_y2storage.rb similarity index 99% rename from service/lib/agama/storage/volume_conversion/to_y2storage.rb rename to service/lib/agama/storage/volume_conversions/to_y2storage.rb index dd258c3bce..be7a7d43d5 100644 --- a/service/lib/agama/storage/volume_conversion/to_y2storage.rb +++ b/service/lib/agama/storage/volume_conversions/to_y2storage.rb @@ -24,7 +24,7 @@ module Agama module Storage - module VolumeConversion + module VolumeConversions # Volume conversion to Y2Storage. class ToY2Storage # @param volume [Agama::Storage::Volume] diff --git a/service/test/agama/storage/proposal_settings_conversion_test.rb b/service/test/agama/storage/proposal_settings_conversion_test.rb deleted file mode 100644 index dfb1db457a..0000000000 --- a/service/test/agama/storage/proposal_settings_conversion_test.rb +++ /dev/null @@ -1,49 +0,0 @@ -# frozen_string_literal: true - -# Copyright (c) [2023-2024] 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 "agama/storage/proposal_settings" -require "agama/storage/proposal_settings_conversion" -require "y2storage" - -describe Agama::Storage::ProposalSettingsConversion do - describe "#from_y2storage" do - let(:y2storage_settings) { Y2Storage::ProposalSettings.new } - - let(:settings) { Agama::Storage::ProposalSettings.new } - - it "generates proposal settings from Y2Storage settings" do - result = described_class.from_y2storage(y2storage_settings, settings) - expect(result).to be_a(Agama::Storage::ProposalSettings) - end - end - - describe "#to_y2storage" do - let(:config) { Agama::Config.new } - - let(:settings) { Agama::Storage::ProposalSettings.new } - - it "generates Y2Storage settings from proposal settings" do - result = described_class.to_y2storage(settings, config: config) - expect(result).to be_a(Y2Storage::ProposalSettings) - end - end -end diff --git a/service/test/agama/storage/proposal_settings_conversions/from_y2storage_test.rb b/service/test/agama/storage/proposal_settings_conversions/from_y2storage_test.rb index 93f0f9e519..cb4b4b4dd0 100644 --- a/service/test/agama/storage/proposal_settings_conversions/from_y2storage_test.rb +++ b/service/test/agama/storage/proposal_settings_conversions/from_y2storage_test.rb @@ -22,10 +22,10 @@ require_relative "../../../test_helper" require "agama/config" require "agama/storage/proposal_settings" -require "agama/storage/proposal_settings_conversion/from_y2storage" +require "agama/storage/proposal_settings_conversions/from_y2storage" require "y2storage" -describe Agama::Storage::ProposalSettingsConversion::FromY2Storage do +describe Agama::Storage::ProposalSettingsConversions::FromY2Storage do subject { described_class.new(y2storage_settings, original_settings) } let(:y2storage_settings) do diff --git a/service/test/agama/storage/proposal_settings_conversions/to_y2storage_test.rb b/service/test/agama/storage/proposal_settings_conversions/to_y2storage_test.rb index 48918aa4bb..82b5d6a110 100644 --- a/service/test/agama/storage/proposal_settings_conversions/to_y2storage_test.rb +++ b/service/test/agama/storage/proposal_settings_conversions/to_y2storage_test.rb @@ -24,10 +24,10 @@ require "agama/config" require "agama/storage/device_settings" require "agama/storage/proposal_settings" -require "agama/storage/proposal_settings_conversion/to_y2storage" +require "agama/storage/proposal_settings_conversions/to_y2storage" require "y2storage" -describe Agama::Storage::ProposalSettingsConversion::ToY2Storage do +describe Agama::Storage::ProposalSettingsConversions::ToY2Storage do include Agama::RSpec::StorageHelpers subject { described_class.new(settings, config: config) } diff --git a/service/test/agama/storage/proposal_settings_test.rb b/service/test/agama/storage/proposal_settings_test.rb index 0e2aafcdad..2d509f4fd4 100644 --- a/service/test/agama/storage/proposal_settings_test.rb +++ b/service/test/agama/storage/proposal_settings_test.rb @@ -24,6 +24,7 @@ require "agama/storage/device_settings" require "agama/storage/proposal_settings" require "agama/storage/volume" +require "y2storage/proposal_settings" describe Agama::Storage::ProposalSettings do describe "#default_boot_device" do @@ -197,4 +198,15 @@ expect(result).to be_a(Hash) end end + + describe "#to_y2storage" do + let(:config) { Agama::Config.new } + + let(:settings) { Agama::Storage::ProposalSettings.new } + + it "generates Y2Storage settings from proposal settings" do + result = subject.to_y2storage(config: config) + expect(result).to be_a(Y2Storage::ProposalSettings) + end + end end diff --git a/service/test/agama/storage/volume_conversion_test.rb b/service/test/agama/storage/volume_conversion_test.rb deleted file mode 100644 index b63866edf5..0000000000 --- a/service/test/agama/storage/volume_conversion_test.rb +++ /dev/null @@ -1,45 +0,0 @@ -# frozen_string_literal: true - -# Copyright (c) [2023-2024] 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 "agama/storage/volume" -require "agama/storage/volume_conversion" -require "y2storage" - -describe Agama::Storage::VolumeConversion do - describe "#from_y2storage" do - let(:volume) { Agama::Storage::Volume.new("/test") } - - it "generates a volume" do - result = described_class.from_y2storage(volume) - expect(result).to be_a(Agama::Storage::Volume) - end - end - - describe "#to_y2storage" do - let(:volume) { Agama::Storage::Volume.new("/test") } - - it "generates a Y2Storage volume spec" do - result = described_class.to_y2storage(volume) - expect(result).to be_a(Y2Storage::VolumeSpecification) - end - end -end diff --git a/service/test/agama/storage/volume_conversions/from_y2storage_test.rb b/service/test/agama/storage/volume_conversions/from_y2storage_test.rb index ceecba866b..7fa66d558b 100644 --- a/service/test/agama/storage/volume_conversions/from_y2storage_test.rb +++ b/service/test/agama/storage/volume_conversions/from_y2storage_test.rb @@ -23,10 +23,10 @@ require_relative "../storage_helpers" require_relative "../../rspec/matchers/storage" require "agama/storage/volume" -require "agama/storage/volume_conversion/from_y2storage" +require "agama/storage/volume_conversions/from_y2storage" require "y2storage" -describe Agama::Storage::VolumeConversion::FromY2Storage do +describe Agama::Storage::VolumeConversions::FromY2Storage do include Agama::RSpec::StorageHelpers before { mock_storage } diff --git a/service/test/agama/storage/volume_conversions/to_y2storage_test.rb b/service/test/agama/storage/volume_conversions/to_y2storage_test.rb index 8b52a1372f..5c7ae44b53 100644 --- a/service/test/agama/storage/volume_conversions/to_y2storage_test.rb +++ b/service/test/agama/storage/volume_conversions/to_y2storage_test.rb @@ -20,10 +20,10 @@ # find current contact information at www.suse.com. require_relative "../../../test_helper" -require "agama/storage/volume_conversion/to_y2storage" +require "agama/storage/volume_conversions/to_y2storage" require "y2storage" -describe Agama::Storage::VolumeConversion::ToY2Storage do +describe Agama::Storage::VolumeConversions::ToY2Storage do subject { described_class.new(volume) } describe "#convert" do diff --git a/service/test/agama/storage/volume_test.rb b/service/test/agama/storage/volume_test.rb index f0e4ae8a7d..2c46c68acc 100644 --- a/service/test/agama/storage/volume_test.rb +++ b/service/test/agama/storage/volume_test.rb @@ -22,6 +22,7 @@ require_relative "../../test_helper" require "agama/config" require "agama/storage/volume" +require "y2storage/volume_specification" describe Agama::Storage::Volume do describe ".new_from_json" do @@ -50,4 +51,13 @@ expect(result).to be_a(Hash) end end + + describe "#to_y2storage" do + let(:volume) { Agama::Storage::Volume.new("/test") } + + it "generates a Y2Storage volume spec" do + result = volume.to_y2storage + expect(result).to be_a(Y2Storage::VolumeSpecification) + end + end end