diff options
author | wilkosz <joshua@wilkosz.com.au> | 2020-04-27 15:23:31 +1000 |
---|---|---|
committer | wilkosz <joshua@wilkosz.com.au> | 2020-04-27 15:23:31 +1000 |
commit | 8699b6d1e8d9c3fdb859fbe8083fd4291ff8816d (patch) | |
tree | f17e543450509e0e12d3f98b7e9fdf6fb40cfe99 | |
parent | 466c94b07bd4535ba869140c0f7d7655334090cf (diff) | |
download | ohai-8699b6d1e8d9c3fdb859fbe8083fd4291ff8816d.tar.gz |
qucalyotus and openstack spec updates to allow put commands on http double spec client
-rw-r--r-- | spec/unit/plugins/ec2_spec.rb | 112 | ||||
-rw-r--r-- | spec/unit/plugins/eucalyptus_spec.rb | 27 | ||||
-rw-r--r-- | spec/unit/plugins/openstack_spec.rb | 6 |
3 files changed, 76 insertions, 69 deletions
diff --git a/spec/unit/plugins/ec2_spec.rb b/spec/unit/plugins/ec2_spec.rb index a7a7bb50..5c26f926 100644 --- a/spec/unit/plugins/ec2_spec.rb +++ b/spec/unit/plugins/ec2_spec.rb @@ -48,11 +48,11 @@ 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 = "AQAEAE4UUd-3NE5EEeYYXKxicVfDOHsx0YSHFFSuCvo2GfCcxzJsvg==" - @get_token_header = {:'X-aws-ec2-metadata-token' => @token} - allow(@http_client).to receive(:put) { double("Net::HTTP::PUT Response", body: @token, code: "200") } + token = "AQAEAE4UUd-3NE5EEeYYXKxicVfDOHsx0YSHFFSuCvo2GfCcxzJsvg==" + @get_req_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("/", @get_token_header) + .with("/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "2012-01-12", code: "200")) end @@ -68,14 +68,14 @@ 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}", @get_token_header) + .with("/2012-01-12/#{name}", @get_req_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/", @get_token_header) + .with("/2012-01-12/user-data/", @get_req_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/", @get_token_header) + .with("/2012-01-12/dynamic/instance-identity/document/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) plugin.run @@ -89,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}", @get_token_header) + .with("/2012-01-12/#{name}", @get_req_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/", @get_token_header) + .with("/2012-01-12/user-data/", @get_req_token_header) .and_return(double("Net::HTTP::GET 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/", @get_token_header) + .with("/2012-01-12/dynamic/instance-identity/document/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) plugin.run @@ -111,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}", @get_token_header) + .with("/2012-01-12/#{name}", @get_req_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/", @get_token_header) + .with("/2012-01-12/user-data/", @get_req_token_header) .and_return(double("Net::HTTP::GET 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/", @get_token_header) + .with("/2012-01-12/dynamic/instance-identity/document/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) plugin.run @@ -133,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}", @get_token_header) + .with("/2012-01-12/#{name}", @get_req_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/", @get_token_header) + .with("/2012-01-12/user-data/", @get_req_token_header) .and_return(double("Net::HTTP::GET 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/", @get_token_header) + .with("/2012-01-12/dynamic/instance-identity/document/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "{\"region\":\"us-east-1\"}", code: "200")) plugin.run @@ -155,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}", @get_token_header) + .with("/2012-01-12/#{name}", @get_req_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/", @get_token_header) + .with("/2012-01-12/user-data/", @get_req_token_header) .and_return(double("Net::HTTP::GET 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/", @get_token_header) + .with("/2012-01-12/dynamic/instance-identity/document/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "{\"availabilityZone\":\"us-east-1d\"}", code: "200")) plugin.run @@ -177,28 +177,28 @@ 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/", @get_token_header) + .with("/2012-01-12/meta-data/", @get_req_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/", @get_token_header) + .with("/2012-01-12/meta-data/network/", @get_req_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/", @get_token_header) + .with("/2012-01-12/meta-data/network/interfaces/", @get_req_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/", @get_token_header) + .with("/2012-01-12/meta-data/network/interfaces/macs/", @get_req_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/", @get_token_header) + .with("/2012-01-12/meta-data/network/interfaces/macs/12:34:56:78:9a:bc/", @get_req_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", @get_token_header) + .with("/2012-01-12/meta-data/network/interfaces/macs/12:34:56:78:9a:bc/public_hostname", @get_req_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/", @get_token_header) + .with("/2012-01-12/user-data/", @get_req_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/", @get_token_header) + .with("/2012-01-12/dynamic/instance-identity/document/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) plugin.run @@ -214,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/", @get_token_header) + .with("/2012-01-12/meta-data/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "iam/", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/iam/", @get_token_header) + .with("/2012-01-12/meta-data/iam/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "security-credentials/", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/iam/security-credentials/", @get_token_header) + .with("/2012-01-12/meta-data/iam/security-credentials/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "MyRole", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/iam/security-credentials/MyRole", @get_token_header) + .with("/2012-01-12/meta-data/iam/security-credentials/MyRole", @get_req_token_header) .and_return(double("Net::HTTP::GET 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/", @get_token_header) + .with("/2012-01-12/user-data/", @get_req_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/", @get_token_header) + .with("/2012-01-12/dynamic/instance-identity/document/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) plugin.run @@ -247,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/", @get_token_header) + .with("/2012-01-12/meta-data/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "iam/", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/iam/", @get_token_header) + .with("/2012-01-12/meta-data/iam/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "security-credentials/", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/iam/security-credentials/", @get_token_header) + .with("/2012-01-12/meta-data/iam/security-credentials/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "MyRole", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/iam/security-credentials/MyRole", @get_token_header) + .with("/2012-01-12/meta-data/iam/security-credentials/MyRole", @get_req_token_header) .and_return(double("Net::HTTP::GET 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/", @get_token_header) + .with("/2012-01-12/user-data/", @get_req_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/", @get_token_header) + .with("/2012-01-12/dynamic/instance-identity/document/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) plugin.run @@ -274,36 +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/", @get_token_header) + .with("/2012-01-12/meta-data/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: ".\n./\n..\n../\npath1/.\npath2/./\npath3/..\npath4/../", code: "200")) expect(@http_client).not_to receive(:get) - .with("/2012-01-12/meta-data/.", @get_token_header) + .with("/2012-01-12/meta-data/.", @get_req_token_header) expect(@http_client).not_to receive(:get) - .with("/2012-01-12/meta-data/./", @get_token_header) + .with("/2012-01-12/meta-data/./", @get_req_token_header) expect(@http_client).not_to receive(:get) - .with("/2012-01-12/meta-data/..", @get_token_header) + .with("/2012-01-12/meta-data/..", @get_req_token_header) expect(@http_client).not_to receive(:get) - .with("/2012-01-12/meta-data/../", @get_token_header) + .with("/2012-01-12/meta-data/../", @get_req_token_header) expect(@http_client).not_to receive(:get) - .with("/2012-01-12/meta-data/path1/..", @get_token_header) + .with("/2012-01-12/meta-data/path1/..", @get_req_token_header) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/path1/", @get_token_header) + .with("/2012-01-12/meta-data/path1/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/path2/", @get_token_header) + .with("/2012-01-12/meta-data/path2/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/path3/", @get_token_header) + .with("/2012-01-12/meta-data/path3/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/path4/", @get_token_header) + .with("/2012-01-12/meta-data/path4/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/user-data/", @get_token_header) + .with("/2012-01-12/user-data/", @get_req_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/", @get_token_header) + .with("/2012-01-12/dynamic/instance-identity/document/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) plugin.run @@ -313,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/", @get_token_header) + .with("/2012-01-12/meta-data/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "metrics/", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/metrics/", @get_token_header) + .with("/2012-01-12/meta-data/metrics/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "vhostmd", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/metrics/vhostmd", @get_token_header) + .with("/2012-01-12/meta-data/metrics/vhostmd", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "", code: "404")) expect(@http_client).to receive(:get) - .with("/2012-01-12/user-data/", @get_token_header) + .with("/2012-01-12/user-data/", @get_req_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/", @get_token_header) + .with("/2012-01-12/dynamic/instance-identity/document/", @get_req_token_header) .and_return(double("Net::HTTP::GET Response", body: "{\"accountId\":\"4815162342\"}", code: "200")) plugin.run diff --git a/spec/unit/plugins/eucalyptus_spec.rb b/spec/unit/plugins/eucalyptus_spec.rb index c4a4d628..d27298a4 100644 --- a/spec/unit/plugins/eucalyptus_spec.rb +++ b/spec/unit/plugins/eucalyptus_spec.rb @@ -33,26 +33,29 @@ describe Ohai::System, "plugin eucalyptus" do shared_examples_for "eucalyptus" do before do @http_client = double("Net::HTTP client") + @token = "AQAEAE4UUd-3NE5EEeYYXKxicVfDOHsx0YSHFFSuCvo2GfCcxzJsvg==" + @get_req_token_header = {:'X-aws-ec2-metadata-token' => @token} allow(plugin).to receive(:http_client).and_return(@http_client) + 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_req_token_header) + .and_return(double("Net::HTTP::GET Response", body: "2012-01-12", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/") - .and_return(double("Net::HTTP Response", body: "instance_type\nami_id\nsecurity-groups", code: "200")) + .with("/2012-01-12/meta-data/", @get_req_token_header) + .and_return(double("Net::HTTP::GET Response", body: "instance_type\nami_id\nsecurity-groups", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/instance_type") - .and_return(double("Net::HTTP Response", body: "c1.medium", code: "200")) + .with("/2012-01-12/meta-data/instance_type", @get_req_token_header) + .and_return(double("Net::HTTP::GET Response", body: "c1.medium", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/ami_id") - .and_return(double("Net::HTTP Response", body: "ami-5d2dc934", code: "200")) + .with("/2012-01-12/meta-data/ami_id", @get_req_token_header) + .and_return(double("Net::HTTP::GET Response", body: "ami-5d2dc934", code: "200")) expect(@http_client).to receive(:get) - .with("/2012-01-12/meta-data/security-groups") - .and_return(double("Net::HTTP Response", body: "group1\ngroup2", code: "200")) + .with("/2012-01-12/meta-data/security-groups", @get_req_token_header) + .and_return(double("Net::HTTP::GET Response", body: "group1\ngroup2", 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_req_token_header) + .and_return(double("Net::HTTP::GET Response", body: "By the pricking of my thumb...", code: "200")) end it "recursively fetches all the eucalyptus metadata" do diff --git a/spec/unit/plugins/openstack_spec.rb b/spec/unit/plugins/openstack_spec.rb index 530059dd..8700957f 100644 --- a/spec/unit/plugins/openstack_spec.rb +++ b/spec/unit/plugins/openstack_spec.rb @@ -187,12 +187,16 @@ describe Ohai::System, "plugin openstack" do let(:http_client) { double("Net::HTTP", { :read_timeout= => nil, :keep_alive_timeout= => nil } ) } def allow_get(url, response_body) + token = "AQAEAE4UUd-3NE5EEeYYXKxicVfDOHsx0YSHFFSuCvo2GfCcxzJsvg==" + allow(http_client).to receive(:put) { double("Net::HTTP::PUT Response", body: token, code: "200") } allow(http_client).to receive(:get) - .with(url) + .with(url, {:'X-aws-ec2-metadata-token' =>token}) .and_return(double("HTTP Response", code: "200", body: response_body)) end def allow_get_response(url, response_body) + token = "AQAEAE4UUd-3NE5EEeYYXKxicVfDOHsx0YSHFFSuCvo2GfCcxzJsvg==" + allow(http_client).to receive(:put) { double("Net::HTTP::PUT Response", body: token, code: "200") } allow(http_client).to receive(:get_response) .with(url, nil, nil) .and_return(double("HTTP Response", code: "200", body: response_body)) |