How to taunt with aws-sdk gem?

I have code that uploads a file to Amazon S3 using aws-sdk stone. Apparently the HTTP file is loading the file.

Is there a good way to make fun of this aws-sdk hard drive functionality?

I tried using Webmock, but it looks like the aws-sdk gem first does get latest/meta-data/iam/security-credentials/ . It seems that using Webmock might not be the best way to mock this functionality.

Work in RSpec.

+6
source share
2 answers

There are many ways to mock queries in the AWS SDK for Ruby . Recently, Trevor Rowe published an article about using the built-in SDK support for stubbing objects , which does not require any external dependencies such as Webmock. You can also use tools like VCR (the link will send you to another blog post) to create cached integration tests; this way you can test against a live service if you want accuracy and avoid getting into the network when you want speed.

Regarding the get request to latest/meta-data/iam/security-credentials/ , this is because the SDK is trying to find the credentials, and if none of them are provided, it will check if the EC2 instance is running in as a last resort, resulting in an SDK for adding an additional HTTP request. You can avoid this check by simply providing dummy static credentials, although if you are using something like a VCR, you need to provide valid credentials for the first run. You can read about how to provide static credentials in another blog post about what Trevor wrote about credential management (this should also be in the developer documentation and SDK documentation).

+11
source

If you are using aws-sdk gem version 2, add:

 Aws.config.update(stub_responses: true) 

into your RSpec.configure block (usually located in the rails_helper.rb file)


While the above works, it will return empty answers if you do not specify additional response content - not necessarily correct, but trimmed.

You can generate and return delayed response data from a named operation:

 s3 = Aws::S3::Client.new s3.stub_data(:list_buckets) #=> #<struct Aws::S3::Types::ListBucketsOutput buckets=[], owner=#<struct Aws::S3::Types::Owner display_name="DisplayName", id="ID">> 

In addition to generating default stubs, you can provide data to apply to the response stub.

 s3.stub_data(:list_buckets, buckets:[{name:'aws-sdk'}]) #=> #<struct Aws::S3::Types::ListBucketsOutput buckets=[#<struct Aws::S3::Types::Bucket name="aws-sdk", creation_date=nil>], owner=#<struct Aws::S3::Types::Owner display_name="DisplayName", id="ID">> 

For more details see: http://docs.aws.amazon.com/sdkforruby/api/Aws/ClientStubs.html

+14
source

Source: https://habr.com/ru/post/944296/


All Articles