serverspec 0.1.4 → 0.1.5

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.
@@ -43,6 +43,13 @@ module Serverspec
43
43
  "grep -q '#{expected_pattern}' #{file}"
44
44
  end
45
45
 
46
+ def check_file_contain_within file, expected_pattern, from=nil, to=nil
47
+ from ||= '1'
48
+ to ||= '$'
49
+ checker = check_file_contain("-", expected_pattern)
50
+ "sed -n '#{from},#{to}p' #{file} | #{checker}"
51
+ end
52
+
46
53
  def check_mode file, mode
47
54
  "stat -c %a #{file} | grep #{mode}"
48
55
  end
@@ -1,6 +1,26 @@
1
- RSpec::Matchers.define :contain do |expected|
2
- match do |actual|
3
- ret = ssh_exec(commands.check_file_contain(actual, expected))
1
+ RSpec::Matchers.define :contain do |pattern|
2
+ match do |file|
3
+ if (@from || @to).nil?
4
+ cmd = commands.check_file_contain(file, pattern)
5
+ else
6
+ cmd = commands.check_file_contain_within(file, pattern, @from, @to)
7
+ end
8
+ ret = ssh_exec(cmd)
4
9
  ret[:exit_code] == 0
5
10
  end
11
+ # for contain(pattern).from(/A/).to(/B/)
12
+ chain :from do |from|
13
+ @from = Regexp.new(from).inspect
14
+ end
15
+ chain :to do |to|
16
+ @to = Regexp.new(to).inspect
17
+ end
18
+ # for contain(pattern).after(/A/)
19
+ chain :after do |after|
20
+ @from = Regexp.new(after).inspect
21
+ end
22
+ # for contain(pattern).before(/B/)
23
+ chain :before do |before|
24
+ @to = Regexp.new(before).inspect
25
+ end
6
26
  end
@@ -1,3 +1,3 @@
1
1
  module Serverspec
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -42,6 +42,22 @@ describe commands.check_file_contain('/etc/passwd', 'root') do
42
42
  it { should eq "grep -q 'root' /etc/passwd" }
43
43
  end
44
44
 
45
+ describe commands.check_file_contain_within('Gemfile', 'rspec') do
46
+ it { should eq "sed -n '1,$p' Gemfile | grep -q 'rspec' -" }
47
+ end
48
+
49
+ describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/') do
50
+ it { should eq "sed -n '/^group :test do/,$p' Gemfile | grep -q 'rspec' -" }
51
+ end
52
+
53
+ describe commands.check_file_contain_within('Gemfile', 'rspec', nil, '/^end/') do
54
+ it { should eq "sed -n '1,/^end/p' Gemfile | grep -q 'rspec' -" }
55
+ end
56
+
57
+ describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/', '/^end/') do
58
+ it { should eq "sed -n '/^group :test do/,/^end/p' Gemfile | grep -q 'rspec' -" }
59
+ end
60
+
45
61
  describe commands.check_mode('/etc/sudoers', 440) do
46
62
  it { should eq 'stat -c %a /etc/sudoers | grep 440' }
47
63
  end
@@ -7,6 +7,9 @@ describe 'Serverspec matchers of Debian family', :os => :debian do
7
7
  it_behaves_like 'support be_listening matcher', 22
8
8
  it_behaves_like 'support be_file matcher', '/etc/ssh/sshd_config'
9
9
  it_behaves_like 'support contain matcher', '/etc/ssh/sshd_config', 'See the sshd_config(5) manpage'
10
+ it_behaves_like 'support contain.from.to matcher', 'Gemfile', 'rspec', /^group :test do/, /^end/
11
+ it_behaves_like 'support contain.after matcher', 'Gemfile', 'rspec', /^group :test do/
12
+ it_behaves_like 'support contain.before matcher', 'Gemfile', 'rspec', /^end/
10
13
  it_behaves_like 'support be_user matcher', 'root'
11
14
  it_behaves_like 'support be_group matcher', 'wheel'
12
15
 
@@ -42,6 +42,22 @@ describe commands.check_file_contain('/etc/passwd', 'root') do
42
42
  it { should eq "grep -q 'root' /etc/passwd" }
43
43
  end
44
44
 
45
+ describe commands.check_file_contain_within('Gemfile', 'rspec') do
46
+ it { should eq "sed -n '1,$p' Gemfile | grep -q 'rspec' -" }
47
+ end
48
+
49
+ describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/') do
50
+ it { should eq "sed -n '/^group :test do/,$p' Gemfile | grep -q 'rspec' -" }
51
+ end
52
+
53
+ describe commands.check_file_contain_within('Gemfile', 'rspec', nil, '/^end/') do
54
+ it { should eq "sed -n '1,/^end/p' Gemfile | grep -q 'rspec' -" }
55
+ end
56
+
57
+ describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/', '/^end/') do
58
+ it { should eq "sed -n '/^group :test do/,/^end/p' Gemfile | grep -q 'rspec' -" }
59
+ end
60
+
45
61
  describe commands.check_mode('/etc/sudoers', 440) do
46
62
  it { should eq 'stat -c %a /etc/sudoers | grep 440' }
47
63
  end
@@ -8,6 +8,9 @@ describe 'Serverspec matchers of Gentoo family', :os => :gentoo do
8
8
  it_behaves_like 'support be_file matcher', '/etc/ssh/sshd_config'
9
9
  it_behaves_like 'support be_directory matcher', '/etc/ssh'
10
10
  it_behaves_like 'support contain matcher', '/etc/ssh/sshd_config', 'This is the sshd server system-wide configuration file'
11
+ it_behaves_like 'support contain.from.to matcher', 'Gemfile', 'rspec', /^group :test do/, /^end/
12
+ it_behaves_like 'support contain.after matcher', 'Gemfile', 'rspec', /^group :test do/
13
+ it_behaves_like 'support contain.before matcher', 'Gemfile', 'rspec', /^end/
11
14
  it_behaves_like 'support be_user matcher', 'root'
12
15
  it_behaves_like 'support be_group matcher', 'wheel'
13
16
 
@@ -42,6 +42,22 @@ describe commands.check_file_contain('/etc/passwd', 'root') do
42
42
  it { should eq "grep -q 'root' /etc/passwd" }
43
43
  end
44
44
 
45
+ describe commands.check_file_contain_within('Gemfile', 'rspec') do
46
+ it { should eq "sed -n '1,$p' Gemfile | grep -q 'rspec' -" }
47
+ end
48
+
49
+ describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/') do
50
+ it { should eq "sed -n '/^group :test do/,$p' Gemfile | grep -q 'rspec' -" }
51
+ end
52
+
53
+ describe commands.check_file_contain_within('Gemfile', 'rspec', nil, '/^end/') do
54
+ it { should eq "sed -n '1,/^end/p' Gemfile | grep -q 'rspec' -" }
55
+ end
56
+
57
+ describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/', '/^end/') do
58
+ it { should eq "sed -n '/^group :test do/,/^end/p' Gemfile | grep -q 'rspec' -" }
59
+ end
60
+
45
61
  describe commands.check_mode('/etc/sudoers', 440) do
46
62
  it { should eq 'stat -c %a /etc/sudoers | grep 440' }
47
63
  end
@@ -8,6 +8,9 @@ describe 'Serverspec matchers of Red Hat family', :os => :redhat do
8
8
  it_behaves_like 'support be_file matcher', '/etc/ssh/sshd_config'
9
9
  it_behaves_like 'support be_directory matcher', '/etc/ssh'
10
10
  it_behaves_like 'support contain matcher', '/etc/ssh/sshd_config', 'This is the sshd server system-wide configuration file'
11
+ it_behaves_like 'support contain.from.to matcher', 'Gemfile', 'rspec', /^group :test do/, /^end/
12
+ it_behaves_like 'support contain.after matcher', 'Gemfile', 'rspec', /^group :test do/
13
+ it_behaves_like 'support contain.before matcher', 'Gemfile', 'rspec', /^end/
11
14
  it_behaves_like 'support be_user matcher', 'root'
12
15
  it_behaves_like 'support be_group matcher', 'wheel'
13
16
 
@@ -42,6 +42,22 @@ describe commands.check_file_contain('/etc/passwd', 'root') do
42
42
  it { should eq "grep -q 'root' /etc/passwd" }
43
43
  end
44
44
 
45
+ describe commands.check_file_contain_within('Gemfile', 'rspec') do
46
+ it { should eq "sed -n '1,$p' Gemfile | grep -q 'rspec' -" }
47
+ end
48
+
49
+ describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/') do
50
+ it { should eq "sed -n '/^group :test do/,$p' Gemfile | grep -q 'rspec' -" }
51
+ end
52
+
53
+ describe commands.check_file_contain_within('Gemfile', 'rspec', nil, '/^end/') do
54
+ it { should eq "sed -n '1,/^end/p' Gemfile | grep -q 'rspec' -" }
55
+ end
56
+
57
+ describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/', '/^end/') do
58
+ it { should eq "sed -n '/^group :test do/,/^end/p' Gemfile | grep -q 'rspec' -" }
59
+ end
60
+
45
61
  describe commands.check_mode('/etc/sudoers', 440) do
46
62
  it { should eq 'stat -c %a /etc/sudoers | grep 440' }
47
63
  end
@@ -8,6 +8,9 @@ describe 'Serverspec matchers of Solaris family', :os => :solaris do
8
8
  it_behaves_like 'support be_file matcher', '/etc/ssh/sshd_config'
9
9
  it_behaves_like 'support be_directory matcher', '/etc/ssh'
10
10
  it_behaves_like 'support contain matcher', '/etc/ssh/sshd_config', 'Configuration file for sshd(1m) (see also sshd_config(4))'
11
+ it_behaves_like 'support contain.from.to matcher', 'Gemfile', 'rspec', /^group :test do/, /^end/
12
+ it_behaves_like 'support contain.after matcher', 'Gemfile', 'rspec', /^group :test do/
13
+ it_behaves_like 'support contain.before matcher', 'Gemfile', 'rspec', /^end/
11
14
  it_behaves_like 'support be_user matcher', 'root'
12
15
  it_behaves_like 'support be_group matcher', 'root'
13
16
 
@@ -92,6 +92,42 @@ shared_examples_for 'support contain matcher' do |valid_file, pattern|
92
92
  end
93
93
  end
94
94
 
95
+ shared_examples_for 'support contain.from.to matcher' do |valid_file, pattern, from, to|
96
+ describe 'contain' do
97
+ describe valid_file do
98
+ it { should contain(pattern).from(from).to(to) }
99
+ end
100
+
101
+ describe '/etc/ssh/sshd_config' do
102
+ it { should_not contain('This is invalid text!!').from(from).to(to) }
103
+ end
104
+ end
105
+ end
106
+
107
+ shared_examples_for 'support contain.after matcher' do |valid_file, pattern, after|
108
+ describe 'contain' do
109
+ describe valid_file do
110
+ it { should contain(pattern).after(after) }
111
+ end
112
+
113
+ describe '/etc/ssh/sshd_config' do
114
+ it { should_not contain('This is invalid text!!').after(after) }
115
+ end
116
+ end
117
+ end
118
+
119
+ shared_examples_for 'support contain.before matcher' do |valid_file, pattern, before|
120
+ describe 'contain' do
121
+ describe valid_file do
122
+ it { should contain(pattern).before(before) }
123
+ end
124
+
125
+ describe '/etc/ssh/sshd_config' do
126
+ it { should_not contain('This is invalid text!!').before(before) }
127
+ end
128
+ end
129
+ end
130
+
95
131
  shared_examples_for 'support be_user matcher' do |valid_user|
96
132
  describe 'be_user' do
97
133
  describe valid_user do
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.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -151,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
151
  version: '0'
152
152
  requirements: []
153
153
  rubyforge_project:
154
- rubygems_version: 1.8.23
154
+ rubygems_version: 1.8.25
155
155
  signing_key:
156
156
  specification_version: 3
157
157
  summary: RSpec tests for your servers provisioned by Puppet, Chef or anything else