serverspec 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,9 +26,9 @@ Or install it yourself as:
26
26
  $ serverspec-init
27
27
  + spec/
28
28
  + spec/www.example.jp/
29
+ + spec/www.example.jp/httpd_spec.rb
29
30
  + spec/spec_helper.rb
30
31
  + Rakefile
31
- + spec/www.example.jp/httpd_spec.rb
32
32
  ```
33
33
  spec/www.example.jp/httpd_spec.rb is a sample spec file and its content is like this.
34
34
 
@@ -59,6 +59,8 @@ You should create ~/.ssh/config like this before running tests.
59
59
  Host *.example.jp
60
60
  User root
61
61
  IdentityFile ~/.ssh/id_rsa
62
+ StrictHostKeyChecking no
63
+ UserKnownHostsFile /dev/null
62
64
  ```
63
65
 
64
66
  Run tests.
@@ -69,7 +71,7 @@ $ rake spec
69
71
  ----
70
72
  ## Multi OS support
71
73
 
72
- Serverspec is supporting Red Hat (and its clone) and Debian (and its clone) now.
74
+ Serverspec is supporting Red Hat based OS and Debian based OS now.
73
75
 
74
76
  If your target host's OS is Debian, you can write the spec like this.
75
77
 
@@ -126,9 +128,9 @@ describe 'www.example.jp', :os => :debian do
126
128
  end
127
129
  ```
128
130
 
129
- Or modify generated spec/spec_helper.rb like this
131
+ Or modify spec/spec_helper.rb generated by serverspec-init command like this
130
132
 
131
- ```
133
+ ```ruby
132
134
  require 'serverspec'
133
135
  require 'pathname'
134
136
 
@@ -8,7 +8,19 @@ module Serverspec
8
8
  end
9
9
 
10
10
  def check_file file
11
- "test -f #{file} 2> /dev/null"
11
+ "test -f #{file}"
12
+ end
13
+
14
+ def check_directory directory
15
+ "test -d #{directory}"
16
+ end
17
+
18
+ def check_user user
19
+ "id #{user}"
20
+ end
21
+
22
+ def check_group group
23
+ "getent group | grep -wq #{group}"
12
24
  end
13
25
 
14
26
  def check_installed package
@@ -16,15 +28,15 @@ module Serverspec
16
28
  end
17
29
 
18
30
  def check_listening port
19
- "netstat -tnl 2> /dev/null | grep ':#{port} '"
31
+ "netstat -tnl | grep ':#{port} '"
20
32
  end
21
33
 
22
34
  def check_running service
23
- "service #{service} status 2> /dev/null"
35
+ "service #{service} status"
24
36
  end
25
37
 
26
38
  def check_file_contain file, expected_pattern
27
- "\"grep -q '#{expected_pattern}' #{file} \" 2> /dev/null"
39
+ "grep -q '#{expected_pattern}' #{file} "
28
40
  end
29
41
  end
30
42
  end
@@ -2,11 +2,11 @@ module Serverspec
2
2
  module Commands
3
3
  class Debian < Base
4
4
  def check_enabled service
5
- "ls /etc/rc3.d/ 2> /dev/null | grep #{service}"
5
+ "ls /etc/rc3.d/ | grep #{service}"
6
6
  end
7
7
 
8
8
  def check_installed package
9
- "dpkg -s #{package} 2> /dev/null"
9
+ "dpkg -s #{package}"
10
10
  end
11
11
  end
12
12
  end
@@ -2,11 +2,11 @@ module Serverspec
2
2
  module Commands
3
3
  class RedHat < Base
4
4
  def check_enabled service
5
- "chkconfig --list #{service} 2> /dev/null | grep 3:on"
5
+ "chkconfig --list #{service} | grep 3:on"
6
6
  end
7
7
 
8
8
  def check_installed package
9
- "rpm -q #{package} 2> /dev/null"
9
+ "rpm -q #{package}"
10
10
  end
11
11
  end
12
12
  end
@@ -1,8 +1,51 @@
1
+ require 'net/ssh'
2
+ require 'etc'
3
+
1
4
  module Serverspec
2
5
  module Helper
3
6
  def ssh_exec(host, cmd, opt={})
4
- `ssh root@#{host} -o'StrictHostKeyChecking no' -o'UserKnownHostsFile /dev/null' #{cmd}`
7
+ options = Net::SSH::Config.for(host)
8
+ user = options[:user] || Etc.getlogin
9
+
10
+ ret = {}
11
+ Net::SSH.start(host, user, options) do |ssh|
12
+ ret = ssh_exec!(ssh, cmd)
13
+ end
14
+ ret
15
+ end
16
+
17
+ private
18
+ def ssh_exec!(ssh, command)
19
+ stdout_data = ''
20
+ stderr_data = ''
21
+ exit_code = nil
22
+ exit_signal = nil
23
+ ssh.open_channel do |channel|
24
+ channel.exec(command) do |ch, success|
25
+ unless success
26
+ abort "FAILED: couldn't execute command (ssh.channel.exec)"
27
+ end
28
+ channel.on_data do |ch,data|
29
+ stdout_data += data
30
+ end
31
+
32
+ channel.on_extended_data do |ch,type,data|
33
+ stderr_data += data
34
+ end
35
+
36
+ channel.on_request("exit-status") do |ch,data|
37
+ exit_code = data.read_long
38
+ end
39
+
40
+ channel.on_request("exit-signal") do |ch, data|
41
+ exit_signal = data.read_long
42
+ end
43
+ end
44
+ end
45
+ ssh.loop
46
+ { :stdout => stdout_data, :stderr => stderr_data, :exit_code => exit_code, :exit_signal => exit_signal }
5
47
  end
48
+
6
49
  end
7
50
 
8
51
  module RedHatHelper
@@ -1,6 +1,9 @@
1
1
  require 'serverspec/matchers/be_enabled'
2
2
  require 'serverspec/matchers/be_file'
3
+ require 'serverspec/matchers/be_directory'
3
4
  require 'serverspec/matchers/be_installed'
4
5
  require 'serverspec/matchers/be_listening'
5
6
  require 'serverspec/matchers/be_running'
6
7
  require 'serverspec/matchers/contain'
8
+ require 'serverspec/matchers/be_user'
9
+ require 'serverspec/matchers/be_group'
@@ -0,0 +1,6 @@
1
+ RSpec::Matchers.define :be_directory do
2
+ match do |actual|
3
+ ret = ssh_exec(RSpec.configuration.host, commands.check_directory(actual))
4
+ ret[:exit_code] == 0
5
+ end
6
+ end
@@ -1,6 +1,6 @@
1
1
  RSpec::Matchers.define :be_enabled do
2
2
  match do |actual|
3
- ssh_exec(RSpec.configuration.host, commands.check_enabled(actual))
4
- $? == 0
3
+ ret = ssh_exec(RSpec.configuration.host, commands.check_enabled(actual))
4
+ ret[:exit_code] == 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, commands.check_file(actual))
4
- $? == 0
3
+ ret = ssh_exec(RSpec.configuration.host, commands.check_file(actual))
4
+ ret[:exit_code] == 0
5
5
  end
6
6
  end
@@ -0,0 +1,6 @@
1
+ RSpec::Matchers.define :be_group do
2
+ match do |actual|
3
+ ret = ssh_exec(RSpec.configuration.host, commands.check_group(actual))
4
+ ret[:exit_code] == 0
5
+ end
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, commands.check_installed(actual))
4
- $? == 0
3
+ ret = ssh_exec(RSpec.configuration.host, commands.check_installed(actual))
4
+ ret[:exit_code] == 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, commands.check_listening(port))
5
- $? == 0
4
+ ret = ssh_exec(RSpec.configuration.host, commands.check_listening(port))
5
+ ret[:exit_code] == 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, commands.check_running(actual))
4
- $? == 0
3
+ ret = ssh_exec(RSpec.configuration.host, commands.check_running(actual))
4
+ ret[:exit_code] == 0
5
5
  end
6
6
  end
@@ -0,0 +1,6 @@
1
+ RSpec::Matchers.define :be_user do
2
+ match do |actual|
3
+ ret = ssh_exec(RSpec.configuration.host, commands.check_user(actual))
4
+ ret[:exit_code] == 0
5
+ end
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, commands.check_file_contain(actual, expected))
4
- $? == 0
3
+ ret = ssh_exec(RSpec.configuration.host, commands.check_file_contain(actual, expected))
4
+ ret[:exit_code] == 0
5
5
  end
6
6
  end
@@ -1,3 +1,3 @@
1
1
  module Serverspec
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -8,5 +8,8 @@ describe 'Serverspec matchers of Red Hat family', :os => :redhat do
8
8
  it_behaves_like 'support be_running matcher', 'sshd'
9
9
  it_behaves_like 'support be_listening matcher', 22
10
10
  it_behaves_like 'support be_file matcher', '/etc/ssh/sshd_config'
11
+ it_behaves_like 'support be_directory matcher', '/etc/ssh'
11
12
  it_behaves_like 'support contain matcher', '/etc/ssh/sshd_config', 'This is the sshd server system-wide configuration file'
13
+ it_behaves_like 'support be_user matcher', 'root'
14
+ it_behaves_like 'support be_group matcher', 'wheel'
12
15
  end
@@ -59,6 +59,18 @@ shared_examples_for 'support be_file matcher' do |valid_file|
59
59
  end
60
60
  end
61
61
 
62
+ shared_examples_for 'support be_directory matcher' do |valid_directory|
63
+ describe 'be_directory' do
64
+ describe valid_directory do
65
+ it { should be_directory }
66
+ end
67
+
68
+ describe '/etc/thid_is_dummy_directory' do
69
+ it { should_not be_directory }
70
+ end
71
+ end
72
+ end
73
+
62
74
  shared_examples_for 'support contain matcher' do |valid_file, pattern|
63
75
  describe 'contain' do
64
76
  describe valid_file do
@@ -70,3 +82,27 @@ shared_examples_for 'support contain matcher' do |valid_file, pattern|
70
82
  end
71
83
  end
72
84
  end
85
+
86
+ shared_examples_for 'support be_user matcher' do |valid_user|
87
+ describe 'be_user' do
88
+ describe valid_user do
89
+ it { should be_user }
90
+ end
91
+
92
+ describe 'i_am_dummy_user' do
93
+ it { should_not be_user }
94
+ end
95
+ end
96
+ end
97
+
98
+ shared_examples_for 'support be_group matcher' do |valid_group|
99
+ describe 'be_group' do
100
+ describe valid_group do
101
+ it { should be_group }
102
+ end
103
+
104
+ describe 'we_are_dummy_group' do
105
+ it { should_not be_group }
106
+ end
107
+ end
108
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serverspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-24 00:00:00.000000000 Z
12
+ date: 2013-03-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -63,11 +63,14 @@ files:
63
63
  - lib/serverspec/commands/redhat.rb
64
64
  - lib/serverspec/helper.rb
65
65
  - lib/serverspec/matchers.rb
66
+ - lib/serverspec/matchers/be_directory.rb
66
67
  - lib/serverspec/matchers/be_enabled.rb
67
68
  - lib/serverspec/matchers/be_file.rb
69
+ - lib/serverspec/matchers/be_group.rb
68
70
  - lib/serverspec/matchers/be_installed.rb
69
71
  - lib/serverspec/matchers/be_listening.rb
70
72
  - lib/serverspec/matchers/be_running.rb
73
+ - lib/serverspec/matchers/be_user.rb
71
74
  - lib/serverspec/matchers/contain.rb
72
75
  - lib/serverspec/setup.rb
73
76
  - lib/serverspec/version.rb