diff --git a/service/lib/agama/storage/config_conversions/filesystem/from_json.rb b/service/lib/agama/storage/config_conversions/filesystem/from_json.rb index ac637f2434..2e2d442595 100644 --- a/service/lib/agama/storage/config_conversions/filesystem/from_json.rb +++ b/service/lib/agama/storage/config_conversions/filesystem/from_json.rb @@ -21,6 +21,7 @@ require "agama/storage/config_conversions/filesystem_type/from_json" require "agama/storage/configs/filesystem" +require "y2storage/filesystems/mount_by_type" module Agama module Storage @@ -41,12 +42,15 @@ def convert(default = nil) default_config = default.dup || Configs::Filesystem.new default_config.tap do |config| + mount_by = convert_mount_by + type = convert_type(config.type) label = filesystem_json[:label] mkfs_options = filesystem_json[:mkfsOptions] config.path = filesystem_json[:path] config.mount_options = filesystem_json[:mountOptions] || [] - config.type = convert_type(config.type) + config.mount_by = mount_by if mount_by + config.type = type if type config.label = label if label config.mkfs_options = mkfs_options if mkfs_options end @@ -57,10 +61,21 @@ def convert(default = nil) # @return [Hash] attr_reader :filesystem_json - # @param default [Configs::Filesystem, nil] - # @return [Configs::FilesystemType] + # @return [Y2Storage::Filesystems::MountByType, nil] + def convert_mount_by + value = filesystem_json[:mountBy] + return unless value + + Y2Storage::Filesystems::MountByType.find(value.to_sym) + end + + # @param default [Configs::FilesystemType, nil] + # @return [Configs::FilesystemType, nil] def convert_type(default = nil) - FilesystemType::FromJSON.new(filesystem_json[:type]).convert(default) + filesystem_type_json = filesystem_json[:type] + return unless filesystem_type_json + + FilesystemType::FromJSON.new(filesystem_type_json).convert(default) end end end diff --git a/service/lib/agama/storage/config_conversions/filesystem_type/from_json.rb b/service/lib/agama/storage/config_conversions/filesystem_type/from_json.rb index 5df77eb2d9..960838f84c 100644 --- a/service/lib/agama/storage/config_conversions/filesystem_type/from_json.rb +++ b/service/lib/agama/storage/config_conversions/filesystem_type/from_json.rb @@ -27,48 +27,45 @@ module Agama module Storage module ConfigConversions module FilesystemType - # Filesystem conversion from JSON hash according to schema. + # Filesystem type conversion from JSON hash according to schema. class FromJSON - # @param filesystem_json [Hash, String, nil] - def initialize(filesystem_json) - @filesystem_json = filesystem_json + # @param filesystem_type_json [Hash, String] + def initialize(filesystem_type_json) + @filesystem_type_json = filesystem_type_json end # Performs the conversion from Hash according to the JSON schema. # - # @param default [Configs::Filesystem, nil] - # @return [Configs::Filesystem] + # @param default [Configs::FilesystemType, nil] + # @return [Configs::FilesystemType] def convert(default = nil) default_config = default.dup || Configs::FilesystemType.new default_config.tap do |config| btrfs = convert_btrfs(config.btrfs) - type = convert_type - config.fs_type = type if type + config.fs_type = convert_type config.btrfs = btrfs if btrfs end end private - # @return [Hash] - attr_reader :filesystem_json + # @return [Hash, String] + attr_reader :filesystem_type_json # @return [Y2Storage::Filesystems::Type] def convert_type - return if filesystem_json.nil? - - value = filesystem_json.is_a?(String) ? filesystem_json : "btrfs" + value = filesystem_type_json.is_a?(String) ? filesystem_type_json : "btrfs" Y2Storage::Filesystems::Type.find(value.to_sym) end - # @param default [Configs::Btrfs] + # @param default [Configs::Btrfs, nil] # @return [Configs::Btrfs, nil] def convert_btrfs(default = nil) - return if filesystem_json.nil? || filesystem_json.is_a?(String) + return if filesystem_type_json.nil? || filesystem_type_json.is_a?(String) - btrfs_json = filesystem_json[:btrfs] + btrfs_json = filesystem_type_json[:btrfs] default_config = default.dup || Configs::Btrfs.new default_config.tap do |config| diff --git a/service/lib/agama/storage/configs/filesystem_type.rb b/service/lib/agama/storage/configs/filesystem_type.rb index 6895f4e26e..d9ef3cddf1 100644 --- a/service/lib/agama/storage/configs/filesystem_type.rb +++ b/service/lib/agama/storage/configs/filesystem_type.rb @@ -23,7 +23,7 @@ module Agama module Storage module Configs class FilesystemType - # @return [Y2Storage::Filesystems::Type] + # @return [Y2Storage::Filesystems::Type, nil] attr_accessor :fs_type # @return [Configs::Btrfs, nil] diff --git a/service/test/agama/storage/config_conversions/from_json_test.rb b/service/test/agama/storage/config_conversions/from_json_test.rb index c648874d6d..a5204cce91 100644 --- a/service/test/agama/storage/config_conversions/from_json_test.rb +++ b/service/test/agama/storage/config_conversions/from_json_test.rb @@ -20,9 +20,11 @@ # find current contact information at www.suse.com. require_relative "../../../test_helper" -require "agama/storage/config_conversions/from_json" require "agama/config" +require "agama/storage/config_conversions/from_json" require "y2storage/encryption_method" +require "y2storage/filesystems/mount_by_type" +require "y2storage/filesystems/type" require "y2storage/pbkd_function" describe Agama::Storage::ConfigConversions::FromJSON do @@ -462,6 +464,28 @@ } end + let(:filesystem) do + { + path: "/", + type: "xfs", + label: "root", + mkfsOptions: ["version=2"], + mountOptions: ["rw"], + mountBy: "label" + } + end + + it "uses the specified attributes" do + config = subject.convert + filesystem = config.drives.first.filesystem + expect(filesystem.path).to eq "/" + expect(filesystem.type.fs_type).to eq Y2Storage::Filesystems::Type::XFS + expect(filesystem.label).to eq "root" + expect(filesystem.mkfs_options).to eq ["version=2"] + expect(filesystem.mount_options).to eq ["rw"] + expect(filesystem.mount_by).to eq Y2Storage::Filesystems::MountByType::LABEL + end + context "if the filesystem specification only contains a path" do let(:filesystem) { { path: "/" } } @@ -490,6 +514,18 @@ # expect(filesystem.type.btrfs.default_subvolume).to eq "" # expect(filesystem.type.btrfs.subvolumes.map(&:path)).to eq ["tmp"] end + + context "and the default filesystem type is not btrfs" do + let(:filesystem) do + { path: "/home", type: { btrfs: { snapshots: false } } } + end + + it "uses btrfs filesystem" do + config = subject.convert + filesystem = config.drives.first.filesystem + expect(filesystem.type.fs_type).to eq Y2Storage::Filesystems::Type::BTRFS + end + end end end