Skip to content

Commit

Permalink
Filter out loop and squashfs devices
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Oct 31, 2018
1 parent 7b8b592 commit b599ef4
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 10 deletions.
20 changes: 16 additions & 4 deletions src/lib/users/leaf_blk_device.rb
Expand Up @@ -49,8 +49,8 @@ def new_from_hash(hash)
parent = find_root_device(hash)
removable = hash["rm"] == "1"
new(
name: hash["name"], disk: parent["name"], fstype: hash["fstype"],
model: parent["model"], removable: removable
name: hash["name"], disk: parent["name"], model: parent["model"],
transport: parent["tran"], fstype: hash["fstype"], removable: removable
)
end

Expand Down Expand Up @@ -86,6 +86,9 @@ def find_root_device(hash)
# @return [String] Disk's kernel name
attr_reader :disk

# @return [Symbol] Disk's transport (:usb, :ata, etc.)
attr_reader :transport

# @return [Symbol] Filesystem type
attr_reader :fstype

Expand All @@ -97,14 +100,16 @@ def find_root_device(hash)
# Constructor
#
# @param name [String] Kernel name
# @param model [String] Hardware model
# @param disk [String] Disk's kernel name
# @param model [String] Hardware model
# @param transport [symbol] Transport
# @param fstype [Symbol] Filesystem type
# @param removable [Boolean] Indicates whether the device is mountable or not
def initialize(name:, model:, disk:, fstype:, removable:)
def initialize(name:, disk:, model:, transport: nil, fstype: nil, removable: false)
@name = name
@model = model
@disk = disk
@transport = transport.to_sym if transport
@fstype = fstype.to_sym if fstype
@removable = removable
end
Expand All @@ -115,5 +120,12 @@ def initialize(name:, model:, disk:, fstype:, removable:)
def filesystem?
!!fstype
end

# Determines whether the device has a transport
#
# @return [Boolean]
def transport?
!!transport
end
end
end
13 changes: 12 additions & 1 deletion src/lib/users/widgets/public_key_selector.rb
Expand Up @@ -166,9 +166,20 @@ def blk_devices_combo_box

# Returns a list of devices that can be selected
#
# Only the devices that meets those conditions are considered:
#
# * It has a transport (so loop devices are automatically discarded).
# * It has a filesystem but it is not squashfs, as it is used by the installer.
#
# The first condition should be enough. However, we want to avoid future problems if the lsblk
# authors decide to show some information in the 'TRAN' (transport) property for those devices
# that does not have one (for instance, something like 'none').
#
# @return [Array<LeafBlkDevice>] List of devices
def available_blk_devices
@available_blk_devices ||= LeafBlkDevice.all.select(&:filesystem?)
@available_blk_devices ||= LeafBlkDevice.all.select do |dev|
dev.filesystem? && dev.fstype != :squash && dev.transport?
end
end

# Selects the current block device
Expand Down
25 changes: 25 additions & 0 deletions test/lib/users/leaf_blk_device_test.rb
Expand Up @@ -64,5 +64,30 @@
end
end
end

describe "#transport?" do
subject do
Y2Users::LeafBlkDevice.new(
name: "/dev/sdb1", model: "MyBrand 8G", disk: "/dev/sdb", fstype: "ext4",
transport: transport, removable: true
)
end

context "when the device has a transport" do
let(:transport) { "usb" }

it "returns true" do
expect(subject.transport?).to eq(true)
end
end

context "when the device does not have a transport" do
let(:transport) { nil }

it "returns false" do
expect(subject.transport?).to eq(false)
end
end
end
end

34 changes: 29 additions & 5 deletions test/lib/users/widgets/public_key_selector_test.rb
Expand Up @@ -39,17 +39,33 @@
end

describe "#contents" do
let(:blk_devices) { [usb_with_fs, usb_no_fs] }
let(:blk_devices) { [usb_with_fs, usb_no_fs, squashfs, no_transport] }

let(:usb_with_fs) do
Y2Users::LeafBlkDevice.new(
name: "/dev/sdb1", model: "MyBrand 8G", disk: "/dev/sdb", fstype: :vfat, removable: true
name: "/dev/sdb1", model: "MyBrand 8G", disk: "/dev/sdb", transport: :usb,
fstype: :vfat, removable: true
)
end

let(:usb_no_fs) do
Y2Users::LeafBlkDevice.new(
name: "/dev/sdc1", model: "MyBrand 4G", disk: "/dev/sdc", fstype: nil, removable: true
name: "/dev/sdc1", model: "MyBrand 4G", disk: "/dev/sdc", transport: :usb,
fstype: nil, removable: true
)
end

let(:squashfs) do
Y2Users::LeafBlkDevice.new(
name: "/dev/some", model: "MyBrand 4G", disk: "/dev/sdc", transport: :unknown,
fstype: :squashfs, removable: true
)
end

let(:no_transport) do
Y2Users::LeafBlkDevice.new(
name: "/dev/loop1", model: "MyBrand 8G", disk: "/dev/sdb", transport: nil,
fstype: :unknown, removable: true
)
end

Expand All @@ -61,11 +77,19 @@
let(:value) { nil }

it "includes devices containing a filesystem" do
expect(widget.contents.to_s).to include("MyBrand 8G")
expect(widget.contents.to_s).to include("/dev/sdb1")
end

it "does not include devices which does not have a filesystem" do
expect(widget.contents.to_s).to_not include("MyBrand 4G")
expect(widget.contents.to_s).to_not include("/dev/sdc1")
end

it "does not include devices which has a squashfs filesystem" do
expect(widget.contents.to_s).to_not include("/dev/loop1")
end

it "does not include devices which does not have a transport" do
expect(widget.contents.to_s).to_not include("/dev/loop1")
end

context "when a key is selected" do
Expand Down

0 comments on commit b599ef4

Please sign in to comment.