forked from sprinkle-tool/sprinkle
/
verify_spec.rb
201 lines (157 loc) · 6 KB
/
verify_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
require File.expand_path("../spec_helper", File.dirname(__FILE__))
describe Sprinkle::Verify do
before do
@name = :package
@package = package @name do
gem 'nonexistent'
verify 'moo' do
# Check a file exists
has_file 'my_file.txt'
# Check that a file contains a substring
file_contains 'my_file.txt', 'A sunny day on the lower east-side'
# Check a directory exists
has_directory 'mydir'
# Check for a user
has_user "bob"
# Check for user with old API
user_present "someuser"
# Check for user in a group
has_user "alf", :in_group => "alien"
# Check for a group
has_group "bobgroup"
# Check a symlink exists
has_symlink 'mypointer'
# Check a symlink points to a certain file
has_symlink 'mypointer', 'myfile'
# Check if an executable exists
has_executable '/usr/bin/ruby'
# Check if a global executable exists (in PATH)
has_executable 'rails'
# Check for a process
has_process 'httpd'
# Check that ruby can include files
ruby_can_load 'a', 'b', 'c'
# Check that a gem exists
has_gem 'rails'
has_gem 'rails', '2.1.0'
# Check for a certain RPM package
has_rpm 'ntp'
end
end
@verification = @package.verifications[0]
@delivery = mock(Sprinkle::Deployment, :process => true)
@verification.delivery = @delivery
end
describe 'when created' do
it 'should raise error without a block' do
lambda { Verify.new(nil, '') }.should raise_error
end
end
describe 'with checks' do
it 'should do a "test -f" on the has_file check' do
@verification.commands.should include('test -f my_file.txt')
end
it 'should do a grep to see if a file contains a text string' do
@verification.commands.should include("grep 'A sunny day on the lower east-side' my_file.txt")
end
it 'should do a "test -d" on the has_directory check' do
@verification.commands.should include('test -d mydir')
end
it 'should use id to check for user in group' do
@verification.commands.should include("id -G alf | xargs -n1 echo | grep alien")
end
it 'should use id to check for user' do
@verification.commands.should include('id bob')
end
it 'should use id to check for user via user_present' do
@verification.commands.should include('id someuser')
end
it 'should use id to check for group' do
@verification.commands.should include('id -g bobgroup')
end
it 'should do a "test -L" to check something is a symbolic link' do
@verification.commands.should include('test -L mypointer')
end
it 'should do a test equality to check a symlink points to a specific file' do
@verification.commands.should include("test 'myfile' = `readlink mypointer`")
end
it 'should do a "test -x" to check for an executable' do
@verification.commands.should include("test -x /usr/bin/ruby")
end
it 'should test the "which" command to look for a global executable' do
@verification.commands.should include('[ -n "`echo \`which rails\``" ]')
end
it 'should test the process list to find a process' do
@verification.commands.should include("ps -C httpd")
end
it 'should check if ruby can include a, b, c' do
@verification.commands.should include("ruby -e \"require 'rubygems';require 'a';require 'b';require 'c'\"")
end
it 'should check that a ruby gem is installed' do
@verification.commands.should include("sudo gem list 'rails' --installed --version '2.1.0' > /dev/null")
end
it 'should check that an RPM is installed' do
@verification.commands.should include("rpm -qa | grep ntp")
end
end
describe 'with configurations' do
# Make sure it includes Sprinkle::Configurable
it 'should respond to configurable methods' do
@verification.should respond_to(:defaults)
end
it 'should default padding option to 4' do
@verification.padding.should eql(4)
end
end
describe 'with process' do
it 'should raise an error when no delivery mechanism is set' do
@verification.instance_variable_set(:@delivery, nil)
lambda { @verification.process([]) }.should raise_error
end
describe 'when not testing' do
before do
# To be explicit
Sprinkle::OPTIONS[:testing] = false
end
it 'should call process on the delivery with the correct parameters' do
@delivery.should_receive(:process).with(@name, @verification.commands, [:app], true).once.and_return(true)
@verification.process([:app])
end
it 'should raise Sprinkle::VerificationFailed exception when commands fail' do
@delivery.should_receive(:process).once.and_return(false)
lambda { @verification.process([:app]) }.should raise_error(Sprinkle::VerificationFailed) do |error|
error.package.should eql(@package)
error.description.should eql('moo')
end
end
end
describe 'when testing' do
before do
Sprinkle::OPTIONS[:testing] = true
@logger = mock(ActiveSupport::BufferedLogger, :debug => true, :debug? => true)
end
it 'should not call process on the delivery' do
@delivery.should_not_receive(:process)
end
it 'should print the install sequence to the console' do
@verification.should_receive(:logger).twice.and_return(@logger)
end
after do
@verification.process([:app])
Sprinkle::OPTIONS[:testing] = false
end
end
end
describe 'with registering new verification modules' do
module MyModule
def rawr; end
end
it 'should not respond to rawr initially' do
@verification.should_not respond_to(:rawr)
end
it 'should respond to rawr after registering the module' do
Sprinkle::Verify.register(MyModule)
@verification.should respond_to(:rawr)
end
end
end