diff options
author | Tim Smith <tsmith@chef.io> | 2019-06-13 16:02:47 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-13 16:02:47 -0700 |
commit | 9b9fdd4914fa3694ae6c4f96311f43e64befd0f7 (patch) | |
tree | cf90137285fbf86f3d112cb87ea3063d3d1a4943 | |
parent | abb2eb272583ad004d39f104f5047384e8e8be05 (diff) | |
parent | 5baab4acaa099b98eaabc7c76d45db4e05404ce8 (diff) | |
download | ohai-9b9fdd4914fa3694ae6c4f96311f43e64befd0f7.tar.gz |
Merge pull request #1376 from chef/virtualbox
Merge the new vbox plugin into the existing virtualbox plugin
-rw-r--r-- | lib/ohai/plugins/vbox_host.rb | 201 | ||||
-rw-r--r-- | lib/ohai/plugins/virtualbox.rb | 148 | ||||
-rw-r--r-- | spec/unit/plugins/vbox_host_spec.rb | 318 | ||||
-rw-r--r-- | spec/unit/plugins/virtualbox_spec.rb | 327 |
4 files changed, 445 insertions, 549 deletions
diff --git a/lib/ohai/plugins/vbox_host.rb b/lib/ohai/plugins/vbox_host.rb deleted file mode 100644 index f1657dd2..00000000 --- a/lib/ohai/plugins/vbox_host.rb +++ /dev/null @@ -1,201 +0,0 @@ -# Author:: "Joshua Colson" <joshua.colson@gmail.com> -# 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. -# - -Ohai.plugin(:VboxHost) do - depends "virtualization" - provides "vbox" - - # determine if this host is configured with virtualbox or not - # the determination is ultimately controlled by the "virtualization" plugin - def vbox_host? - virtualization.dig("systems", "vbox") == "host" - end - - # query virtualbox for each configured vm, as well as - # each guest"s individual configuration settings - def vboxmanage_list_vms - vms = Mash.new - if vbox_host? - so_cmd = "VBoxManage list --sorted vms" - logger.trace(so_cmd) - so = shell_out(so_cmd) - - if so.exitstatus == 0 - # parse the output - so.stdout.lines.each do |line| - case line - when /^"(\S*)" \{(\S*)\}$/ - name = Regexp.last_match(1) - uuid = Regexp.last_match(2) - vms[name] = vboxmanage_vminfo(uuid) - end - end - end - end - vms - rescue Ohai::Exceptions::Exec - logger.trace("Plugin VboxHost: Could not run 'VBoxManage list --sorted vms'. Skipping data") - end - - # query the vminfo for particular guest instance, normalizing - # the fields so that they"re not enclosed in double-quotes (") - def vboxmanage_vminfo(machine_id) - vm = Mash.new - - if vbox_host? - so_cmd = "VBoxManage showvminfo #{machine_id} --machinereadable" - logger.trace(so_cmd) - so = shell_out(so_cmd) - - if so.exitstatus == 0 - so.stdout.lines.each do |line| - line.chomp! - left, right = line.split("=") - - # remove enclosing quotes, if needed - key = - case left - when /^"(.*)"$/ - Regexp.last_match(1) - else - left - end - - # skip the name attribute since that is the parent key - next if left == "name" - - # remove enclosing quotes, if needed - value = - case right - when /^"(.*)"$/ - Regexp.last_match(1) - else - right - end - - vm[key.downcase] = value - end - end - end - vm - rescue Ohai::Exceptions::Exec - logger.trace("Plugin VboxHost: Could not run '#{so_cmd}'. Skipping data") - end - - # query virtualbox for a list of #{query_type} items - # these queries return a result set that is delimited by - # multiple successive newlines, with each block containing - # key/value pairs delimited by a colon (:) and column aligned - # - # the keys of each k/v pair are normalized to lowercase - def vboxmanage_list_blocks(query_type, name_key) - # ignore unrecognized query type - supported_queries = %w{ - bridgedifs dhcpservers dvds hdds hostdvds - hostfloppies hostonlyifs natnets ostypes - } - return nil unless supported_queries.include? query_type - results = Mash.new - - if vbox_host? - so_cmd = "VBoxManage list --sorted #{query_type}" - logger.trace(so_cmd) - so = shell_out(so_cmd) - # raise an exception if the command fails - # so.error! - - if so.exitstatus == 0 - # break the result into paragraph blocks, on successive newlines - so.stdout.each_line("") do |blk| - # remove the multiple newlines of each record - blk.chomp!.chomp! - # initialize a blank record hash - record = Mash.new - # parse the record block into key/value pairs - blk.each_line() do |line| - next unless line.include? ":" - # split the line into key/value pair - key, right = line.split(":", 2) - - # strip the leading/trailing whitespace if the value is not nil - value = right.nil? ? "" : right.strip - record[key.downcase] = value - end - - # insert the record into the list of results - if record.key? name_key.downcase - name = record.delete(name_key.downcase) - results[name] = record - end - end - end - end - results - rescue Ohai::Exceptions::Exec - logger.trace("Plugin VboxHost: Could not run '#{so_cmd}'. Skipping data") - end - - # collect the data for a virtualization host running VirtualBox - collect_data(:default) do - ostypes = "ostypes" - guests = "guests" - natnets = "natnets" - hostonlyifs = "hostonlyifs" - bridgedifs = "bridgedifs" - dhcpservers = "dhcpservers" - hdds = "hdds" - dvds = "dvds" - hostdvds = "hostdvds" - hostfloppies = "hostfloppies" - - if vbox_host? - vbox Mash.new unless vbox - - # get a list of virtualbox virtual hard disk drives - vbox[ostypes] = vboxmanage_list_blocks(ostypes, "ID") - - # get a list of virtualbox guest vms - vbox[guests] = vboxmanage_list_vms - - # get a list of virtualbox virtual hard disk drives - vbox[hdds] = vboxmanage_list_blocks(hdds, "Location") - - # get a list of virtualbox virtual dvd drives - vbox[dvds] = vboxmanage_list_blocks(dvds, "Location") - - # get a list of virtualbox host dvd drives - vbox[hostdvds] = vboxmanage_list_blocks(hostdvds, "Name") - - # get a list of virtualbox host floppy drives - vbox[hostfloppies] = vboxmanage_list_blocks(hostfloppies, "Name") - - # get a list of virtualbox hostonly network interfaces - vbox[hostonlyifs] = vboxmanage_list_blocks(hostonlyifs, "Name") - - # get a list of virtualbox bridged network interfaces - vbox[bridgedifs] = vboxmanage_list_blocks(bridgedifs, "Name") - - # get a list of virtualbox dhcp servers - vbox[dhcpservers] = vboxmanage_list_blocks(dhcpservers, "NetworkName") - - # get a list of virtualbox nat networks - vbox[natnets] = vboxmanage_list_blocks(natnets, "NetworkName") - end - vbox - rescue Ohai::Exceptions::Exec - logger.trace("Plugin VboxHost: Could not collect data for VirtualBox host. Skipping data") - end -end diff --git a/lib/ohai/plugins/virtualbox.rb b/lib/ohai/plugins/virtualbox.rb index 537d0b12..34743315 100644 --- a/lib/ohai/plugins/virtualbox.rb +++ b/lib/ohai/plugins/virtualbox.rb @@ -1,6 +1,8 @@ # # Author:: Tim Smith <tsmith@chef.io> -# Copyright:: Copyright (c) 2015-2019 Chef Software, Inc. +# Author:: Joshua Colson <joshua.colson@gmail.com> +# Copyright:: 2015-2019 Chef Software, Inc. +# Copyright:: 2019 Joshua Colson # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,8 +22,113 @@ Ohai.plugin(:Virtualbox) do depends "virtualization" provides "virtualbox" + # query virtualbox for each configured vm, as well as + # each guest's individual configuration settings + def vboxmanage_list_vms + vms = Mash.new + so_cmd = "VBoxManage list --sorted vms" + logger.trace(so_cmd) + so = shell_out(so_cmd) + + if so.exitstatus == 0 + # parse the output + so.stdout.lines.each do |line| + case line + when /^"(\S*)" \{(\S*)\}$/ + name = Regexp.last_match(1) + uuid = Regexp.last_match(2) + vms[name] = vboxmanage_vminfo(uuid) + end + end + end + vms + rescue Ohai::Exceptions::Exec + logger.trace("Plugin VboxHost: Could not run 'VBoxManage list --sorted vms'. Skipping data") + end + + # query the vminfo for particular guest instance, normalizing + # the fields so that they're not enclosed in double-quotes (") + def vboxmanage_vminfo(machine_id) + vm = Mash.new + + so_cmd = "VBoxManage showvminfo #{machine_id} --machinereadable" + logger.trace(so_cmd) + so = shell_out(so_cmd) + + if so.exitstatus == 0 + so.stdout.lines.each do |line| + line.chomp! + left, right = line.split("=") + + # remove enclosing quotes, if needed + key = left.delete_prefix('"').delete_suffix('"') + + # skip the name attribute since that is the parent key + next if key == "name" + + vm[key.downcase] = right.delete_prefix('"').delete_suffix('"') + end + end + vm + rescue Ohai::Exceptions::Exec + logger.trace("Plugin VboxHost: Could not run '#{so_cmd}'. Skipping data") + end + + # query virtualbox for a list of #{query_type} items + # these queries return a result set that is delimited by + # multiple successive newlines, with each block containing + # key/value pairs delimited by a colon (:) and column aligned + # + # the keys of each k/v pair are normalized to lowercase + def vboxmanage_list_blocks(query_type, name_key) + # ignore unrecognized query type + supported_queries = %w{ + bridgedifs dhcpservers dvds hdds hostdvds + hostfloppies hostonlyifs natnets + } + return nil unless supported_queries.include? query_type + results = Mash.new + + so_cmd = "VBoxManage list --sorted #{query_type}" + logger.trace(so_cmd) + so = shell_out(so_cmd) + # raise an exception if the command fails + # so.error! + + if so.exitstatus == 0 + # break the result into paragraph blocks, on successive newlines + so.stdout.each_line("") do |blk| + # remove the multiple newlines of each record + blk.chomp!.chomp! + # initialize a blank record hash + record = Mash.new + # parse the record block into key/value pairs + blk.each_line() do |line| + next unless line.include? ":" + # split the line into key/value pair + key, right = line.split(":", 2) + + # strip the leading/trailing whitespace if the value is not nil + value = right.nil? ? "" : right.strip + record[key.downcase] = value + end + + # insert the record into the list of results + if record.key? name_key.downcase + name = record.delete(name_key.downcase) + results[name] = record + end + end + end + results + rescue Ohai::Exceptions::Exec + logger.trace("Plugin VboxHost: Could not run '#{so_cmd}'. Skipping data") + end + collect_data(:default) do - if virtualization.dig("systems", "vbox") == "guest" + case virtualization.dig("systems", "vbox") + when "guest" + logger.trace("Plugin Virtualbox: Node detected as vbox guest. Collecting guest data.") begin so = shell_out("VBoxControl guestproperty enumerate") @@ -45,10 +152,43 @@ Ohai.plugin(:Virtualbox) do end end rescue Ohai::Exceptions::Exec - logger.trace('Plugin Virtualbox: Could not execute "VBoxControl guestproperty enumerate". Skipping plugin.') + logger.trace('Plugin Virtualbox: Could not execute "VBoxControl guestproperty enumerate". Skipping data collection.') + end + when "host" + logger.trace("Plugin Virtualbox: Node detected as vbox host. Collecting host data.") + virtualbox Mash.new + begin + # get a list of virtualbox guest vms + virtualbox["guests"] = vboxmanage_list_vms + + # get a list of virtualbox virtual hard disk drives + virtualbox["hdds"] = vboxmanage_list_blocks("hdds", "Location") + + # get a list of virtualbox virtual dvd drives + virtualbox["dvds"] = vboxmanage_list_blocks("dvds", "Location") + + # get a list of virtualbox host dvd drives + virtualbox["hostdvds"] = vboxmanage_list_blocks("hostdvds", "Name") + + # get a list of virtualbox host floppy drives + virtualbox["hostfloppies"] = vboxmanage_list_blocks("hostfloppies", "Name") + + # get a list of virtualbox hostonly network interfaces + virtualbox["hostonlyifs"] = vboxmanage_list_blocks("hostonlyifs", "Name") + + # get a list of virtualbox bridged network interfaces + virtualbox["bridgedifs"] = vboxmanage_list_blocks("bridgedifs", "Name") + + # get a list of virtualbox dhcp servers + virtualbox["dhcpservers"] = vboxmanage_list_blocks("dhcpservers", "NetworkName") + + # get a list of virtualbox nat networks + virtualbox["natnets"] = vboxmanage_list_blocks("natnets", "NetworkName") + rescue Ohai::Exceptions::Exec + logger.trace("Plugin VboxHost: Could not collect data for VirtualBox host. Skipping data") end else - logger.trace("Plugin Virtualbox: Not on a Virtualbox guest. Skipping plugin.") + logger.trace("Plugin Virtualbox: Not on a Virtualbox host or guest. Skipping plugin.") end end end diff --git a/spec/unit/plugins/vbox_host_spec.rb b/spec/unit/plugins/vbox_host_spec.rb index b030d31a..70c659c6 100644 --- a/spec/unit/plugins/vbox_host_spec.rb +++ b/spec/unit/plugins/vbox_host_spec.rb @@ -12,321 +12,3 @@ # 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" - -vbox_list_ostypes_stdout = <<~EOF - ID: Other - Description: Other/Unknown - Family ID: Other - Family Desc: Other - 64 bit: false - - ID: Other_64 - Description: Other/Unknown (64-bit) - Family ID: Other - Family Desc: Other - 64 bit: true - - ID: Windows31 - Description: Windows 3.1 - Family ID: Windows - Family Desc: Microsoft Windows - 64 bit: false - -EOF - -vbox_list_vms_stdout = <<~EOF - "ubuntu-18.04-amd64_1549746024485_35372" {6294f16b-4f05-4430-afb9-773bdb237aec} -EOF - -vbox_vminfo_stdout = <<~EOF - name="ubuntu-18.04-amd64_1549746024485_35372" - groups="/" - ostype="Ubuntu (64-bit)" - UUID="6294f16b-4f05-4430-afb9-773bdb237aec" - CfgFile="/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/ubuntu-18.04-amd64_1549746024485_35372.vbox" - SnapFldr="/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/Snapshots" - LogFldr="/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/Logs" - hardwareuuid="6294f16b-4f05-4430-afb9-773bdb237aec" - memory=1024 - pagefusion="off" - vram=8 - cpuexecutioncap=100 - hpet="off" - chipset="piix3" - firmware="BIOS" - cpus=1 - pae="on" - longmode="on" - triplefaultreset="off" - apic="on" - x2apic="on" - cpuid-portability-level=0 - bootmenu="messageandmenu" - boot1="disk" - boot2="dvd" - boot3="none" - boot4="none" - acpi="on" - ioapic="on" - biosapic="apic" - biossystemtimeoffset=0 - rtcuseutc="off" - hwvirtex="on" - nestedpaging="on" - largepages="on" - vtxvpid="on" - vtxux="on" - paravirtprovider="default" - effparavirtprovider="kvm" - VMState="poweroff" - VMStateChangeTime="2019-02-09T21:00:33.575000000" - monitorcount=1 - accelerate3d="off" - accelerate2dvideo="off" - teleporterenabled="off" - teleporterport=0 - teleporteraddress="" - teleporterpassword="" - tracing-enabled="off" - tracing-allow-vm-access="off" - tracing-config="" - autostart-enabled="off" - autostart-delay=0 - defaultfrontend="" - storagecontrollername0="IDE Controller" - storagecontrollertype0="PIIX4" - storagecontrollerinstance0="0" - storagecontrollermaxportcount0="2" - storagecontrollerportcount0="2" - storagecontrollerbootable0="on" - storagecontrollername1="SATA Controller" - storagecontrollertype1="IntelAhci" - storagecontrollerinstance1="0" - storagecontrollermaxportcount1="30" - storagecontrollerportcount1="1" - storagecontrollerbootable1="on" - "IDE Controller-0-0"="none" - "IDE Controller-0-1"="none" - "IDE Controller-1-0"="none" - "IDE Controller-1-1"="none" - "SATA Controller-0-0"="/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/Snapshots/{1c182745-4b09-41a1-a147-d3ced46f72f6}.vmdk" - "SATA Controller-ImageUUID-0-0"="1c182745-4b09-41a1-a147-d3ced46f72f6" - natnet1="nat" - macaddress1="080027E5FA8F" - cableconnected1="on" - nic1="nat" - nictype1="82540EM" - nicspeed1="0" - mtu="0" - sockSnd="64" - sockRcv="64" - tcpWndSnd="64" - tcpWndRcv="64" - nic2="none" - nic3="none" - nic4="none" - nic5="none" - nic6="none" - nic7="none" - nic8="none" - hidpointing="ps2mouse" - hidkeyboard="ps2kbd" - uart1="off" - uart2="off" - uart3="off" - uart4="off" - lpt1="off" - lpt2="off" - audio="pulse" - audio_in="false" - audio_out="false" - clipboard="disabled" - draganddrop="disabled" - vrde="on" - vrdeport=-1 - vrdeports="5947" - vrdeaddress="127.0.0.1" - vrdeauthtype="null" - vrdemulticon="off" - vrdereusecon="off" - vrdevideochannel="off" - vrdeproperty[TCP/Ports]="5947" - vrdeproperty[TCP/Address]="127.0.0.1" - usb="off" - ehci="off" - xhci="off" - GuestMemoryBalloon=0 - SnapshotName="base" - SnapshotUUID="085cbbec-70cd-4864-9208-5d938dcabb71" - CurrentSnapshotName="base" - CurrentSnapshotUUID="085cbbec-70cd-4864-9208-5d938dcabb71" - CurrentSnapshotNode="SnapshotName" -EOF - -vbox_list_hdds_stdout = <<~EOF - UUID: ebb6dca0-879f-480b-a50e-9efe330bd021 - Parent UUID: base - State: locked read - Type: normal (base) - Location: /virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/ubuntu-18.04-amd64-disk001.vmdk - Storage format: VMDK - Capacity: 65536 MBytes - Encryption: disabled - - UUID: 1c182745-4b09-41a1-a147-d3ced46f72f6 - Parent UUID: ebb6dca0-879f-480b-a50e-9efe330bd021 - State: created - Type: normal (differencing) - Location: /virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/Snapshots/{1c182745-4b09-41a1-a147-d3ced46f72f6}.vmdk - Storage format: VMDK - Capacity: 65536 MBytes - Encryption: disabled - -EOF - -vbox_list_dvds_stdout = <<~EOF - UUID: 897aa7bc-1ec1-4e13-a16d-101d3716c72d - State: created - Type: normal (base) - Location: /tmp/test.dvd - Storage format: RAW - Capacity: 100 MBytes - Encryption: disabled - -EOF - -vbox_list_hostdvds_stdout = <<~EOF - UUID: 00445644-0000-0000-2f64-65762f737230 - Name: /dev/sr0 - -EOF - -vbox_list_hostfloppies_stdout = <<~EOF - -EOF - -vbox_list_hostonlyifs_stdout = <<~EOF - Name: vboxnet0 - GUID: 786f6276-656e-4074-8000-0a0027000000 - DHCP: Disabled - IPAddress: 192.168.33.1 - NetworkMask: 255.255.255.0 - IPV6Address: - IPV6NetworkMaskPrefixLength: 0 - HardwareAddress: 0a:00:27:00:00:00 - MediumType: Ethernet - Wireless: No - Status: Down - VBoxNetworkName: HostInterfaceNetworking-vboxnet0 - - Name: vboxnet1 - GUID: 786f6276-656e-4174-8000-0a0027000001 - DHCP: Disabled - IPAddress: 192.168.19.1 - NetworkMask: 255.255.255.0 - IPV6Address: fe80::800:27ff:fe00:1 - IPV6NetworkMaskPrefixLength: 64 - HardwareAddress: 0a:00:27:00:00:01 - MediumType: Ethernet - Wireless: No - Status: Up - VBoxNetworkName: HostInterfaceNetworking-vboxnet1 - -EOF - -vbox_list_bridgedifs_stdout = <<~EOF - Name: eno1 - GUID: 316f6e65-0000-4000-8000-309c233b62a9 - DHCP: Disabled - IPAddress: 10.143.72.133 - NetworkMask: 255.255.255.224 - IPV6Address: fe80::9226:82e9:1101:60e6 - IPV6NetworkMaskPrefixLength: 64 - HardwareAddress: 30:9c:23:3b:62:a9 - MediumType: Ethernet - Wireless: No - Status: Up - VBoxNetworkName: HostInterfaceNetworking-eno1 - -EOF - -vbox_list_dhcpservers_stdout = <<~EOF - NetworkName: HostInterfaceNetworking-vboxnet0 - IP: 192.168.56.100 - NetworkMask: 255.255.255.0 - lowerIPAddress: 192.168.56.101 - upperIPAddress: 192.168.56.254 - Enabled: Yes - - NetworkName: HostInterfaceNetworking-vboxnet1 - IP: 192.168.19.2 - NetworkMask: 255.255.255.0 - lowerIPAddress: 192.168.19.3 - upperIPAddress: 192.168.19.254 - Enabled: Yes - -EOF - -# output of: VBoxManage list --sorted natnets -vbox_list_natnets_stdout = <<~EOF - NetworkName: NatNetwork - IP: 10.0.2.1 - Network: 10.0.2.0/24 - IPv6 Enabled: No - IPv6 Prefix: fd17:625c:f037:2::/64 - DHCP Enabled: Yes - Enabled: Yes - loopback mappings (ipv4) - 127.0.0.1=2 - -EOF - -expected_output = { "ostypes" => { "Other" => { "description" => "Other/Unknown", "family id" => "Other", "family desc" => "Other", "64 bit" => "false" }, "Other_64" => { "description" => "Other/Unknown (64-bit)", "family id" => "Other", "family desc" => "Other", "64 bit" => "true" }, "Windows31" => { "description" => "Windows 3.1", "family id" => "Windows", "family desc" => "Microsoft Windows", "64 bit" => "false" } }, "guests" => { "ubuntu-18.04-amd64_1549746024485_35372" => { "groups" => "/", "ostype" => "Ubuntu (64-bit)", "uuid" => "6294f16b-4f05-4430-afb9-773bdb237aec", "cfgfile" => "/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/ubuntu-18.04-amd64_1549746024485_35372.vbox", "snapfldr" => "/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/Snapshots", "logfldr" => "/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/Logs", "hardwareuuid" => "6294f16b-4f05-4430-afb9-773bdb237aec", "memory" => "1024", "pagefusion" => "off", "vram" => "8", "cpuexecutioncap" => "100", "hpet" => "off", "chipset" => "piix3", "firmware" => "BIOS", "cpus" => "1", "pae" => "on", "longmode" => "on", "triplefaultreset" => "off", "apic" => "on", "x2apic" => "on", "cpuid-portability-level" => "0", "bootmenu" => "messageandmenu", "boot1" => "disk", "boot2" => "dvd", "boot3" => "none", "boot4" => "none", "acpi" => "on", "ioapic" => "on", "biosapic" => "apic", "biossystemtimeoffset" => "0", "rtcuseutc" => "off", "hwvirtex" => "on", "nestedpaging" => "on", "largepages" => "on", "vtxvpid" => "on", "vtxux" => "on", "paravirtprovider" => "default", "effparavirtprovider" => "kvm", "vmstate" => "poweroff", "vmstatechangetime" => "2019-02-09T21:00:33.575000000", "monitorcount" => "1", "accelerate3d" => "off", "accelerate2dvideo" => "off", "teleporterenabled" => "off", "teleporterport" => "0", "teleporteraddress" => "", "teleporterpassword" => "", "tracing-enabled" => "off", "tracing-allow-vm-access" => "off", "tracing-config" => "", "autostart-enabled" => "off", "autostart-delay" => "0", "defaultfrontend" => "", "storagecontrollername0" => "IDE Controller", "storagecontrollertype0" => "PIIX4", "storagecontrollerinstance0" => "0", "storagecontrollermaxportcount0" => "2", "storagecontrollerportcount0" => "2", "storagecontrollerbootable0" => "on", "storagecontrollername1" => "SATA Controller", "storagecontrollertype1" => "IntelAhci", "storagecontrollerinstance1" => "0", "storagecontrollermaxportcount1" => "30", "storagecontrollerportcount1" => "1", "storagecontrollerbootable1" => "on", "ide controller-0-0" => "none", "ide controller-0-1" => "none", "ide controller-1-0" => "none", "ide controller-1-1" => "none", "sata controller-0-0" => "/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/Snapshots/{1c182745-4b09-41a1-a147-d3ced46f72f6}.vmdk", "sata controller-imageuuid-0-0" => "1c182745-4b09-41a1-a147-d3ced46f72f6", "natnet1" => "nat", "macaddress1" => "080027E5FA8F", "cableconnected1" => "on", "nic1" => "nat", "nictype1" => "82540EM", "nicspeed1" => "0", "mtu" => "0", "socksnd" => "64", "sockrcv" => "64", "tcpwndsnd" => "64", "tcpwndrcv" => "64", "nic2" => "none", "nic3" => "none", "nic4" => "none", "nic5" => "none", "nic6" => "none", "nic7" => "none", "nic8" => "none", "hidpointing" => "ps2mouse", "hidkeyboard" => "ps2kbd", "uart1" => "off", "uart2" => "off", "uart3" => "off", "uart4" => "off", "lpt1" => "off", "lpt2" => "off", "audio" => "pulse", "audio_in" => "false", "audio_out" => "false", "clipboard" => "disabled", "draganddrop" => "disabled", "vrde" => "on", "vrdeport" => "-1", "vrdeports" => "5947", "vrdeaddress" => "127.0.0.1", "vrdeauthtype" => "null", "vrdemulticon" => "off", "vrdereusecon" => "off", "vrdevideochannel" => "off", "vrdeproperty[tcp/ports]" => "5947", "vrdeproperty[tcp/address]" => "127.0.0.1", "usb" => "off", "ehci" => "off", "xhci" => "off", "guestmemoryballoon" => "0", "snapshotname" => "base", "snapshotuuid" => "085cbbec-70cd-4864-9208-5d938dcabb71", "currentsnapshotname" => "base", "currentsnapshotuuid" => "085cbbec-70cd-4864-9208-5d938dcabb71", "currentsnapshotnode" => "SnapshotName" } }, "hdds" => { "/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/ubuntu-18.04-amd64-disk001.vmdk" => { "uuid" => "ebb6dca0-879f-480b-a50e-9efe330bd021", "parent uuid" => "base", "state" => "locked read", "type" => "normal (base)", "storage format" => "VMDK", "capacity" => "65536 MBytes", "encryption" => "disabled" }, "/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/Snapshots/{1c182745-4b09-41a1-a147-d3ced46f72f6}.vmdk" => { "uuid" => "1c182745-4b09-41a1-a147-d3ced46f72f6", "parent uuid" => "ebb6dca0-879f-480b-a50e-9efe330bd021", "state" => "created", "type" => "normal (differencing)", "storage format" => "VMDK", "capacity" => "65536 MBytes", "encryption" => "disabled" } }, "dvds" => { "/tmp/test.dvd" => { "uuid" => "897aa7bc-1ec1-4e13-a16d-101d3716c72d", "state" => "created", "type" => "normal (base)", "storage format" => "RAW", "capacity" => "100 MBytes", "encryption" => "disabled" } }, "hostdvds" => { "/dev/sr0" => { "uuid" => "00445644-0000-0000-2f64-65762f737230" } }, "hostfloppies" => {}, "hostonlyifs" => { "vboxnet0" => { "guid" => "786f6276-656e-4074-8000-0a0027000000", "dhcp" => "Disabled", "ipaddress" => "192.168.33.1", "networkmask" => "255.255.255.0", "ipv6address" => "", "ipv6networkmaskprefixlength" => "0", "hardwareaddress" => "0a:00:27:00:00:00", "mediumtype" => "Ethernet", "wireless" => "No", "status" => "Down", "vboxnetworkname" => "HostInterfaceNetworking-vboxnet0" }, "vboxnet1" => { "guid" => "786f6276-656e-4174-8000-0a0027000001", "dhcp" => "Disabled", "ipaddress" => "192.168.19.1", "networkmask" => "255.255.255.0", "ipv6address" => "fe80::800:27ff:fe00:1", "ipv6networkmaskprefixlength" => "64", "hardwareaddress" => "0a:00:27:00:00:01", "mediumtype" => "Ethernet", "wireless" => "No", "status" => "Up", "vboxnetworkname" => "HostInterfaceNetworking-vboxnet1" } }, "bridgedifs" => { "eno1" => { "guid" => "316f6e65-0000-4000-8000-309c233b62a9", "dhcp" => "Disabled", "ipaddress" => "10.143.72.133", "networkmask" => "255.255.255.224", "ipv6address" => "fe80::9226:82e9:1101:60e6", "ipv6networkmaskprefixlength" => "64", "hardwareaddress" => "30:9c:23:3b:62:a9", "mediumtype" => "Ethernet", "wireless" => "No", "status" => "Up", "vboxnetworkname" => "HostInterfaceNetworking-eno1" } }, "dhcpservers" => { "HostInterfaceNetworking-vboxnet0" => { "ip" => "192.168.56.100", "networkmask" => "255.255.255.0", "loweripaddress" => "192.168.56.101", "upperipaddress" => "192.168.56.254", "enabled" => "Yes" }, "HostInterfaceNetworking-vboxnet1" => { "ip" => "192.168.19.2", "networkmask" => "255.255.255.0", "loweripaddress" => "192.168.19.3", "upperipaddress" => "192.168.19.254", "enabled" => "Yes" } }, "natnets" => { "NatNetwork" => { "ip" => "10.0.2.1", "network" => "10.0.2.0/24", "ipv6 enabled" => "No", "ipv6 prefix" => "fd17:625c:f037:2::/64", "dhcp enabled" => "Yes", "enabled" => "Yes" } } } - -describe Ohai::System, "plugin vbox_host" do - let(:plugin) { get_plugin("vbox_host") } - - context "if the host does not have virtualbox installed" do - it "should not create a vbox attribute" do - plugin[:virtualization] = Mash.new - plugin[:virtualization][:systems] = Mash.new - plugin.run - expect(plugin).not_to have_key(:vbox) - end - end - - context "if the host has virtualbox installed" do - it "should create a vbox attribute with accurate data" do - plugin[:virtualization] = Mash.new - plugin[:virtualization][:systems] = Mash.new - plugin[:virtualization][:systems][:vbox] = "host" - allow(plugin).to receive(:which).with("VBoxManage").and_return("/usr/bin/VBoxManage") - allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted ostypes").and_return(mock_shell_out(0, vbox_list_ostypes_stdout, "")) - allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted vms").and_return(mock_shell_out(0, vbox_list_vms_stdout, "")) - allow(plugin).to receive(:shell_out).with("VBoxManage showvminfo 6294f16b-4f05-4430-afb9-773bdb237aec --machinereadable").and_return(mock_shell_out(0, vbox_vminfo_stdout, "")) - allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted hdds").and_return(mock_shell_out(0, vbox_list_hdds_stdout, "")) - allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted dvds").and_return(mock_shell_out(0, vbox_list_dvds_stdout, "")) - allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted hostdvds").and_return(mock_shell_out(0, vbox_list_hostdvds_stdout, "")) - allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted hostfloppies").and_return(mock_shell_out(0, vbox_list_hostfloppies_stdout, "")) - allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted hostonlyifs").and_return(mock_shell_out(0, vbox_list_hostonlyifs_stdout, "")) - allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted bridgedifs").and_return(mock_shell_out(0, vbox_list_bridgedifs_stdout, "")) - allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted dhcpservers").and_return(mock_shell_out(0, vbox_list_dhcpservers_stdout, "")) - allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted natnets").and_return(mock_shell_out(0, vbox_list_natnets_stdout, "")) - plugin.run - expect(plugin).to have_key(:vbox) - # expect(plugin[:vbox]['ostypes']).to eq(expected_output['ostypes']) - # expect(plugin[:vbox]['guests']).to eq(expected_output['guests']) - # expect(plugin[:vbox]['hdds']).to eq(expected_output['hdds']) - # expect(plugin[:vbox]['dvds']).to eq(expected_output['dvds']) - # expect(plugin[:vbox]['hostdvds']).to eq(expected_output['hostdvds']) - # expect(plugin[:vbox]['hostfloppies']).to eq(expected_output['hostfloppies']) - # expect(plugin[:vbox]['hostonlyifs']).to eq(expected_output['hostonlyifs']) - # expect(plugin[:vbox]['bridgedifs']).to eq(expected_output['bridgedifs']) - # expect(plugin[:vbox]['dhcpservers']).to eq(expected_output['dhcpservers']) - # expect(plugin[:vbox]['natnets']).to eq(expected_output['natnets']) - expect(plugin[:vbox]).to eq(expected_output) - end - end -end diff --git a/spec/unit/plugins/virtualbox_spec.rb b/spec/unit/plugins/virtualbox_spec.rb index b016ead6..27ad6fd5 100644 --- a/spec/unit/plugins/virtualbox_spec.rb +++ b/spec/unit/plugins/virtualbox_spec.rb @@ -17,6 +17,255 @@ require "spec_helper" +vbox_list_vms_stdout = <<~EOF + "ubuntu-18.04-amd64_1549746024485_35372" {6294f16b-4f05-4430-afb9-773bdb237aec} +EOF + +vbox_vminfo_stdout = <<~EOF + name="ubuntu-18.04-amd64_1549746024485_35372" + groups="/" + ostype="Ubuntu (64-bit)" + UUID="6294f16b-4f05-4430-afb9-773bdb237aec" + CfgFile="/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/ubuntu-18.04-amd64_1549746024485_35372.vbox" + SnapFldr="/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/Snapshots" + LogFldr="/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/Logs" + hardwareuuid="6294f16b-4f05-4430-afb9-773bdb237aec" + memory=1024 + pagefusion="off" + vram=8 + cpuexecutioncap=100 + hpet="off" + chipset="piix3" + firmware="BIOS" + cpus=1 + pae="on" + longmode="on" + triplefaultreset="off" + apic="on" + x2apic="on" + cpuid-portability-level=0 + bootmenu="messageandmenu" + boot1="disk" + boot2="dvd" + boot3="none" + boot4="none" + acpi="on" + ioapic="on" + biosapic="apic" + biossystemtimeoffset=0 + rtcuseutc="off" + hwvirtex="on" + nestedpaging="on" + largepages="on" + vtxvpid="on" + vtxux="on" + paravirtprovider="default" + effparavirtprovider="kvm" + VMState="poweroff" + VMStateChangeTime="2019-02-09T21:00:33.575000000" + monitorcount=1 + accelerate3d="off" + accelerate2dvideo="off" + teleporterenabled="off" + teleporterport=0 + teleporteraddress="" + teleporterpassword="" + tracing-enabled="off" + tracing-allow-vm-access="off" + tracing-config="" + autostart-enabled="off" + autostart-delay=0 + defaultfrontend="" + storagecontrollername0="IDE Controller" + storagecontrollertype0="PIIX4" + storagecontrollerinstance0="0" + storagecontrollermaxportcount0="2" + storagecontrollerportcount0="2" + storagecontrollerbootable0="on" + storagecontrollername1="SATA Controller" + storagecontrollertype1="IntelAhci" + storagecontrollerinstance1="0" + storagecontrollermaxportcount1="30" + storagecontrollerportcount1="1" + storagecontrollerbootable1="on" + "IDE Controller-0-0"="none" + "IDE Controller-0-1"="none" + "IDE Controller-1-0"="none" + "IDE Controller-1-1"="none" + "SATA Controller-0-0"="/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/Snapshots/{1c182745-4b09-41a1-a147-d3ced46f72f6}.vmdk" + "SATA Controller-ImageUUID-0-0"="1c182745-4b09-41a1-a147-d3ced46f72f6" + natnet1="nat" + macaddress1="080027E5FA8F" + cableconnected1="on" + nic1="nat" + nictype1="82540EM" + nicspeed1="0" + mtu="0" + sockSnd="64" + sockRcv="64" + tcpWndSnd="64" + tcpWndRcv="64" + nic2="none" + nic3="none" + nic4="none" + nic5="none" + nic6="none" + nic7="none" + nic8="none" + hidpointing="ps2mouse" + hidkeyboard="ps2kbd" + uart1="off" + uart2="off" + uart3="off" + uart4="off" + lpt1="off" + lpt2="off" + audio="pulse" + audio_in="false" + audio_out="false" + clipboard="disabled" + draganddrop="disabled" + vrde="on" + vrdeport=-1 + vrdeports="5947" + vrdeaddress="127.0.0.1" + vrdeauthtype="null" + vrdemulticon="off" + vrdereusecon="off" + vrdevideochannel="off" + vrdeproperty[TCP/Ports]="5947" + vrdeproperty[TCP/Address]="127.0.0.1" + usb="off" + ehci="off" + xhci="off" + GuestMemoryBalloon=0 + SnapshotName="base" + SnapshotUUID="085cbbec-70cd-4864-9208-5d938dcabb71" + CurrentSnapshotName="base" + CurrentSnapshotUUID="085cbbec-70cd-4864-9208-5d938dcabb71" + CurrentSnapshotNode="SnapshotName" +EOF + +vbox_list_hdds_stdout = <<~EOF + UUID: ebb6dca0-879f-480b-a50e-9efe330bd021 + Parent UUID: base + State: locked read + Type: normal (base) + Location: /virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/ubuntu-18.04-amd64-disk001.vmdk + Storage format: VMDK + Capacity: 65536 MBytes + Encryption: disabled + + UUID: 1c182745-4b09-41a1-a147-d3ced46f72f6 + Parent UUID: ebb6dca0-879f-480b-a50e-9efe330bd021 + State: created + Type: normal (differencing) + Location: /virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/Snapshots/{1c182745-4b09-41a1-a147-d3ced46f72f6}.vmdk + Storage format: VMDK + Capacity: 65536 MBytes + Encryption: disabled + +EOF + +vbox_list_dvds_stdout = <<~EOF + UUID: 897aa7bc-1ec1-4e13-a16d-101d3716c72d + State: created + Type: normal (base) + Location: /tmp/test.dvd + Storage format: RAW + Capacity: 100 MBytes + Encryption: disabled + +EOF + +vbox_list_hostdvds_stdout = <<~EOF + UUID: 00445644-0000-0000-2f64-65762f737230 + Name: /dev/sr0 + +EOF + +vbox_list_hostfloppies_stdout = <<~EOF + +EOF + +vbox_list_hostonlyifs_stdout = <<~EOF + Name: vboxnet0 + GUID: 786f6276-656e-4074-8000-0a0027000000 + DHCP: Disabled + IPAddress: 192.168.33.1 + NetworkMask: 255.255.255.0 + IPV6Address: + IPV6NetworkMaskPrefixLength: 0 + HardwareAddress: 0a:00:27:00:00:00 + MediumType: Ethernet + Wireless: No + Status: Down + VBoxNetworkName: HostInterfaceNetworking-vboxnet0 + + Name: vboxnet1 + GUID: 786f6276-656e-4174-8000-0a0027000001 + DHCP: Disabled + IPAddress: 192.168.19.1 + NetworkMask: 255.255.255.0 + IPV6Address: fe80::800:27ff:fe00:1 + IPV6NetworkMaskPrefixLength: 64 + HardwareAddress: 0a:00:27:00:00:01 + MediumType: Ethernet + Wireless: No + Status: Up + VBoxNetworkName: HostInterfaceNetworking-vboxnet1 + +EOF + +vbox_list_bridgedifs_stdout = <<~EOF + Name: eno1 + GUID: 316f6e65-0000-4000-8000-309c233b62a9 + DHCP: Disabled + IPAddress: 10.143.72.133 + NetworkMask: 255.255.255.224 + IPV6Address: fe80::9226:82e9:1101:60e6 + IPV6NetworkMaskPrefixLength: 64 + HardwareAddress: 30:9c:23:3b:62:a9 + MediumType: Ethernet + Wireless: No + Status: Up + VBoxNetworkName: HostInterfaceNetworking-eno1 + +EOF + +vbox_list_dhcpservers_stdout = <<~EOF + NetworkName: HostInterfaceNetworking-vboxnet0 + IP: 192.168.56.100 + NetworkMask: 255.255.255.0 + lowerIPAddress: 192.168.56.101 + upperIPAddress: 192.168.56.254 + Enabled: Yes + + NetworkName: HostInterfaceNetworking-vboxnet1 + IP: 192.168.19.2 + NetworkMask: 255.255.255.0 + lowerIPAddress: 192.168.19.3 + upperIPAddress: 192.168.19.254 + Enabled: Yes + +EOF + +# output of: VBoxManage list --sorted natnets +vbox_list_natnets_stdout = <<~EOF + NetworkName: NatNetwork + IP: 10.0.2.1 + Network: 10.0.2.0/24 + IPv6 Enabled: No + IPv6 Prefix: fd17:625c:f037:2::/64 + DHCP Enabled: Yes + Enabled: Yes + loopback mappings (ipv4) + 127.0.0.1=2 + +EOF + +expected_output = { "guests" => { "ubuntu-18.04-amd64_1549746024485_35372" => { "groups" => "/", "ostype" => "Ubuntu (64-bit)", "uuid" => "6294f16b-4f05-4430-afb9-773bdb237aec", "cfgfile" => "/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/ubuntu-18.04-amd64_1549746024485_35372.vbox", "snapfldr" => "/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/Snapshots", "logfldr" => "/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/Logs", "hardwareuuid" => "6294f16b-4f05-4430-afb9-773bdb237aec", "memory" => "1024", "pagefusion" => "off", "vram" => "8", "cpuexecutioncap" => "100", "hpet" => "off", "chipset" => "piix3", "firmware" => "BIOS", "cpus" => "1", "pae" => "on", "longmode" => "on", "triplefaultreset" => "off", "apic" => "on", "x2apic" => "on", "cpuid-portability-level" => "0", "bootmenu" => "messageandmenu", "boot1" => "disk", "boot2" => "dvd", "boot3" => "none", "boot4" => "none", "acpi" => "on", "ioapic" => "on", "biosapic" => "apic", "biossystemtimeoffset" => "0", "rtcuseutc" => "off", "hwvirtex" => "on", "nestedpaging" => "on", "largepages" => "on", "vtxvpid" => "on", "vtxux" => "on", "paravirtprovider" => "default", "effparavirtprovider" => "kvm", "vmstate" => "poweroff", "vmstatechangetime" => "2019-02-09T21:00:33.575000000", "monitorcount" => "1", "accelerate3d" => "off", "accelerate2dvideo" => "off", "teleporterenabled" => "off", "teleporterport" => "0", "teleporteraddress" => "", "teleporterpassword" => "", "tracing-enabled" => "off", "tracing-allow-vm-access" => "off", "tracing-config" => "", "autostart-enabled" => "off", "autostart-delay" => "0", "defaultfrontend" => "", "storagecontrollername0" => "IDE Controller", "storagecontrollertype0" => "PIIX4", "storagecontrollerinstance0" => "0", "storagecontrollermaxportcount0" => "2", "storagecontrollerportcount0" => "2", "storagecontrollerbootable0" => "on", "storagecontrollername1" => "SATA Controller", "storagecontrollertype1" => "IntelAhci", "storagecontrollerinstance1" => "0", "storagecontrollermaxportcount1" => "30", "storagecontrollerportcount1" => "1", "storagecontrollerbootable1" => "on", "ide controller-0-0" => "none", "ide controller-0-1" => "none", "ide controller-1-0" => "none", "ide controller-1-1" => "none", "sata controller-0-0" => "/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/Snapshots/{1c182745-4b09-41a1-a147-d3ced46f72f6}.vmdk", "sata controller-imageuuid-0-0" => "1c182745-4b09-41a1-a147-d3ced46f72f6", "natnet1" => "nat", "macaddress1" => "080027E5FA8F", "cableconnected1" => "on", "nic1" => "nat", "nictype1" => "82540EM", "nicspeed1" => "0", "mtu" => "0", "socksnd" => "64", "sockrcv" => "64", "tcpwndsnd" => "64", "tcpwndrcv" => "64", "nic2" => "none", "nic3" => "none", "nic4" => "none", "nic5" => "none", "nic6" => "none", "nic7" => "none", "nic8" => "none", "hidpointing" => "ps2mouse", "hidkeyboard" => "ps2kbd", "uart1" => "off", "uart2" => "off", "uart3" => "off", "uart4" => "off", "lpt1" => "off", "lpt2" => "off", "audio" => "pulse", "audio_in" => "false", "audio_out" => "false", "clipboard" => "disabled", "draganddrop" => "disabled", "vrde" => "on", "vrdeport" => "-1", "vrdeports" => "5947", "vrdeaddress" => "127.0.0.1", "vrdeauthtype" => "null", "vrdemulticon" => "off", "vrdereusecon" => "off", "vrdevideochannel" => "off", "vrdeproperty[tcp/ports]" => "5947", "vrdeproperty[tcp/address]" => "127.0.0.1", "usb" => "off", "ehci" => "off", "xhci" => "off", "guestmemoryballoon" => "0", "snapshotname" => "base", "snapshotuuid" => "085cbbec-70cd-4864-9208-5d938dcabb71", "currentsnapshotname" => "base", "currentsnapshotuuid" => "085cbbec-70cd-4864-9208-5d938dcabb71", "currentsnapshotnode" => "SnapshotName" } }, "hdds" => { "/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/ubuntu-18.04-amd64-disk001.vmdk" => { "uuid" => "ebb6dca0-879f-480b-a50e-9efe330bd021", "parent uuid" => "base", "state" => "locked read", "type" => "normal (base)", "storage format" => "VMDK", "capacity" => "65536 MBytes", "encryption" => "disabled" }, "/virtual/machines/ubuntu-18.04-amd64_1549746024485_35372/Snapshots/{1c182745-4b09-41a1-a147-d3ced46f72f6}.vmdk" => { "uuid" => "1c182745-4b09-41a1-a147-d3ced46f72f6", "parent uuid" => "ebb6dca0-879f-480b-a50e-9efe330bd021", "state" => "created", "type" => "normal (differencing)", "storage format" => "VMDK", "capacity" => "65536 MBytes", "encryption" => "disabled" } }, "dvds" => { "/tmp/test.dvd" => { "uuid" => "897aa7bc-1ec1-4e13-a16d-101d3716c72d", "state" => "created", "type" => "normal (base)", "storage format" => "RAW", "capacity" => "100 MBytes", "encryption" => "disabled" } }, "hostdvds" => { "/dev/sr0" => { "uuid" => "00445644-0000-0000-2f64-65762f737230" } }, "hostfloppies" => {}, "hostonlyifs" => { "vboxnet0" => { "guid" => "786f6276-656e-4074-8000-0a0027000000", "dhcp" => "Disabled", "ipaddress" => "192.168.33.1", "networkmask" => "255.255.255.0", "ipv6address" => "", "ipv6networkmaskprefixlength" => "0", "hardwareaddress" => "0a:00:27:00:00:00", "mediumtype" => "Ethernet", "wireless" => "No", "status" => "Down", "vboxnetworkname" => "HostInterfaceNetworking-vboxnet0" }, "vboxnet1" => { "guid" => "786f6276-656e-4174-8000-0a0027000001", "dhcp" => "Disabled", "ipaddress" => "192.168.19.1", "networkmask" => "255.255.255.0", "ipv6address" => "fe80::800:27ff:fe00:1", "ipv6networkmaskprefixlength" => "64", "hardwareaddress" => "0a:00:27:00:00:01", "mediumtype" => "Ethernet", "wireless" => "No", "status" => "Up", "vboxnetworkname" => "HostInterfaceNetworking-vboxnet1" } }, "bridgedifs" => { "eno1" => { "guid" => "316f6e65-0000-4000-8000-309c233b62a9", "dhcp" => "Disabled", "ipaddress" => "10.143.72.133", "networkmask" => "255.255.255.224", "ipv6address" => "fe80::9226:82e9:1101:60e6", "ipv6networkmaskprefixlength" => "64", "hardwareaddress" => "30:9c:23:3b:62:a9", "mediumtype" => "Ethernet", "wireless" => "No", "status" => "Up", "vboxnetworkname" => "HostInterfaceNetworking-eno1" } }, "dhcpservers" => { "HostInterfaceNetworking-vboxnet0" => { "ip" => "192.168.56.100", "networkmask" => "255.255.255.0", "loweripaddress" => "192.168.56.101", "upperipaddress" => "192.168.56.254", "enabled" => "Yes" }, "HostInterfaceNetworking-vboxnet1" => { "ip" => "192.168.19.2", "networkmask" => "255.255.255.0", "loweripaddress" => "192.168.19.3", "upperipaddress" => "192.168.19.254", "enabled" => "Yes" } }, "natnets" => { "NatNetwork" => { "ip" => "10.0.2.1", "network" => "10.0.2.0/24", "ipv6 enabled" => "No", "ipv6 prefix" => "fd17:625c:f037:2::/64", "dhcp enabled" => "Yes", "enabled" => "Yes" } } } + vbox_output = <<~EOF Oracle VM VirtualBox Guest Additions Command Line Management Interface Version 5.0.2 (C) 2008-2015 Oracle Corporation @@ -53,48 +302,74 @@ describe Ohai::System, "plugin virtualbox" do before(:each) do plugin[:virtualization] = Mash.new plugin[:virtualization][:systems] = Mash.new - plugin[:virtualization][:systems][:vbox] = "guest" end context "when not on a virtualbox guest" do it "should not set the virtualbox attribute" do - plugin[:virtualization][:systems][:vbox] = "host" - plugin.run - expect(plugin).not_to have_key(:virtualbox) - end - end - context "when VBoxControl shellout fails" do - it "should not set the virtualbox attribute" do - allow(plugin).to receive(:shell_out).with("VBoxControl guestproperty enumerate").and_return(mock_shell_out(1, "", "")) + plugin[:virtualization][:systems][:vmware] = "host" plugin.run expect(plugin).not_to have_key(:virtualbox) end end - context "when VBoxControl shellout succeeds" do - before(:each) do - allow(plugin).to receive(:shell_out).with("VBoxControl guestproperty enumerate").and_return(mock_shell_out(0, vbox_output, "")) - plugin.run + context "when on a vbox guest" do + context "when VBoxControl shellout fails" do + it "should not set the virtualbox attribute" do + plugin[:virtualization][:systems][:vbox] = "guest" + allow(plugin).to receive(:shell_out).with("VBoxControl guestproperty enumerate").and_return(mock_shell_out(1, "", "")) + plugin.run + expect(plugin).not_to have_key(:virtualbox) + end end - it "should set the host version" do - expect(plugin[:virtualbox][:host][:version]).to eql("5.0.10") - end + context "when VBoxControl shellout succeeds" do + before(:each) do + plugin[:virtualization][:systems][:vbox] = "guest" + allow(plugin).to receive(:shell_out).with("VBoxControl guestproperty enumerate").and_return(mock_shell_out(0, vbox_output, "")) + plugin.run + end - it "should set the host revision" do - expect(plugin[:virtualbox][:host][:revision]).to eql("104061") - end + it "should set the host version" do + expect(plugin[:virtualbox][:host][:version]).to eql("5.0.10") + end - it "should set the host language" do - expect(plugin[:virtualbox][:host][:language]).to eql("en_US") - end + it "should set the host revision" do + expect(plugin[:virtualbox][:host][:revision]).to eql("104061") + end + + it "should set the host language" do + expect(plugin[:virtualbox][:host][:language]).to eql("en_US") + end - it "should set the guest additions version" do - expect(plugin[:virtualbox][:guest][:guest_additions_version]).to eql("5.0.2") + it "should set the guest additions version" do + expect(plugin[:virtualbox][:guest][:guest_additions_version]).to eql("5.0.2") + end + + it "should set the guest additions revision" do + expect(plugin[:virtualbox][:guest][:guest_additions_revision]).to eql("102096") + end end + end - it "should set the guest additions revision" do - expect(plugin[:virtualbox][:guest][:guest_additions_revision]).to eql("102096") + context "when on a vbox host" do + context "if the host has virtualbox installed" do + it "should create a vbox attribute with accurate data" do + plugin[:virtualization][:systems][:vbox] = "host" + allow(plugin).to receive(:which).with("VBoxManage").and_return("/usr/bin/VBoxManage") + allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted vms").and_return(mock_shell_out(0, vbox_list_vms_stdout, "")) + allow(plugin).to receive(:shell_out).with("VBoxManage showvminfo 6294f16b-4f05-4430-afb9-773bdb237aec --machinereadable").and_return(mock_shell_out(0, vbox_vminfo_stdout, "")) + allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted hdds").and_return(mock_shell_out(0, vbox_list_hdds_stdout, "")) + allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted dvds").and_return(mock_shell_out(0, vbox_list_dvds_stdout, "")) + allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted hostdvds").and_return(mock_shell_out(0, vbox_list_hostdvds_stdout, "")) + allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted hostfloppies").and_return(mock_shell_out(0, vbox_list_hostfloppies_stdout, "")) + allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted hostonlyifs").and_return(mock_shell_out(0, vbox_list_hostonlyifs_stdout, "")) + allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted bridgedifs").and_return(mock_shell_out(0, vbox_list_bridgedifs_stdout, "")) + allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted dhcpservers").and_return(mock_shell_out(0, vbox_list_dhcpservers_stdout, "")) + allow(plugin).to receive(:shell_out).with("VBoxManage list --sorted natnets").and_return(mock_shell_out(0, vbox_list_natnets_stdout, "")) + plugin.run + expect(plugin).to have_key(:virtualbox) + expect(plugin[:virtualbox]).to eq(expected_output) + end end end end |