serverspec 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,6 +3,7 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
+ .rspec
6
7
  Gemfile.lock
7
8
  InstalledFiles
8
9
  _yardoc
@@ -15,3 +16,4 @@ spec/reports
15
16
  test/tmp
16
17
  test/version_tmp
17
18
  tmp
19
+ Vagrantfile
data/README.md CHANGED
@@ -35,17 +35,17 @@ spec/www.example.jp/httpd_spec.rb is a sample spec file and its content is like
35
35
  ```ruby
36
36
  require 'spec_helper'
37
37
 
38
- describe 'httpd' do
38
+ describe 'httpd', :os => :redhat do
39
39
  it { should be_installed }
40
40
  it { should be_enabled }
41
41
  it { should be_running }
42
42
  end
43
43
 
44
- describe 'port 80' do
44
+ describe 'port 80', :os => :redhat do
45
45
  it { should be_listening }
46
46
  end
47
47
 
48
- describe '/etc/httpd/conf/httpd.conf' do
48
+ describe '/etc/httpd/conf/httpd.conf', :os => :redhat do
49
49
  it { should be_file }
50
50
  it { should contain "ServerName www.example.jp" }
51
51
  end
@@ -61,6 +61,109 @@ Host *.example.jp
61
61
  IdentityFile ~/.ssh/id_rsa
62
62
  ```
63
63
 
64
+ Run tests.
65
+
66
+ ```
67
+ $ rake spec
68
+ ```
69
+ ----
70
+ ## Multi OS support
71
+
72
+ Serverspec is supporting Red Hat (and its clone) and Debian (and its clone) now.
73
+
74
+ If your target host's OS is Debian, you can write the spec like this.
75
+
76
+ ```ruby
77
+ require 'spec_helper'
78
+
79
+ describe 'httpd', :os => :debian do
80
+ it { should be_installed }
81
+ it { should be_enabled }
82
+ it { should be_running }
83
+ end
84
+
85
+ describe 'port 80', :os => :debian do
86
+ it { should be_listening }
87
+ end
88
+
89
+ describe '/etc/httpd/conf/httpd.conf', :os => :debian do
90
+ it { should be_file }
91
+ it { should contain "ServerName www.example.jp" }
92
+ end
93
+ ```
94
+
95
+ Or like this.
96
+
97
+ ```ruby
98
+ require 'spec_helper'
99
+
100
+ describe 'www.example.jp', :os => :debian do
101
+ it do
102
+ 'httpd'.should be_installed
103
+ end
104
+
105
+ it do
106
+ 'httpd'.should be_enabled
107
+ end
108
+
109
+ it do
110
+ 'httpd'.should be_running
111
+ end
112
+
113
+ it do
114
+ 'port 80'.should be_listening
115
+ end
116
+
117
+ conf = '/etc/httpd/conf/httpd.conf'
118
+
119
+ it do
120
+ conf.should be_file
121
+ end
122
+
123
+ it do
124
+ conf.should contain "ServerName www.example.jp"
125
+ end
126
+ end
127
+ ```
128
+
129
+ Or modify generated spec/spec_helper.rb like this
130
+
131
+ ```
132
+ require 'serverspec'
133
+ require 'pathname'
134
+
135
+ RSpec.configure do |c|
136
+ c.include(Serverspec::DebianHelper)
137
+ c.before do
138
+ c.host = File.basename(Pathname.new(example.metadata[:location]).dirname)
139
+ end
140
+ end
141
+ ```
142
+
143
+ And write the spec like this.
144
+
145
+ ```ruby
146
+ require 'spec_helper'
147
+
148
+ describe 'httpd' do
149
+ it { should be_installed }
150
+ it { should be_enabled }
151
+ it { should be_running }
152
+ end
153
+
154
+ describe 'port 80' do
155
+ it { should be_listening }
156
+ end
157
+
158
+ describe '/etc/httpd/conf/httpd.conf' do
159
+ it { should be_file }
160
+ it { should contain "ServerName www.example.jp" }
161
+ end
162
+ ```
163
+
164
+ Choose any style you like.
165
+
166
+
64
167
  ----
65
168
 
66
169
  ## Contributing
data/Rakefile CHANGED
@@ -1 +1,4 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
data/lib/serverspec.rb CHANGED
@@ -4,8 +4,13 @@ require 'serverspec/version'
4
4
  require 'serverspec/matchers'
5
5
  require 'serverspec/helper'
6
6
  require 'serverspec/setup'
7
+ require 'serverspec/commands/base'
8
+ require 'serverspec/commands/redhat'
9
+ require 'serverspec/commands/debian'
7
10
 
8
11
  RSpec.configure do |c|
9
12
  c.include(Serverspec::Helper)
13
+ c.include(Serverspec::RedHatHelper, :os => :redhat)
14
+ c.include(Serverspec::DebianHelper, :os => :debian)
10
15
  c.add_setting :host, :default => nil
11
16
  end
@@ -0,0 +1,31 @@
1
+ module Serverspec
2
+ module Commands
3
+ class Base
4
+ class NotImplementedError < Exception; end
5
+
6
+ def check_enabled service
7
+ raise NotImplementedError.new
8
+ end
9
+
10
+ def check_file file
11
+ "test -f #{file} 2> /dev/null"
12
+ end
13
+
14
+ def check_installed package
15
+ raise NotImplementedError.new
16
+ end
17
+
18
+ def check_listening port
19
+ "netstat -tnl 2> /dev/null | grep ':#{port} '"
20
+ end
21
+
22
+ def check_running service
23
+ "service #{service} status 2> /dev/null"
24
+ end
25
+
26
+ def check_file_contain file, expected_pattern
27
+ "\"grep -q '#{expected_pattern}' #{file} \" 2> /dev/null"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,13 @@
1
+ module Serverspec
2
+ module Commands
3
+ class Debian < Base
4
+ def check_enabled service
5
+ "ls /etc/rc3.d/ 2> /dev/null | grep #{service}"
6
+ end
7
+
8
+ def check_installed package
9
+ "dpkg -s #{package} 2> /dev/null"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Serverspec
2
+ module Commands
3
+ class RedHat < Base
4
+ def check_enabled service
5
+ "chkconfig --list #{service} 2> /dev/null | grep 3:on"
6
+ end
7
+
8
+ def check_installed package
9
+ "rpm -q #{package} 2> /dev/null"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -4,5 +4,16 @@ module Serverspec
4
4
  `ssh root@#{host} -o'StrictHostKeyChecking no' -o'UserKnownHostsFile /dev/null' #{cmd}`
5
5
  end
6
6
  end
7
- end
8
7
 
8
+ module RedHatHelper
9
+ def commands
10
+ Serverspec::Commands::RedHat.new
11
+ end
12
+ end
13
+
14
+ module DebianHelper
15
+ def commands
16
+ Serverspec::Commands::Debian.new
17
+ end
18
+ end
19
+ end
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :be_enabled do
2
2
  match do |actual|
3
- ssh_exec(RSpec.configuration.host, "chkconfig --list #{actual} 2> /dev/null | grep 3:on")
3
+ ssh_exec(RSpec.configuration.host, commands.check_enabled(actual))
4
4
  $? == 0
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :be_file do
2
2
  match do |actual|
3
- ssh_exec(RSpec.configuration.host, "test -f #{actual} 2> /dev/null")
3
+ ssh_exec(RSpec.configuration.host, commands.check_file(actual))
4
4
  $? == 0
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :be_installed do
2
2
  match do |actual|
3
- ssh_exec(RSpec.configuration.host, "rpm -q #{actual} 2> /dev/null")
3
+ ssh_exec(RSpec.configuration.host, commands.check_installed(actual))
4
4
  $? == 0
5
5
  end
6
6
  end
@@ -1,7 +1,7 @@
1
1
  RSpec::Matchers.define :be_listening do
2
2
  match do |actual|
3
3
  port = actual.gsub(/port\s+/, '')
4
- ssh_exec(RSpec.configuration.host, "netstat -tnl 2> /dev/null | grep ':#{port} '")
4
+ ssh_exec(RSpec.configuration.host, commands.check_listening(port))
5
5
  $? == 0
6
6
  end
7
7
  end
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :be_running do
2
2
  match do |actual|
3
- ssh_exec(RSpec.configuration.host, "service #{actual} status 2> /dev/null")
3
+ ssh_exec(RSpec.configuration.host, commands.check_running(actual))
4
4
  $? == 0
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :contain do |expected|
2
2
  match do |actual|
3
- ssh_exec(RSpec.configuration.host, "\"grep -q '#{expected}' #{actual} \" 2> /dev/null")
3
+ ssh_exec(RSpec.configuration.host, commands.check_file_contain(actual, expected))
4
4
  $? == 0
5
5
  end
6
6
  end
@@ -4,26 +4,26 @@ module Serverspec
4
4
  class Setup
5
5
  def self.run
6
6
  %w( spec spec/www.example.jp ).each { |dir| safe_mkdir(dir) }
7
+ safe_create_spec
7
8
  safe_create_spec_helper
8
9
  safe_create_rakefile
9
- safe_create_spec
10
10
  end
11
11
 
12
12
  def self.safe_create_spec
13
13
  content = <<-EOF
14
14
  require 'spec_helper'
15
15
 
16
- describe 'httpd' do
16
+ describe 'httpd', :os => :redhat do
17
17
  it { should be_installed }
18
18
  it { should be_enabled }
19
19
  it { should be_running }
20
20
  end
21
21
 
22
- describe 'port 80' do
22
+ describe 'port 80', :os => :redhat do
23
23
  it { should be_listening }
24
24
  end
25
25
 
26
- describe '/etc/httpd/conf/httpd.conf' do
26
+ describe '/etc/httpd/conf/httpd.conf', :os => :redhat do
27
27
  it { should be_file }
28
28
  it { should contain "ServerName www.example.jp" }
29
29
  end
@@ -1,3 +1,3 @@
1
1
  module Serverspec
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Serverspec matchers of Debian family', :os => :debian do
4
+ let(:test_server_host) { 'serverspec-debian-host' }
5
+
6
+ it_behaves_like 'support be_enabled matcher', 'rc.local'
7
+ it_behaves_like 'support be_installed matcher', 'openssh-server'
8
+ it_behaves_like 'support be_running matcher', 'ssh'
9
+ it_behaves_like 'support be_listening matcher', 22
10
+ it_behaves_like 'support be_file matcher', '/etc/ssh/sshd_config'
11
+ it_behaves_like 'support contain matcher', '/etc/ssh/sshd_config', 'See the sshd_config(5) manpage'
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Serverspec matchers of Red Hat family', :os => :redhat do
4
+ let(:test_server_host) { 'serverspec-redhat-host' }
5
+
6
+ it_behaves_like 'support be_enabled matcher', 'sshd'
7
+ it_behaves_like 'support be_installed matcher', 'openssh'
8
+ it_behaves_like 'support be_running matcher', 'sshd'
9
+ it_behaves_like 'support be_listening matcher', 22
10
+ it_behaves_like 'support be_file matcher', '/etc/ssh/sshd_config'
11
+ it_behaves_like 'support contain matcher', '/etc/ssh/sshd_config', 'This is the sshd server system-wide configuration file'
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'serverspec'
2
+ require 'pathname'
3
+
4
+ PROJECT_ROOT = (Pathname.new(File.dirname(__FILE__)) + '..').expand_path
5
+
6
+ Dir[PROJECT_ROOT.join("spec/support/**/*.rb")].each { |file| require(file) }
7
+
8
+ RSpec.configure do |c|
9
+ c.before do
10
+ c.host = test_server_host
11
+ end
12
+ end
@@ -0,0 +1,72 @@
1
+
2
+ shared_examples_for 'support be_enabled matcher' do |valid_service|
3
+ describe 'be_enabled' do
4
+ describe valid_service do
5
+ it { should be_enabled }
6
+ end
7
+
8
+ describe 'this-is-dummy-service' do
9
+ it { should_not be_enabled }
10
+ end
11
+ end
12
+ end
13
+
14
+ shared_examples_for 'support be_installed matcher' do |valid_package|
15
+ describe 'be_installed' do
16
+ describe valid_package do
17
+ it { should be_installed }
18
+ end
19
+
20
+ describe 'this-is-dummy-package' do
21
+ it { should_not be_installed }
22
+ end
23
+ end
24
+ end
25
+
26
+ shared_examples_for 'support be_running matcher' do |valid_service|
27
+ describe 'be_running' do
28
+ describe valid_service do
29
+ it { should be_running }
30
+ end
31
+
32
+ describe 'this-is-dummy-daemon' do
33
+ it { should_not be_running }
34
+ end
35
+ end
36
+ end
37
+
38
+ shared_examples_for 'support be_listening matcher' do |valid_port|
39
+ describe 'be_listening' do
40
+ describe "port #{ valid_port }" do
41
+ it { should be_listening }
42
+ end
43
+
44
+ describe 'port 9999' do
45
+ it { should_not be_listening }
46
+ end
47
+ end
48
+ end
49
+
50
+ shared_examples_for 'support be_file matcher' do |valid_file|
51
+ describe 'be_file' do
52
+ describe valid_file do
53
+ it { should be_file }
54
+ end
55
+
56
+ describe '/etc/thid_is_dummy_file' do
57
+ it { should_not be_file }
58
+ end
59
+ end
60
+ end
61
+
62
+ shared_examples_for 'support contain matcher' do |valid_file, pattern|
63
+ describe 'contain' do
64
+ describe valid_file do
65
+ it { should contain pattern }
66
+ end
67
+
68
+ describe '/etc/ssh/sshd_config' do
69
+ it { should_not contain 'This is duymmy text!!' }
70
+ end
71
+ end
72
+ end
metadata CHANGED
@@ -1,62 +1,56 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
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
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Gosuke Miyashita
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2013-03-24 00:00:00 +09:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2013-03-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: bundler
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
18
+ requirements:
27
19
  - - ~>
28
- - !ruby/object:Gem::Version
29
- hash: 9
30
- segments:
31
- - 1
32
- - 3
33
- version: "1.3"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
34
22
  type: :development
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: rake
38
23
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
40
33
  none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 3
45
- segments:
46
- - 0
47
- version: "0"
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
48
38
  type: :development
49
- version_requirements: *id002
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
50
46
  description: RSpec tests for your provisioned servers
51
- email:
47
+ email:
52
48
  - gosukenator@gmail.com
53
- executables:
49
+ executables:
54
50
  - serverspec-init
55
51
  extensions: []
56
-
57
52
  extra_rdoc_files: []
58
-
59
- files:
53
+ files:
60
54
  - .gitignore
61
55
  - Gemfile
62
56
  - LICENSE.txt
@@ -64,6 +58,9 @@ files:
64
58
  - Rakefile
65
59
  - bin/serverspec-init
66
60
  - lib/serverspec.rb
61
+ - lib/serverspec/commands/base.rb
62
+ - lib/serverspec/commands/debian.rb
63
+ - lib/serverspec/commands/redhat.rb
67
64
  - lib/serverspec/helper.rb
68
65
  - lib/serverspec/matchers.rb
69
66
  - lib/serverspec/matchers/be_enabled.rb
@@ -75,39 +72,37 @@ files:
75
72
  - lib/serverspec/setup.rb
76
73
  - lib/serverspec/version.rb
77
74
  - serverspec.gemspec
78
- has_rdoc: true
75
+ - spec/debian/matchers_spec.rb
76
+ - spec/redhat/matchers_spec.rb
77
+ - spec/spec_helper.rb
78
+ - spec/support/shared_matcher_examples.rb
79
79
  homepage: https://github.com/mizzy/serverspec
80
- licenses:
80
+ licenses:
81
81
  - MIT
82
82
  post_install_message:
83
83
  rdoc_options: []
84
-
85
- require_paths:
84
+ require_paths:
86
85
  - lib
87
- required_ruby_version: !ruby/object:Gem::Requirement
86
+ required_ruby_version: !ruby/object:Gem::Requirement
88
87
  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
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
93
  none: false
98
- requirements:
99
- - - ">="
100
- - !ruby/object:Gem::Version
101
- hash: 3
102
- segments:
103
- - 0
104
- version: "0"
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
105
98
  requirements: []
106
-
107
99
  rubyforge_project:
108
- rubygems_version: 1.3.7
100
+ rubygems_version: 1.8.25
109
101
  signing_key:
110
102
  specification_version: 3
111
103
  summary: RSpec tests for your provisioned servers
112
- test_files: []
113
-
104
+ test_files:
105
+ - spec/debian/matchers_spec.rb
106
+ - spec/redhat/matchers_spec.rb
107
+ - spec/spec_helper.rb
108
+ - spec/support/shared_matcher_examples.rb