diff options
author | wilkosz <joshua@wilkosz.com.au> | 2020-04-27 14:56:20 +1000 |
---|---|---|
committer | wilkosz <joshua@wilkosz.com.au> | 2020-04-27 14:56:20 +1000 |
commit | 857390570e72c718040b4799f48dc6b98b7aa52c (patch) | |
tree | ec08cf4c626d6459c0eac241a659f24128f3f7c1 | |
parent | 6a5ccfe36bde5c42019e66644fcf5d632c29534f (diff) | |
download | ohai-857390570e72c718040b4799f48dc6b98b7aa52c.tar.gz |
unit test plugins ec2
-rw-r--r-- | spec/unit/plugins/ec2_spec.rb | 134 |
1 files changed, 68 insertions, 66 deletions
diff --git a/spec/unit/plugins/ec2_spec.rb b/spec/unit/plugins/ec2_spec.rb index 823d5ed3..bcd3e6c8 100644 --- a/spec/unit/plugins/ec2_spec.rb +++ b/spec/unit/plugins/ec2_spec.rb @@ -48,9 +48,12 @@ describe Ohai::System, "plugin ec2" do t = double("connection") allow(t).to receive(:connect_nonblock).and_raise(Errno::EINPROGRESS) allow(Socket).to receive(:new).and_return(t) + @token = "xxxxxxxxxxxxxxxxxxxxxxxx==" + @get_token_header = {:'X-aws-ec2-metadata-token' => @token} + allow(@http_client).to receive(:put) { double("Net::HTTP::PUT Response", body: @token, code: "200") } expect(@http_client).to receive(:get) - .with("/") - .and_return(double("Net::HTTP Response", body: "2012-01-12", code: "200")) + .with("/", @get_token_header) + .and_return(double("Net::HTTP::GET Response", body: "2012-01-12", code: "200")) end context "with common metadata paths" do @@ -65,15 +68,15 @@ describe Ohai::System, "plugin ec2" do it "recursively fetches all the ec2 metadata" do paths.each do |name, body| expect(@http_client).to receive(:get) - .with("/2012-01-12/#{name}") - .and_return(double("Net::HTTP Response", body: body, code: "200")) + .with("/2012-01-12/#{name}", @get_token_header) + .and_return(double("Net::HTTP::GET Response", body: body, code: "200")) end expect(@http_client).to receive(:get) - .with("/2012-01-12/user-data/") - .and_return(double("Net::HTTP Response", body: "By the pricking of my thumb...", code: "200")) + .with("/2012-01-12/user-data/", @get_token_header) + .and_return(double("Net::HTTP::GET Response", body: "By the pricking of my thumb...", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/dynamic/instance-identity/document/") - .and_return(double("Net::HTTP Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) + .with("/2012-01-12/dynamic/instance-identity/document/", @get_token_header) + .and_return(double("Net::HTTP::GET Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) plugin.run @@ -86,14 +89,14 @@ describe Ohai::System, "plugin ec2" do it "fetches binary userdata opaquely" do paths.each do |name, body| expect(@http_client).to receive(:get) - .with("/2012-01-12/#{name}") + .with("/2012-01-12/#{name}", @get_token_header) .and_return(double("Net::HTTP Response", body: body, code: "200")) end expect(@http_client).to receive(:get) - .with("/2012-01-12/user-data/") + .with("/2012-01-12/user-data/", @get_token_header) .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")) expect(@http_client).to receive(:get) - .with("/2012-01-12/dynamic/instance-identity/document/") + .with("/2012-01-12/dynamic/instance-identity/document/", @get_token_header) .and_return(double("Net::HTTP Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) plugin.run @@ -108,14 +111,14 @@ describe Ohai::System, "plugin ec2" do it "fetches AWS account id" do paths.each do |name, body| expect(@http_client).to receive(:get) - .with("/2012-01-12/#{name}") + .with("/2012-01-12/#{name}", @get_token_header) .and_return(double("Net::HTTP Response", body: body, code: "200")) end expect(@http_client).to receive(:get) - .with("/2012-01-12/user-data/") + .with("/2012-01-12/user-data/", @get_token_header) .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")) expect(@http_client).to receive(:get) - .with("/2012-01-12/dynamic/instance-identity/document/") + .with("/2012-01-12/dynamic/instance-identity/document/", @get_token_header) .and_return(double("Net::HTTP Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) plugin.run @@ -130,14 +133,14 @@ describe Ohai::System, "plugin ec2" do it "fetches AWS region" do paths.each do |name, body| expect(@http_client).to receive(:get) - .with("/2012-01-12/#{name}") + .with("/2012-01-12/#{name}", @get_token_header) .and_return(double("Net::HTTP Response", body: body, code: "200")) end expect(@http_client).to receive(:get) - .with("/2012-01-12/user-data/") + .with("/2012-01-12/user-data/", @get_token_header) .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")) expect(@http_client).to receive(:get) - .with("/2012-01-12/dynamic/instance-identity/document/") + .with("/2012-01-12/dynamic/instance-identity/document/", @get_token_header) .and_return(double("Net::HTTP Response", body: "{\"region\":\"us-east-1\"}", code: "200")) plugin.run @@ -152,14 +155,14 @@ describe Ohai::System, "plugin ec2" do it "fetches AWS availability zone" do paths.each do |name, body| expect(@http_client).to receive(:get) - .with("/2012-01-12/#{name}") + .with("/2012-01-12/#{name}", @get_token_header) .and_return(double("Net::HTTP Response", body: body, code: "200")) end expect(@http_client).to receive(:get) - .with("/2012-01-12/user-data/") + .with("/2012-01-12/user-data/", @get_token_header) .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")) expect(@http_client).to receive(:get) - .with("/2012-01-12/dynamic/instance-identity/document/") + .with("/2012-01-12/dynamic/instance-identity/document/", @get_token_header) .and_return(double("Net::HTTP Response", body: "{\"availabilityZone\":\"us-east-1d\"}", code: "200")) plugin.run @@ -174,29 +177,29 @@ describe Ohai::System, "plugin ec2" 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")) + .with("/2012-01-12/meta-data/", @get_token_header) + .and_return(double("Net::HTTP::GET Response", body: "network/", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/network/") - .and_return(double("Net::HTTP Response", body: "interfaces/", code: "200")) + .with("/2012-01-12/meta-data/network/", @get_token_header) + .and_return(double("Net::HTTP::GET Response", body: "interfaces/", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/network/interfaces/") - .and_return(double("Net::HTTP Response", body: "macs/", code: "200")) + .with("/2012-01-12/meta-data/network/interfaces/", @get_token_header) + .and_return(double("Net::HTTP::GET Response", body: "macs/", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/network/interfaces/macs/") - .and_return(double("Net::HTTP Response", body: "12:34:56:78:9a:bc/", code: "200")) + .with("/2012-01-12/meta-data/network/interfaces/macs/", @get_token_header) + .and_return(double("Net::HTTP::GET Response", body: "12:34:56:78:9a:bc/", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/network/interfaces/macs/12:34:56:78:9a:bc/") - .and_return(double("Net::HTTP Response", body: "public_hostname", code: "200")) + .with("/2012-01-12/meta-data/network/interfaces/macs/12:34:56:78:9a:bc/", @get_token_header) + .and_return(double("Net::HTTP::GET Response", body: "public_hostname", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/network/interfaces/macs/12:34:56:78:9a:bc/public_hostname") - .and_return(double("Net::HTTP Response", body: "server17.opscode.com", code: "200")) + .with("/2012-01-12/meta-data/network/interfaces/macs/12:34:56:78:9a:bc/public_hostname", @get_token_header) + .and_return(double("Net::HTTP::GET Response", body: "server17.opscode.com", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/user-data/") - .and_return(double("Net::HTTP Response", body: "By the pricking of my thumb...", code: "200")) + .with("/2012-01-12/user-data/", @get_token_header) + .and_return(double("Net::HTTP::GET Response", body: "By the pricking of my thumb...", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/dynamic/instance-identity/document/") - .and_return(double("Net::HTTP Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) + .with("/2012-01-12/dynamic/instance-identity/document/", @get_token_header) + .and_return(double("Net::HTTP::GET Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) plugin.run @@ -211,22 +214,22 @@ describe Ohai::System, "plugin ec2" do it "parses ec2 iam/ directory and collect iam/security-credentials/" do expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/") + .with("/2012-01-12/meta-data/", @get_token_header) .and_return(double("Net::HTTP Response", body: "iam/", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/iam/") + .with("/2012-01-12/meta-data/iam/", @get_token_header) .and_return(double("Net::HTTP Response", body: "security-credentials/", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/iam/security-credentials/") + .with("/2012-01-12/meta-data/iam/security-credentials/", @get_token_header) .and_return(double("Net::HTTP Response", body: "MyRole", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/iam/security-credentials/MyRole") + .with("/2012-01-12/meta-data/iam/security-credentials/MyRole", @get_token_header) .and_return(double("Net::HTTP Response", body: "{\n \"Code\" : \"Success\",\n \"LastUpdated\" : \"2012-08-22T07:47:22Z\",\n \"Type\" : \"AWS-HMAC\",\n \"AccessKeyId\" : \"AAAAAAAA\",\n \"SecretAccessKey\" : \"SSSSSSSS\",\n \"Token\" : \"12345678\",\n \"Expiration\" : \"2012-08-22T11:25:52Z\"\n}", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/user-data/") + .with("/2012-01-12/user-data/", @get_token_header) .and_return(double("Net::HTTP Response", body: "By the pricking of my thumb...", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/dynamic/instance-identity/document/") + .with("/2012-01-12/dynamic/instance-identity/document/", @get_token_header) .and_return(double("Net::HTTP Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) plugin.run @@ -244,22 +247,22 @@ describe Ohai::System, "plugin ec2" 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/") + .with("/2012-01-12/meta-data/", @get_token_header) .and_return(double("Net::HTTP Response", body: "iam/", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/iam/") + .with("/2012-01-12/meta-data/iam/", @get_token_header) .and_return(double("Net::HTTP Response", body: "security-credentials/", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/iam/security-credentials/") + .with("/2012-01-12/meta-data/iam/security-credentials/", @get_token_header) .and_return(double("Net::HTTP Response", body: "MyRole", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/iam/security-credentials/MyRole") + .with("/2012-01-12/meta-data/iam/security-credentials/MyRole", @get_token_header) .and_return(double("Net::HTTP Response", body: "{\n \"Code\" : \"Success\",\n \"LastUpdated\" : \"2012-08-22T07:47:22Z\",\n \"Type\" : \"AWS-HMAC\",\n \"AccessKeyId\" : \"AAAAAAAA\",\n \"SecretAccessKey\" : \"SSSSSSSS\",\n \"Token\" : \"12345678\",\n \"Expiration\" : \"2012-08-22T11:25:52Z\"\n}", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/user-data/") + .with("/2012-01-12/user-data/", @get_token_header) .and_return(double("Net::HTTP Response", body: "By the pricking of my thumb...", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/dynamic/instance-identity/document/") + .with("/2012-01-12/dynamic/instance-identity/document/", @get_token_header) .and_return(double("Net::HTTP Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) plugin.run @@ -271,37 +274,36 @@ describe Ohai::System, "plugin ec2" 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/") + .with("/2012-01-12/meta-data/", @get_token_header) .and_return(double("Net::HTTP Response", body: ".\n./\n..\n../\npath1/.\npath2/./\npath3/..\npath4/../", code: "200")) - expect(@http_client).not_to receive(:get) - .with("/2012-01-12/meta-data/.") + .with("/2012-01-12/meta-data/.", @get_token_header) expect(@http_client).not_to receive(:get) - .with("/2012-01-12/meta-data/./") + .with("/2012-01-12/meta-data/./", @get_token_header) expect(@http_client).not_to receive(:get) - .with("/2012-01-12/meta-data/..") + .with("/2012-01-12/meta-data/..", @get_token_header) expect(@http_client).not_to receive(:get) - .with("/2012-01-12/meta-data/../") + .with("/2012-01-12/meta-data/../", @get_token_header) expect(@http_client).not_to receive(:get) - .with("/2012-01-12/meta-data/path1/..") + .with("/2012-01-12/meta-data/path1/..", @get_token_header) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/path1/") + .with("/2012-01-12/meta-data/path1/", @get_token_header) .and_return(double("Net::HTTP Response", body: "", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/path2/") + .with("/2012-01-12/meta-data/path2/", @get_token_header) .and_return(double("Net::HTTP Response", body: "", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/path3/") + .with("/2012-01-12/meta-data/path3/", @get_token_header) .and_return(double("Net::HTTP Response", body: "", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/path4/") + .with("/2012-01-12/meta-data/path4/", @get_token_header) .and_return(double("Net::HTTP Response", body: "", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/user-data/") + .with("/2012-01-12/user-data/", @get_token_header) .and_return(double("Net::HTTP Response", body: "By the pricking of my thumb...", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/dynamic/instance-identity/document/") + .with("/2012-01-12/dynamic/instance-identity/document/", @get_token_header) .and_return(double("Net::HTTP Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) plugin.run @@ -311,19 +313,19 @@ describe Ohai::System, "plugin ec2" do it "completes the run despite unavailable metadata" do expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/") + .with("/2012-01-12/meta-data/", @get_token_header) .and_return(double("Net::HTTP Response", body: "metrics/", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/metrics/") + .with("/2012-01-12/meta-data/metrics/", @get_token_header) .and_return(double("Net::HTTP Response", body: "vhostmd", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/metrics/vhostmd") + .with("/2012-01-12/meta-data/metrics/vhostmd", @get_token_header) .and_return(double("Net::HTTP Response", body: "", code: "404")) expect(@http_client).to receive(:get) - .with("/2012-01-12/user-data/") + .with("/2012-01-12/user-data/", @get_token_header) .and_return(double("Net::HTTP Response", body: "By the pricking of my thumb...", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/dynamic/instance-identity/document/") + .with("/2012-01-12/dynamic/instance-identity/document/", @get_token_header) .and_return(double("Net::HTTP Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) plugin.run |