diff options
author | Tim Smith <tsmith@chef.io> | 2017-09-07 13:14:03 -0700 |
---|---|---|
committer | Tim Smith <tsmith@chef.io> | 2017-09-07 13:19:12 -0700 |
commit | 4960a0764993191dbcc065ae17ffa67635fdf56b (patch) | |
tree | 2e75ebed5fbd3b9e8eb8e9bd2981f877f88bd416 | |
parent | f351de80210b53e042ae96d862f01a1c438eebc4 (diff) | |
download | ohai-4960a0764993191dbcc065ae17ffa67635fdf56b.tar.gz |
Move windows detect to its own method and add specs
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r-- | lib/ohai/plugins/ec2.rb | 30 | ||||
-rw-r--r-- | spec/unit/plugins/ec2_spec.rb | 36 |
2 files changed, 54 insertions, 12 deletions
diff --git a/lib/ohai/plugins/ec2.rb b/lib/ohai/plugins/ec2.rb index c2666488..2dd0fd63 100644 --- a/lib/ohai/plugins/ec2.rb +++ b/lib/ohai/plugins/ec2.rb @@ -33,7 +33,6 @@ Ohai.plugin(:EC2) do include Ohai::Mixin::HttpHelper provides "ec2" - depends "dmi" # look for amazon string in dmi bios data @@ -55,14 +54,7 @@ Ohai.plugin(:EC2) do # this gets us detection of HVM and Paravirt hosts # @return [Boolean] do we have a Xen UUID or not? def has_ec2_xen_uuid? - if RUBY_PLATFORM =~ /mswin|mingw32|windows/ - require "wmi-lite/wmi" - wmi = WmiLite::Wmi.new - if wmi.query("select uuid from Win32_ComputerSystemProduct")[0]["identifyingnumber"] =~ /^ec2/ - Ohai::Log.debug("Plugin EC2: has_ec2_xen_uuid? == true") - return true - end - elsif ::File.exist?("/sys/hypervisor/uuid") + if ::File.exist?("/sys/hypervisor/uuid") if ::File.read("/sys/hypervisor/uuid") =~ /^ec2/ Ohai::Log.debug("Plugin EC2: has_ec2_xen_uuid? == true") return true @@ -72,13 +64,31 @@ Ohai.plugin(:EC2) do false end + # looks at the identifying number WMI value to see if it starts with ec2. + # this is actually the same value we're looking at in has_ec2_xen_uuid? on + # linux hosts + # @return [Boolean] do we have a Xen Identifying Number or not? + def has_ec2_identifying_number? + if RUBY_PLATFORM =~ /mswin|mingw32|windows/ + # require "wmi-lite/wmi" + wmi = WmiLite::Wmi.new + if wmi.first_of("Win32_ComputerSystemProduct")["identifyingnumber"] =~ /^ec2/ + Ohai::Log.debug("Plugin EC2: has_ec2_identifying_number? == true") + return true + end + else + Ohai::Log.debug("Plugin EC2: has_ec2_identifying_number? == false") + false + end + end + # a single check that combines all the various detection methods for EC2 # @return [Boolean] Does the system appear to be on EC2 def looks_like_ec2? return true if hint?("ec2") # Even if it looks like EC2 try to connect first - if has_ec2_xen_uuid? || has_ec2_dmi? + if has_ec2_xen_uuid? || has_ec2_dmi? || has_ec2_identifying_number? return true if can_socket_connect?(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80) end end diff --git a/spec/unit/plugins/ec2_spec.rb b/spec/unit/plugins/ec2_spec.rb index 39ad41a7..25c3eba7 100644 --- a/spec/unit/plugins/ec2_spec.rb +++ b/spec/unit/plugins/ec2_spec.rb @@ -342,7 +342,7 @@ describe Ohai::System, "plugin ec2" do end end - describe "with EC2 Xen UUID on Linux" do + describe "with EC2 Xen UUID" do it_behaves_like "ec2" before(:each) do @@ -351,7 +351,7 @@ describe Ohai::System, "plugin ec2" do end end - describe "with non-EC2 Xen UUID on Linux" do + describe "with non-EC2 Xen UUID" do it_behaves_like "!ec2" before(:each) do @@ -360,6 +360,38 @@ describe Ohai::System, "plugin ec2" do end end + describe "with EC2 Identifying Number", :windows_only do + it_behaves_like "ec2" + + before do + allow_any_instance_of(WmiLite::Wmi).to receive(:first_of).and_return( + { "caption" => "Computer System Product", + "description" => "Computer System Product", + "identifyingnumber" => "ec2a355a-91cd-5fe8-bbfc-cc891d0bf9d6", + "name" => "HVM domU", + "skunumber" => nil, + "uuid" => "5A352AEC-CD91-E85F-BBFC-CC891D0BF9D6", + "vendor" => "Xen", + "version" => "4.2.amazon" }) + end + end + + describe "without EC2 Identifying Number", :windows_only do + it_behaves_like "!ec2" + + before do + allow_any_instance_of(WmiLite::Wmi).to receive(:first_of).and_return( + { "caption" => "Computer System Product", + "description" => "Computer System Product", + "identifyingnumber" => "1234", + "name" => "HVM domU", + "skunumber" => nil, + "uuid" => "5A352AEC-CD91-E85F-BBFC-CC891D0BF9D6", + "vendor" => "Xen", + "version" => "1.2.3" }) + end + end + describe "with ec2 hint file" do it_behaves_like "ec2" |