serverspec 0.2.11 → 0.2.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ require 'open3'
2
+
1
3
  module Serverspec
2
4
  module Backend
3
5
  class Exec
@@ -10,16 +12,22 @@ module Serverspec
10
12
  end
11
13
 
12
14
  def do_check(cmd, opts={})
13
- stdout = `#{cmd} 2>&1`
14
15
  # In ruby 1.9, it is possible to use Open3.capture3, but not in 1.8
15
16
  #stdout, stderr, status = Open3.capture3(cmd)
16
- { :stdout => stdout, :stderr => nil,
17
- :exit_code => $?, :exit_signal => nil }
17
+ # So get exit status with `command`
18
+ `#{cmd} 2>&1`
19
+ ret = { :exit_status => $?, :exit_signal => nil }
20
+
21
+ # Get stdout and stderr
22
+ stdin, stdout, stderr = Open3.popen3(cmd)
23
+ ret[:stdout] = stdout.read
24
+ ret[:stderr] = stderr.read
25
+ ret
18
26
  end
19
27
 
20
28
  def check_zero(cmd, *args)
21
29
  ret = do_check(commands.send(cmd, *args))
22
- ret[:exit_code] == 0
30
+ ret[:exit_status] == 0
23
31
  end
24
32
 
25
33
  # Default action is to call check_zero with args
@@ -31,7 +39,7 @@ module Serverspec
31
39
 
32
40
  def check_installed_by_gem(example, package, version)
33
41
  ret = do_check(commands.check_installed_by_gem(package))
34
- res = ret[:exit_code] == 0
42
+ res = ret[:exit_status] == 0
35
43
  if res && version
36
44
  res = false if not ret[:stdout].match(/\(#{version}\)/)
37
45
  end
@@ -40,15 +48,15 @@ module Serverspec
40
48
 
41
49
  def check_running(example, process)
42
50
  ret = do_check(commands.check_running(process))
43
- if ret[:exit_code] == 1 || ret[:stdout] =~ /stopped/
51
+ if ret[:exit_status] == 1 || ret[:stdout] =~ /stopped/
44
52
  ret = do_check(commands.check_process(process))
45
53
  end
46
- ret[:exit_code] == 0
54
+ ret[:exit_status] == 0
47
55
  end
48
56
 
49
57
  def check_running_under_supervisor(example, process)
50
58
  ret = do_check(commands.check_running_under_supervisor(process))
51
- ret[:exit_code] == 0 && ret[:stdout] =~ /RUNNING/
59
+ ret[:exit_status] == 0 && ret[:stdout] =~ /RUNNING/
52
60
  end
53
61
 
54
62
  def check_readable(example, file, by_whom)
@@ -100,11 +108,11 @@ module Serverspec
100
108
  end
101
109
 
102
110
  def check_os
103
- if do_check('ls /etc/redhat-release')[:exit_code] == 0
111
+ if do_check('ls /etc/redhat-release')[:exit_status] == 0
104
112
  'RedHat'
105
- elsif do_check('ls /etc/debian_version')[:exit_code] == 0
113
+ elsif do_check('ls /etc/debian_version')[:exit_status] == 0
106
114
  'Debian'
107
- elsif do_check('ls /etc/gentoo-release')[:exit_code] == 0
115
+ elsif do_check('ls /etc/gentoo-release')[:exit_status] == 0
108
116
  'Gentoo'
109
117
  elsif do_check('uname -s')[:stdout] =~ /SunOS/i
110
118
  'Solaris'
@@ -12,7 +12,7 @@ module Serverspec
12
12
  def ssh_exec!(command)
13
13
  stdout_data = ''
14
14
  stderr_data = ''
15
- exit_code = nil
15
+ exit_status = nil
16
16
  exit_signal = nil
17
17
 
18
18
  ssh = RSpec.configuration.ssh
@@ -35,7 +35,7 @@ module Serverspec
35
35
  end
36
36
 
37
37
  channel.on_request("exit-status") do |ch, data|
38
- exit_code = data.read_long
38
+ exit_status = data.read_long
39
39
  end
40
40
 
41
41
  channel.on_request("exit-signal") do |ch, data|
@@ -44,7 +44,7 @@ module Serverspec
44
44
  end
45
45
  end
46
46
  ssh.loop
47
- { :stdout => stdout_data, :stderr => stderr_data, :exit_code => exit_code, :exit_signal => exit_signal }
47
+ { :stdout => stdout_data, :stderr => stderr_data, :exit_status => exit_status, :exit_signal => exit_signal }
48
48
  end
49
49
  end
50
50
  end
@@ -6,7 +6,7 @@ RSpec::Matchers.define :contain do |pattern|
6
6
  cmd = backend.commands.check_file_contain_within(file, pattern, @from, @to)
7
7
  end
8
8
  ret = backend.do_check(cmd)
9
- ret[:exit_code] == 0
9
+ ret[:exit_status] == 0
10
10
  end
11
11
  # for contain(pattern).from(/A/).to(/B/)
12
12
  chain :from do |from|
@@ -0,0 +1,6 @@
1
+ RSpec::Matchers.define :return_exit_status do |status|
2
+ match do |command|
3
+ ret = backend.do_check(command)
4
+ ret[:exit_status].to_i == status
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ RSpec::Matchers.define :return_stderr do |content|
2
+ match do |command|
3
+ ret = backend.do_check(command)
4
+ if content.instance_of?(Regexp)
5
+ ret[:stderr] =~ content
6
+ else
7
+ ret[:stderr].strip == content
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ RSpec::Matchers.define :return_stdout do |content|
2
+ match do |command|
3
+ ret = backend.do_check(command)
4
+ if content.instance_of?(Regexp)
5
+ ret[:stdout] =~ content
6
+ else
7
+ ret[:stdout].strip == content
8
+ end
9
+ end
10
+ end
@@ -24,3 +24,6 @@ require 'serverspec/matchers/have_ipfilter_rule'
24
24
  require 'serverspec/matchers/have_ipnat_rule'
25
25
  require 'serverspec/matchers/have_svcprop'
26
26
  require 'serverspec/matchers/have_svcprops'
27
+ require 'serverspec/matchers/return_exit_status'
28
+ require 'serverspec/matchers/return_stdout'
29
+ require 'serverspec/matchers/return_stderr'
@@ -1,3 +1,3 @@
1
1
  module Serverspec
2
- VERSION = "0.2.11"
2
+ VERSION = "0.2.12"
3
3
  end
@@ -51,4 +51,12 @@ describe 'Serverspec matchers of Debian family', :os => :debian do
51
51
  it_behaves_like 'support be_executable_by_owner matcher', '/dev'
52
52
  it_behaves_like 'support be_executable_by_group matcher', '/dev'
53
53
  it_behaves_like 'support be_executable_by_others matcher', '/dev'
54
+
55
+ it_behaves_like 'support return_exit_status matcher', 'ls /tmp', 0
56
+
57
+ it_behaves_like 'support return_stdout matcher', 'cat /etc/resolv.conf', 'localhost'
58
+ it_behaves_like 'support return_stdout matcher with regexp', 'cat /etc/resolv.conf', /localhost/
59
+
60
+ it_behaves_like 'support return_stderr matcher', 'cat /foo', 'cat: /foo: No such file or directory'
61
+ it_behaves_like 'support return_stderr matcher with regexp', 'cat /foo', /No such file or directory/
54
62
  end
@@ -52,4 +52,12 @@ describe 'Serverspec matchers of Gentoo family', :os => :gentoo do
52
52
  it_behaves_like 'support be_executable_by_owner matcher', '/dev'
53
53
  it_behaves_like 'support be_executable_by_group matcher', '/dev'
54
54
  it_behaves_like 'support be_executable_by_others matcher', '/dev'
55
+
56
+ it_behaves_like 'support return_exit_status matcher', 'ls /tmp', 0
57
+
58
+ it_behaves_like 'support return_stdout matcher', 'cat /etc/resolv.conf', 'localhost'
59
+ it_behaves_like 'support return_stdout matcher with regexp', 'cat /etc/resolv.conf', /localhost/
60
+
61
+ it_behaves_like 'support return_stderr matcher', 'cat /foo', 'cat: /foo: No such file or directory'
62
+ it_behaves_like 'support return_stderr matcher with regexp', 'cat /foo', /No such file or directory/
55
63
  end
@@ -53,4 +53,12 @@ describe 'Serverspec matchers of Red Hat family', :os => :redhat do
53
53
  it_behaves_like 'support be_executable_by_owner matcher', '/dev'
54
54
  it_behaves_like 'support be_executable_by_group matcher', '/dev'
55
55
  it_behaves_like 'support be_executable_by_others matcher', '/dev'
56
+
57
+ it_behaves_like 'support return_exit_status matcher', 'ls /tmp', 0
58
+
59
+ it_behaves_like 'support return_stdout matcher', 'cat /etc/resolv.conf', 'localhost'
60
+ it_behaves_like 'support return_stdout matcher with regexp', 'cat /etc/resolv.conf', /localhost/
61
+
62
+ it_behaves_like 'support return_stderr matcher', 'cat /foo', 'cat: /foo: No such file or directory'
63
+ it_behaves_like 'support return_stderr matcher with regexp', 'cat /foo', /No such file or directory/
56
64
  end
@@ -58,4 +58,12 @@ describe 'Serverspec matchers of Solaris family', :os => :solaris do
58
58
 
59
59
  it_behaves_like 'support have_svcprop.with_value matcher', 'svc:/network/http:apache22', 'httpd/server_type', 'worker'
60
60
  it_behaves_like 'support have_svcprops matcher', 'svc:/network/http:apache22', {'httpd/enable_64bit' => 'false', 'httpd/server_type' => 'worker' }
61
+
62
+ it_behaves_like 'support return_exit_status matcher', 'ls /tmp', 0
63
+
64
+ it_behaves_like 'support return_stdout matcher', 'cat /etc/resolv.conf', 'localhost'
65
+ it_behaves_like 'support return_stdout matcher with regexp', 'cat /etc/resolv.conf', /localhost/
66
+
67
+ it_behaves_like 'support return_stderr matcher', 'cat /foo', 'cat: /foo: No such file or directory'
68
+ it_behaves_like 'support return_stderr matcher with regexp', 'cat /foo', /No such file or directory/
61
69
  end
data/spec/spec_helper.rb CHANGED
@@ -10,9 +10,19 @@ module Serverspec
10
10
  class Ssh
11
11
  def do_check(cmd)
12
12
  if cmd =~ /invalid/
13
- { :stdout => '', :stderr => '', :exit_code => 1, :exit_signal => nil }
13
+ {
14
+ :stdout => ::RSpec.configuration.stdout,
15
+ :stderr => ::RSpec.configuration.stderr,
16
+ :exit_status => 1,
17
+ :exit_signal => nil
18
+ }
14
19
  else
15
- { :stdout => ::RSpec.configuration.stdout, :stderr => '', :exit_code => 0, :exit_signal => nil }
20
+ {
21
+ :stdout => ::RSpec.configuration.stdout,
22
+ :stderr => ::RSpec.configuration.stderr,
23
+ :exit_status => 0,
24
+ :exit_signal => nil
25
+ }
16
26
  end
17
27
  end
18
28
  end
@@ -21,4 +31,5 @@ end
21
31
 
22
32
  RSpec.configure do |c|
23
33
  c.add_setting :stdout, :default => ''
34
+ c.add_setting :stderr, :default => ''
24
35
  end
@@ -679,3 +679,112 @@ shared_examples_for 'support have_svcprops matcher' do |svc, property|
679
679
  end
680
680
  end
681
681
  end
682
+
683
+ shared_examples_for 'support return_exit_status matcher' do |command, status|
684
+ describe 'return_exit_status' do
685
+ describe command do
686
+ it { should return_exit_status(status) }
687
+ end
688
+
689
+ describe 'this-is-invalid-command' do
690
+ it { should_not return_exit_status(status) }
691
+ end
692
+ end
693
+ end
694
+
695
+ shared_examples_for 'support return_stdout matcher' do |command, content|
696
+ describe 'return_stdout' do
697
+ describe command do
698
+ before :all do
699
+ RSpec.configure do |c|
700
+ c.stdout = "#{content}\r\n"
701
+ end
702
+ end
703
+ it { should return_stdout(content) }
704
+ end
705
+
706
+ describe command do
707
+ before :all do
708
+ RSpec.configure do |c|
709
+ c.stdout = "foo#{content}bar\r\n"
710
+ end
711
+ end
712
+ it { should_not return_stdout(content) }
713
+ end
714
+
715
+
716
+ describe 'this-is-invalid-command' do
717
+ it { should_not return_stdout(content) }
718
+ end
719
+ end
720
+ end
721
+
722
+ shared_examples_for 'support return_stdout matcher with regexp' do |command, content|
723
+ describe 'return_stdout' do
724
+ describe command do
725
+ before :all do
726
+ RSpec.configure do |c|
727
+ c.stdout = "foo#{content}bar\r\n"
728
+ end
729
+ end
730
+ it { should return_stdout(content) }
731
+ end
732
+
733
+ describe command do
734
+ before :all do
735
+ RSpec.configure do |c|
736
+ c.stdout = "foobar\r\n"
737
+ end
738
+ end
739
+ it { should_not return_stdout(content) }
740
+ end
741
+
742
+ describe 'this-is-invalid-command' do
743
+ it { should_not return_stdout(content) }
744
+ end
745
+ end
746
+ end
747
+
748
+ shared_examples_for 'support return_stderr matcher' do |command, content|
749
+ describe 'return_stderr' do
750
+ describe command do
751
+ before :all do
752
+ RSpec.configure do |c|
753
+ c.stderr = "#{content}\r\n"
754
+ end
755
+ end
756
+ it { should return_stderr(content) }
757
+ end
758
+
759
+ describe command do
760
+ before :all do
761
+ RSpec.configure do |c|
762
+ c.stderr = "No such file or directory\r\n"
763
+ end
764
+ end
765
+ it { should_not return_stderr(content) }
766
+ end
767
+ end
768
+ end
769
+
770
+ shared_examples_for 'support return_stderr matcher with regexp' do |command, content|
771
+ describe 'return_stderr' do
772
+ describe command do
773
+ before :all do
774
+ RSpec.configure do |c|
775
+ c.stdout = "cat: /foo: No such file or directory\r\n"
776
+ end
777
+ end
778
+ it { should return_stdout(content) }
779
+ end
780
+
781
+ describe command do
782
+ before :all do
783
+ RSpec.configure do |c|
784
+ c.stdout = "foobar\r\n"
785
+ end
786
+ end
787
+ it { should_not return_stdout(content) }
788
+ end
789
+ end
790
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serverspec
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 11
10
- version: 0.2.11
9
+ - 12
10
+ version: 0.2.12
11
11
  platform: ruby
12
12
  authors:
13
13
  - Gosuke Miyashita
@@ -153,6 +153,9 @@ files:
153
153
  - lib/serverspec/matchers/have_iptables_rule.rb
154
154
  - lib/serverspec/matchers/have_svcprop.rb
155
155
  - lib/serverspec/matchers/have_svcprops.rb
156
+ - lib/serverspec/matchers/return_exit_status.rb
157
+ - lib/serverspec/matchers/return_stderr.rb
158
+ - lib/serverspec/matchers/return_stdout.rb
156
159
  - lib/serverspec/setup.rb
157
160
  - lib/serverspec/version.rb
158
161
  - serverspec.gemspec