From ab74f62de70a61b3193833e35bd3a58522c9c776 Mon Sep 17 00:00:00 2001 From: Mo Morsi Date: Thu, 7 Nov 2013 14:57:40 -0500 Subject: [PATCH] Accept path parameter in LogicalVolume initialization --- lib/linux_admin/hosts.rb | 2 +- lib/linux_admin/logical_volume.rb | 21 ++++++++++++++++++--- spec/logical_volume_spec.rb | 28 ++++++++++++++++++++++++---- spec/volume_group_spec.rb | 4 ++-- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/lib/linux_admin/hosts.rb b/lib/linux_admin/hosts.rb index 69bfbb3..9a63f96 100644 --- a/lib/linux_admin/hosts.rb +++ b/lib/linux_admin/hosts.rb @@ -70,4 +70,4 @@ def build_line(address, hosts, comment) line.flatten.join(" ").strip end end -end \ No newline at end of file +end diff --git a/lib/linux_admin/logical_volume.rb b/lib/linux_admin/logical_volume.rb index dab4f58..47ec7d9 100644 --- a/lib/linux_admin/logical_volume.rb +++ b/lib/linux_admin/logical_volume.rb @@ -5,6 +5,11 @@ class LinuxAdmin class LogicalVolume < Volume + DEVICE_PATH = Pathname.new('/dev/') + + # path to logical volume + attr_accessor :path + # logical volume name attr_accessor :name @@ -26,10 +31,19 @@ class LogicalVolume < Volume # major device number of logical volume # minor device number of logical volume + def path=(value) + @path = value.include?(File::SEPARATOR) ? value : DEVICE_PATH.join(@volume_group.name, value) + end + + def name=(value) + @name = value.include?(File::SEPARATOR) ? value.split(File::SEPARATOR).last : value + end + def initialize(args = {}) - @name = args[:name] @volume_group = args[:volume_group] @sectors = args[:sectors] + self.path = args[:name] + self.name = args[:name] end def extend_with(vg) @@ -66,9 +80,10 @@ def self.create(name, vg, value) params.merge!({'-L' => bytes_to_string(size)}) end run!(cmd(:lvcreate), :params => params) - lv = LogicalVolume.new :name => name, + + lv = LogicalVolume.new(:name => name, :volume_group => vg, - :sectors => size + :sectors => size) @lvs << lv lv end diff --git a/spec/logical_volume_spec.rb b/spec/logical_volume_spec.rb index ac2f7e8..a622a27 100644 --- a/spec/logical_volume_spec.rb +++ b/spec/logical_volume_spec.rb @@ -23,8 +23,8 @@ describe "#extend_with" do it "uses lvextend" do - lv = described_class.new :name => 'lv' vg = LinuxAdmin::VolumeGroup.new :name => 'vg' + lv = described_class.new :name => 'lv', :volume_group => vg lv.should_receive(:run!). with(vg.cmd(:lvextend), :params => ['lv', 'vg']) @@ -32,8 +32,8 @@ end it "returns self" do - lv = described_class.new :name => 'lv' vg = LinuxAdmin::VolumeGroup.new :name => 'vg' + lv = described_class.new :name => 'lv', :volume_group => vg lv.stub(:run!) lv.extend_with(vg).should == lv end @@ -86,6 +86,24 @@ lv.name.should == 'lv' end + context "name is specified" do + it "sets path under volume group" do + LinuxAdmin::VolumeGroup.stub(:run! => double(:output => "")) + described_class.stub(:run! => double(:output => "")) + lv = described_class.create 'lv', @vg, 256.gigabytes + lv.path.to_s.should == "#{described_class::DEVICE_PATH}#{@vg.name}/lv" + end + end + + context "path is specified" do + it "sets name" do + LinuxAdmin::VolumeGroup.stub(:run! => double(:output => "")) + described_class.stub(:run! => double(:output => "")) + lv = described_class.create '/dev/lv', @vg, 256.gigabytes + lv.name.should == "lv" + end + end + it "adds logical volume to local registry" do LinuxAdmin::VolumeGroup.stub(:run! => double(:output => "")) described_class.stub(:run! => double(:output => "")) @@ -110,11 +128,13 @@ lvs = described_class.scan lvs[0].should be_an_instance_of(described_class) - lvs[0].name.should == '/dev/vg_foobar/lv_swap' + lvs[0].path.should == '/dev/vg_foobar/lv_swap' + lvs[0].name.should == 'lv_swap' lvs[0].sectors.should == 4128768 lvs[1].should be_an_instance_of(described_class) - lvs[1].name.should == '/dev/vg_foobar/lv_root' + lvs[1].path.should == '/dev/vg_foobar/lv_root' + lvs[1].name.should == 'lv_root' lvs[1].sectors.should == 19988480 end diff --git a/spec/volume_group_spec.rb b/spec/volume_group_spec.rb index 447e54b..817acc0 100644 --- a/spec/volume_group_spec.rb +++ b/spec/volume_group_spec.rb @@ -18,8 +18,8 @@ describe "#attach_to" do it "uses lvextend" do - lv = LinuxAdmin::LogicalVolume.new :name => 'lv' vg = described_class.new :name => 'vg' + lv = LinuxAdmin::LogicalVolume.new :name => 'lv', :volume_group => vg vg.should_receive(:run!). with(vg.cmd(:lvextend), :params => ['lv', 'vg']) @@ -27,8 +27,8 @@ end it "returns self" do - lv = LinuxAdmin::LogicalVolume.new :name => 'lv' vg = described_class.new :name => 'vg' + lv = LinuxAdmin::LogicalVolume.new :name => 'lv', :volume_group => vg vg.stub(:run!) vg.attach_to(lv).should == vg end