Aws-sdk-ruby: HTTP request made for iam credentials during test ONLY on CI

Created on 10 Feb 2017  路  4Comments  路  Source: aws/aws-sdk-ruby

My tests for uploading an image to s3 only fail on CI with this error from WebMock:

```ruby
WebMock::NetConnectNotAllowedError: Real HTTP connections are disabled. Unregistered request: GET http://169.254.169.254/latest/meta-data/iam/security-credentials/ with headers {'Accept'=>'/', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}

You can stub this request with the following snippet:

stub_request(:get, "http://169.254.169.254/latest/meta-data/iam/security-credentials/").
with(:headers => {'Accept'=>'/', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => "", :headers => {})

guidance response-requested

Most helpful comment

As you found, you'll get that error if the default credential provider chain doesn't find credentials. If you expect that no network calls should occur in your test environment, as for unit testing, stub clients won't look for credentials and won't ever make HTTP requests. You can make one like so:

client = Aws::S3::Client.new(stub_responses: true)

All 4 comments

@NomNomCameron Could you provide a bit more information regarding how the error is generated? Is our tests failing on your side, or perhaps you are trying to make real request in spec tests? In mocking request and response for testing, you might be interested in ClientStub.

Also, our getting help section has been updated recently, feel free to get engaged in other channel as well : ). If you still find this might be a bug with our repo, it would be great if you could provide more information as I mentioned above : )

This issue was caused by not having the AWS id and secret key set in ENV for the CI container. Thank you so much for getting back to me though! ClientStub looks like it will be very useful!

As you found, you'll get that error if the default credential provider chain doesn't find credentials. If you expect that no network calls should occur in your test environment, as for unit testing, stub clients won't look for credentials and won't ever make HTTP requests. You can make one like so:

client = Aws::S3::Client.new(stub_responses: true)

Awesome, thank you @awood45 I think I'll implement that instead of using WebMock!

Was this page helpful?
0 / 5 - 0 ratings