diff options
author | Tim Smith <tsmith@chef.io> | 2019-06-13 11:13:51 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-13 11:13:51 -0700 |
commit | f159ce44f25b2012bb1ab63c26285f6c89bec169 (patch) | |
tree | 9197365aaff809286b6f8721bd8cf674b2e0971e | |
parent | f01921fd12746b9edaf14b77ddea7d91a90ba494 (diff) | |
parent | b881d30096dc8bd81687b49136b3afe06767a66f (diff) | |
download | ohai-f159ce44f25b2012bb1ab63c26285f6c89bec169.tar.gz |
Merge pull request #1373 from chef/simpler_vbox
Use virtualization attributes to run or skip the virtualbox plugin
-rw-r--r-- | lib/ohai/plugins/vbox_host.rb | 6 | ||||
-rw-r--r-- | lib/ohai/plugins/virtualbox.rb | 53 | ||||
-rw-r--r-- | spec/unit/plugins/virtualbox_spec.rb | 20 |
3 files changed, 46 insertions, 33 deletions
diff --git a/lib/ohai/plugins/vbox_host.rb b/lib/ohai/plugins/vbox_host.rb index b78047ee..f1657dd2 100644 --- a/lib/ohai/plugins/vbox_host.rb +++ b/lib/ohai/plugins/vbox_host.rb @@ -21,11 +21,7 @@ Ohai.plugin(:VboxHost) do # determine if this host is configured with virtualbox or not # the determination is ultimately controlled by the "virtualization" plugin def vbox_host? - host = false - if !virtualization.nil? && (virtualization["system"] == "vbox" || virtualization["systems"]["vbox"] == "host") - host = true if which("VBoxManage") - end - host + virtualization.dig("systems", "vbox") == "host" end # query virtualbox for each configured vm, as well as diff --git a/lib/ohai/plugins/virtualbox.rb b/lib/ohai/plugins/virtualbox.rb index 80e73c86..537d0b12 100644 --- a/lib/ohai/plugins/virtualbox.rb +++ b/lib/ohai/plugins/virtualbox.rb @@ -1,6 +1,6 @@ # -# Author:: "Tim Smith" <tsmith@chef.io> -# Copyright:: Copyright (c) 2015-2016 Chef Software, Inc. +# Author:: Tim Smith <tsmith@chef.io> +# Copyright:: Copyright (c) 2015-2019 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,33 +17,38 @@ # Ohai.plugin(:Virtualbox) do + depends "virtualization" provides "virtualbox" - collect_data do + collect_data(:default) do + if virtualization.dig("systems", "vbox") == "guest" + begin + so = shell_out("VBoxControl guestproperty enumerate") - so = shell_out("VBoxControl guestproperty enumerate") - - if so.exitstatus == 0 - virtualbox Mash.new - virtualbox[:host] = Mash.new - virtualbox[:guest] = Mash.new - so.stdout.lines.each do |line| - case line - when /LanguageID, value: (\S*),/ - virtualbox[:host][:language] = Regexp.last_match(1) - when /VBoxVer, value: (\S*),/ - virtualbox[:host][:version] = Regexp.last_match(1) - when /VBoxRev, value: (\S*),/ - virtualbox[:host][:revision] = Regexp.last_match(1) - when /GuestAdd\/VersionExt, value: (\S*),/ - virtualbox[:guest][:guest_additions_version] = Regexp.last_match(1) - when /GuestAdd\/Revision, value: (\S*),/ - virtualbox[:guest][:guest_additions_revision] = Regexp.last_match(1) + if so.exitstatus == 0 + virtualbox Mash.new + virtualbox[:host] = Mash.new + virtualbox[:guest] = Mash.new + so.stdout.lines.each do |line| + case line + when /LanguageID, value: (\S*),/ + virtualbox[:host][:language] = Regexp.last_match(1) + when /VBoxVer, value: (\S*),/ + virtualbox[:host][:version] = Regexp.last_match(1) + when /VBoxRev, value: (\S*),/ + virtualbox[:host][:revision] = Regexp.last_match(1) + when /GuestAdd\/VersionExt, value: (\S*),/ + virtualbox[:guest][:guest_additions_version] = Regexp.last_match(1) + when /GuestAdd\/Revision, value: (\S*),/ + virtualbox[:guest][:guest_additions_revision] = Regexp.last_match(1) + end + end end + rescue Ohai::Exceptions::Exec + logger.trace('Plugin Virtualbox: Could not execute "VBoxControl guestproperty enumerate". Skipping plugin.') end + else + logger.trace("Plugin Virtualbox: Not on a Virtualbox guest. Skipping plugin.") end - rescue Ohai::Exceptions::Exec - logger.trace('Plugin Virtualbox: Could not execute "VBoxControl guestproperty enumerate". Skipping data') - end end diff --git a/spec/unit/plugins/virtualbox_spec.rb b/spec/unit/plugins/virtualbox_spec.rb index a746167e..b016ead6 100644 --- a/spec/unit/plugins/virtualbox_spec.rb +++ b/spec/unit/plugins/virtualbox_spec.rb @@ -1,5 +1,5 @@ # Author:: Tim Smith (<tsmith@chef.io>) -# Copyright:: Copyright (c) 2015-2016 Chef Software, Inc. +# Copyright:: Copyright (c) 2015-2019 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -48,9 +48,23 @@ vbox_output = <<~EOF EOF describe Ohai::System, "plugin virtualbox" do + let(:plugin) { get_plugin("virtualbox") } + + 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 - plugin = get_plugin("virtualbox") 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) @@ -58,8 +72,6 @@ describe Ohai::System, "plugin virtualbox" do end context "when VBoxControl shellout succeeds" do - let(:plugin) { get_plugin("virtualbox") } - before(:each) do allow(plugin).to receive(:shell_out).with("VBoxControl guestproperty enumerate").and_return(mock_shell_out(0, vbox_output, "")) plugin.run |