Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Aug 27, 2024
1 parent e45d584 commit eccb6b0
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
2 changes: 1 addition & 1 deletion service/lib/agama/storage/configs/filesystem_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
38 changes: 37 additions & 1 deletion service/test/agama/storage/config_conversions/from_json_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: "/" } }

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit eccb6b0

Please sign in to comment.