Very nice.  +1

On Jul 16, 2009, at 6:17 AM, Paul Nasrat wrote:

>
> Add tests and utility for virtual fact detection
>
> Signed-off-by: Paul Nasrat <[email protected]>
> ---
> lib/facter/util/virtual.rb     |   27 ++++++++++++++++++
> lib/facter/virtual.rb          |   35 +++++++----------------
> spec/unit/util/virtual_spec.rb |   60 +++++++++++++++++++++++++++++++ 
> +++++++++
> spec/unit/virtual_spec.rb      |   49 ++++++++++++++++++++++++++++++++
> 4 files changed, 147 insertions(+), 24 deletions(-)
> create mode 100644 lib/facter/util/virtual.rb
> create mode 100644 spec/unit/util/virtual_spec.rb
> create mode 100644 spec/unit/virtual_spec.rb
>
> diff --git a/lib/facter/util/virtual.rb b/lib/facter/util/virtual.rb
> new file mode 100644
> index 0000000..8db57a3
> --- /dev/null
> +++ b/lib/facter/util/virtual.rb
> @@ -0,0 +1,27 @@
> +module Facter::Util::Virtual
> +    def self.openvz?
> +        FileTest.exists?("/proc/vz/veinfo")
> +    end
> +
> +    def self.openvz_type
> +        return nil unless self.openvz?
> +        if FileTest.exists?("/proc/vz/version")
> +            result = "openvzhn"
> +        else
> +            result = "openvzve"
> +        end
> +    end
> +
> +    def self.zone?
> +        z = Facter::Util::Resolution.exec("/sbin/zonename")
> +        return false unless z
> +        return z.chomp != 'global'
> +    end
> +
> +    def self.vserver?
> +        return false unless FileTest.exists?("/proc/self/status")
> +        txt = File.read("/proc/self/status")
> +        return true if txt =~ /^(s_context|VxID):[[:blank:]]*[1-9]/
> +        return false
> +    end
> +end
> diff --git a/lib/facter/virtual.rb b/lib/facter/virtual.rb
> index 72bfe51..299ebb4 100644
> --- a/lib/facter/virtual.rb
> +++ b/lib/facter/virtual.rb
> @@ -1,32 +1,19 @@
> +require 'facter/util/virtual'
> +
> Facter.add("virtual") do
>     confine :kernel => %w{Linux FreeBSD OpenBSD SunOS}
>
>     result = "physical"
>
>     setcode do
> -    require 'thread'
>
> -        if FileTest.exists?("/sbin/zonename")
> -            z = %x{"/sbin/zonename"}.chomp
> -            if z != 'global'
> -                result = 'zone'
> -            end
> -        end
> +        result = "zone" if Facter::Util::Virtual.zone?
>
> -        if FileTest.exists?("/proc/vz/veinfo")
> -            if FileTest.exists?("/proc/vz/version")
> -                result = "openvzhn"
> -            else
> -                result = "openvzve"
> -            end
> +        if Facter::Util::Virtual.openvz?
> +            result = Facter::Util::Virtual.openvz_type()
>         end
>
> -        if FileTest.exists?("/proc/self/status")
> -            txt = File.read("/proc/self/status")
> -            if txt =~ /^(s_context|VxID):[[:blank:]]*[1-9]/
> -                result = "vserver"
> -            end
> -        end
> +        result = "vserver" if Facter::Util::Virtual.vserver?
>
>         if FileTest.exists?("/proc/virtual")
>             result = "vserver_host"
> @@ -34,19 +21,19 @@ Facter.add("virtual") do
>
>         # new Xen domains have this in dom0 not domu :(
>         if FileTest.exists?("/proc/sys/xen/independent_wallclock")
> -            result = "xenu"
> +            result = "xenu"
>         end
>         if FileTest.exists?("/sys/bus/xen")
> -            result = "xenu"
> +            result = "xenu"
>         end
>
>         if FileTest.exists?("/proc/xen/capabilities")
>             txt = File.read("/proc/xen/capabilities")
>             if txt =~ /control_d/i
> -                result = "xen0"
> +                result = "xen0"
>             end
>         end
> -
> +
>         if result == "physical"
>             output = Facter::Util::Resolution.exec('lspci')
>             if not output.nil?
> @@ -86,7 +73,7 @@ Facter.add("is_virtual") do
>
>     setcode do
>         case Facter.value(:virtual)
> -        when "xenu", "openvzve", "vmware"
> +        when "xenu", "openvzve", "vmware"
>             true
>         else
>             false
> diff --git a/spec/unit/util/virtual_spec.rb b/spec/unit/util/ 
> virtual_spec.rb
> new file mode 100644
> index 0000000..3552c45
> --- /dev/null
> +++ b/spec/unit/util/virtual_spec.rb
> @@ -0,0 +1,60 @@
> +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_spec.rb b/spec/unit/virtual_spec.rb
> new file mode 100644
> index 0000000..68cd258
> --- /dev/null
> +++ b/spec/unit/virtual_spec.rb
> @@ -0,0 +1,49 @@
> +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
> -- 
> 1.6.1.3
>
>
> >


-- 
It's very hard to predict things . . . Especially the future.
     -- Prof. Charles Kelemen, Swarthmore CS Dept.
---------------------------------------------------------------------
Luke Kanies | http://reductivelabs.com | http://madstop.com


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to