Skip to content

Commit

Permalink
Merge pull request #374 from v-yarotsky/vy-fix-mocking
Browse files Browse the repository at this point in the history
Fix AWS credential mocking
  • Loading branch information
geemus authored Jul 6, 2017
2 parents 4db4b3c + e85c2c9 commit e91cd8e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/fog/aws/credential_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module CredentialFetcher
module ServiceMethods
def fetch_credentials(options)
if options[:use_iam_profile] && Fog.mocking?
Fog::Compute::AWS::Mock.data[:iam_role_based_creds]
return Fog::Compute::AWS::Mock.data[:iam_role_based_creds]
end
if options[:use_iam_profile]
begin
Expand Down
19 changes: 15 additions & 4 deletions tests/credentials_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
old_mock_value = Excon.defaults[:mock]
fog_was_mocked = Fog.mocking?
Excon.stubs.clear
Fog.mock!
Fog.unmock!
begin
Excon.defaults[:mock] = true
default_credentials = Fog::Compute::AWS.fetch_credentials({})
Excon.stub({:method => :get, :path => "/latest/meta-data/iam/security-credentials/"}, {:status => 200, :body => 'arole'})
Excon.stub({:method => :get, :path => "/latest/meta-data/placement/availability-zone/"}, {:status => 200, :body => 'us-west-1a'})

Expand All @@ -17,7 +16,6 @@
'Expiration' => expires_at.xmlschema
}

Fog::Compute::AWS::Mock.data[:iam_role_based_creds] = credentials
Excon.stub({:method => :get, :path => "/latest/meta-data/iam/security-credentials/arole"}, {:status => 200, :body => Fog::JSON.encode(credentials)})

tests("#fetch_credentials") do
Expand Down Expand Up @@ -60,16 +58,29 @@
end
Fog::Time.now = Time.now

default_credentials = Fog::Compute::AWS.fetch_credentials({})
tests("#fetch_credentials when the url 404s") do
Excon.stub({:method => :get, :path => "/latest/meta-data/iam/security-credentials/"}, {:status => 404, :body => 'not bound'})
Excon.stub({:method => :get, :path => "/latest/meta-data/placement/availability-zone/"}, {:status => 400, :body => 'not found'})
returns(default_credentials) {Fog::Compute::AWS.fetch_credentials(:use_iam_profile => true)}
end

mocked_credentials = {
:aws_access_key_id => "access-key-id",
:aws_secret_access_key => "secret-access-key",
:aws_session_token => "session-token",
:aws_credentials_expire_at => Time.at(Time.now.to_i + 500).xmlschema
}
tests("#fetch_credentials when mocking") do
Fog.mock!
Fog::Compute::AWS::Mock.data[:iam_role_based_creds] = mocked_credentials
returns(mocked_credentials) {Fog::Compute::AWS.fetch_credentials(:use_iam_profile => true)}
end

ensure
ENV["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"] = nil
Excon.stubs.clear
Excon.defaults[:mock] = old_mock_value
Fog.unmock! if !fog_was_mocked
Fog.mock! if fog_was_mocked
end
end

0 comments on commit e91cd8e

Please sign in to comment.