serverspec 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in serverspec.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Gosuke Miyashita
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # Serverspec
2
+
3
+ RSpec tests for your provisioned servers
4
+
5
+ ----
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'serverspec'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install serverspec
20
+
21
+ ----
22
+
23
+ ## Usage
24
+
25
+ ```
26
+ $ serverspec-init
27
+ + spec/
28
+ + spec/www.example.jp/
29
+ + spec/spec_helper.rb
30
+ + Rakefile
31
+ + spec/www.example.jp/httpd_spec.rb
32
+ ```
33
+ spec/www.example.jp/httpd_spec.rb is a sample spec file and its content is like this.
34
+
35
+ ```ruby
36
+ require 'spec_helper'
37
+
38
+ describe 'httpd' do
39
+ it { should be_installed }
40
+ it { should be_enabled }
41
+ it { should be_running }
42
+ end
43
+
44
+ describe 'port 80' do
45
+ it { should be_listening }
46
+ end
47
+
48
+ describe '/etc/httpd/conf/httpd.conf' do
49
+ it { should be_file }
50
+ it { should contain "ServerName www.example.jp" }
51
+ end
52
+ ```
53
+
54
+ You can write spec for testing provisioned servers like this.
55
+
56
+ You should create ~/.ssh/config like this before running tests.
57
+
58
+ ```
59
+ Host *.example.jp
60
+ User root
61
+ IdentityFile ~/.ssh/id_rsa
62
+ ```
63
+
64
+ ----
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
4
+
5
+ require 'serverspec'
6
+
7
+ Serverspec::Setup.run
data/lib/serverspec.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require 'serverspec/version'
4
+ require 'serverspec/matchers'
5
+ require 'serverspec/helper'
6
+ require 'serverspec/setup'
7
+
8
+ RSpec.configure do |c|
9
+ c.include(Serverspec::Helper)
10
+ c.add_setting :host, :default => nil
11
+ end
@@ -0,0 +1,8 @@
1
+ module Serverspec
2
+ module Helper
3
+ def ssh_exec(host, cmd, opt={})
4
+ `ssh root@#{host} -o'StrictHostKeyChecking no' -o'UserKnownHostsFile /dev/null' #{cmd}`
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,6 @@
1
+ require 'serverspec/matchers/be_enabled'
2
+ require 'serverspec/matchers/be_file'
3
+ require 'serverspec/matchers/be_installed'
4
+ require 'serverspec/matchers/be_listening'
5
+ require 'serverspec/matchers/be_running'
6
+ require 'serverspec/matchers/contain'
@@ -0,0 +1,6 @@
1
+ RSpec::Matchers.define :be_enabled do
2
+ match do |actual|
3
+ ssh_exec(RSpec.configuration.host, "chkconfig --list #{actual} 2> /dev/null | grep 3:on")
4
+ $? == 0
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ RSpec::Matchers.define :be_file do
2
+ match do |actual|
3
+ ssh_exec(RSpec.configuration.host, "test -f #{actual} 2> /dev/null")
4
+ $? == 0
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ RSpec::Matchers.define :be_installed do
2
+ match do |actual|
3
+ ssh_exec(RSpec.configuration.host, "rpm -q #{actual} 2> /dev/null")
4
+ $? == 0
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ RSpec::Matchers.define :be_listening do
2
+ match do |actual|
3
+ port = actual.gsub(/port\s+/, '')
4
+ ssh_exec(RSpec.configuration.host, "netstat -tnl 2> /dev/null | grep ':#{port} '")
5
+ $? == 0
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ RSpec::Matchers.define :be_running do
2
+ match do |actual|
3
+ ssh_exec(RSpec.configuration.host, "service #{actual} status 2> /dev/null")
4
+ $? == 0
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ RSpec::Matchers.define :contain do |expected|
2
+ match do |actual|
3
+ ssh_exec(RSpec.configuration.host, "\"grep -q '#{expected}' #{actual} \" 2> /dev/null")
4
+ $? == 0
5
+ end
6
+ end
@@ -0,0 +1,102 @@
1
+ require 'fileutils'
2
+
3
+ module Serverspec
4
+ class Setup
5
+ def self.run
6
+ %w( spec spec/www.example.jp ).each { |dir| safe_mkdir(dir) }
7
+ safe_create_spec_helper
8
+ safe_create_rakefile
9
+ safe_create_spec
10
+ end
11
+
12
+ def self.safe_create_spec
13
+ content = <<-EOF
14
+ require 'spec_helper'
15
+
16
+ describe 'httpd' do
17
+ it { should be_installed }
18
+ it { should be_enabled }
19
+ it { should be_running }
20
+ end
21
+
22
+ describe 'port 80' do
23
+ it { should be_listening }
24
+ end
25
+
26
+ describe '/etc/httpd/conf/httpd.conf' do
27
+ it { should be_file }
28
+ it { should contain "ServerName www.example.jp" }
29
+ end
30
+ EOF
31
+
32
+ if File.exists? 'spec/www.example.jp/httpd_spec.rb'
33
+ old_content = File.read('spec/www.example.jp/httpd_spec.rb')
34
+ if old_content != content
35
+ $stderr.puts "!! spec/www.example.jp/httpd_spec.rb already exists and differs from template"
36
+ end
37
+ else
38
+ File.open('spec/www.example.jp/httpd_spec.rb', 'w') do |f|
39
+ f.puts content
40
+ end
41
+ puts ' + spec/www.example.jp/httpd_spec.rb'
42
+ end
43
+ end
44
+
45
+ def self.safe_mkdir(dir)
46
+ if File.exists? dir
47
+ unless File.directory? dir
48
+ $stderr.puts "!! #{dir} already exists and is not a directory"
49
+ end
50
+ else
51
+ FileUtils.mkdir dir
52
+ puts " + #{dir}/"
53
+ end
54
+ end
55
+
56
+ def self.safe_create_spec_helper
57
+ content = <<-EOF
58
+ require 'serverspec'
59
+ require 'pathname'
60
+
61
+ RSpec.configure do |c|
62
+ c.before do
63
+ c.host = File.basename(Pathname.new(example.metadata[:location]).dirname)
64
+ end
65
+ end
66
+ EOF
67
+ if File.exists? 'spec/spec_helper.rb'
68
+ old_content = File.read('spec/spec_helper.rb')
69
+ if old_content != content
70
+ $stderr.puts "!! spec/spec_helper.rb already exists and differs from template"
71
+ end
72
+ else
73
+ File.open('spec/spec_helper.rb', 'w') do |f|
74
+ f.puts content
75
+ end
76
+ puts ' + spec/spec_helper.rb'
77
+ end
78
+ end
79
+
80
+ def self.safe_create_rakefile
81
+ content = <<-'EOF'
82
+ require 'rake'
83
+ require 'rspec/core/rake_task'
84
+
85
+ RSpec::Core::RakeTask.new(:spec) do |t|
86
+ t.pattern = 'spec/*/*_spec.rb'
87
+ end
88
+ EOF
89
+ if File.exists? 'Rakefile'
90
+ old_content = File.read('Rakefile')
91
+ if old_content != content
92
+ $stderr.puts "!! Rakefile already exists and differs from template"
93
+ end
94
+ else
95
+ File.open('Rakefile', 'w') do |f|
96
+ f.puts content
97
+ end
98
+ puts ' + Rakefile'
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,3 @@
1
+ module Serverspec
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'serverspec/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "serverspec"
8
+ spec.version = Serverspec::VERSION
9
+ spec.authors = ["Gosuke Miyashita"]
10
+ spec.email = ["gosukenator@gmail.com"]
11
+ spec.description = %q{RSpec tests for your provisioned servers}
12
+ spec.summary = %q{RSpec tests for your provisioned servers}
13
+ spec.homepage = "https://github.com/mizzy/serverspec"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serverspec
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Gosuke Miyashita
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-03-24 00:00:00 +09:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bundler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 9
30
+ segments:
31
+ - 1
32
+ - 3
33
+ version: "1.3"
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :development
49
+ version_requirements: *id002
50
+ description: RSpec tests for your provisioned servers
51
+ email:
52
+ - gosukenator@gmail.com
53
+ executables:
54
+ - serverspec-init
55
+ extensions: []
56
+
57
+ extra_rdoc_files: []
58
+
59
+ files:
60
+ - .gitignore
61
+ - Gemfile
62
+ - LICENSE.txt
63
+ - README.md
64
+ - Rakefile
65
+ - bin/serverspec-init
66
+ - lib/serverspec.rb
67
+ - lib/serverspec/helper.rb
68
+ - lib/serverspec/matchers.rb
69
+ - lib/serverspec/matchers/be_enabled.rb
70
+ - lib/serverspec/matchers/be_file.rb
71
+ - lib/serverspec/matchers/be_installed.rb
72
+ - lib/serverspec/matchers/be_listening.rb
73
+ - lib/serverspec/matchers/be_running.rb
74
+ - lib/serverspec/matchers/contain.rb
75
+ - lib/serverspec/setup.rb
76
+ - lib/serverspec/version.rb
77
+ - serverspec.gemspec
78
+ has_rdoc: true
79
+ homepage: https://github.com/mizzy/serverspec
80
+ licenses:
81
+ - MIT
82
+ post_install_message:
83
+ rdoc_options: []
84
+
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ requirements: []
106
+
107
+ rubyforge_project:
108
+ rubygems_version: 1.3.7
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: RSpec tests for your provisioned servers
112
+ test_files: []
113
+