rspec-infrastructure 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ pkg/*
2
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
@@ -0,0 +1,6 @@
1
+ Infrastructure testing with rspec
2
+
3
+
4
+ require 'rspec/infrastructure'
5
+
6
+
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ Bundler.require :development
6
+
7
+ RSpec::Core::RakeTask.new(:spec) do |task|
8
+ task.rspec_opts = [ '-c', '-f documentation' ]
9
+ task.pattern = 'spec/**/*_spec.rb'
10
+ end
11
+
12
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ require 'rspec/infrastructure/dsl'
2
+ require 'rspec/infrastructure/nagios_matchers'
3
+ require 'rspec/infrastructure/host_manager'
@@ -0,0 +1,36 @@
1
+ module RSpec
2
+ module Core
3
+ #class ExampleGroup
4
+
5
+ module SharedExampleGroup
6
+ alias_method :application, :shared_examples
7
+ end
8
+
9
+ module DSL
10
+ def environment(*args, &block)
11
+ *args = [ 'asdasd Shrubbery', { :environment => :shrubbery }]
12
+ env = RSpec::Core::ExampleGroup.describe(*args, &block)
13
+ env.register
14
+ end
15
+
16
+ def roles(roles, &block)
17
+ roles.each do |role|
18
+ Nagios::HostManager.hostnames_for_roles(metadata[:environment], role).each do |hostname|
19
+ *args = [ "Host: #{hostname}", { :host => hostname }]
20
+ bleh = RSpec::Core::ExampleGroup.describe(*args, &block)
21
+ bleh.before :all do
22
+ @host_manager = Nagios::HostManager.new
23
+ end
24
+ bleh.after :all do
25
+ @host_manager.hosts.each { |hostname, host| host.ssh.close }
26
+ end
27
+ bleh.before do
28
+ @host = @host_manager.manage(hostname)
29
+ end
30
+ bleh.register
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,41 @@
1
+ require 'awesome_print'
2
+ require 'hashie'
3
+ require 'net/ssh/simple'
4
+
5
+ module Nagios
6
+
7
+ class HostManager
8
+
9
+ attr_reader :hosts
10
+
11
+ def initialize
12
+ @hosts = {}
13
+ end
14
+
15
+ def manage hostname
16
+ @hosts[hostname] ||= Host.new(hostname)
17
+ end
18
+
19
+ def self.hostnames_for_roles environment, roles
20
+ [ *roles ].map do |role|
21
+ $hosts[environment][role]
22
+ end.flatten
23
+ end
24
+ end
25
+
26
+ class Host
27
+
28
+ attr_reader :hostname, :ssh
29
+
30
+ def initialize(hostname)
31
+ @hostname = hostname
32
+ @user = 'deploy'
33
+ @keys = [ '/Users/james/.ssh/id_rsa' ]
34
+ @ssh = Net::SSH::Simple.new
35
+ end
36
+
37
+ def exec(command)
38
+ Hashie::Mash.new(@ssh.ssh(hostname, command, { :user => @user, :keys => @keys, :timeout => 300 }))
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,53 @@
1
+ require 'socket'
2
+ require 'timeout'
3
+
4
+ RSpec::Matchers.define :have_remote_file do |file, options|
5
+
6
+ options ||= {}.reverse_merge!({ :sudo => false })
7
+
8
+ match do |host|
9
+ command = "[ -f #{file} ]"
10
+ command = "sudo #{command}" if options.fetch(:sudo)
11
+ host.exec(command).exit_code == 0
12
+ end
13
+
14
+ failure_message_for_should do |host|
15
+ "#{host.hostname} is missing #{@expected.first} "
16
+ end
17
+ end
18
+
19
+ RSpec::Matchers.define :listen_to_port do |port, options|
20
+
21
+ match do |host|
22
+ port_open?(host.hostname, port)
23
+ end
24
+
25
+ failure_message_for_should do |host|
26
+ "#{host.hostname} is not listening on port #{@expected.first} "
27
+ end
28
+ end
29
+
30
+ RSpec::Matchers.define :listen_on_local_port do |port, options|
31
+
32
+ match do |host|
33
+ command = "nc -z #{host.hostname} #{port}"
34
+ host.exec(command).exit_code == 0
35
+ end
36
+
37
+ failure_message_for_should do |host|
38
+ "#{host.hostname} is not listening on local port #{@expected.first} "
39
+ end
40
+ end
41
+
42
+ def port_open?(ip, port, seconds=1)
43
+ Timeout::timeout(seconds) do
44
+ begin
45
+ TCPSocket.new(ip, port).close
46
+ true
47
+ rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
48
+ false
49
+ end
50
+ end
51
+ rescue Timeout::Error
52
+ false
53
+ end
@@ -0,0 +1,5 @@
1
+ module RSpec
2
+ module Infrastructure
3
+ VERSION = '0.1'
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path('../lib/rspec/infrastructure/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = "rspec-infrastructure"
5
+ gem.version = RSpec::Infrastructure::VERSION
6
+ gem.authors = [ "James Martelletti", "Ash McKenzie" ]
7
+ gem.email = [ "james@hooroo.com", "ash@hooroo.com" ]
8
+
9
+ gem.summary = "Test your application infrastructure and environments with rspec"
10
+ gem.description = "Perform high level specification and testing of your application infrastructure and environments using familiar rspec syntax and matchers"
11
+ gem.homepage = "http://github.com/hooroo/rspec-infrastructure"
12
+
13
+ gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
14
+ gem.files = `git ls-files`.split("\n")
15
+ gem.test_files = `git ls-files -- spec/*`.split("\n")
16
+
17
+ gem.add_dependency 'rspec', '~>2.11'
18
+ gem.add_development_dependency 'rake', '~>0.9'
19
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-infrastructure
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - James Martelletti
9
+ - Ash McKenzie
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-11-27 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '2.11'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '2.11'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '0.9'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '0.9'
47
+ description: Perform high level specification and testing of your application infrastructure
48
+ and environments using familiar rspec syntax and matchers
49
+ email:
50
+ - james@hooroo.com
51
+ - ash@hooroo.com
52
+ executables: []
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - .gitignore
57
+ - Gemfile
58
+ - README.md
59
+ - Rakefile
60
+ - lib/rspec/infrastructure.rb
61
+ - lib/rspec/infrastructure/dsl.rb
62
+ - lib/rspec/infrastructure/host_manager.rb
63
+ - lib/rspec/infrastructure/nagios_matchers.rb
64
+ - lib/rspec/infrastructure/version.rb
65
+ - rspec-infrastructure.gemspec
66
+ homepage: http://github.com/hooroo/rspec-infrastructure
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ segments:
79
+ - 0
80
+ hash: -2192991719490512288
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ segments:
88
+ - 0
89
+ hash: -2192991719490512288
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.23
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Test your application infrastructure and environments with rspec
96
+ test_files: []