Skip to content

Commit

Permalink
started work on create stack method
Browse files Browse the repository at this point in the history
  • Loading branch information
pghalliday committed Jan 8, 2016
1 parent c863621 commit 03e2280
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 17 deletions.
21 changes: 16 additions & 5 deletions lib/formatron/aws/cloud_formation_stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@ class Formatron
class AWS
# utilities for monitoring CloudFormation stack activities
class CloudFormationStack
CAPABILITIES = %w(CAPABILITY_IAM)

def initialize(stack_name:, client:)
puts stack_name
puts client
@stack_name = stack_name
@client = client
@stack = Aws::CloudFormation::Stack.new(
stack_name,
client: @client
)
end

def exists?
true
@stack.exists?
end

def create(template_url:, parameters:)
puts template_url
puts parameters
@client.create_stack(
stack_name: @stack_name,
template_url: template_url,
capabilities: CAPABILITIES,
on_failure: 'DO_NOTHING',
parameters: parameters
)
true
end

Expand Down
81 changes: 69 additions & 12 deletions spec/formatron/aws/cloud_formation_stack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,86 @@ class AWS
parameters = 'parameters'

before :each do
@cloudformation_client = instance_double('Aws::CloudFormation::Client')
@aws_cloudformation_client = instance_double(
'Aws::CloudFormation::Client'
)
@aws_cloudformation_stack = instance_double(
'Aws::CloudFormation::Stack'
)
aws_cloudformation_stack_class = class_double(
'Aws::CloudFormation::Stack'
).as_stubbed_const
expect(aws_cloudformation_stack_class).to receive(:new).with(
stack_name,
client: @aws_cloudformation_client
) { @aws_cloudformation_stack }
@cloudformation_stack = CloudFormationStack.new(
stack_name: stack_name,
client: @cloudformation_client
client: @aws_cloudformation_client
)
end

describe '#exists?' do
it 'should return whether the stack exists and has not been deleted' do
expect(
@cloudformation_stack.exists?
).to eql true
context 'when the stack does not exist' do
before :each do
allow(@aws_cloudformation_stack).to receive(
:exists?
) { false }
end

it 'should return false' do
expect(
@cloudformation_stack.exists?
).to eql false
end
end

context 'when the stack exists' do
before :each do
allow(@aws_cloudformation_stack).to receive(
:exists?
) { true }
end

it 'should return true' do
expect(
@cloudformation_stack.exists?
).to eql true
end
end
end

describe '#create' do
it 'should create a stack and wait for the operation to finish' do
expect(
@cloudformation_stack.create(
template_url: template_url,
parameters: parameters
before :each do
expect(@aws_cloudformation_client).to receive(:create_stack).with(
stack_name: stack_name,
template_url: template_url,
capabilities: %w(CAPABILITY_IAM),
on_failure: 'DO_NOTHING',
parameters: parameters
)
end

context 'when the create completes successfully' do
before :each do
allow(@aws_cloudformation_stack).to receive(
:events
).and_return(
*CloudformationStackEventsResponses.new(
stack_name: stack_name,
final_status: 'CREATE_COMPLETE'
).responses
)
).to eql true
end

it 'should create a stack and wait for the operation to finish' do
expect(
@cloudformation_stack.create(
template_url: template_url,
parameters: parameters
)
).to eql true
end
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'fakefs/spec_helpers'

require 'cloudformation_describe_stacks_response'
require 'cloudformation_stack_events_responses'
require 's3_get_object_response'
require 's3_list_objects_response'
require 'route53_get_hosted_zone_response'
Expand Down
66 changes: 66 additions & 0 deletions support/cloudformation_stack_events_responses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'aws-sdk'

class Formatron
module Support
# Stub CloudFormation describe_stacks response class
class CloudformationStackEventsResponses
include RSpec::Mocks::ExampleMethods

attr_reader :responses

@next_event_id = 0

# rubocop:disable Metrics/MethodLength
def initialize(stack_name:, final_status:)
first_response = [
event,
event,
event
]
second_response = [
event,
event,
event,
*first_response
]
third_response = [
event(
logical_resource_id: stack_name,
resource_status: final_status,
resource_type: 'AWS::CloudFormation::Stack'
),
event,
event,
*second_response
]
@responses = [
first_response,
second_response,
third_response
]
end
# rubocop:enable Metrics/MethodLength

# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
def event(
logical_resource_id: 'logical_resource_id',
resource_status: 'resource_status',
resource_type: 'resource_type'
)
event = instance_double(Aws::CloudFormation::Event)
allow(event).to receive(:logical_resource_id) { logical_resource_id }
allow(event).to receive(:resource_status) { resource_status }
allow(event).to receive(:resource_type) { resource_type }
allow(event).to receive(:timestamp) { 'timestamp' }
allow(event).to receive(
:resource_status_reason
) { 'resource_status_reason' }
allow(event).to receive(:event_id) { self.class.next_event_id }
event
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength
end
end
end

0 comments on commit 03e2280

Please sign in to comment.