From 49470cf776f2c23cabec00b68b85a1264a3f7b48 Mon Sep 17 00:00:00 2001 From: Paul Nasrat Date: Sat, 5 Sep 2009 06:45:50 +0100 Subject: Fix broken solaris zone tests on EC2 This cleans up xen and vserver detection to enable us to stub out so when we happen to be running tests on xen we don't report as that. More cleanup is needed in this area but this should give us a green build. This renames the tests to be consistent with current naming convention --- lib/facter/util/virtual.rb | 16 +++++++ lib/facter/virtual.rb | 39 +++++++++-------- spec/unit/util/virtual.rb | 96 ++++++++++++++++++++++++++++++++++++++++++ spec/unit/util/virtual_spec.rb | 60 -------------------------- spec/unit/virtual.rb | 51 ++++++++++++++++++++++ spec/unit/virtual_spec.rb | 49 --------------------- 6 files changed, 182 insertions(+), 129 deletions(-) create mode 100644 spec/unit/util/virtual.rb delete mode 100644 spec/unit/util/virtual_spec.rb create mode 100644 spec/unit/virtual.rb delete mode 100644 spec/unit/virtual_spec.rb diff --git a/lib/facter/util/virtual.rb b/lib/facter/util/virtual.rb index 8db57a3..0c3fb73 100644 --- a/lib/facter/util/virtual.rb +++ b/lib/facter/util/virtual.rb @@ -24,4 +24,20 @@ module Facter::Util::Virtual return true if txt =~ /^(s_context|VxID):[[:blank:]]*[1-9]/ return false end + + def self.vserver_type + if self.vserver? + if FileTest.exists?("/proc/virtual") + "vserver_host" + else + "vserver" + end + end + end + + def self.xen? + ["/proc/sys/xen", "/sys/bus/xen", "/proc/xen" ].detect do |f| + FileTest.exists?(f) + end + end end diff --git a/lib/facter/virtual.rb b/lib/facter/virtual.rb index 299ebb4..78413a9 100644 --- a/lib/facter/virtual.rb +++ b/lib/facter/virtual.rb @@ -13,24 +13,24 @@ Facter.add("virtual") do result = Facter::Util::Virtual.openvz_type() end - result = "vserver" if Facter::Util::Virtual.vserver? - - if FileTest.exists?("/proc/virtual") - result = "vserver_host" + if Facter::Util::Virtual.vserver? + result = Facter::Util::Virtual.vserver_type() end - # new Xen domains have this in dom0 not domu :( - if FileTest.exists?("/proc/sys/xen/independent_wallclock") - result = "xenu" - end - if FileTest.exists?("/sys/bus/xen") - result = "xenu" - end + if Facter::Util::Virtual.xen? + # new Xen domains have this in dom0 not domu :( + if FileTest.exists?("/proc/sys/xen/independent_wallclock") + result = "xenu" + end + if FileTest.exists?("/sys/bus/xen") + result = "xenu" + end - if FileTest.exists?("/proc/xen/capabilities") - txt = File.read("/proc/xen/capabilities") - if txt =~ /control_d/i - result = "xen0" + if FileTest.exists?("/proc/xen/capabilities") + txt = File.read("/proc/xen/capabilities") + if txt =~ /control_d/i + result = "xen0" + end end end @@ -57,11 +57,10 @@ Facter.add("virtual") do end end end - end - - # VMware server 1.0.3 rpm places vmware-vmx in this place, other versions or platforms may not. - if FileTest.exists?("/usr/lib/vmware/bin/vmware-vmx") - result = "vmware_server" + # VMware server 1.0.3 rpm places vmware-vmx in this place, other versions or platforms may not. + if FileTest.exists?("/usr/lib/vmware/bin/vmware-vmx") + result = "vmware_server" + end end result diff --git a/spec/unit/util/virtual.rb b/spec/unit/util/virtual.rb new file mode 100644 index 0000000..1f1c0f8 --- /dev/null +++ b/spec/unit/util/virtual.rb @@ -0,0 +1,96 @@ +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'facter/util/virtual' + +describe Facter::Util::Virtual do + + after do + Facter.clear + end + it "should detect openvz" do + FileTest.stubs(:exists?).with("/proc/vz/veinfo").returns(true) + Facter::Util::Virtual.should be_openvz + end + + it "should identify openvzhn when version file exists" do + Facter::Util::Virtual.stubs(:openvz?).returns(true) + FileTest.stubs(:exists?).with("/proc/vz/version").returns(true) + Facter::Util::Virtual.openvz_type().should == "openvzhn" + end + + it "should identify openvzve when no version file exists" do + Facter::Util::Virtual.stubs(:openvz?).returns(true) + FileTest.stubs(:exists?).with("/proc/vz/version").returns(false) + Facter::Util::Virtual.openvz_type().should == "openvzve" + end + + it "should identify Solaris zones when non-global zone" do + Facter::Util::Resolution.stubs(:exec).with("/sbin/zonename").returns("somezone") + Facter::Util::Virtual.should be_zone + end + + it "should not identify Solaris zones when global zone" do + Facter::Util::Resolution.stubs(:exec).with("/sbin/zonename").returns("global") + Facter::Util::Virtual.should_not be_zone + end + + it "should not detect vserver if no self status" do + FileTest.stubs(:exists?).with("/proc/self/status").returns(false) + Facter::Util::Virtual.should_not be_vserver + end + + it "should detect vserver when vxid present in process status" do + FileTest.stubs(:exists?).with("/proc/self/status").returns(true) + File.stubs(:read).with("/proc/self/status").returns("VxID: 42\n") + Facter::Util::Virtual.should be_vserver + end + + it "should detect vserver when s_context present in process status" do + FileTest.stubs(:exists?).with("/proc/self/status").returns(true) + File.stubs(:read).with("/proc/self/status").returns("s_context: 42\n") + Facter::Util::Virtual.should be_vserver + end + + it "should not detect vserver when vserver flags not present in process status" do + FileTest.stubs(:exists?).with("/proc/self/status").returns(true) + File.stubs(:read).with("/proc/self/status").returns("wibble: 42\n") + Facter::Util::Virtual.should_not be_vserver + end + + it "should identify vserver_host when /proc/virtual exists" do + Facter::Util::Virtual.expects(:vserver?).returns(true) + FileTest.stubs(:exists?).with("/proc/virtual").returns(true) + Facter::Util::Virtual.vserver_type().should == "vserver_host" + end + + it "should identify vserver_type as vserver when /proc/virtual does not exist" do + Facter::Util::Virtual.expects(:vserver?).returns(true) + FileTest.stubs(:exists?).with("/proc/virtual").returns(false) + Facter::Util::Virtual.vserver_type().should == "vserver" + end + + it "should detect xen when /proc/sys/xen exists" do + FileTest.expects(:exists?).with("/proc/sys/xen").returns(true) + Facter::Util::Virtual.should be_xen + end + + it "should detect xen when /sys/bus/xen exists" do + FileTest.expects(:exists?).with("/proc/sys/xen").returns(false) + FileTest.expects(:exists?).with("/sys/bus/xen").returns(true) + Facter::Util::Virtual.should be_xen + end + + it "should detect xen when /proc/xen exists" do + FileTest.expects(:exists?).with("/proc/sys/xen").returns(false) + FileTest.expects(:exists?).with("/sys/bus/xen").returns(false) + FileTest.expects(:exists?).with("/proc/xen").returns(true) + Facter::Util::Virtual.should be_xen + end + + it "should not detect xen when no sysfs/proc xen directories exist" do + FileTest.expects(:exists?).with("/proc/sys/xen").returns(false) + FileTest.expects(:exists?).with("/sys/bus/xen").returns(false) + FileTest.expects(:exists?).with("/proc/xen").returns(false) + Facter::Util::Virtual.should_not be_xen + end +end diff --git a/spec/unit/util/virtual_spec.rb b/spec/unit/util/virtual_spec.rb deleted file mode 100644 index 3552c45..0000000 --- a/spec/unit/util/virtual_spec.rb +++ /dev/null @@ -1,60 +0,0 @@ -require File.dirname(__FILE__) + '/../../spec_helper' - -require 'facter/util/virtual' - -describe Facter::Util::Virtual do - - after do - Facter.clear - end - it "should detect openvz" do - FileTest.stubs(:exists?).with("/proc/vz/veinfo").returns(true) - Facter::Util::Virtual.should be_openvz - end - - it "should identify openvzhn when version file exists" do - Facter::Util::Virtual.stubs(:openvz?).returns(true) - FileTest.stubs(:exists?).with("/proc/vz/version").returns(true) - Facter::Util::Virtual.openvz_type().should == "openvzhn" - end - - it "should identify openvzve when no version file exists" do - Facter::Util::Virtual.stubs(:openvz?).returns(true) - FileTest.stubs(:exists?).with("/proc/vz/version").returns(false) - Facter::Util::Virtual.openvz_type().should == "openvzve" - end - - it "should identify Solaris zones when non-global zone" do - Facter::Util::Resolution.stubs(:exec).with("/sbin/zonename").returns("somezone") - Facter::Util::Virtual.should be_zone - end - - it "should not identify Solaris zones when global zone" do - Facter::Util::Resolution.stubs(:exec).with("/sbin/zonename").returns("global") - Facter::Util::Virtual.should_not be_zone - end - - it "should not detect vserver if no self status" do - FileTest.stubs(:exists?).with("/proc/self/status").returns(false) - Facter::Util::Virtual.should_not be_vserver - end - - it "should detect vserver when vxid present in process status" do - FileTest.stubs(:exists?).with("/proc/self/status").returns(true) - File.stubs(:read).with("/proc/self/status").returns("VxID: 42\n") - Facter::Util::Virtual.should be_vserver - end - - it "should detect vserver when s_context present in process status" do - FileTest.stubs(:exists?).with("/proc/self/status").returns(true) - File.stubs(:read).with("/proc/self/status").returns("s_context: 42\n") - Facter::Util::Virtual.should be_vserver - end - - it "should not detect vserver when vserver flags not present in process status" do - FileTest.stubs(:exists?).with("/proc/self/status").returns(true) - File.stubs(:read).with("/proc/self/status").returns("wibble: 42\n") - Facter::Util::Virtual.should_not be_vserver - end - -end diff --git a/spec/unit/virtual.rb b/spec/unit/virtual.rb new file mode 100644 index 0000000..cc72ffc --- /dev/null +++ b/spec/unit/virtual.rb @@ -0,0 +1,51 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +require 'facter' +require 'facter/util/virtual' + +describe "Virtual fact" do + + after do + Facter.clear + end + + it "should be zone on Solaris when a zone" do + Facter.fact(:kernel).stubs(:value).returns("SunOS") + Facter::Util::Virtual.stubs(:zone?).returns(true) + Facter::Util::Virtual.stubs(:vserver?).returns(false) + Facter::Util::Virtual.stubs(:xen?).returns(false) + Facter.fact(:virtual).value.should == "zone" + end + +end + +describe "is_virtual fact" do + + after do + Facter.clear + end + + it "should be virtual when running on xen" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter.fact(:virtual).stubs(:value).returns("xenu") + Facter.fact(:is_virtual).value.should == true + end + + it "should be false when running on xen0" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter.fact(:virtual).stubs(:value).returns("xen0") + Facter.fact(:is_virtual).value.should == false + end + + it "should be true when running on vmware" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter.fact(:virtual).stubs(:value).returns("vmware") + Facter.fact(:is_virtual).value.should == true + end + + it "should be true when running on openvz" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter.fact(:virtual).stubs(:value).returns("openvzve") + Facter.fact(:is_virtual).value.should == true + end +end diff --git a/spec/unit/virtual_spec.rb b/spec/unit/virtual_spec.rb deleted file mode 100644 index 68cd258..0000000 --- a/spec/unit/virtual_spec.rb +++ /dev/null @@ -1,49 +0,0 @@ -require File.dirname(__FILE__) + '/../spec_helper' - -require 'facter' -require 'facter/util/virtual' - -describe "Virtual fact" do - - after do - Facter.clear - end - - it "should be zone on Solaris when a zone" do - Facter.fact(:kernel).stubs(:value).returns("SunOS") - Facter::Util::Virtual.stubs(:zone?).returns(true) - Facter.fact(:virtual).value.should == "zone" - end - -end - -describe "is_virtual fact" do - - after do - Facter.clear - end - - it "should be virtual when running on xen" do - Facter.fact(:kernel).stubs(:value).returns("Linux") - Facter.fact(:virtual).stubs(:value).returns("xenu") - Facter.fact(:is_virtual).value.should == true - end - - it "should be false when running on xen0" do - Facter.fact(:kernel).stubs(:value).returns("Linux") - Facter.fact(:virtual).stubs(:value).returns("xen0") - Facter.fact(:is_virtual).value.should == false - end - - it "should be true when running on vmware" do - Facter.fact(:kernel).stubs(:value).returns("Linux") - Facter.fact(:virtual).stubs(:value).returns("vmware") - Facter.fact(:is_virtual).value.should == true - end - - it "should be true when running on openvz" do - Facter.fact(:kernel).stubs(:value).returns("Linux") - Facter.fact(:virtual).stubs(:value).returns("openvzve") - Facter.fact(:is_virtual).value.should == true - end -end -- cgit