Skip to content

Commit

Permalink
move md2partitions method and add tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Aug 5, 2014
1 parent 0ea488a commit 42e44d4
Show file tree
Hide file tree
Showing 6 changed files with 736 additions and 47 deletions.
46 changes: 0 additions & 46 deletions src/include/bootloader/routines/lilolike.rb
Expand Up @@ -251,52 +251,6 @@ def DetectDisks
nil
end

# Converts the md device to the list of devices building it
# @param [String] md_device string md device
# @return a map of devices (from device name to BIOS ID or nil if
# not detected) building the md device
def Md2Partitions(md_device)
ret = {}
tm = Storage.GetTargetMap
Builtins.foreach(tm) do |disk, descr_a|
descr = Convert.convert(
descr_a,
:from => "any",
:to => "map <string, any>"
)
bios_id_str = Ops.get_string(descr, "bios_id", "")
bios_id = 256 # maximum + 1 (means: no bios_id found)
bios_id = Builtins.tointeger(bios_id) if bios_id_str != ""
partitions = Ops.get_list(descr, "partitions", [])
Builtins.foreach(partitions) do |partition|
if Ops.get_string(partition, "used_by_device", "") == md_device
d = Ops.get_string(partition, "device", "")
Ops.set(ret, d, bios_id)
end
end
end
Builtins.y2milestone("Partitions building %1: %2", md_device, ret)
deep_copy(ret)
end

# Converts the md device to the first of its members
# @param [String] md_device string md device
# @return [String] one of devices building the md array
def Md2Partition(md_device)
devices = Md2Partitions(md_device)
return md_device if Builtins.size(devices) == 0
minimal = 129 # maximum + 2
found = ""
Builtins.foreach(devices) do |k, v|
if Ops.less_than(v, minimal)
found = k
minimal = v
end
end
found
end


# Run delayed updates
#
# This is used by perl-Bootloader when it cannot remove sections from the
Expand Down
6 changes: 5 additions & 1 deletion src/modules/BootCommon.rb
Expand Up @@ -1008,6 +1008,11 @@ def VerifyMDArray
ret
end

# FIXME just backward compatible interface, call directly BootStorage
def Md2Partitions(md_device)
BootStorage.Md2Partitions(md_device)
end

publish :variable => :global_options, :type => "map <string, any>"
publish :variable => :globals, :type => "map <string, string>"
publish :variable => :sections, :type => "list <map <string, any>>"
Expand Down Expand Up @@ -1086,7 +1091,6 @@ def VerifyMDArray
publish :function => :AddFirmwareToBootloader, :type => "boolean (string)"
publish :function => :PostUpdateMBR, :type => "boolean ()"
publish :function => :FindMBRDisk, :type => "string ()"
publish :function => :Md2Partition, :type => "string (string)"
publish :function => :RunDelayedUpdates, :type => "void ()"
publish :function => :FixGlobals, :type => "void ()"
publish :function => :FixSections, :type => "void (void ())"
Expand Down
29 changes: 29 additions & 0 deletions src/modules/BootStorage.rb
Expand Up @@ -1513,6 +1513,34 @@ def addMDSettingsToGlobals
ret
end

# Converts the md device to the list of devices building it
# @param [String] md_device string md device
# @return a map of devices (from device name to BIOS ID or nil if
# not detected) building the md device
def Md2Partitions(md_device)
ret = {}
tm = Storage.GetTargetMap
Builtins.foreach(tm) do |disk, descr_a|
descr = Convert.convert(
descr_a,
:from => "any",
:to => "map <string, any>"
)
bios_id_str = Ops.get_string(descr, "bios_id", "")
bios_id = 256 # maximum + 1 (means: no bios_id found)
bios_id = Builtins.tointeger(bios_id) if bios_id_str != ""
partitions = Ops.get_list(descr, "partitions", [])
Builtins.foreach(partitions) do |partition|
if Ops.get_string(partition, "used_by_device", "") == md_device
d = Ops.get_string(partition, "device", "")
Ops.set(ret, d, bios_id)
end
end
end
Builtins.y2milestone("Partitions building %1: %2", md_device, ret)
deep_copy(ret)
end

publish :variable => :all_devices, :type => "map <string, string>"
publish :variable => :multipath_mapping, :type => "map <string, string>"
publish :variable => :mountpoints, :type => "map <string, any>"
Expand All @@ -1534,6 +1562,7 @@ def addMDSettingsToGlobals
publish :function => :getHintedPartitionList, :type => "list <string> (list <string>)"
publish :function => :getPartitionList, :type => "list <string> (symbol, string)"
publish :function => :addMDSettingsToGlobals, :type => "string ()"
publish :function => :Md2Partitions, :type => "map <string, integer> (string)"
end

BootStorage = BootStorageClass.new
Expand Down
23 changes: 23 additions & 0 deletions test/boot_storage_test.rb
Expand Up @@ -3,6 +3,29 @@
Yast.import "BootStorage"

describe Yast::BootStorage do
def target_map_stub(name)
path = File.join(File.dirname(__FILE__), "data", name)
tm = eval(File.read(path))
allow(Yast::Storage).to receive(:GetTargetMap).and_return(tm)
end

describe ".Md2Partitions" do
it "returns map with devices creating virtual device as key and bios id as value" do
target_map_stub("storage_mdraid.rb")
result = Yast::BootStorage.Md2Partitions("/dev/md1")
expect(result).to include("/dev/vda1")
expect(result).to include("/dev/vdb1")
expect(result).to include("/dev/vdc1")
expect(result).to include("/dev/vdd1")
end

it "returns empty map if device is not created from other devices" do
target_map_stub("storage_mdraid.rb")
result = Yast::BootStorage.Md2Partitions("/dev/vda1")
expect(result).to be_empty
end
end

describe ".changeOrderInDeviceMapping" do
it "place priority device on top of device mapping" do
device_map = { "/dev/sda" => "hd1", "/dev/sdb" => "hd0" }
Expand Down

0 comments on commit 42e44d4

Please sign in to comment.