diff options
author | Tim Smith <tsmith84@gmail.com> | 2016-04-07 10:02:21 -0700 |
---|---|---|
committer | Tim Smith <tsmith84@gmail.com> | 2016-04-07 10:02:21 -0700 |
commit | 572dc0577984d40538910659eecc42592f831752 (patch) | |
tree | 696b8354c5b21d84eae0213ae2669052e3b8b55e | |
parent | 5d40c3cd9bc3b2beec7ed73a2b65e54992e08f30 (diff) | |
download | ohai-572dc0577984d40538910659eecc42592f831752.tar.gz |
Add tests for the UUID EC2 detection method
-rw-r--r-- | lib/ohai/plugins/ec2.rb | 4 | ||||
-rw-r--r-- | spec/unit/plugins/ec2_spec.rb | 105 |
2 files changed, 66 insertions, 43 deletions
diff --git a/lib/ohai/plugins/ec2.rb b/lib/ohai/plugins/ec2.rb index ae217015..6f643188 100644 --- a/lib/ohai/plugins/ec2.rb +++ b/lib/ohai/plugins/ec2.rb @@ -53,8 +53,8 @@ Ohai.plugin(:EC2) do # looks for a xen UUID that starts with ec2 # this gets us detection of Linux HVM and Paravirt hosts def has_ec2_xen_uuid? - if ::File.exist?('/sys/hypervisor/uuid') - if ::File.read('/sys/hypervisor/uuid') =~ /^ec2/ + if ::File.exist?("/sys/hypervisor/uuid") + if ::File.read("/sys/hypervisor/uuid") =~ /^ec2/ Ohai::Log.debug("ec2 plugin: has_ec2_xen_uuid? == true") return true end diff --git a/spec/unit/plugins/ec2_spec.rb b/spec/unit/plugins/ec2_spec.rb index ef67bfba..1818d325 100644 --- a/spec/unit/plugins/ec2_spec.rb +++ b/spec/unit/plugins/ec2_spec.rb @@ -22,24 +22,29 @@ require "open-uri" require "base64" describe Ohai::System, "plugin ec2" do + before(:each) do - @plugin = get_plugin("ec2") allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/ec2.json").and_return(false) allow(File).to receive(:exist?).with('C:\chef\ohai\hints/ec2.json').and_return(false) + allow(File).to receive(:exist?).with("/sys/hypervisor/uuid").and_return(false) end shared_examples_for "!ec2" do - it "should NOT attempt to fetch the ec2 metadata or set ec2 attribute" do - expect(@plugin).not_to receive(:http_client) - expect(@plugin[:ec2]).to be_nil - @plugin.run + let(:plugin) { get_plugin("ec2") } + + it "DOESN'T attempt to fetch the ec2 metadata or set ec2 attribute" do + expect(plugin).not_to receive(:http_client) + expect(plugin[:ec2]).to be_nil + plugin.run end end shared_examples_for "ec2" do + let(:plugin) { get_plugin("ec2") } + before(:each) do @http_client = double("Net::HTTP client") - allow(@plugin).to receive(:http_client).and_return(@http_client) + allow(plugin).to receive(:http_client).and_return(@http_client) allow(IO).to receive(:select).and_return([[], [1], []]) t = double("connection") allow(t).to receive(:connect_nonblock).and_raise(Errno::EINPROGRESS) @@ -68,12 +73,12 @@ describe Ohai::System, "plugin ec2" do with("/2012-01-12/user-data/"). and_return(double("Net::HTTP Response", :body => "By the pricking of my thumb...", :code => "200")) - @plugin.run + plugin.run - expect(@plugin[:ec2]).not_to be_nil - expect(@plugin[:ec2]["instance_type"]).to eq("c1.medium") - expect(@plugin[:ec2]["ami_id"]).to eq("ami-5d2dc934") - expect(@plugin[:ec2]["security_groups"]).to eql %w{group1 group2} + expect(plugin[:ec2]).not_to be_nil + expect(plugin[:ec2]["instance_type"]).to eq("c1.medium") + expect(plugin[:ec2]["ami_id"]).to eq("ami-5d2dc934") + expect(plugin[:ec2]["security_groups"]).to eql %w{group1 group2} end it "fetches binary userdata opaquely" do @@ -86,17 +91,17 @@ describe Ohai::System, "plugin ec2" do with("/2012-01-12/user-data/"). and_return(double("Net::HTTP Response", :body => "^_<8B>^H^H<C7>U^@^Csomething^@KT<C8><C9>,)<C9>IU(I-.I<CB><CC>I<E5>^B^@^Qz<BF><B0>^R^@^@^@", :code => "200")) - @plugin.run + plugin.run - expect(@plugin[:ec2]).not_to be_nil - expect(@plugin[:ec2]["instance_type"]).to eq("c1.medium") - expect(@plugin[:ec2]["ami_id"]).to eq("ami-5d2dc934") - expect(@plugin[:ec2]["security_groups"]).to eql %w{group1 group2} - expect(@plugin[:ec2]["userdata"]).to eq(Base64.decode64("Xl88OEI+XkheSDxDNz5VXkBeQ3NvbWV0aGluZ15AS1Q8Qzg+PEM5PiwpPEM5\nPklVKEktLkk8Q0I+PENDPkk8RTU+XkJeQF5RejxCRj48QjA+XlJeQF5AXkA=")) + expect(plugin[:ec2]).not_to be_nil + expect(plugin[:ec2]["instance_type"]).to eq("c1.medium") + expect(plugin[:ec2]["ami_id"]).to eq("ami-5d2dc934") + expect(plugin[:ec2]["security_groups"]).to eql %w{group1 group2} + expect(plugin[:ec2]["userdata"]).to eq(Base64.decode64("Xl88OEI+XkheSDxDNz5VXkBeQ3NvbWV0aGluZ15AS1Q8Qzg+PEM5PiwpPEM5\nPklVKEktLkk8Q0I+PENDPkk8RTU+XkJeQF5RejxCRj48QjA+XlJeQF5AXkA=")) end end - it "should parse ec2 network/ directory as a multi-level hash" do + it "parses ec2 network/ directory as a multi-level hash" do expect(@http_client).to receive(:get). with("/2012-01-12/meta-data/"). and_return(double("Net::HTTP Response", :body => "network/", :code => "200")) @@ -119,10 +124,10 @@ describe Ohai::System, "plugin ec2" do with("/2012-01-12/user-data/"). and_return(double("Net::HTTP Response", :body => "By the pricking of my thumb...", :code => "200")) - @plugin.run + plugin.run - expect(@plugin[:ec2]).not_to be_nil - expect(@plugin[:ec2]["network_interfaces_macs"]["12:34:56:78:9a:bc"]["public_hostname"]).to eql("server17.opscode.com") + expect(plugin[:ec2]).not_to be_nil + expect(plugin[:ec2]["network_interfaces_macs"]["12:34:56:78:9a:bc"]["public_hostname"]).to eql("server17.opscode.com") end # context with common metadata paths context "with ec2_iam hint file" do @@ -136,7 +141,7 @@ describe Ohai::System, "plugin ec2" do end end - it "should parse ec2 iam/ directory and collect iam/security-credentials/" do + it "parses ec2 iam/ directory and collect iam/security-credentials/" do expect(@http_client).to receive(:get). with("/2012-01-12/meta-data/"). and_return(double("Net::HTTP Response", :body => "iam/", :code => "200")) @@ -153,11 +158,11 @@ describe Ohai::System, "plugin ec2" do with("/2012-01-12/user-data/"). and_return(double("Net::HTTP Response", :body => "By the pricking of my thumb...", :code => "200")) - @plugin.run + plugin.run - expect(@plugin[:ec2]).not_to be_nil - expect(@plugin[:ec2]["iam"]["security-credentials"]["MyRole"]["Code"]).to eql "Success" - expect(@plugin[:ec2]["iam"]["security-credentials"]["MyRole"]["Token"]).to eql "12345678" + expect(plugin[:ec2]).not_to be_nil + expect(plugin[:ec2]["iam"]["security-credentials"]["MyRole"]["Code"]).to eql "Success" + expect(plugin[:ec2]["iam"]["security-credentials"]["MyRole"]["Token"]).to eql "12345678" end end @@ -170,7 +175,7 @@ describe Ohai::System, "plugin ec2" do end end - it "should parse ec2 iam/ directory and NOT collect iam/security-credentials/" do + it "parses ec2 iam/ directory and NOT collect iam/security-credentials/" do expect(@http_client).to receive(:get). with("/2012-01-12/meta-data/"). and_return(double("Net::HTTP Response", :body => "iam/", :code => "200")) @@ -187,14 +192,14 @@ describe Ohai::System, "plugin ec2" do with("/2012-01-12/user-data/"). and_return(double("Net::HTTP Response", :body => "By the pricking of my thumb...", :code => "200")) - @plugin.run + plugin.run - expect(@plugin[:ec2]).not_to be_nil - expect(@plugin[:ec2]["iam"]).to be_nil + expect(plugin[:ec2]).not_to be_nil + expect(plugin[:ec2]["iam"]).to be_nil end end - it "should ignore \"./\" and \"../\" on ec2 metadata paths to avoid infinity loops" do + it "ignores \"./\" and \"../\" on ec2 metadata paths to avoid infinity loops" do expect(@http_client).to receive(:get). with("/2012-01-12/meta-data/"). and_return(double("Net::HTTP Response", :body => ".\n./\n..\n../\npath1/.\npath2/./\npath3/..\npath4/../", :code => "200")) @@ -226,12 +231,12 @@ describe Ohai::System, "plugin ec2" do with("/2012-01-12/user-data/"). and_return(double("Net::HTTP Response", :body => "By the pricking of my thumb...", :code => "200")) - @plugin.run + plugin.run - expect(@plugin[:ec2]).not_to be_nil + expect(plugin[:ec2]).not_to be_nil end - it "should complete the run despite unavailable metadata" do + it "completes the run despite unavailable metadata" do expect(@http_client).to receive(:get). with("/2012-01-12/meta-data/"). and_return(double("Net::HTTP Response", :body => "metrics/", :code => "200")) @@ -245,11 +250,11 @@ describe Ohai::System, "plugin ec2" do with("/2012-01-12/user-data/"). and_return(double("Net::HTTP Response", :body => "By the pricking of my thumb...", :code => "200")) - @plugin.run + plugin.run - expect(@plugin[:ec2]).not_to be_nil - expect(@plugin[:ec2]["metrics"]).to be_nil - expect(@plugin[:ec2]["metrics_vhostmd"]).to be_nil + expect(plugin[:ec2]).not_to be_nil + expect(plugin[:ec2]["metrics"]).to be_nil + expect(plugin[:ec2]["metrics_vhostmd"]).to be_nil end end # shared examples for ec2 @@ -257,7 +262,7 @@ describe Ohai::System, "plugin ec2" do it_should_behave_like "ec2" before(:each) do - @plugin[:dmi] = { :bios => { :all_records => [ { :Version => "4.2.amazon" } ] } } + plugin[:dmi] = { :bios => { :all_records => [ { :Version => "4.2.amazon" } ] } } end end @@ -265,7 +270,25 @@ describe Ohai::System, "plugin ec2" do it_should_behave_like "ec2" before(:each) do - @plugin[:kernel] = { :os_info => { :organization => "Amazon.com" } } + plugin[:kernel] = { :os_info => { :organization => "Amazon.com" } } + end + end + + describe "with EC2 Xen UUID" do + it_should_behave_like "ec2" + + before(:each) do + allow(File).to receive(:exist?).with("/sys/hypervisor/uuid").and_return(true) + allow(File).to receive(:read).with("/sys/hypervisor/uuid").and_return("ec2a0561-e4d6-8e15-d9c8-2e0e03adcde8") + end + end + + describe "with non-EC2 Xen UUID" do + it_should_behave_like "!ec2" + + before(:each) do + allow(File).to receive(:exist?).with("/sys/hypervisor/uuid").and_return(true) + allow(File).to receive(:read).with("/sys/hypervisor/uuid").and_return("123a0561-e4d6-8e15-d9c8-2e0e03adcde8") end end @@ -300,7 +323,7 @@ describe Ohai::System, "plugin ec2" do before(:each) do allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/ec2.json").and_return(false) allow(File).to receive(:exist?).with('C:\chef\ohai\hints/ec2.json').and_return(false) - @plugin[:dmi] = nil + plugin[:dmi] = nil end end |