Skip to content

Commit

Permalink
Add tests for the UUID EC2 detection method
Browse files Browse the repository at this point in the history
  • Loading branch information
tas50 committed Apr 7, 2016
1 parent 5d40c3c commit 572dc05
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 43 deletions.
4 changes: 2 additions & 2 deletions lib/ohai/plugins/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def has_ec2_dmi?
# 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
Expand Down
105 changes: 64 additions & 41 deletions spec/unit/plugins/ec2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,29 @@
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)
Expand Down Expand Up @@ -68,12 +73,12 @@
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
Expand All @@ -86,17 +91,17 @@
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"))
Expand All @@ -119,10 +124,10 @@
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
Expand All @@ -136,7 +141,7 @@
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"))
Expand All @@ -153,11 +158,11 @@
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

Expand All @@ -170,7 +175,7 @@
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"))
Expand All @@ -187,14 +192,14 @@
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"))
Expand Down Expand Up @@ -226,12 +231,12 @@
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"))
Expand All @@ -245,27 +250,45 @@
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

describe "with ec2 dmi data" 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

describe "with amazon kernel data" 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

Expand Down Expand Up @@ -300,7 +323,7 @@
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

Expand Down

0 comments on commit 572dc05

Please sign in to comment.