Module: AwsIntTestRspecHelper
- Defined in:
- lib/aws-int-test-rspec-helper.rb
Overview
Some methods to make integration testing AWS SDK code a bit more convenient. Easily create AWS resources with cfndsl specifications as part of RSpec tests.
Instance Method Summary collapse
-
#cleanup(cloudformation_stack_name) ⇒ Object
Delete the specified Cloudformation stack by name.
-
#stack(stack_name:, path_to_stack:, bindings: nil) ⇒ Object
Creates a Cloudformation stack.
-
#stack_outputs ⇒ Object
Returns a Hash of the Cloudformation stack outputs.
Instance Method Details
#cleanup(cloudformation_stack_name) ⇒ Object
Delete the specified Cloudformation stack by name
15 16 17 |
# File 'lib/aws-int-test-rspec-helper.rb', line 15 def cleanup(cloudformation_stack_name) Aws::CloudFormation::Client.new.delete_stack(stack_name: cloudformation_stack_name) end |
#stack(stack_name:, path_to_stack:, bindings: nil) ⇒ Object
Creates a Cloudformation stack.
To elaborate, this will create a stack from a cfndsl file and wait until the stack creation is completed (or failed). You can optionally parameterise the stack with a Hash. The outputs of the stack are available by referencing #stack_outputs. The return value of the method is the full stack name that is created.
-
stack_name- a stem for the name of the stack to create. the final name of the stackwill be this concatenated with a -
path_to_stack- this is the path to a cfndsl file to create the stack from -
bindings- this is an optional Hash of variables that fill in variables in the cfndsl stackthis is how you can parameterise the stack (without Parameters)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/aws-int-test-rspec-helper.rb', line 32 def stack(stack_name:, path_to_stack:, bindings: nil) full_stack_name = "#{stack_name}#{Time.now.to_i}" extras = [] unless bindings.nil? temp_file = Tempfile.new('cfnstackfortesting') temp_file.write bindings.to_yaml temp_file.close extras << [:yaml,File.(temp_file)] end verbose = false model = CfnDsl::eval_file_with_extras(File.(path_to_stack), extras, verbose) resource = Aws::CloudFormation::Resource.new created_stack = resource.create_stack(stack_name: full_stack_name, template_body: model.to_json, disable_rollback: true, capabilities: %w{CAPABILITY_IAM}) #need to provide more details to the waiter - or deal with more stack outcomes? created_stack.wait_until(max_attempts:100, delay:15) do |stack| stack.stack_status.match /COMPLETE/ or stack.stack_status.match /FAIL/ end @stack_outputs = created_stack.outputs.inject({}) do |hash, output| hash[output.output_key] = output.output_value hash end full_stack_name end |
#stack_outputs ⇒ Object
Returns a Hash of the Cloudformation stack outputs
74 75 76 |
# File 'lib/aws-int-test-rspec-helper.rb', line 74 def stack_outputs @stack_outputs end |