Skip to content

Commit

Permalink
Add support to a drive corresponding to a RAID
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Apr 16, 2020
1 parent 608b6cc commit 8add7f4
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/lib/autoinstall/storage_controller.rb
Expand Up @@ -40,7 +40,8 @@ def initialize(partitioning)
end

TYPES_MAP = {
disk: :CT_DISK
disk: :CT_DISK,
raid: :CT_RAID
}.freeze

# Adds a new drive section of the given type
Expand Down
14 changes: 8 additions & 6 deletions src/lib/autoinstall/widgets/storage/add_drive_button.rb
Expand Up @@ -50,19 +50,21 @@ def label
# @return [Array<Symbol,String>]
def items
[
[:add_disk, _("Disk")]
[:add_disk, _("Disk")],
[:add_raid, _("RAID")]
]
end

# Handles the events
#
# @param event [Hash] Event to handle
def handle(event)
case event["ID"]
when :add_disk
controller.add_drive(:disk)
:redraw
end
event_id = event["ID"].to_s
return unless event_id.start_with?("add_")

type = event_id.split("_", 2).last
controller.add_drive(type.to_sym)
:redraw
end

private
Expand Down
31 changes: 20 additions & 11 deletions src/lib/autoinstall/widgets/storage/overview_tree_pager.rb
Expand Up @@ -21,6 +21,7 @@
require "cwm/tree_pager"
require "autoinstall/widgets/storage/overview_tree"
require "autoinstall/widgets/storage/disk_page"
require "autoinstall/widgets/storage/raid_page"
require "autoinstall/widgets/storage/partition_page"
require "autoinstall/widgets/storage/add_drive_button"
require "autoinstall/ui_state"
Expand Down Expand Up @@ -85,23 +86,31 @@ def tree
# @param section [Y2Storage::AutoinstProfile::DriveSection] Drive section
# @return [CWM::PagerTreeItem] Tree item
def drive_item(section)
case section.type
when :CT_DISK
disk_item(section)
end
page_klass = page_klass_for(section.type)
page = page_klass.new(controller, section)
CWM::PagerTreeItem.new(page, children: partition_items(section))
end

# Determines the widget class for the given type
def page_klass_for(type)
type ||= :CT_DISK
name = type.to_s.split("_", 2).last.downcase
Y2Autoinstallation::Widgets::Storage.const_get("#{name.capitalize}Page")
rescue NameError
Y2Autoinstallation::Widgets::Storage::DiskPage
end

# @param section [Y2Storage::AutoinstProfile::DriveSection] Drive section corresponding
# to a disk
def disk_item(section)
children = section.partitions.map do |part|
# Returns the pages for a given list of partition sections
#
# @param partitions [Array<Y2Storage::AutoinstProfile::PartitionSection>]
# List of partition partition sections
def partition_items(drive)
drive.partitions.map do |part|
part_page = Y2Autoinstallation::Widgets::Storage::PartitionPage.new(
controller, section, part
controller, drive, part
)
CWM::PagerTreeItem.new(part_page)
end
page = Y2Autoinstallation::Widgets::Storage::DiskPage.new(controller, section)
CWM::PagerTreeItem.new(page, children: children)
end
end
end
Expand Down
92 changes: 92 additions & 0 deletions src/lib/autoinstall/widgets/storage/raid_page.rb
@@ -0,0 +1,92 @@
# Copyright (c) [2020] 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 "yast"
require "cwm/page"
require "autoinstall/widgets/storage/add_children_button"
require "autoinstall/widgets/storage/raid_name"
# require "autoinstall/widgets/storage/raid_options"

module Y2Autoinstallation
module Widgets
module Storage
# This page allows to edit a `drive` section representing a RAID device
class RaidPage < ::CWM::Page
# Constructor
#
# @param controller [Y2Autoinstallation::StorageController] UI controller
# @param section [Y2Storage::AutoinstProfile::DriveSection] Drive section corresponding
# to a RAID
def initialize(controller, section)
textdomain "autoinst"
@controller = controller
@section = section
super()
self.widget_id = "raid_page:#{section.object_id}"
self.handle_all_events = true
end

# @macro seeAbstractWidget
def label
format(_("RAID %{device}"), device: section.device)
end

# @macro seeCustomWidget
def contents
VBox(
Left(Heading(_("RAID"))),
VBox(
Left(raid_name_widget),
VStretch()
),
HBox(
HStretch(),
AddChildrenButton.new(controller, section)
)
)
end

# @macro seeAbstractWidget
def init
raid_name_widget.value = section.device
end

# @macro seeAbstractWidget
def store
section.device = raid_name_widget.value
end

private

# @return [Y2Autoinstallation::StorageController]
attr_reader :controller

# @return [Y2Storage::AutoinstProfile::DriveSection]
attr_reader :section

# RAID name input field
#
# @return [RaidName]
def raid_name_widget
Y2Autoinstallation::Widgets::Storage::RaidName.new
end
end
end
end
end
18 changes: 15 additions & 3 deletions test/lib/widgets/storage/add_drive_button_test.rb
Expand Up @@ -19,6 +19,7 @@

require_relative "../../../test_helper"
require "autoinstall/widgets/storage/add_drive_button"
require "autoinstall/storage_controller"

describe Y2Autoinstallation::Widgets::Storage::AddDriveButton do
subject { described_class.new(controller) }
Expand All @@ -29,13 +30,24 @@
end

describe "#handle" do
let(:event) do
{ "ID" => :add_disk }
context "adding a disk" do
let(:event) do
{ "ID" => :add_disk }
end

it "adds a disk" do
expect(controller).to receive(:add_drive).with(:disk)
subject.handle(event)
end
end

context "adding a disk" do
let(:event) do
{ "ID" => :add_raid }
end

it "adds a disk" do
expect(controller).to receive(:add_drive).with(:disk)
expect(controller).to receive(:add_drive).with(:raid)
subject.handle(event)
end
end
Expand Down
26 changes: 20 additions & 6 deletions test/lib/widgets/storage/overview_tree_pager_test.rb
Expand Up @@ -25,18 +25,32 @@
describe Y2Autoinstallation::Widgets::Storage::OverviewTreePager do
subject { described_class.new(controller) }

let(:partitioning) do
Y2Storage::AutoinstProfile::PartitioningSection.new_from_hashes(
[{ "mount" => "/dev/sda" }]
)
let(:partitioning) { Y2Storage::AutoinstProfile::PartitioningSection.new_from_hashes(attrs) }
let(:attrs) do
[{ "device" => "/dev/sda", "partitions" => [{ "mount" => "/" }] },
{ "type" => :CT_RAID }]
end

let(:controller) { Y2Autoinstallation::StorageController.new(partitioning) }

describe "#items" do
it "returns one item for each drive" do
items = subject.items
expect(items.size).to eq(1)
expect(subject.items.map(&:page)).to contain_exactly(
an_instance_of(Y2Autoinstallation::Widgets::Storage::DiskPage),
an_instance_of(Y2Autoinstallation::Widgets::Storage::RaidPage)
)
end

context "when the type is unknown" do
let(:attrs) do
[{ "type" => :CT_UNKNOWN }]
end

it "uses considers that the section corresponds to a disk" do
expect(subject.items.map(&:page)).to contain_exactly(
an_instance_of(Y2Autoinstallation::Widgets::Storage::DiskPage)
)
end
end
end
end
2 changes: 1 addition & 1 deletion test/lib/widgets/storage/partition_page_test.rb
Expand Up @@ -41,7 +41,7 @@
describe "#label" do
context "when the partition is used as a filesystem" do
context "when the partition mount point is defined" do
let(:partition_hash) { { "mount" => "/opt"} }
let(:partition_hash) { { "mount" => "/opt" } }

it "includes the mount point" do
expect(subject.label).to include("/opt")
Expand Down
55 changes: 55 additions & 0 deletions test/lib/widgets/storage/raid_page_test.rb
@@ -0,0 +1,55 @@
# Copyright (c) [2020] 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 "autoinstall/widgets/storage/raid_page"
require "autoinstall/storage_controller"
require "y2storage/autoinst_profile"
require "cwm/rspec"

describe Y2Autoinstallation::Widgets::Storage::RaidPage do
subject { described_class.new(controller, drive) }

let(:partitioning) do
Y2Storage::AutoinstProfile::PartitioningSection.new_from_hashes(
[{ "device" => "/dev/md0", "type" => :CT_RAID }]
)
end
let(:drive) { partitioning.drives.first }
let(:controller) { Y2Autoinstallation::StorageController.new(partitioning) }

let(:raid_name_widget) do
instance_double(
Y2Autoinstallation::Widgets::Storage::RaidName,
value: "/dev/md1"
)
end

before do
allow(Y2Autoinstallation::Widgets::Storage::RaidName)
.to receive(:new).and_return(raid_name_widget)
end

describe "#init" do
it "sets the widget initial values" do
expect(raid_name_widget).to receive(:value=).with("/dev/md0")
subject.init
end
end
end

0 comments on commit 8add7f4

Please sign in to comment.