summaryrefslogtreecommitdiffstats
path: root/spec/unit/virtual_spec.rb
blob: d1691924271bbc661b47192af98ab53f91bb8bdc (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

require 'facter'
require 'facter/util/virtual'
require 'facter/util/macosx'

describe "Virtual fact" do
  before do
      Facter::Util::Virtual.stubs(:zone?).returns(false)
      Facter::Util::Virtual.stubs(:openvz?).returns(false)
      Facter::Util::Virtual.stubs(:vserver?).returns(false)
      Facter::Util::Virtual.stubs(:xen?).returns(false)
      Facter::Util::Virtual.stubs(:kvm?).returns(false)
      Facter::Util::Virtual.stubs(:hpvm?).returns(false)
      Facter::Util::Virtual.stubs(:zlinux?).returns(false)
  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

  it "should be jail on FreeBSD when a jail in kvm" do
      Facter.fact(:kernel).stubs(:value).returns("FreeBSD")
      Facter::Util::Virtual.stubs(:jail?).returns(true)
      Facter::Util::Virtual.stubs(:kvm?).returns(true)
      Facter.fact(:virtual).value.should == "jail"
  end

  it "should be hpvm on HP-UX when in HP-VM" do
     Facter.fact(:kernel).stubs(:value).returns("HP-UX")
     Facter::Util::Virtual.stubs(:hpvm?).returns(true)
     Facter.fact(:virtual).value.should == "hpvm"
  end

  it "should be zlinux on s390x" do
     Facter.fact(:kernel).stubs(:value).returns("Linux")
     Facter.fact(:architecture).stubs(:value).returns("s390x")
     Facter::Util::Virtual.stubs(:zlinux?).returns(true)
     Facter.fact(:virtual).value.should == "zlinux"
  end

  describe "on Darwin" do
      it "should be parallels with Parallels vendor id" do
          Facter.fact(:kernel).stubs(:value).returns("Darwin")
          Facter::Util::Macosx.stubs(:profiler_data).returns({ "spdisplays_vendor-id" => "0x1ab8" })
          Facter.fact(:virtual).value.should == "parallels"
      end

      it "should be parallels with Parallels vendor name" do
          Facter.fact(:kernel).stubs(:value).returns("Darwin")
          Facter::Util::Macosx.stubs(:profiler_data).returns({ "spdisplays_vendor" => "Parallels" })
          Facter.fact(:virtual).value.should == "parallels"
      end

      it "should be vmware with VMWare vendor id" do
          Facter.fact(:kernel).stubs(:value).returns("Darwin")
          Facter::Util::Macosx.stubs(:profiler_data).returns({ "spdisplays_vendor-id" => "0x15ad" })
          Facter.fact(:virtual).value.should == "vmware"
      end

      it "should be vmware with VMWare vendor name" do
          Facter.fact(:kernel).stubs(:value).returns("Darwin")
          Facter::Util::Macosx.stubs(:profiler_data).returns({ "spdisplays_vendor" => "VMWare" })
          Facter.fact(:virtual).value.should == "vmware"
      end
  end

  describe "on Linux" do

      before do
        FileTest.expects(:exists?).with("/usr/lib/vmware/bin/vmware-vmx").returns false
        Facter.fact(:architecture).stubs(:value).returns(true)
      end

      it "should be parallels with Parallels vendor id from lspci" do
          Facter.fact(:kernel).stubs(:value).returns("Linux")
          Facter::Util::Resolution.stubs(:exec).with('lspci').returns("01:00.0 VGA compatible controller: Unknown device 1ab8:4005")
          Facter.fact(:virtual).value.should == "parallels"
      end

      it "should be parallels with Parallels vendor name from lspci" do
          Facter.fact(:kernel).stubs(:value).returns("Linux")
          Facter::Util::Resolution.stubs(:exec).with('lspci').returns("01:00.0 VGA compatible controller: Parallels Display Adapter")
          Facter.fact(:virtual).value.should == "parallels"
      end

      it "should be vmware with VMware vendor name from lspci" do
          Facter.fact(:kernel).stubs(:value).returns("Linux")
          Facter::Util::Resolution.stubs(:exec).with('lspci').returns("00:0f.0 VGA compatible controller: VMware Inc [VMware SVGA II] PCI Display Adapter")
          Facter.fact(:virtual).value.should == "vmware"
      end

      it "should be virtualbox with VirtualBox vendor name from lspci" do
          Facter.fact(:kernel).stubs(:value).returns("Linux")
          Facter::Util::Resolution.stubs(:exec).with('lspci').returns("00:02.0 VGA compatible controller: InnoTek Systemberatung GmbH VirtualBox Graphics Adapter")
          Facter.fact(:virtual).value.should == "virtualbox"
      end

      it "should be vmware with VMWare vendor name from dmidecode" do
          Facter.fact(:kernel).stubs(:value).returns("Linux")
          Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil)
          Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns("On Board Device 1 Information\nType: Video\nStatus: Disabled\nDescription: VMware SVGA II")
          Facter.fact(:virtual).value.should == "vmware"
      end

      it "should be parallels with Parallels vendor name from dmidecode" do
          Facter.fact(:kernel).stubs(:value).returns("Linux")
          Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil)
          Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns("On Board Device Information\nType: Video\nStatus: Disabled\nDescription: Parallels Video Adapter")
          Facter.fact(:virtual).value.should == "parallels"
      end

      it "should be virtualbox with VirtualBox vendor name from dmidecode" do
          Facter.fact(:kernel).stubs(:value).returns("Linux")
          Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil)
          Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns("BIOS Information\nVendor: innotek GmbH\nVersion: VirtualBox\n\nSystem Information\nManufacturer: innotek GmbH\nProduct Name: VirtualBox\nFamily: Virtual Machine")
          Facter.fact(:virtual).value.should == "virtualbox"
      end

  end
  describe "on Solaris" do
      it "should be vmware with VMWare vendor name from prtdiag" do
          Facter.fact(:kernel).stubs(:value).returns("SunOS")
          Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil)
          Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns(nil)
          Facter::Util::Resolution.stubs(:exec).with('prtdiag').returns("System Configuration: VMware, Inc. VMware Virtual Platform")
          Facter.fact(:virtual).value.should == "vmware"
      end

      it "should be parallels with Parallels vendor name from prtdiag" do
          Facter.fact(:kernel).stubs(:value).returns("SunOS")
          Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil)
          Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns(nil)
          Facter::Util::Resolution.stubs(:exec).with('prtdiag').returns("System Configuration: Parallels Virtual Platform")
          Facter.fact(:virtual).value.should == "parallels"
      end

      it "should be virtualbox with VirtualBox vendor name from prtdiag" do
          Facter.fact(:kernel).stubs(:value).returns("SunOS")
          Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil)
          Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns(nil)
          Facter::Util::Resolution.stubs(:exec).with('prtdiag').returns("System Configuration: innotek GmbH VirtualBox")
          Facter.fact(:virtual).value.should == "virtualbox"
      end
  end
end

describe "is_virtual fact" do

    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 false when running on physical" do
       Facter.fact(:kernel).stubs(:value).returns("Linux")
       Facter.fact(:virtual).stubs(:value).returns("physical")
       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 virtualbox" do
        Facter.fact(:kernel).stubs(:value).returns("Linux")
        Facter.fact(:virtual).stubs(:value).returns("virtualbox")
        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

    it "should be true when running on kvm" do
        Facter.fact(:kernel).stubs(:value).returns("Linux")
        Facter.fact(:virtual).stubs(:value).returns("kvm")
        Facter.fact(:is_virtual).value.should == "true"
    end

    it "should be true when running in jail" do
        Facter.fact(:kernel).stubs(:value).returns("FreeBSD")
        Facter.fact(:virtual).stubs(:value).returns("jail")
        Facter.fact(:is_virtual).value.should == "true"
    end

    it "should be true when running in zone" do
        Facter.fact(:kernel).stubs(:value).returns("SunOS")
        Facter.fact(:virtual).stubs(:value).returns("zone")
        Facter.fact(:is_virtual).value.should == "true"
    end

    it "should be true when running on hp-vm" do
        Facter.fact(:kernel).stubs(:value).returns("HP-UX")
        Facter.fact(:virtual).stubs(:value).returns("hpvm")
        Facter.fact(:is_virtual).value.should == "true"
    end

    it "should be true when running on S390" do
        Facter.fact(:architecture).stubs(:value).returns("s390x")
        Facter.fact(:virtual).stubs(:value).returns("zlinux")
        Facter.fact(:is_virtual).value.should == "true"
    end

    it "should be true when running on parallels" do
        Facter.fact(:kernel).stubs(:value).returns("Darwin")
        Facter.fact(:virtual).stubs(:value).returns("parallels")
        Facter.fact(:is_virtual).value.should == "true"
    end
end