diff --git a/lib/linux_admin/common.rb b/lib/linux_admin/common.rb index fc4905e..22f9adf 100644 --- a/lib/linux_admin/common.rb +++ b/lib/linux_admin/common.rb @@ -3,7 +3,7 @@ class LinuxAdmin module Common def cmd(cmd) - Distro.local.class::COMMANDS[cmd] + Distro.local.commands[cmd] || raise(ArgumentError, "command #{cmd} not defined for #{Distro.local.id}") end def run(cmd, options = {}) diff --git a/lib/linux_admin/distro.rb b/lib/linux_admin/distro.rb index db25100..8387d98 100644 --- a/lib/linux_admin/distro.rb +++ b/lib/linux_admin/distro.rb @@ -5,10 +5,11 @@ class LinuxAdmin class Distro < LinuxAdmin - attr_accessor :id + attr_accessor :id, :commands - def initialize(id) + def initialize(id, commands) @id = id + @commands = commands end def self.local @@ -50,42 +51,39 @@ def self.all end class Generic < Distro - COMMANDS = {} - def initialize - @id = :generic + super(:generic, {}) end end class RedHat < Distro - COMMANDS = {:service => '/sbin/service', - :chkconfig => '/sbin/chkconfig', - :parted => '/sbin/parted', - :mount => '/bin/mount', - :umount => '/bin/umount', - :shutdown => '/sbin/shutdown', - :mke2fs => '/sbin/mke2fs', - :fdisk => '/sbin/fdisk', - :dd => '/bin/dd', - :vgdisplay => '/sbin/vgdisplay', - :pvdisplay => '/sbin/pvdisplay', - :lvdisplay => '/sbin/lvdisplay', - :lvextend => '/sbin/lvextend', - :vgextend => '/sbin/vgextend', - :lvcreate => '/sbin/lvcreate', - :pvcreate => '/sbin/pvcreate', - :vgcreate => '/sbin/vgcreate'} - def initialize - @id = :redhat + super( + :redhat, + :service => '/sbin/service', + :chkconfig => '/sbin/chkconfig', + :parted => '/sbin/parted', + :mount => '/bin/mount', + :umount => '/bin/umount', + :shutdown => '/sbin/shutdown', + :mke2fs => '/sbin/mke2fs', + :fdisk => '/sbin/fdisk', + :dd => '/bin/dd', + :vgdisplay => '/sbin/vgdisplay', + :pvdisplay => '/sbin/pvdisplay', + :lvdisplay => '/sbin/lvdisplay', + :lvextend => '/sbin/lvextend', + :vgextend => '/sbin/vgextend', + :lvcreate => '/sbin/lvcreate', + :pvcreate => '/sbin/pvcreate', + :vgcreate => '/sbin/vgcreate' + ) end end class Ubuntu < Distro - COMMANDS = {} - def initialize - @id = :ubuntu + super(:ubuntu, {}) end end end diff --git a/spec/common_spec.rb b/spec/common_spec.rb index fcfc1ad..15ff911 100644 --- a/spec/common_spec.rb +++ b/spec/common_spec.rb @@ -15,11 +15,13 @@ class TestClass context "#cmd" do it "looks up local command from id" do - d = double(LinuxAdmin::Distro) - d.class::COMMANDS = {:sh => '/bin/sh'} - LinuxAdmin::Distro.should_receive(:local).and_return(d) + stub_distro(:sh => '/bin/sh') subject.cmd(:sh).should == '/bin/sh' end + + it "complains when command not found" do + expect { subject.cmd(:notfound) }.to raise_error + end end it "#run" do diff --git a/spec/distro_spec.rb b/spec/distro_spec.rb index a0a88f4..be04a40 100644 --- a/spec/distro_spec.rb +++ b/spec/distro_spec.rb @@ -2,6 +2,10 @@ describe LinuxAdmin::Distro do describe "#local" do + before do + LinuxAdmin::Distro.unstub(:local) + end + after(:each) do # distro generates a local copy, reset after each run LinuxAdmin::Distro.instance_variable_set(:@local, nil) diff --git a/spec/logical_volume_spec.rb b/spec/logical_volume_spec.rb index 7e14da2..a039e56 100644 --- a/spec/logical_volume_spec.rb +++ b/spec/logical_volume_spec.rb @@ -2,8 +2,6 @@ describe LinuxAdmin::LogicalVolume do before(:each) do - LinuxAdmin::Distro.stub(:local => LinuxAdmin::Distros::Test.new) - @logical_volumes = < LinuxAdmin::Distros::Test.new) - @physical_volumes = < distro) +end def data_file_path(to) File.expand_path(to, File.join(File.dirname(__FILE__), "data")) @@ -44,10 +51,3 @@ def sample_output(to) def clear_caches LinuxAdmin::RegistrationSystem.instance_variable_set(:@registration_type, nil) end - -class LinuxAdmin - module Distros - # simply alias test distro to redhat distro for time being - Distros::Test = Distros::RedHat - end -end diff --git a/spec/volume_group_spec.rb b/spec/volume_group_spec.rb index 817acc0..c25ecbf 100644 --- a/spec/volume_group_spec.rb +++ b/spec/volume_group_spec.rb @@ -2,8 +2,6 @@ describe LinuxAdmin::VolumeGroup do before(:each) do - LinuxAdmin::Distro.stub(:local => LinuxAdmin::Distros::Test.new) - @groups = <