Skip to content
Permalink
Browse files

Merge pull request #872 from cropalato/2.x

(FACT-830) Use xen-toolstack to find xen command.
  • Loading branch information...
melissa committed Mar 30, 2015
2 parents 028d90f + 2c86313 commit 5cd1fffc0026cdabcddaa406c3721c55a8e84d93
Showing with 83 additions and 26 deletions.
  1. +10 −1 lib/facter/util/xendomains.rb
  2. +73 −25 spec/unit/util/xendomains_spec.rb
@@ -4,7 +4,16 @@ module Facter::Util::Xendomains
XEN_COMMANDS = ['/usr/sbin/xl', '/usr/sbin/xm']

def self.xen_command
XEN_COMMANDS.find { |cmd| Facter::Util::Resolution.which(cmd) }
if File.file?('/usr/lib/xen-common/bin/xen-toolstack')
xen_toolstack_cmd = Facter::Util::Resolution.exec('/usr/lib/xen-common/bin/xen-toolstack')
if xen_toolstack_cmd
xen_toolstack_cmd.chomp
else
nil
end
else
XEN_COMMANDS.find { |cmd| Facter::Util::Resolution.which(cmd) }
end
end

def self.get_domains
@@ -7,60 +7,108 @@

let(:xen0_domains) { my_fixture_read("xendomains") }

describe "when the xl command is present" do
describe "when the xen-toolstack command is present" do
before do
Facter::Util::Resolution.stubs(:which).with('/usr/sbin/xl').returns('/usr/sbin/xl')
File.expects(:file?).with('/usr/lib/xen-common/bin/xen-toolstack').returns true
end

describe "and the xm command is not present" do

describe "and the xen-toolstack returns xl" do
before do
Facter::Util::Resolution.stubs(:which).with('/usr/sbin/xm').returns(nil)
Facter::Util::Resolution.expects(:exec).with('/usr/sbin/xm list 2>/dev/null').never
Facter::Util::Resolution.stubs(:exec).with('/usr/lib/xen-common/bin/xen-toolstack').returns('/usr/sbin/xl')
end

it "lists the domains running on Xen0 with the 'xl' command" do
Facter::Util::Resolution.expects(:exec).with('/usr/sbin/xl list 2>/dev/null').returns(xen0_domains)
Facter::Util::Xendomains.get_domains.should == %{web01,mailserver}
end

end

describe "and the xm command is also present" do
describe "and the xen-toolstack returns xm" do
before do
Facter::Util::Resolution.stubs(:which).with('/usr/sbin/xm').returns('/usr/bin/xm')
Facter::Util::Resolution.expects(:exec).with('/usr/sbin/xm list 2>/dev/null').never
Facter::Util::Resolution.stubs(:exec).with('/usr/lib/xen-common/bin/xen-toolstack').returns('/usr/sbin/xm')
end

it "prefers xl over xm" do
Facter::Util::Resolution.expects(:exec).with('/usr/sbin/xl list 2>/dev/null').returns(xen0_domains)
it "lists the domains running on Xen0 with the 'xm' command" do
Facter::Util::Resolution.expects(:exec).with('/usr/sbin/xm list 2>/dev/null').returns(xen0_domains)
Facter::Util::Xendomains.get_domains.should == %{web01,mailserver}
end

end

describe "and the xen-toolstack returns nil" do
before do
Facter::Util::Resolution.stubs(:exec).with('/usr/lib/xen-common/bin/xen-toolstack').returns(nil)
end

it "returns nil" do
Facter::Util::Xendomains.get_domains.should == nil
end

end

end

describe "when xl is not present" do
describe "when the xen-toolstack command is not present" do
before do
Facter::Util::Resolution.stubs(:which).with('/usr/sbin/xl').returns(nil)
Facter::Util::Resolution.expects(:exec).with('/usr/sbin/xl list 2>/dev/null').never
File.expects(:file?).with('/usr/lib/xen-common/bin/xen-toolstack').returns false
end

describe "and the xm command is present" do
describe "and the xl command is present" do
before do
Facter::Util::Resolution.stubs(:which).with('/usr/sbin/xm').returns('/usr/sbin/xm')
Facter::Util::Resolution.stubs(:which).with('/usr/sbin/xl').returns('/usr/sbin/xl')
end

it "lists the domains running on Xen0 with the 'xm' command" do
Facter::Util::Resolution.expects(:exec).with('/usr/sbin/xm list 2>/dev/null').returns(xen0_domains)
Facter::Util::Xendomains.get_domains.should == %{web01,mailserver}
describe "and the xm command is not present" do

before do
Facter::Util::Resolution.stubs(:which).with('/usr/sbin/xm').returns(nil)
Facter::Util::Resolution.expects(:exec).with('/usr/sbin/xm list 2>/dev/null').never
end

it "lists the domains running on Xen0 with the 'xl' command" do
Facter::Util::Resolution.expects(:exec).with('/usr/sbin/xl list 2>/dev/null').returns(xen0_domains)
Facter::Util::Xendomains.get_domains.should == %{web01,mailserver}
end
end

describe "and the xm command is also present" do
before do
Facter::Util::Resolution.stubs(:which).with('/usr/sbin/xm').returns('/usr/bin/xm')
Facter::Util::Resolution.expects(:exec).with('/usr/sbin/xm list 2>/dev/null').never
end

it "prefers xl over xm" do
Facter::Util::Resolution.expects(:exec).with('/usr/sbin/xl list 2>/dev/null').returns(xen0_domains)
Facter::Util::Xendomains.get_domains.should == %{web01,mailserver}
end
end
end

describe "when xl is not present" do
before do
Facter::Util::Resolution.stubs(:which).with('/usr/sbin/xl').returns(nil)
Facter::Util::Resolution.expects(:exec).with('/usr/sbin/xl list 2>/dev/null').never
end

describe "and the xm command is present" do
before do
Facter::Util::Resolution.stubs(:which).with('/usr/sbin/xm').returns('/usr/sbin/xm')
end

it "lists the domains running on Xen0 with the 'xm' command" do
Facter::Util::Resolution.expects(:exec).with('/usr/sbin/xm list 2>/dev/null').returns(xen0_domains)
Facter::Util::Xendomains.get_domains.should == %{web01,mailserver}
end
end
end
end

describe "neither xl or xm are present" do
it "returns nil" do
Facter::Util::Resolution.stubs(:which).with('/usr/sbin/xl').returns(nil)
Facter::Util::Resolution.stubs(:which).with('/usr/sbin/xm').returns(nil)
Facter::Util::Xendomains.get_domains.should == nil
describe "neither xl or xm are present" do
it "returns nil" do
Facter::Util::Resolution.stubs(:which).with('/usr/sbin/xl').returns(nil)
Facter::Util::Resolution.stubs(:which).with('/usr/sbin/xm').returns(nil)
Facter::Util::Xendomains.get_domains.should == nil
end
end
end
end

0 comments on commit 5cd1fff

Please sign in to comment.
You can’t perform that action at this time.