summaryrefslogtreecommitdiffstats
path: root/spec/unit/util/virtual_spec.rb
blob: aa2de5af1bf19655a944dc0fd987b6f4d6403174 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
require File.expand_path(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(:directory?).with("/proc/vz").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

    fixture_path = File.join(SPECDIR, 'fixtures', 'virtual', 'proc_self_status')

    test_cases = [
        [File.join(fixture_path, 'vserver_2_1', 'guest'), true, 'vserver 2.1 guest'],
        [File.join(fixture_path, 'vserver_2_1', 'host'),  true, 'vserver 2.1 host'],
        [File.join(fixture_path, 'vserver_2_3', 'guest'), true, 'vserver 2.3 guest'],
        [File.join(fixture_path, 'vserver_2_3', 'host'),  true, 'vserver 2.3 host']
    ]

    test_cases.each do |status_file, expected, description|
        describe "with /proc/self/status from #{description}" do
            it "should detect vserver as #{expected.inspect}" do
                status = File.read(status_file)
                FileTest.stubs(:exists?).with("/proc/self/status").returns(true)
                File.stubs(:read).with("/proc/self/status").returns(status)
                Facter::Util::Virtual.vserver?.should == expected
            end
        end
    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

    it "should detect kvm" do
        FileTest.stubs(:exists?).with("/proc/cpuinfo").returns(true)
        File.stubs(:read).with("/proc/cpuinfo").returns("model name : QEMU Virtual CPU version 0.9.1\n")
        Facter::Util::Virtual.should be_kvm
    end

    it "should detect kvm on FreeBSD" do
        FileTest.stubs(:exists?).with("/proc/cpuinfo").returns(false)
        Facter.fact(:kernel).stubs(:value).returns("FreeBSD")
        Facter::Util::Resolution.stubs(:exec).with("/sbin/sysctl -n hw.model").returns("QEMU Virtual CPU version 0.12.4")
        Facter::Util::Virtual.should be_kvm
    end

    it "should identify FreeBSD jail when in jail" do
        Facter.fact(:kernel).stubs(:value).returns("FreeBSD")
        Facter::Util::Resolution.stubs(:exec).with("/sbin/sysctl -n security.jail.jailed").returns("1")
        Facter::Util::Virtual.should be_jail
    end

    it "should not identify GNU/kFreeBSD jail when not in jail" do
        Facter.fact(:kernel).stubs(:value).returns("GNU/kFreeBSD")
        Facter::Util::Resolution.stubs(:exec).with("/bin/sysctl -n security.jail.jailed").returns("0")
        Facter::Util::Virtual.should_not be_jail
    end

    it "should detect hpvm on HP-UX" do
        Facter.fact(:kernel).stubs(:value).returns("HP-UX")
        Facter::Util::Resolution.stubs(:exec).with("/usr/bin/getconf MACHINE_MODEL").returns('ia64 hp server Integrity Virtual Machine')
        Facter::Util::Virtual.should be_hpvm
    end

    it "should not detect hpvm on HP-UX when not in hpvm" do
        Facter.fact(:kernel).stubs(:value).returns("HP-UX")
        Facter::Util::Resolution.stubs(:exec).with("/usr/bin/getconf MACHINE_MODEL").returns('ia64 hp server rx660')
        Facter::Util::Virtual.should_not be_hpvm
    end
end