Skip to content

Commit

Permalink
Add integration testing
Browse files Browse the repository at this point in the history
  • Loading branch information
vladgh committed Jun 11, 2014
1 parent 8821acd commit a875320
Show file tree
Hide file tree
Showing 9 changed files with 383 additions and 23 deletions.
86 changes: 79 additions & 7 deletions spec/aws_spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,82 @@
require 'spec_helper'
require 'aws-sdk'

::AWS.config({
:access_key_id => '1234',
:secret_access_key => '5678',
:region => 'us-east-1',
:logger => nil,
:stub_requests => true
})
shared_context 'Not an EC2 Instance' do
before(:each) do
allow(Net::HTTP).to receive(:get_response).and_return(false)
end
end

shared_context 'Amazon Web Services' do
# Preload AWS library
AWS.eager_autoload! AWS::Core # Make sure to load Core first.
AWS.eager_autoload! AWS::EC2 # Load the EC2 class

::AWS.config({
:access_key_id => '1234',
:secret_access_key => '5678',
:logger => nil,
:stub_requests => true
})

let(:region) {'us-east-1'}

let(:fake_instances) {::AWS::Core::Data.new([
{id: 'i-abcdefg1', status: :running, tags: tags},
{id: 'i-abcdefg2', status: :terminated, tags: tags},
{id: 'i-abcdefg3', status: :running, tags: {}},
{id: 'i-abcdefg4', status: :terminated, tags: {}},
])}

let(:tags) {{
'Name' => 'TestName-1.TestDomain.tld',
'Domain' => 'TestDomainValue.tld',
'TestTagKey' => 'TestTagValue',
}}

before(:each) do
allow_any_instance_of(VScripts::AWS::EC2)
.to receive_message_chain('ec2.instances.tagged')
.and_return(fake_instances)
allow_any_instance_of(VScripts::AWS::EC2)
.to receive_message_chain(:named_instances)
.and_return(fake_instances[0..1])
end
end

shared_context 'EC2 Instance' do
include_context 'Amazon Web Services'

before(:each) do
allow(Net::HTTP).to receive(:get_response).and_return(true)
allow(VScripts::AWS::Metadata).to receive_message_chain('open.read')
.and_return('Remote server response')
allow_any_instance_of(VScripts::AWS::Metadata).to receive(:zone)
allow_any_instance_of(VScripts::AWS::Metadata).to receive(:region)
.and_return('us-east-1')
allow_any_instance_of(VScripts::AWS::Metadata).to receive(:instance_id)
.and_return(fake_instances[1].id)
allow_any_instance_of(VScripts::AWS::EC2)
.to receive_message_chain('ec2.tags.create')
end
end

shared_context 'EC2 Instance without tags' do
include_context 'EC2 Instance'

before(:each) do
allow_any_instance_of(VScripts::AWS::EC2)
.to receive_message_chain('instance.tags')
.and_return({})
end
end

shared_context 'EC2 Instance with tags' do
include_context 'EC2 Instance'

before(:each) do
allow_any_instance_of(VScripts::AWS::EC2)
.to receive_message_chain('instance.tags')
.and_return(tags)
end
end
85 changes: 85 additions & 0 deletions spec/integration/commands/identify_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
require 'integration/spec_helper'

describe 'Command: Identify' do
include_context 'System files'
include_context 'Suppressed output'

subject { VScripts }

before(:each) do
allow_any_instance_of(VScripts::Commands::Identify)
.to receive('`')
end

context 'when \'--help\'' do
it 'returns command specific help' do
expect{subject.run(['identify', '--help'])}.to raise_error(SystemExit)
expect($stdout.string).to match(/USAGE:/)
expect($stdout.string).to match(/OPTIONS:/)
end
end

context 'when unknown argument' do
it 'returns error with message' do
expect{subject.run(['identify', '--xyz'])}.to raise_error(SystemExit)
expect($stderr.string).to match(/Error: unknown argument/)
end
end

context 'when not an EC2 instance' do
include_context 'Not an EC2 Instance'
it 'returns error with message' do
expect{subject.run(['identify'])}.to raise_error(SystemExit)
expect($stderr.string)
.to match('FATAL: NOT an EC2 instance or could not connect to Metadata')
end
end

context 'when EC2 instance' do
include_context 'EC2 Instance without tags'

context 'without tags' do
it 'writes default hostname' do
allow_any_instance_of(VScripts::Commands::Identify).to receive(:tag)
subject.run(['identify'])
expect(IO.read(hostname_file)).to eq('1')
expect($stdout.string).to match('Done.')
end
end

context 'and when \'--ec2-tag-theme\'' do
it 'returns the themed host' do
allow_any_instance_of(VScripts::Commands::Identify).to receive(:tag)
.and_return('TestValue')
subject.run(['identify', '--ec2-tag-theme=Test-#'])
expect(IO.read(hostname_file)).to eq('TestValue-1')
end
end

context 'and when \'--host\'' do
it 'returns the new host' do
subject.run(['identify', '--host=test-host'])
expect(IO.read(hostname_file)).to eq('test-host')
end
end

context 'and when \'--domain\'' do
it 'returns the new domain' do
subject.run(['identify', '--domain=example.tld'])
expect(IO.read(hosts_file)).to match('example.tld')
end
end

context 'and when similar found' do
it 'returns the incremented host' do
allow_any_instance_of(VScripts::Commands::Identify).to receive(:tag)
.and_return('TestName')
allow_any_instance_of(VScripts::Commands::Identify).to receive(:domain)
.and_return('TestDomain.tld')
subject.run(['identify', '--ec2-tag-theme=Test-#'])
expect(IO.read(hostname_file)).to eq('TestName-2')
expect(IO.read(hosts_file)).to match('TestName-2.TestDomain.tld')
end
end
end
end
87 changes: 87 additions & 0 deletions spec/integration/commands/tags2facts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'integration/spec_helper'

describe 'Command: Tags2Facts' do
include_context 'Suppressed output'
include_context 'Temporary'

subject { VScripts }

context 'when \'--help\'' do
it 'returns command specific help' do
expect{subject.run(['tags2facts', '--help'])}.to raise_error(SystemExit)
expect($stdout.string).to match(/USAGE:/)
expect($stdout.string).to match(/OPTIONS:/)
end
end

context 'when unknown argument' do
it 'returns error with message' do
expect{subject.run(['tags2facts', '--xyz'])}.to raise_error(SystemExit)
expect($stderr.string).to match(/Error: unknown argument/)
end
end

context 'when not an EC2 instance' do
include_context 'Not an EC2 Instance'
it 'returns error with message' do
expect{subject.run(['tags2facts'])}.to raise_error(SystemExit)
expect($stderr.string)
.to match('FATAL: NOT an EC2 instance or could not connect to Metadata')
end
end

context 'when EC2 instance' do
context 'without tags' do
include_context 'EC2 Instance without tags'
it 'returns error with message' do
expect{subject.run(['tags2facts'])}.to raise_error(SystemExit)
expect($stderr.string).to match(/No tags were found/)
end
end

context 'with tags' do
before(:each) do
allow_any_instance_of(VScripts::Commands::Tags2facts)
.to receive_message_chain('cli.file')
.and_return(test_file)
allow_any_instance_of(VScripts::Commands::Tags2facts)
.to receive_message_chain('cli.all').and_return(false)
end

context '--all specified' do
include_context 'EC2 Instance with tags'
it 'creates file' do
allow_any_instance_of(VScripts::Commands::Tags2facts)
.to receive_message_chain('cli.all').and_return(true)
subject.run(['tags2facts', '--all'])
expect(IO.read(test_file))
.to match(tags['Name'])
end
end

context '--all not specified' do
include_context 'EC2 Instance with tags'
it 'creates file' do
subject.run(['tags2facts'])
expect(IO.read(test_file)).to match(tags.keys.last)
end
end

context '--file specified' do
include_context 'EC2 Instance with tags'
it 'creates file' do
subject.run(['tags2facts', "--file #{test_file}"])
expect(IO.read(test_file)).to match(tags.keys.last)
end
end

context '--file not specified' do
include_context 'EC2 Instance with tags'
it 'creates file' do
subject.run(['tags2facts'])
expect(IO.read(test_file)).to match(tags.keys.last)
end
end
end
end
end
12 changes: 12 additions & 0 deletions spec/integration/commands_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'integration/spec_helper'

describe 'Command: Unknown' do
include_context 'Suppressed output'

subject { VScripts }

it 'returns error with message' do
expect{subject.run(['xyz'])}.to raise_error(SystemExit)
expect($stderr.string).to match(/Error: unknown subcommand/)
end
end
62 changes: 62 additions & 0 deletions spec/integration/global_opts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'integration/spec_helper'

describe 'Global Options' do
subject { VScripts }

before(:all) do
$stdout = StringIO.new
$stderr = StringIO.new
end

after(:all) do
$stdout = STDOUT
$stderr = STDERR
end

describe 'Help' do
context 'when \'-h\'' do
it 'returns help and exits' do
expect{subject.run(['-h'])}.to raise_error(SystemExit)
expect($stdout.string).to match(/Available commands/)
end
end

context 'when \'--help\'' do
it 'returns help and exits' do
expect{subject.run(['--help'])}.to raise_error(SystemExit)
expect($stdout.string).to match(/Available commands/)
end
end
end

describe 'Version' do
context 'when \'-v\'' do
it 'returns version and exits' do
expect{subject.run(['-v'])}.to raise_error(SystemExit)
expect($stdout.string).to match(/VScripts.*(c)/)
end
end

context 'when \'--version\'' do
it 'returns version and exits' do
expect{subject.run(['--version'])}.to raise_error(SystemExit)
expect($stdout.string).to match(/VScripts.*(c)/)
end
end
end
describe 'Unknown argument' do
context 'when short' do
it 'returns error with message' do
expect{subject.run(['-z'])}.to raise_error(SystemExit)
expect($stderr.string).to match(/Error: unknown argument/)
end
end

context 'when long' do
it 'returns error with message' do
expect{subject.run(['--xyz'])}.to raise_error(SystemExit)
expect($stderr.string).to match(/Error: unknown argument/)
end
end
end
end
2 changes: 2 additions & 0 deletions spec/integration/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'aws_spec_helper'
require 'vscripts'
Loading

0 comments on commit a875320

Please sign in to comment.