ami_spec 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.1.8
5
+ - 2.2.4
data/README.md CHANGED
@@ -17,11 +17,11 @@ The problem with this approach is:
17
17
 
18
18
  ## Installation
19
19
 
20
- System-wide: gem install ami-spec
20
+ System-wide: gem install ami\_spec
21
21
 
22
22
  With bundler:
23
23
 
24
- Add `gem 'ami-spec'` to your Gemfile.
24
+ Add `gem 'ami_spec'` to your Gemfile.
25
25
  Run `bundle install`
26
26
 
27
27
  ## CLI Usage
@@ -61,7 +61,39 @@ AmiSpec will launch an EC2 instance from the given AMI (`--ami`), in a subnet (`
61
61
  and try to SSH to it (`--ssh-user` and `--key-file`).
62
62
  When the instances becomes reachable it will run all Specs inside the role spec directory (`--role` i.e. `my_project/spec/web_server`).
63
63
 
64
- Alternative to the `--ami` and `--role` variables, a file of comma separated roles and AMIs (`ROLE,AMI\n`) can be supplied to `--role-ami-file`.
64
+ Alternative to the `--ami` and `--role` variables, a file of comma separated roles and AMIs (`ROLE,AMI\n`) can be supplied to `--role-ami-file`.
65
+
66
+ ## Known caveats
67
+
68
+ ### RSpec conditions in examples
69
+
70
+ [ServerSpecs advanced tips](http://serverspec.org/advanced_tips.html) provides a mechanism to conditionally apply tests based on server information.
71
+
72
+ ```ruby
73
+ describe file('/usr/lib64'), :if => os[:arch] == 'x86_64' do
74
+ it { should be_directory }
75
+ end
76
+ ```
77
+
78
+ If these are used in shared examples, say loaded via a rspec helper, this doesn't work with AmiSpec, because the evaluation of `os[:arch] == 'x86_64'` is done when the spec is loaded not at run time.
79
+
80
+ Working around this is tricky. We need to move the evaluation of `os[:arch]` to runtime not load time. Since RSpec example metadata can only be a bool, string or symbol we set a metadata key of `:os_arch` to the value we expect:
81
+
82
+ ```ruby
83
+ describe file('/usr/lib64'), :os_arch => 'x86_64' do
84
+ it { should be_directory }
85
+ end
86
+ ```
87
+
88
+ We then have to set an RSpec exclusion of examples where the architecture does not match the host under test's architecture. This can be done in the `spec_helper` with a lambda function that tests this:
89
+
90
+ ```ruby
91
+ RSpec.configure do |c|
92
+ c.filter_run_excluding :os_arch => lambda { |arch| arch if os[:arch] != arch }
93
+ end
94
+ ```
95
+
96
+ We are exluding any example with the metadata key :os_arch where the value does not match our architecture. Similar examples can be included for os family etc.
65
97
 
66
98
  ## Development Status
67
99
 
@@ -28,7 +28,7 @@ module AmiSpec
28
28
  client = Aws::EC2::Client.new(client_options)
29
29
  placeholder_instance = client.run_instances(instances_options).instances.first
30
30
 
31
- @instance = Aws::EC2::Instance.new(placeholder_instance.instance_id)
31
+ @instance = Aws::EC2::Instance.new(placeholder_instance.instance_id, client_options)
32
32
  @instance.wait_until_running
33
33
  tag_instance
34
34
  end
@@ -18,6 +18,8 @@ module AmiSpec
18
18
  end
19
19
 
20
20
  def run
21
+ puts "Running tests for #{@role}"
22
+
21
23
  $LOAD_PATH.unshift(@spec) unless $LOAD_PATH.include?(@spec)
22
24
  require File.join(@spec, 'spec_helper')
23
25
 
@@ -1,3 +1,3 @@
1
1
  module AmiSpec
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -0,0 +1,16 @@
1
+ require 'net/ssh'
2
+
3
+ module AmiSpec
4
+ class WaitForRC
5
+ def self.wait(ip_address, user, key)
6
+ Net::SSH.start(ip_address, user, keys: [key], paranoid: false) do |ssh|
7
+ # Wait for SystemV to start
8
+ # This only works for Ubuntu with upstart.
9
+ # Detecting OS and Release will need something like this
10
+ # https://github.com/mizzy/specinfra/blob/master/lib/specinfra/helper/detect_os/debian.rb
11
+ ssh.exec 'while /usr/sbin/service rc status | grep -q "^rc start/running, process"; do sleep 1; done'
12
+ end
13
+ end
14
+ end
15
+ end
16
+
data/lib/ami_spec.rb CHANGED
@@ -3,6 +3,7 @@ require 'ami_spec/aws_instance_options'
3
3
  require 'ami_spec/server_spec'
4
4
  require 'ami_spec/server_spec_options'
5
5
  require 'ami_spec/wait_for_ssh'
6
+ require 'ami_spec/wait_for_rc'
6
7
  require 'trollop'
7
8
 
8
9
  module AmiSpec
@@ -50,6 +51,7 @@ module AmiSpec
50
51
  instances.each do |instance|
51
52
  ip_address = options[:aws_public_ip] ? instance.public_ip_address : instance.private_ip_address
52
53
  WaitForSSH.wait(ip_address, options[:ssh_user], options[:key_file], options[:ssh_retries])
54
+ WaitForRC.wait(ip_address, options[:ssh_user], options[:key_file]) if options[:wait_for_rc]
53
55
 
54
56
  server_spec_options = ServerSpecOptions.new(options.merge(instance: instance))
55
57
  results << ServerSpec.new(server_spec_options).run
@@ -95,6 +97,7 @@ module AmiSpec
95
97
  opt :ssh_retries, "The number of times we should try sshing to the ec2 instance before giving up. Defaults to 30",
96
98
  type: :int, default: 30
97
99
  opt :debug, "Don't terminate instances on exit"
100
+ opt :wait_for_rc, "Wait for oldschool SystemV scripts to run before conducting tests. Currently only supports Ubuntu with upstart"
98
101
  end
99
102
 
100
103
  if options[:role] && options[:ami]
@@ -62,12 +62,20 @@ describe AmiSpec::AwsInstance do
62
62
  context 'with region' do
63
63
  let(:region) { 'us-east-1' }
64
64
 
65
- it 'does include the region' do
65
+ it 'does include the region in the intial connection' do
66
66
  expect(Aws::EC2::Client).to receive(:new).with(
67
67
  hash_including(:region => region)
68
68
  )
69
69
  start
70
70
  end
71
+
72
+ it 'does include the region in the subsequent connection' do
73
+ expect(Aws::EC2::Instance).to receive(:new).with(
74
+ anything,
75
+ hash_including(:region => region)
76
+ )
77
+ start
78
+ end
71
79
  end
72
80
 
73
81
  it 'tags the instance with a role' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ami_spec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-12-18 00:00:00.000000000 Z
13
+ date: 2016-05-20 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: aws-sdk
@@ -133,6 +133,7 @@ extra_rdoc_files: []
133
133
  files:
134
134
  - .gitignore
135
135
  - .ruby-version
136
+ - .travis.yml
136
137
  - Gemfile
137
138
  - LICENSE.txt
138
139
  - README.md
@@ -145,6 +146,7 @@ files:
145
146
  - lib/ami_spec/server_spec.rb
146
147
  - lib/ami_spec/server_spec_options.rb
147
148
  - lib/ami_spec/version.rb
149
+ - lib/ami_spec/wait_for_rc.rb
148
150
  - lib/ami_spec/wait_for_ssh.rb
149
151
  - spec/ami_spec_spec.rb
150
152
  - spec/aws_instance_spec.rb