Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/linux_admin/disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,19 @@ def partitions
end
end

def create_partition_table(type = "msdos")
run!(cmd(:parted), :params => { nil => [path, "mklabel", type]})
end

def has_partition_table?
result = run(cmd(:parted), :params => { nil => [path, "print"]})

result_indicates_partition_table?(result)
end

def create_partition(partition_type, size)
create_partition_table unless has_partition_table?

id, start =
partitions.empty? ? [1, 0] :
[(partitions.last.id + 1),
Expand Down Expand Up @@ -118,5 +130,13 @@ def clear!

self
end

private

def result_indicates_partition_table?(result)
# parted exits with 1 but writes this oddly spelled error to stdout.
missing = (result.exit_status == 1 && result.output.include?("unrecognised disk label"))
!missing
end
end
end
30 changes: 30 additions & 0 deletions spec/disk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
@disk.instance_variable_set(:@partitions,
[LinuxAdmin::Partition.new(:id => 1,
:end_sector => 1024)])
@disk.stub(:has_partition_table? => true)
end

it "uses parted" do
Expand Down Expand Up @@ -139,6 +140,35 @@
@disk.create_partition 'primary', 1024
}.should change{@disk.partitions.size}.by(1)
end

it "creates partition table if missing" do
@disk.stub(:has_partition_table? => false)
@disk.should_receive(:create_partition_table)
@disk.should_receive(:run!)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be .should_receive(:run!).with(something)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fryguy No, this is tested extensively in create_partition's examples. I only care if the run! is executed after the partition table is created.

@disk.create_partition 'primary', 1024
end
end

describe "#has_partition_table?" do
it "positive case" do
disk = LinuxAdmin::Disk.new :path => '/dev/hda'
disk.should_receive(:run).and_return(double(:output => "", :exit_status => 0))
disk.should have_partition_table
end

it "negative case" do
disk = LinuxAdmin::Disk.new :path => '/dev/hda'
output = "\e[?1034h\r\rError: /dev/sdb: unrecognised disk label\n"
disk.should_receive(:run).and_return(double(:output => output, :exit_status => 1))
disk.should_not have_partition_table
end
end

it "#create_partition_table" do
disk = LinuxAdmin::Disk.new :path => '/dev/hda'
options = {:params => {nil => ["/dev/hda", "mklabel", "msdos"]}}
disk.should_receive(:run!).with(disk.cmd(:parted), options)
disk.create_partition_table
end

describe "#clear!" do
Expand Down