Skip to content

Commit 8bedfff

Browse files
committed
added infrastructure tests
1 parent a75125b commit 8bedfff

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Feature: Scripted provisioning of target environment
2+
As a developer
3+
I would like a scripted deployment
4+
so that I can assume the deployment will work the same way everytime
5+
6+
Background:
7+
Given I am sshed into the environment
8+
9+
Scenario: Is the rails application deployed into the correct directory?
10+
When I run "ls -las /var/www/rails/"
11+
Then I should see "Gemfile"
12+
13+
Scenario Outline: Have all the required gems been installed?
14+
When I run "gem list"
15+
Then I should see "<gem>"
16+
17+
Scenarios: Gems installed
18+
| gem |
19+
| rails |
20+
| sqlite |
21+
22+
Scenario: Is the Apache service running?
23+
When I run "/sbin/chkconfig --list httpd"
24+
Then I should see "3:on"
25+
26+
Scenario: The application is up and running
27+
When I run "/usr/bin/wget -qO- http://target.devopscloud.com"
28+
Then I should see "Sample App"
29+
30+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
$:.unshift(File.dirname(__FILE__) + '/../support')
2+
require "rubygems"
3+
require 'runcommand'
4+
5+
6+
#-------------------------
7+
# Local or remote testing
8+
#-------------------------
9+
# Run tests locally
10+
Given /^I am testing the local environment$/ do
11+
@run_command = RunCommand.new
12+
end
13+
14+
# Run tests remotely using ssh
15+
Given /^I am sshed into the environment$/ do
16+
# Sanity check
17+
missing_envs = [ 'env_ip', 'env_user' ].select {|e| ENV[e].nil?}
18+
raise "ERROR: missing variables: [ #{missing_envs.join(', ')} ]" unless missing_envs.empty?
19+
@run_command = RunCommand.new(:host => ENV["env_ip"],
20+
:user => ENV["env_user"])
21+
end
22+
23+
#-------------------------
24+
# Running external commands
25+
#-------------------------
26+
# Run a command
27+
When /^I run "([^"]*)"$/ do |cmd|
28+
@output = @run_command.run(cmd)
29+
end
30+
31+
# Scrape a file
32+
When /^I scrape "([^"]*)"$/ do |file_to_scrape|
33+
@file_to_scrape = file_to_scrape
34+
end
35+
36+
# Partial match on command output
37+
Then /^I should see "([^"]*)"$/ do |value|
38+
@output.should include value
39+
end
40+
41+
#-------------------------
42+
# Property files
43+
#-------------------------
44+
# Check properties file value
45+
Then /^"([^"]*)" should equal "([^"]*)"$/ do |property, value|
46+
@props[property].should == value
47+
end
48+
49+
# Check for string in file
50+
Then /^"([^"]*)" should be present/ do |search_token|
51+
output_lines = @run_command.run("grep \"#{search_token}\" #{@file_to_scrape}")
52+
end
53+
54+
#-------------------------
55+
# Directories
56+
#-------------------------
57+
# Test directory ownership
58+
Then /^I should not be able to find any files under "([^"]*)" that are not owned by user "([^"]*)" in group "([^"]*)"$/ do |dir, user, group|
59+
@output = @run_command.run("sudo find #{dir} ! -user #{user} ! -group #{group}")
60+
@output.strip.should be_empty
61+
end
62+
63+
64+
65+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require "net/ssh"
2+
3+
class RunCommand
4+
attr_reader :last_cmd_output
5+
attr_accessor :base_directory
6+
7+
def initialize(args = {})
8+
@host = args[:host] || 'localhost'
9+
ssh_opts = {:paranoid => false}
10+
@ssh = Net::SSH.start(args[:host], args[:user], ssh_opts)
11+
end
12+
13+
def run(cmd)
14+
run_cmd = (@base_directory ? "cd #{@base_directory} && " : "") + cmd + " 2>&1"
15+
@last_cmd_output = run_remote(run_cmd)
16+
end
17+
18+
##################################################################################
19+
private
20+
21+
# Stolen from http://stackoverflow.com/a/3386375
22+
def run_remote(cmd)
23+
stdout_data = ""
24+
exit_code = nil
25+
26+
@ssh.open_channel do |channel|
27+
channel.exec(cmd) do |ch, success|
28+
unless success
29+
abort "FAILED: couldn't execute command (ssh.channel.exec)"
30+
end
31+
32+
channel.on_data do |ch,data|
33+
stdout_data+=data
34+
end
35+
36+
channel.on_request("exit-status") do |ch,data|
37+
exit_code = data.read_long
38+
end
39+
end
40+
end
41+
42+
@ssh.loop
43+
raise "FAILED host: #{@host}\nreturn code: #{exit_code}\ncmd: #{cmd}\noutput: #{stdout_data}" unless exit_code == 0
44+
return stdout_data
45+
end
46+
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Feature: Scripted provisioning of target environment
2+
As a developer
3+
I would like a scripted installation of my target environment
4+
so that I can assume the environment will be the same everytime and my deployments will be predictable
5+
6+
Background:
7+
Given I am sshed into the environment
8+
9+
Scenario: Is Passenger installed?
10+
When I run "gem list"
11+
Then I should see "passenger"
12+
13+
Scenario: Is the proper version of Ruby installed?
14+
When I run "/usr/bin/ruby -v"
15+
Then I should see "ruby 1.8.7"
16+
17+
Scenario: Is the proper version of Apache installed?
18+
When I run "/usr/sbin/httpd -v"
19+
Then I should see "2.2.22"
20+
21+
Scenario: Is the Apache service running?
22+
When I run "/sbin/chkconfig --list httpd"
23+
Then I should see "3:on"
24+
25+
Scenario: Httpd conf should have passenger variables
26+
When I scrape "/etc/httpd/conf/httpd.conf"
27+
Then "PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-3.0.12" should be present
28+
29+
Scenario: Httpd conf should have a virtual host added
30+
When I scrape "/etc/httpd/conf/httpd.conf"
31+
Then "NameVirtualHost \*:80" should be present
32+
33+

0 commit comments

Comments
 (0)