Skip to content

Commit

Permalink
feat(storage): add boot to config model
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Dec 12, 2024
1 parent 5cf0904 commit 9d8129f
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# 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/config_conversions/from_model_conversions/boot"
require "agama/storage/config_conversions/from_model_conversions/boot_device"
require "agama/storage/config_conversions/from_model_conversions/config"
require "agama/storage/config_conversions/from_model_conversions/drive"
require "agama/storage/config_conversions/from_model_conversions/filesystem"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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/config_conversions/from_model_conversions/base"
require "agama/storage/config_conversions/from_model_conversions/boot_device"
require "agama/storage/configs/boot"

module Agama
module Storage
module ConfigConversions
module FromModelConversions
# Boot conversion from model according to the JSON schema.
class Boot < Base
private

# @see Base
# @return [Configs::Boot]
def default_config
Configs::Boot.new
end

# @see Base#conversions
# @return [Hash]
def conversions
{
configure: model_json[:configure],
device: convert_device
}
end

# @return [Configs::BootDevice, nil]
def convert_device
boot_device_model = model_json[:device]
return if boot_device_model.nil?

FromModelConversions::BootDevice.new(boot_device_model).convert
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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/config_conversions/from_model_conversions/base"
require "agama/storage/configs/boot_device"

module Agama
module Storage
module ConfigConversions
module FromModelConversions
# Boot device conversion from model according to the JSON schema.
class BootDevice < Base
private

# @see Base
# @return [Configs::Boot]
def default_config
Configs::BootDevice.new
end

# @see Base#conversions
# @return [Hash]
def conversions
{
default: model_json[:default],
device_alias: model_json[:value]
}
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# find current contact information at www.suse.com.

require "agama/storage/config_conversions/from_model_conversions/base"
require "agama/storage/config_conversions/from_model_conversions/boot"
require "agama/storage/config_conversions/from_model_conversions/drive"
require "agama/storage/config"

Expand All @@ -41,10 +42,19 @@ def default_config
# @return [Hash]
def conversions
{
boot: convert_boot,
drives: convert_drives
}
end

# @return [Configs::Boot, nil]
def convert_boot
boot_model = model_json[:boot]
return unless boot_model

FromModelConversions::Boot.new(boot_model).convert
end

# @return [Array<Configs::Drive>, nil]
def convert_drives
drive_models = model_json[:drives]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
# find current contact information at www.suse.com.

require "agama/storage/config_conversions/to_model_conversions/base"
require "agama/storage/config_conversions/to_model_conversions/boot"
require "agama/storage/config_conversions/to_model_conversions/boot_device"
require "agama/storage/config_conversions/to_model_conversions/config"
require "agama/storage/config_conversions/to_model_conversions/drive"
require "agama/storage/config_conversions/to_model_conversions/filesystem"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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/config_conversions/to_model_conversions/base"
require "agama/storage/config_conversions/to_model_conversions/boot_device"

module Agama
module Storage
module ConfigConversions
module ToModelConversions
# Boot config conversion to model according to the JSON schema.
class Boot < Base
# @param config [Storage::Boot]
def initialize(config)
super()
@config = config
end

private

# @see Base#conversions
def conversions
{
configure: config.configure?,
device: convert_device
}
end

# @return [Hash]
def convert_device
return unless config.configure?

ToModelConversions::BootDevice.new(config.device).convert
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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/config_conversions/to_model_conversions/base"

module Agama
module Storage
module ConfigConversions
module ToModelConversions
# Boot device config conversion to model according to the JSON schema.
class BootDevice < Base
# @param config [Storage::BootDevice]
def initialize(config)
super()
@config = config
end

private

# @see Base#conversions
def conversions
{
default: config.default?,
value: config.device_alias
}
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# find current contact information at www.suse.com.

require "agama/storage/config_conversions/to_model_conversions/base"
require "agama/storage/config_conversions/to_model_conversions/boot"
require "agama/storage/config_conversions/to_model_conversions/drive"

module Agama
Expand All @@ -38,7 +39,15 @@ def initialize(config)

# @see Base#conversions
def conversions
{ drives: convert_drives }
{
boot: convert_boot,
drives: convert_drives
}
end

# @return [Hash]
def convert_boot
ToModelConversions::Boot.new(config.boot).convert
end

# @return [Array<Hash>]
Expand Down
72 changes: 72 additions & 0 deletions service/test/agama/storage/config_conversions/from_model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

require_relative "../../../test_helper"
require "agama/config"
require "agama/storage/config"
require "agama/storage/config_conversions"
require "agama/storage/configs"
require "y2storage/encryption_method"
require "y2storage/filesystems/mount_by_type"
require "y2storage/filesystems/type"
Expand Down Expand Up @@ -797,12 +799,82 @@
context "with an empty JSON" do
let(:model_json) { {} }

it "sets #boot to the expected value" do
config = subject.convert
boot = config.boot
expect(boot).to be_a(Agama::Storage::Configs::Boot)
expect(boot.configure?).to eq(true)
expect(boot.device).to be_a(Agama::Storage::Configs::BootDevice)
expect(boot.device.default?).to eq(true)
expect(boot.device.device_alias).to be_nil
end

it "sets #drives to the expected value" do
config = subject.convert
expect(config.drives).to be_empty
end
end

context "with a JSON specifying 'boot'" do
let(:model_json) do
{
boot: {
configure: true,
device: {
default: default,
value: "disk1"
}
}
}
end

context "with a default boot device" do
let(:default) { true }

it "sets #boot to the expected value" do
config = subject.convert
boot = config.boot
expect(boot.configure?).to eq(true)
expect(boot.device.default?).to eq(true)
expect(boot.device.device_alias).to eq("disk1")
end
end

context "with a not default boot device" do
let(:default) { false }

it "sets #boot to the expected value" do
config = subject.convert
boot = config.boot
expect(boot.configure?).to eq(true)
expect(boot.device.default?).to eq(false)
expect(boot.device.device_alias).to eq("disk1")
end
end

context "with boot set to not be configured" do
let(:model_json) do
{
boot: {
configure: false,
device: {
default: false,
value: "disk1"
}
}
}
end

it "sets #boot to the expected value" do
config = subject.convert
boot = config.boot
expect(boot.configure?).to eq(false)
expect(boot.device.default?).to eq(false)
expect(boot.device.device_alias).to eq("disk1")
end
end
end

context "with a JSON specifying 'drives'" do
let(:model_json) do
{ drives: drives }
Expand Down
Loading

0 comments on commit 9d8129f

Please sign in to comment.