From 4f466e1a1959bd8aa14b795e8788bd5bad7abd57 Mon Sep 17 00:00:00 2001 From: Anuj Biyani Date: Fri, 31 May 2013 13:08:23 -0700 Subject: [PATCH] on init, connect to AWS and check for the AS group --- bin/aws-ha-release.rb | 3 +++ features/support/env.rb | 5 ++++ .../aws-ha-release/aws-ha-release.rb | 10 +++++++ .../aws-ha-release/aws-ha-release_spec.rb | 18 ++++++++++--- spec/support/fake_auto_scaling.rb | 26 +++++++++++++++++++ 5 files changed, 59 insertions(+), 3 deletions(-) diff --git a/bin/aws-ha-release.rb b/bin/aws-ha-release.rb index 18f8e2c..4f6539a 100755 --- a/bin/aws-ha-release.rb +++ b/bin/aws-ha-release.rb @@ -25,3 +25,6 @@ if opts[:aws_access_key].nil? || opts[:aws_secret_key].nil? opts[:aws_access_key] = ENV['AWS_ACCESS_KEY'] opts[:aws_secret_key] = ENV['AWS_SECRET_KEY'] end + +require 'aws-missing-tools' +AwsHaRelease.new(opts) diff --git a/features/support/env.rb b/features/support/env.rb index fb0a661..96bece2 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -1 +1,6 @@ require 'aruba/cucumber' +require 'aws-sdk' + +Before do + AWS.stub! +end diff --git a/lib/aws-missing-tools/aws-ha-release/aws-ha-release.rb b/lib/aws-missing-tools/aws-ha-release/aws-ha-release.rb index fdccd04..39c47b7 100755 --- a/lib/aws-missing-tools/aws-ha-release/aws-ha-release.rb +++ b/lib/aws-missing-tools/aws-ha-release/aws-ha-release.rb @@ -1,2 +1,12 @@ class AwsHaRelease + def initialize(opts) + AWS.config(access_key_id: opts[:aws_access_key], secret_access_key: opts[:aws_secret_key], region: opts[:region]) + + @as = AWS::AutoScaling.new + @group = @as.groups[opts[:as_group_name]] + + if @group.nil? + raise ArgumentError, "The Auto Scaling Group named #{opts[:as_group_name]} does not exist in #{opts[:region]}." + end + end end diff --git a/spec/aws-missing-tools/aws-ha-release/aws-ha-release_spec.rb b/spec/aws-missing-tools/aws-ha-release/aws-ha-release_spec.rb index 57d3877..4a05871 100644 --- a/spec/aws-missing-tools/aws-ha-release/aws-ha-release_spec.rb +++ b/spec/aws-missing-tools/aws-ha-release/aws-ha-release_spec.rb @@ -10,13 +10,25 @@ describe 'aws-ha-release' do } end + let(:as) { AWS::FakeAutoScaling.new } + before do AWS.stub(:config) - AWS::AutoScaling.stub(:new).and_return(AWS::FakeAutoScaling.new) + AWS::AutoScaling.stub(:new).and_return(as) end describe '#initialize' do - it 'initializes the AWS connection' - it 'ensures the as group exists' + it 'initializes the AWS connection' do + as.groups.create 'test_group' + + AWS.should_receive(:config).with(access_key_id: 'testaccesskey', secret_access_key: 'testsecretkey', region: 'test-region') + AwsHaRelease.new(opts) + end + + it 'ensures the as group exists' do + lambda { + AwsHaRelease.new(opts.merge!(as_group_name: 'fake_group')) + }.should raise_error + end end end diff --git a/spec/support/fake_auto_scaling.rb b/spec/support/fake_auto_scaling.rb index 4905eec..d1ef401 100644 --- a/spec/support/fake_auto_scaling.rb +++ b/spec/support/fake_auto_scaling.rb @@ -2,5 +2,31 @@ module AWS class FakeAutoScaling def initialize end + + def groups + @groups ||= GroupCollection.new + end + + class GroupCollection + def initialize + @groups = {} + end + + def [](name) + @groups[name] + end + + def create(name, options = {}) + @groups[name] = Group.new name + end + end + + class Group + attr_reader :name + + def initialize(name) + @name = name + end + end end end