diff options
author | Tim Smith <tsmith@chef.io> | 2018-12-05 10:58:22 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-05 10:58:22 -0800 |
commit | 28b349c85027845391d407f364a7d5c96a54a016 (patch) | |
tree | 64bf81659ba0e786e8c8955477c17161513e9565 | |
parent | 67f9f6298083ee29d649dc6d3b7b19ab9f834ec8 (diff) | |
parent | ed6f838ba38a5dd86b50325697312a475b103a27 (diff) | |
download | ohai-28b349c85027845391d407f364a7d5c96a54a016.tar.gz |
Merge pull request #1317 from chef/virt
Unify virtualization detection on a single helper
-rw-r--r-- | .travis.yml | 14 | ||||
-rw-r--r-- | lib/ohai/mixin/dmi_decode.rb | 53 | ||||
-rw-r--r-- | lib/ohai/plugins/bsd/virtualization.rb | 22 | ||||
-rw-r--r-- | lib/ohai/plugins/linux/virtualization.rb | 30 | ||||
-rw-r--r-- | lib/ohai/plugins/solaris2/virtualization.rb | 18 | ||||
-rw-r--r-- | lib/ohai/plugins/windows/virtualization.rb | 44 | ||||
-rw-r--r-- | spec/unit/mixin/dmi_decode.rb | 68 | ||||
-rw-r--r-- | spec/unit/plugins/linux/virtualization_spec.rb | 199 | ||||
-rw-r--r-- | spec/unit/plugins/solaris2/virtualization_spec.rb | 85 | ||||
-rw-r--r-- | spec/unit/plugins/windows/cpu_spec.rb | 1 | ||||
-rw-r--r-- | spec/unit/plugins/windows/virtualization_spec.rb | 256 |
11 files changed, 215 insertions, 575 deletions
diff --git a/.travis.yml b/.travis.yml index 3a0048ae..3442195c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,14 +11,14 @@ branches: bundler_args: --jobs 7 --without docs debug -before_install: - - gem --version - - rvm @global do gem uninstall bundler -a -x -I || true - - gem install bundler - - bundle --version - - rm -f .bundle/config rvm: - - 2.5.1 + - 2.5.3 + before_install: + - gem --version + - rvm @global do gem uninstall bundler -a -x -I || true + - gem install bundler --no-document + - bundle --version + - rm -f .bundle/config - 2.6 - ruby-head diff --git a/lib/ohai/mixin/dmi_decode.rb b/lib/ohai/mixin/dmi_decode.rb index 018b8813..4ef4ef6c 100644 --- a/lib/ohai/mixin/dmi_decode.rb +++ b/lib/ohai/mixin/dmi_decode.rb @@ -1,6 +1,6 @@ # # Author:: Tim Smith <tsmith@chef.io> -# Copyright:: Copyright (c) 2015-2016 Chef Software, Inc. +# Copyright:: Copyright (c) 2015-2018 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,29 +17,34 @@ # http://www.dmo.ca/blog/detecting-virtualization-on-linux module ::Ohai::Mixin::DmiDecode - def guest_from_dmi(dmi_data) - dmi_data.each_line do |line| - case line - when /Manufacturer: Microsoft/ - return "hyperv" if dmi_data =~ /Version: (7.0|Hyper-V)/ - when /Manufacturer: VMware/ - return "vmware" - when /Manufacturer: Xen/ - return "xen" - when /Product.*: VirtualBox/ - return "vbox" - when /Product.*: OpenStack/ - return "openstack" - when /Manufacturer: QEMU|Product Name: (KVM|RHEV)/ - return "kvm" - when /Product.*: BHYVE/ - return "bhyve" - when /Manufacturer: Veertu/ - return "veertu" - when /Manufacturer: Amazon EC2/ - return "amazonec2" - end + def guest_from_dmi_data(manufacturer, product, version) + case manufacturer + when /Xen/ + return "xen" + when /VMware/ + return "vmware" + when /Microsoft/ + return "hyperv" if product =~ /Virtual Machine/ + when /Amazon EC2/ + return "amazonec2" + when /QEMU/ + return "kvm" + when /Veertu/ + return "veertu" + when /Parallels/ + return "parallels" end - nil + + case product + when /VirtualBox/ + return "vbox" + when /OpenStack/ + return "openstack" + when /(KVM|RHEV)/ + return "kvm" + when /BHYVE/ + return "bhyve" + end + nil # doesn't look like a virt end end diff --git a/lib/ohai/plugins/bsd/virtualization.rb b/lib/ohai/plugins/bsd/virtualization.rb index f6913340..9635070f 100644 --- a/lib/ohai/plugins/bsd/virtualization.rb +++ b/lib/ohai/plugins/bsd/virtualization.rb @@ -17,11 +17,11 @@ # limitations under the License. # -require "ohai/mixin/dmi_decode" - Ohai.plugin(:Virtualization) do - include Ohai::Mixin::DmiDecode provides "virtualization" + depends "dmi" + require "ohai/mixin/dmi_decode" + include Ohai::Mixin::DmiDecode collect_data(:freebsd, :openbsd, :netbsd, :dragonflybsd) do @@ -108,15 +108,13 @@ Ohai.plugin(:Virtualization) do logger.trace("Plugin Virtualization: Guest running on #{hypervisor} detected") end - # parse dmidecode to discover various virtualization guests - if File.exist?("/usr/local/sbin/dmidecode") || File.exist?("/usr/pkg/sbin/dmidecode") - guest = guest_from_dmi(shell_out("dmidecode").stdout) - if guest - virtualization[:system] = guest - virtualization[:role] = "guest" - virtualization[:systems][guest.to_sym] = "guest" - logger.trace("Plugin Virtualization: Guest running on #{guest} detected") - end + # parse dmi to discover various virtualization guests + guest = guest_from_dmi_data(get_attribute(:dmi, :system, :manufacturer), get_attribute(:dmi, :system, :product), get_attribute(:dmi, :system, :version)) + if guest + logger.trace("Plugin Virtualization: DMI data indicates #{guest} guest") + virtualization[:system] = guest + virtualization[:role] = "guest" + virtualization[:systems][guest.to_sym] = "guest" end end end diff --git a/lib/ohai/plugins/linux/virtualization.rb b/lib/ohai/plugins/linux/virtualization.rb index a605bd9c..fdc4b197 100644 --- a/lib/ohai/plugins/linux/virtualization.rb +++ b/lib/ohai/plugins/linux/virtualization.rb @@ -17,9 +17,10 @@ # Ohai.plugin(:Virtualization) do + provides "virtualization" + depends "dmi" require "ohai/mixin/dmi_decode" include Ohai::Mixin::DmiDecode - provides "virtualization" def lxc_version_exists? which("lxc-version") || which("lxc-start") @@ -129,25 +130,14 @@ Ohai.plugin(:Virtualization) do virtualization[:systems][:openvz] = "guest" end - # Detect Parallels virtual machine from pci devices - if File.exist?("/proc/bus/pci/devices") - if File.read("/proc/bus/pci/devices") =~ /1ab84000/ - logger.trace("Plugin Virtualization: /proc/bus/pci/devices contains '1ab84000' pci device. Detecting as parallels guest") - virtualization[:system] = "parallels" - virtualization[:role] = "guest" - virtualization[:systems][:parallels] = "guest" - end - end - - # parse dmidecode to discover various virtualization guests - if File.exist?("/usr/sbin/dmidecode") - guest = guest_from_dmi(shell_out("dmidecode").stdout) - if guest - logger.trace("Plugin Virtualization: dmidecode contains string indicating #{guest} guest") - virtualization[:system] = guest - virtualization[:role] = "guest" - virtualization[:systems][guest.to_sym] = "guest" - end + # parse dmi to discover various virtualization guests + logger.trace("Looking up #{get_attribute(:dmi, :system, :manufacturer)}, #{get_attribute(:dmi, :system, :product)} #{get_attribute(:dmi, :system, :version)}") + guest = guest_from_dmi_data(get_attribute(:dmi, :system, :manufacturer), get_attribute(:dmi, :system, :product), get_attribute(:dmi, :system, :version)) + if guest + logger.trace("Plugin Virtualization: DMI data indicates #{guest} guest") + virtualization[:system] = guest + virtualization[:role] = "guest" + virtualization[:systems][guest.to_sym] = "guest" end # Detect Hyper-V guest and the hostname of the host diff --git a/lib/ohai/plugins/solaris2/virtualization.rb b/lib/ohai/plugins/solaris2/virtualization.rb index 3c15a1ef..d0187e87 100644 --- a/lib/ohai/plugins/solaris2/virtualization.rb +++ b/lib/ohai/plugins/solaris2/virtualization.rb @@ -22,6 +22,7 @@ Ohai.plugin(:Virtualization) do require "ohai/mixin/dmi_decode" include Ohai::Mixin::DmiDecode provides "virtualization" + depends "dmi" def collect_solaris_guestid command = "/usr/sbin/zoneadm list -p" @@ -44,16 +45,13 @@ Ohai.plugin(:Virtualization) do end end - # Pass smbios information to the dmi_decode mixin to - # identify possible virtualization systems - smbios_path = Ohai.abs_path("/usr/sbin/smbios") - if File.exist?(smbios_path) - guest = guest_from_dmi(shell_out(smbios_path).stdout) - if guest - virtualization[:system] = guest - virtualization[:role] = "guest" - virtualization[:systems][guest.to_sym] = "guest" - end + # parse dmi to discover various virtualization guests + guest = guest_from_dmi_data(get_attribute(:dmi, :system, :manufacturer), get_attribute(:dmi, :system, :product), get_attribute(:dmi, :system, :version)) + if guest + logger.trace("Plugin Virtualization: DMI data indicates #{guest} guest") + virtualization[:system] = guest + virtualization[:role] = "guest" + virtualization[:systems][guest.to_sym] = "guest" end if File.executable?("/usr/sbin/zoneadm") diff --git a/lib/ohai/plugins/windows/virtualization.rb b/lib/ohai/plugins/windows/virtualization.rb index 16d750ea..b60aff8b 100644 --- a/lib/ohai/plugins/windows/virtualization.rb +++ b/lib/ohai/plugins/windows/virtualization.rb @@ -20,6 +20,8 @@ Ohai.plugin(:Virtualization) do provides "virtualization" + require "ohai/mixin/dmi_decode" + include Ohai::Mixin::DmiDecode collect_data(:windows) do require "wmi-lite/wmi" @@ -27,44 +29,16 @@ Ohai.plugin(:Virtualization) do virtualization Mash.new unless virtualization virtualization[:systems] = Mash.new unless virtualization[:systems] - # Grab BIOS data from WMI to determine vendor information + # Grab system DMI data from WMI to determine vendor information wmi = WmiLite::Wmi.new - bios = wmi.instances_of("Win32_BIOS") + dmi = wmi.first_of("Win32_ComputerSystemProduct") - case bios[0]["manufacturer"] - when "innotek GmbH" - virtualization[:system] = "vbox" + guest = guest_from_dmi_data(dmi["vendor"], dmi["name"], dmi["version"]) + if guest + logger.trace("Plugin Virtualization: DMI data in Win32_ComputerSystemProduct indicates #{guest} guest") + virtualization[:system] = guest virtualization[:role] = "guest" - virtualization[:systems][:vbox] = "guest" - when "Parallels Software International Inc." - virtualization[:system] = "parallels" - virtualization[:role] = "guest" - virtualization[:systems][:parallels] = "guest" - when "Bochs" - virtualization[:system] = "kvm" - virtualization[:role] = "guest" - virtualization[:systems][:kvm] = "guest" - when "American Megatrends Inc." - if bios[0]["version"] =~ /VRTUAL -/ - virtualization[:system] = "hyper-v" - virtualization[:role] = "guest" - virtualization[:systems][:hyperv] = "guest" - end - when "Xen" - virtualization[:system] = "xen" - virtualization[:role] = "guest" - virtualization[:systems][:xen] = "guest" - when "Veertu" - virtualization[:system] = "veertu" - virtualization[:role] = "guest" - virtualization[:systems][:veertu] = "guest" - end - - # vmware fusion detection - if bios[0]["serialnumber"] =~ /VMware/ - virtualization[:system] = "vmware" - virtualization[:role] = "guest" - virtualization[:systems][:vmware] = "guest" + virtualization[:systems][guest.to_sym] = "guest" end end end diff --git a/spec/unit/mixin/dmi_decode.rb b/spec/unit/mixin/dmi_decode.rb new file mode 100644 index 00000000..3cc881ed --- /dev/null +++ b/spec/unit/mixin/dmi_decode.rb @@ -0,0 +1,68 @@ +# +# Author:: Tim Smith (tsmith@chef.io) +# Copyright:: Copyright (c) 2018 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require_relative "../../spec_helper.rb" +require "ohai/mixin/dmi_decode" + +describe Ohai::Mixin::DmiDecode, "guest_from_dmi_data" do + let(:mixin) { Object.new.extend(Ohai::Mixin::DmiDecode) } + + # for the full DMI data used in these tests see https://github.com/chef/dmidecode_collection + { + xen: ["Xen", "HVM domU", "4.2.amazon"], + vmware: ["VMware, Inc.", "VMware Virtual Platform", "None"], + hyperv: ["Microsoft", "Virtual Machine", "7.0"], + amazonec2: ["Amazon EC2", "c5n.large", "Not Specified"], + veertu: ["Veertu", "Veertu", "Not Specified"], + parallels: ["Parallels Software International Inc.", "Parallels Virtual Platform", "None"], + vbox: ["Oracle Corporation", "VirtualBox", "1.2"], + openstack: ["Red Hat Inc.", "OpenStack Nova", "2014.1.2-1.el6"], + kvm: ["Red Hat", "KVM", "RHEL 7.0.0 PC (i440FX + PIIX, 1996"], + bhyve: ["", "BHYVE", "1.0"], + }.each_pair do |hypervisor, values| + describe "when passed #{hypervisor} dmi data" do + it "returns '#{hypervisor}'" do + expect(mixin.guest_from_dmi_data(values[0], values[1], values[2])).to eq("#{hypervisor}") + end + end + end + + describe "When running on RHEV Hypervisor" do + it "returns 'kvm'" do + expect(mixin.guest_from_dmi_data("Red Hat", "RHEV Hypervisor", "6.7-20150911.0.el6ev")).to eq("kvm") + end + end + + describe "When the manufactuer is 'QEMU'" do + it "return kvm" do + expect(mixin.guest_from_dmi_data("QEMU", "", "")).to eq("kvm") + end + end + + describe "returns nil if manufactuer is 'Microsoft', but product is not 'Virtual Machine'" do + it "returns nil" do + expect(mixin.guest_from_dmi_data("Microsot", "Zune", "2018")).to be_nil + end + end + + describe "When running on an unkown system" do + it "returns nil" do + expect(mixin.guest_from_dmi_data("TimCorp", "SuperServer", "2018")).to be_nil + end + end +end diff --git a/spec/unit/plugins/linux/virtualization_spec.rb b/spec/unit/plugins/linux/virtualization_spec.rb index 8285a6f8..13c195ea 100644 --- a/spec/unit/plugins/linux/virtualization_spec.rb +++ b/spec/unit/plugins/linux/virtualization_spec.rb @@ -29,7 +29,6 @@ describe Ohai::System, "Linux virtualization platform" do allow(File).to receive(:exist?).with("/proc/xen/capabilities").and_return(false) allow(File).to receive(:exist?).with("/proc/modules").and_return(false) allow(File).to receive(:exist?).with("/proc/cpuinfo").and_return(false) - allow(File).to receive(:exist?).with("/usr/sbin/dmidecode").and_return(false) allow(File).to receive(:exist?).with("/var/lib/hyperv/.kvp_pool_3").and_return(false) allow(File).to receive(:exist?).with("/proc/self/status").and_return(false) allow(File).to receive(:exist?).with("/proc/bc/0").and_return(false) @@ -37,7 +36,6 @@ describe Ohai::System, "Linux virtualization platform" do allow(File).to receive(:exist?).with("/proc/self/cgroup").and_return(false) allow(File).to receive(:exist?).with("/.dockerenv").and_return(false) allow(File).to receive(:exist?).with("/.dockerinit").and_return(false) - allow(File).to receive(:exist?).with("/proc/bus/pci/devices").and_return(false) allow(File).to receive(:exist?).with("/sys/devices/virtual/misc/kvm").and_return(false) allow(File).to receive(:exist?).with("/dev/lxd/sock").and_return(false) allow(File).to receive(:exist?).with("/var/lib/lxd/devlxd").and_return(false) @@ -187,165 +185,28 @@ describe Ohai::System, "Linux virtualization platform" do end end - describe "when we are parsing dmidecode" do - before(:each) do - expect(File).to receive(:exist?).with("/usr/sbin/dmidecode").and_return(true) - end - - it "sets hyperv guest if dmidecode detects Hyper-V or version 7.0" do - ms_hv_dmidecode = <<~MSHV - System Information - Manufacturer: Microsoft Corporation - Product Name: Virtual Machine - Version: 7.0 - Serial Number: 9242-2608-7031-8934-2088-5216-61 - UUID: C2431A2D-D69C-244F-9DE8-CD5D09E0DA39 - Wake-up Type: Power Switch -MSHV - allow(plugin).to receive(:shell_out).with("dmidecode").and_return(mock_shell_out(0, ms_hv_dmidecode, "")) - plugin.run - expect(plugin[:virtualization][:system]).to eq("hyperv") - expect(plugin[:virtualization][:role]).to eq("guest") - expect(plugin[:virtualization][:systems][:hyperv]).to eq("guest") - end - - it "sets vmware guest if dmidecode detects VMware" do - vmware_dmidecode = <<~VMWARE - System Information - Manufacturer: VMware, Inc. - Product Name: VMware Virtual Platform - Version: None - Serial Number: VMware-50 3f f7 14 42 d1 f1 da-3b 46 27 d0 29 b4 74 1d - UUID: a86cc405-e1b9-447b-ad05-6f8db39d876a - Wake-up Type: Power Switch - SKU Number: Not Specified - Family: Not Specified -VMWARE - allow(plugin).to receive(:shell_out).with("dmidecode").and_return(mock_shell_out(0, vmware_dmidecode, "")) - plugin.run - expect(plugin[:virtualization][:system]).to eq("vmware") - expect(plugin[:virtualization][:role]).to eq("guest") - expect(plugin[:virtualization][:systems][:vmware]).to eq("guest") - end - - it "sets vbox guest if dmidecode detects VirtualBox" do - vbox_dmidecode = <<~VBOX - Base Board Information - Manufacturer: Oracle Corporation - Product Name: VirtualBox - Version: 1.2 - Serial Number: 0 - Asset Tag: Not Specified - Features: - Board is a hosting board - Location In Chasis: Not Specified - Type: Motherboard - Contained Object Handles: 0 -VBOX - allow(plugin).to receive(:shell_out).with("dmidecode").and_return(mock_shell_out(0, vbox_dmidecode, "")) - plugin.run - expect(plugin[:virtualization][:system]).to eq("vbox") - expect(plugin[:virtualization][:role]).to eq("guest") - expect(plugin[:virtualization][:systems][:vbox]).to eq("guest") - end + describe "when we are parsing DMI data" do - it "sets openstack guest if dmidecode detects OpenStack" do - openstack_dmidecode = <<~OPENSTACK - System Information - Manufacturer: Red Hat Inc. - Product Name: OpenStack Nova - Version: 2014.1.2-1.el6 - Serial Number: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX - UUID: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX - Wake-up Type: Power Switch - SKU Number: Not Specified - Family: Red Hat Enterprise Linux -OPENSTACK - allow(plugin).to receive(:shell_out).with("dmidecode").and_return(mock_shell_out(0, openstack_dmidecode, "")) + it "sets virtualization attributes if the appropriate DMI data is present" do + plugin[:dmi] = { system: { + manufacturer: "Amazon EC2", + product: "c5n.large", + version: nil, + }, + } plugin.run - expect(plugin[:virtualization][:system]).to eq("openstack") + expect(plugin[:virtualization][:system]).to eq("amazonec2") expect(plugin[:virtualization][:role]).to eq("guest") - expect(plugin[:virtualization][:systems][:openstack]).to eq("guest") - end - - it "sets kvm guest if dmidecode contains KVM" do - kvm_dmidecode = <<~RKVM - System Information - Manufacturer: Red Hat - Product Name: KVM - Version: RHEL 7.0.0 PC (i440FX + PIIX, 1996) - Serial Number: Not Specified - UUID: 6E56CFE2-2088-4A46-906A-FC49EDC4072C - Wake-up Type: Power Switch - SKU Number: Not Specified - Family: Red Hat Enterprise Linux -RKVM - allow(plugin).to receive(:shell_out).with("dmidecode").and_return(mock_shell_out(0, kvm_dmidecode, "")) - plugin.run - expect(plugin[:virtualization][:system]).to eq("kvm") - expect(plugin[:virtualization][:role]).to eq("guest") - expect(plugin[:virtualization][:systems][:kvm]).to eq("guest") + expect(plugin[:virtualization][:systems][:amazonec2]).to eq("guest") end - it "sets kvm guest if dmidecode detects RHEV" do - kvm_dmidecode = <<~RHEV - System Information - Manufacturer: Red Hat - Product Name: RHEV Hypervisor - Version: 6.7-20150911.0.el6ev - Serial Number: 00000000-0000-0000-0000-000000000000 - UUID: E7F1DC93-3DA1-4EC3-A6AB-F6904BA87985 - Wake-up Type: Power Switch - SKU Number: Not Specified - Family: Red Hat Enterprise Linux -RHEV - allow(plugin).to receive(:shell_out).with("dmidecode").and_return(mock_shell_out(0, kvm_dmidecode, "")) - plugin.run - expect(plugin[:virtualization][:system]).to eq("kvm") - expect(plugin[:virtualization][:role]).to eq("guest") - expect(plugin[:virtualization][:systems][:kvm]).to eq("guest") - end - - it "sets bhyve guest if dmidecode detects bhyve" do - bhyve_dmidecode = <<~OUTPUT - System Information - Manufacturer: - Product Name: BHYVE - Version: 1.0 - Serial Number: None - UUID: 023B323A-E139-4B36-8BC5-CEBB2469DAAA - Wake-up Type: Power Switch - SKU Number: None - Family: -OUTPUT - allow(plugin).to receive(:shell_out).with("dmidecode").and_return(mock_shell_out(0, bhyve_dmidecode, "")) - plugin.run - expect(plugin[:virtualization][:system]).to eq("bhyve") - expect(plugin[:virtualization][:role]).to eq("guest") - expect(plugin[:virtualization][:systems][:bhyve]).to eq("guest") - end - - it "sets veertu guest if dmidecode detects Veertu" do - veertu_dmidecode = <<~VEERTU - System Information - Manufacturer: Veertu - Product Name: Veertu - Version: Not Specified - Serial Number: Not Specified - UUID: Not Settable - Wake-up Type: Power Switch - SKU Number: Not Specified - Family: Not Specified -VEERTU - allow(plugin).to receive(:shell_out).with("dmidecode").and_return(mock_shell_out(0, veertu_dmidecode, "")) - plugin.run - expect(plugin[:virtualization][:system]).to eq("veertu") - expect(plugin[:virtualization][:role]).to eq("guest") - expect(plugin[:virtualization][:systems][:veertu]).to eq("guest") - end - - it "should run dmidecode and not set virtualization if nothing is detected" do - allow(plugin).to receive(:shell_out).with("dmidecode").and_return(mock_shell_out(0, "", "")) + it "sets empty virtualization attributes if nothing is detected" do + plugin[:dmi] = { system: { + manufacturer: "Supermicro", + product: "X10SLH-N6-ST031", + version: "0123456789", + }, + } plugin.run expect(plugin[:virtualization]).to eq({ "systems" => {} }) end @@ -451,32 +312,6 @@ VEERTU end end - describe "when we are checking for parallels" do - it "sets parallels guest if /proc/bus/pci/devices contains 1ab84000" do - devices = <<~DEVICES - 0018 1ab84000 1f 8001 0 0 0 0 0 0 20 0 0 0 0 0 0 prl_tg - 0028 1af41000 17 8201 ee000000 0 0 0 0 0 40 1000 0 0 0 0 0 virtio-pci - DEVICES - expect(File).to receive(:exist?).with("/proc/bus/pci/devices").and_return(true) - allow(File).to receive(:read).with("/proc/bus/pci/devices").and_return(devices) - plugin.run - expect(plugin[:virtualization][:system]).to eq("parallels") - expect(plugin[:virtualization][:role]).to eq("guest") - expect(plugin[:virtualization][:systems][:parallels]).to eq("guest") - end - - it "does not set virtualization if /proc/bus/pci/devices not contains 1ab84000" do - devices = <<~DEVICES - 0030 1af41000 a 8401 ee040000 0 0 0 0 0 40 1000 0 0 0 0 0 virtio-pci - 0050 10110022 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - DEVICES - expect(File).to receive(:exist?).with("/proc/bus/pci/devices").and_return(true) - allow(File).to receive(:read).with("/proc/bus/pci/devices").and_return(devices) - plugin.run - expect(plugin[:virtualization]).to eq({ "systems" => {} }) - end - end - describe "when we are checking for lxd" do it "sets lxc guest if /dev/lxd/sock exists" do expect(File).to receive(:exist?).with("/dev/lxd/sock").and_return(true) diff --git a/spec/unit/plugins/solaris2/virtualization_spec.rb b/spec/unit/plugins/solaris2/virtualization_spec.rb index 2bf890be..1208c203 100644 --- a/spec/unit/plugins/solaris2/virtualization_spec.rb +++ b/spec/unit/plugins/solaris2/virtualization_spec.rb @@ -19,6 +19,8 @@ require_relative "../../../spec_helper.rb" describe Ohai::System, "Solaris virtualization platform" do + let(:plugin) { get_plugin("solaris2/virtualization") } + before(:each) do @psrinfo_pv = <<~PSRINFO_PV The physical processor has 1 virtual processor (0) @@ -26,15 +28,14 @@ describe Ohai::System, "Solaris virtualization platform" do Intel Pentium(r) Pro PSRINFO_PV - @plugin = get_plugin("solaris2/virtualization") - allow(@plugin).to receive(:collect_os).and_return(:solaris2) + allow(plugin).to receive(:collect_os).and_return(:solaris2) # default to all requested Files not existing allow(File).to receive(:exist?).with("/usr/sbin/psrinfo").and_return(false) allow(File).to receive(:exist?).with("/usr/sbin/smbios").and_return(false) allow(File).to receive(:exist?).with("/usr/sbin/zoneadm").and_return(false) - allow(@plugin).to receive(:shell_out).with("/usr/sbin/smbios").and_return(mock_shell_out(0, "", "")) - allow(@plugin).to receive(:shell_out).with("#{Ohai.abs_path( "/usr/sbin/psrinfo" )} -pv").and_return(mock_shell_out(0, "", "")) + allow(plugin).to receive(:shell_out).with("/usr/sbin/smbios").and_return(mock_shell_out(0, "", "")) + allow(plugin).to receive(:shell_out).with("#{Ohai.abs_path( "/usr/sbin/psrinfo" )} -pv").and_return(mock_shell_out(0, "", "")) end describe "when we are checking for kvm" do @@ -43,64 +44,54 @@ PSRINFO_PV end it "should run psrinfo -pv" do - expect(@plugin).to receive(:shell_out).with("#{Ohai.abs_path( "/usr/sbin/psrinfo" )} -pv") - @plugin.run + expect(plugin).to receive(:shell_out).with("#{Ohai.abs_path( "/usr/sbin/psrinfo" )} -pv") + plugin.run end it "Should set kvm guest if psrinfo -pv contains QEMU Virtual CPU" do - allow(@plugin).to receive(:shell_out).with("#{Ohai.abs_path( "/usr/sbin/psrinfo" )} -pv").and_return(mock_shell_out(0, "QEMU Virtual CPU", "")) - @plugin.run - expect(@plugin[:virtualization][:system]).to eq("kvm") - expect(@plugin[:virtualization][:role]).to eq("guest") - expect(@plugin[:virtualization][:systems][:kvm]).to eq("guest") + allow(plugin).to receive(:shell_out).with("#{Ohai.abs_path( "/usr/sbin/psrinfo" )} -pv").and_return(mock_shell_out(0, "QEMU Virtual CPU", "")) + plugin.run + expect(plugin[:virtualization][:system]).to eq("kvm") + expect(plugin[:virtualization][:role]).to eq("guest") + expect(plugin[:virtualization][:systems][:kvm]).to eq("guest") end it "should not set virtualization if kvm isn't there" do - expect(@plugin).to receive(:shell_out).with("#{Ohai.abs_path( "/usr/sbin/psrinfo" )} -pv").and_return(mock_shell_out(0, @psrinfo_pv, "")) - @plugin.run - expect(@plugin[:virtualization][:systems]).to eq({}) + expect(plugin).to receive(:shell_out).with("#{Ohai.abs_path( "/usr/sbin/psrinfo" )} -pv").and_return(mock_shell_out(0, @psrinfo_pv, "")) + plugin.run + expect(plugin[:virtualization][:systems]).to eq({}) end end - describe "when we are parsing smbios" do - before(:each) do - expect(File).to receive(:exist?).with("/usr/sbin/smbios").and_return(true) - end - - it "should run smbios" do - expect(@plugin).to receive(:shell_out).with("/usr/sbin/smbios") - @plugin.run - end - - it "should set vmware guest if smbios detects VMware Virtual Platform" do - vmware_smbios = <<~VMWARE - ID SIZE TYPE - 1 72 SMB_TYPE_SYSTEM (system information) - - Manufacturer: VMware, Inc. - Product: VMware Virtual Platform - Version: None - Serial Number: VMware-50 3f f7 14 42 d1 f1 da-3b 46 27 d0 29 b4 74 1d + describe "when we are parsing DMI data" do - UUID: a86cc405-e1b9-447b-ad05-6f8db39d876a - Wake-Up Event: 0x6 (power switch) -VMWARE - allow(@plugin).to receive(:shell_out).with("/usr/sbin/smbios").and_return(mock_shell_out(0, vmware_smbios, "")) - @plugin.run - expect(@plugin[:virtualization][:system]).to eq("vmware") - expect(@plugin[:virtualization][:role]).to eq("guest") - expect(@plugin[:virtualization][:systems][:vmware]).to eq("guest") + it "sets virtualization attributes if the appropriate DMI data is present" do + plugin[:dmi] = { system: { + manufacturer: "Amazon EC2", + product: "c5n.large", + version: nil, + }, + } + plugin.run + expect(plugin[:virtualization][:system]).to eq("amazonec2") + expect(plugin[:virtualization][:role]).to eq("guest") + expect(plugin[:virtualization][:systems][:amazonec2]).to eq("guest") end - it "should run smbios and not set virtualization if nothing is detected" do - expect(@plugin).to receive(:shell_out).with("/usr/sbin/smbios") - @plugin.run - expect(@plugin[:virtualization][:systems]).to eq({}) + it "sets empty virtualization attributes if nothing is detected" do + plugin[:dmi] = { system: { + manufacturer: "Supermicro", + product: "X10SLH-N6-ST031", + version: "0123456789", + }, + } + plugin.run + expect(plugin[:virtualization]).to eq({ "systems" => {} }) end end it "should not set virtualization if no tests match" do - @plugin.run - expect(@plugin[:virtualization][:systems]).to eq({}) + plugin.run + expect(plugin[:virtualization][:systems]).to eq({}) end end diff --git a/spec/unit/plugins/windows/cpu_spec.rb b/spec/unit/plugins/windows/cpu_spec.rb index 451af6d3..c0a17bd7 100644 --- a/spec/unit/plugins/windows/cpu_spec.rb +++ b/spec/unit/plugins/windows/cpu_spec.rb @@ -65,7 +65,6 @@ describe Ohai::System, "Windows cpu plugin" do @plugin = get_plugin("cpu") allow(@plugin).to receive(:collect_os).and_return(:windows) - @double_wmi = double(WmiLite::Wmi) @double_wmi_instance = instance_double(WmiLite::Wmi) @processors = [{ "description" => "Intel64 Family 6 Model 70 Stepping 1", diff --git a/spec/unit/plugins/windows/virtualization_spec.rb b/spec/unit/plugins/windows/virtualization_spec.rb index 49d4446e..72c09e6b 100644 --- a/spec/unit/plugins/windows/virtualization_spec.rb +++ b/spec/unit/plugins/windows/virtualization_spec.rb @@ -22,201 +22,23 @@ require_relative "../../../spec_helper.rb" describe Ohai::System, "Windows virtualization platform" do let(:plugin) { get_plugin("windows/virtualization") } + let(:wmi) { double("WmiLite::Wmi") } before(:each) do + allow(WmiLite::Wmi).to receive(:new).and_return(wmi) allow(plugin).to receive(:collect_os).and_return(:windows) end - context "when running on vmware" do + describe "it sets virtualization guest status from Win32_ComputerSystemProduct data" do it "system is vmware" do - allow_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with("Win32_BIOS").and_return([{ "bioscharacteristics" => [4, 7, 8, 9, 10, 11, 12, 14, 15, 16, 19, 26, 27, 28, 29, 30, 32, 39, 40, 41, 42, 50, 57, 58], - "biosversion" => ["INTEL - 6040000", "PhoenixBIOS 4.0 Release 6.0 "], - "buildnumber" => nil, - "caption" => "PhoenixBIOS 4.0 Release 6.0 ", - "codeset" => nil, "currentlanguage" => nil, - "description" => "PhoenixBIOS 4.0 Release 6.0 ", - "identificationcode" => nil, - "installablelanguages" => nil, - "installdate" => nil, - "languageedition" => nil, - "listoflanguages" => nil, - "manufacturer" => "Phoenix Technologies LTD", - "name" => "PhoenixBIOS 4.0 Release 6.0 ", - "othertargetos" => nil, - "primarybios" => true, - "releasedate" => "20130731000000.000000+000", - "serialnumber" => "VMware-56 4d 65 24 ac cf ec 72-fa 29 b2 7d 8f df b2 7a", - "smbiosbiosversion" => "6.00", - "smbiosmajorversion" => 2, - "smbiosminorversion" => 4, - "smbiospresent" => true, - "softwareelementid" => "PhoenixBIOS 4.0 Release 6.0 ", - "softwareelementstate" => 3, - "status" => "OK", - "targetoperatingsystem" => 0, - "version" => "INTEL - 6040000" - }]) - plugin.run - expect(plugin[:virtualization][:system]).to eq("vmware") - expect(plugin[:virtualization][:role]).to eq("guest") - expect(plugin[:virtualization][:systems][:vmware]).to eq("guest") - end - end - - context "when running on parallels desktop" do - it "system is parallels" do - allow_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with("Win32_BIOS").and_return([{ "bioscharacteristics" => [4, 7, 9, 10, 15, 24, 25, 27, 28, 29, 30, 32, 42, 44, 48, 49, 51, 64, 65, 67], - "biosversion" => ["PRLS - 1"], - "buildnumber" => nil, - "caption" => "Default System BIOS", - "codeset" => nil, - "currentlanguage" => nil, - "description" => "Default System BIOS", - "identificationcode" => nil, - "installablelanguages" => nil, - "installdate" => nil, - "languageedition" => nil, - "listoflanguages" => nil, - "manufacturer" => "Parallels Software International Inc.", - "name" => "Default System BIOS", - "othertargetos" => nil, - "primarybios" => true, - "releasedate" => "20151005000000.000000+000", - "serialnumber" => "Parallels-82 75 A0 A0 9B B4 47 7C 87 A9 D9 E1 2B 90 4B 1F", - "smbiosbiosversion" => "11.0.2 (31348)", - "smbiosmajorversion" => 2, - "smbiosminorversion" => 7, - "smbiospresent" => true, - "softwareelementid" => "Default System BIOS", - "softwareelementstate" => 3, - "status" => "OK", - "targetoperatingsystem" => 0, - "version" => "PRLS - 1", - }]) - plugin.run - expect(plugin[:virtualization][:system]).to eq("parallels") - expect(plugin[:virtualization][:role]).to eq("guest") - expect(plugin[:virtualization][:systems][:parallels]).to eq("guest") - end - end - - context "when running on kvm" do - it "system is kvm" do - allow_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with("Win32_BIOS").and_return([{ "bioscharacteristics" => [3, 42, 48, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79], - "biosversion" => ["BOCHS - 1"], - "buildnumber" => nil, - "caption" => "Default System BIOS", - "codeset" => nil, - "currentlanguage" => nil, - "description" => "Default System BIOS", - "identificationcode" => nil, - "installablelanguages" => nil, - "installdate" => nil, - "languageedition" => nil, - "listoflanguages" => nil, - "manufacturer" => "Bochs", - "name" => "Default System BIOS", - "othertargetos" => nil, - "primarybios" => true, - "releasedate" => "20110101******.******+***", - "serialnumber" => nil, - "smbiosbiosversion" => "Bochs", - "smbiosmajorversion" => 2, - "smbiosminorversion" => 4, - "smbiospresent" => true, - "softwareelementid" => "Default System BIOS", - "softwareelementstate" => 3, - "status" => "OK", - "targetoperatingsystem" => 0, "version" => "BOCHS -1" - }]) - plugin.run - expect(plugin[:virtualization][:system]).to eq("kvm") - expect(plugin[:virtualization][:role]).to eq("guest") - expect(plugin[:virtualization][:systems][:kvm]).to eq("guest") - end - end - - context "when running on virtualbox" do - it "system is vbox" do - allow_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with("Win32_BIOS").and_return([{ "bioscharacteristics" => [4, 7, 15, 16, 27, 30, 32], - "biosversion" => ["VBOX - 1"], - "buildnumber" => nil, - "caption" => "Default System BIOS", - "codeset" => nil, - "currentlanguage" => nil, - "description" => "Default System BIOS", - "identificationcode" => nil, - "installablelanguages" => nil, - "installdate" => nil, - "languageedition" => nil, - "listoflanguages" => nil, - "manufacturer" => "innotek GmbH", - "name" => "Default System BIOS", - "othertargetos" => nil, - "primarybios" => true, - "releasedate" => "20061201000000.000000+000", - "serialnumber" => "0", - "smbiosbiosversion" => "VirtualBox", - "smbiosmajorversion" => 2, - "smbiosminorversion" => 5, - "smbiospresent" => true, - "softwareelementid" => "Default System BIOS", - "softwareelementstate" => 3, - "status" => "OK", - "targetoperatingsystem" => 0, - "version" => "VBOX - 1", - }]) - plugin.run - expect(plugin[:virtualization][:system]).to eq("vbox") - expect(plugin[:virtualization][:role]).to eq("guest") - expect(plugin[:virtualization][:systems][:vbox]).to eq("guest") - end - end - - context "when running on hyper-v" do - it "system is hyper-v" do - allow_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with("Win32_BIOS").and_return([{ "bioscharacteristics" => [4, 7, 9, 11, 12, 14, 15, 16, 17, 19, 22, 23, 24, 25, 26, 27, 28, 29, 30, 34, 36, 37, 40], - "biosversion" => ["VRTUAL - 4001628, BIOS Date: 04/28/16 13:00:17 Ver: 09.00.06, BIOS Date: 04/28/16 13:00:17 Ver: 09.00.06"], - "buildnumber" => nil, - "codeset" => nil, - "currentlanguage" => "enUS", - "description" => "BIOS Date: 04/28/16 13:00:17 Ver: 09.00.06", - "identificationcode" => nil, - "installablelanguages" => 1, - "installdate" => nil, - "languageedition" => nil, - "listoflanguages" => ["enUS"], - "manufacturer" => "American Megatrends Inc.", - "name" => "BIOS Date: 04/28/16 13:00:17 Ver: 09.00.06", - "othertargetos" => nil, - "primarybios" => true, - "releasedate" => "20160428000000.000000+000", - "serialnumber" => "1158-1757-7941-3855-2170-4122-00", - "smbiosbiosversion" => "090006", - "smbiosmajorversion" => 2, - "smbiosminorversion" => 3, - "smbiospresent" => true, - "softwareelementid" => "BIOS Date: 04/28/16 13:00:17 Ver: 09.00.06", - "softwareelementstate" => 3, - "status" => "OK", - "targetoperatingsystem" => 0, - "version" => "VRTUAL - 4001628", - }]) - plugin.run - expect(plugin[:virtualization][:system]).to eq("hyper-v") - expect(plugin[:virtualization][:role]).to eq("guest") - expect(plugin[:virtualization][:systems][:hyperv]).to eq("guest") - end - end - - context "when running on xen" do - it "system is xen" do - allow_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with("Win32_BIOS").and_return([{ "smbiosbiosversion" => ["4.2.amazon"], - "manufacturer" => "Xen", - "name" => "Revision: 1.221", - "serialnumber" => "ec2b487f-d9ed-7d17-c7c0-1d4599d6c1da", - "version" => "Xen - 0", - }]) + allow(wmi).to receive(:first_of).with("Win32_ComputerSystemProduct").and_return( { "caption" => "Computer System Product", + "description" => "Computer System Product", + "identifyingnumber" => "ec2d6aad-f59b-a10d-5784-ca9b7ba4f727", + "name" => "HVM domU", + "skunumber" => nil, + "uuid" => "EC2D6AAD-F59B-A10D-5784-CA9B7BA4F727", + "vendor" => "Xen", + "version" => "4.2.amazon" } ) plugin.run expect(plugin[:virtualization][:system]).to eq("xen") expect(plugin[:virtualization][:role]).to eq("guest") @@ -224,56 +46,16 @@ describe Ohai::System, "Windows virtualization platform" do end end - context "when running on veertu" do - it "system is veertu" do - allow_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with("Win32_BIOS").and_return([{ "smbiosbiosversion" => ["Veertu"], - "manufacturer" => "Veertu", - "name" => "Default System BIOS", - "serialnumber" => "", - "version" => "Veertu - 1", - - }]) - plugin.run - expect(plugin[:virtualization][:system]).to eq("veertu") - expect(plugin[:virtualization][:role]).to eq("guest") - expect(plugin[:virtualization][:systems][:veertu]).to eq("guest") - end - end - context "when running on a hardware system" do it "does not set virtualization attributes" do - allow_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with("Win32_BIOS").and_return([{ "bioscharacteristics" => [7, 11, 12, 15, 16, 17, 19, 23, 24, 25, 26, 27, 28, 29, 32, 33, 40, 42, 43], - "biosversion" => ["DELL - 1072009", "A10", "American Megatrends - 4028D"], - "buildnumber" => nil, - "caption" => "A10", - "codeset" => nil, - "currentlanguage" => nil, - "description" => "A10", - "embeddedcontrollermajorversion" => 255, - "embeddedcontrollerminorversion" => 255, - "identificationcode" => nil, - "installablelanguages" => nil, - "installdate" => nil, - "languageedition" => nil, - "listoflanguages" => nil, - "manufacturer" => "Dell Inc.", - "name" => "A10", - "othertargetos" => nil, - "primarybios" => true, - "releasedate" => "20130513000000.000000+000", - "serialnumber" => "87GBNY1", - "smbiosbiosversion" => "A10", - "smbiosmajorversion" => 2, - "smbiosminorversion" => 7, - "smbiospresent" => true, - "softwareelementid" => "A10", - "softwareelementstate" => 3, - "status" => "OK", - "systembiosmajorversion" => 4, - "systembiosminorversion" => 6, - "targetoperatingsystem" => 0, - "version" => "DELL - 1072009", - }]) + allow(wmi).to receive(:first_of).with("Win32_ComputerSystemProduct").and_return({ "caption" => "Computer System Product", + "description" => "Computer System Product", + "identifyingnumber" => "0123456789", + "name" => "X10SLH-N6-ST031", + "skunumber" => nil, + "uuid" => "00000000-0000-0000-0000-0CC47A8F7618", + "vendor" => "Supermicro", + "version" => "0123456789" }) plugin.run expect(plugin[:virtualization]).to eq("systems" => {}) end |