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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
#
# Author:: AJ Christensen (<aj@junglist.gen.nz>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'spec_helper'
require 'ostruct'
describe Chef::Daemon do
before do
if windows?
mock_struct = #Struct::Passwd.new(nil, nil, 111, 111)
mock_struct = OpenStruct.new(:uid => 2342, :gid => 2342)
Etc.stub!(:getpwnam).and_return mock_struct
Etc.stub!(:getgrnam).and_return mock_struct
# mock unimplemented methods
Process.stub!(:initgroups).and_return nil
Process::GID.stub!(:change_privilege).and_return 11
Process::UID.stub!(:change_privilege).and_return 11
end
end
describe ".running?" do
before do
Chef::Daemon.name = "spec"
end
describe "when a pid file exists" do
before do
Chef::Daemon.stub!(:pid_from_file).and_return(1337)
end
it "should check that there is a process matching the pidfile" do
Process.should_receive(:kill).with(0, 1337)
Chef::Daemon.running?
end
end
describe "when the pid file is nonexistent" do
before do
Chef::Daemon.stub!(:pid_from_file).and_return(nil)
end
it "should return false" do
Chef::Daemon.running?.should be_false
end
end
end
describe ".pid_file" do
describe "when the pid_file option has been set" do
before do
Chef::Config[:pid_file] = "/var/run/chef/chef-client.pid"
end
it "should return the supplied value" do
Chef::Daemon.pid_file.should eql("/var/run/chef/chef-client.pid")
end
end
describe "without the pid_file option set" do
before do
Chef::Daemon.name = "chef-client"
end
it "should return a valued based on @name" do
Chef::Daemon.pid_file.should eql("/tmp/chef-client.pid")
end
end
end
describe ".pid_from_file" do
before do
Chef::Config[:pid_file] = "/var/run/chef/chef-client.pid"
end
it "should suck the pid out of pid_file" do
File.should_receive(:read).with("/var/run/chef/chef-client.pid").and_return("1337")
Chef::Daemon.pid_from_file
end
end
describe ".save_pid_file" do
before do
Process.stub!(:pid).and_return(1337)
Chef::Config[:pid_file] = "/var/run/chef/chef-client.pid"
Chef::Application.stub!(:fatal!).and_return(true)
@f_mock = mock(File, { :print => true, :close => true, :write => true })
File.stub!(:open).with("/var/run/chef/chef-client.pid", "w").and_yield(@f_mock)
end
it "should try and create the parent directory" do
FileUtils.should_receive(:mkdir_p).with("/var/run/chef")
Chef::Daemon.save_pid_file
end
it "should open the pid file for writing" do
File.should_receive(:open).with("/var/run/chef/chef-client.pid", "w")
Chef::Daemon.save_pid_file
end
it "should write the pid, converted to string, to the pid file" do
@f_mock.should_receive(:write).with("1337").once.and_return(true)
Chef::Daemon.save_pid_file
end
end
describe ".remove_pid_file" do
before do
Chef::Config[:pid_file] = "/var/run/chef/chef-client.pid"
end
describe "when the pid file exists" do
before do
File.stub!(:exists?).with("/var/run/chef/chef-client.pid").and_return(true)
end
it "should remove the file" do
FileUtils.should_receive(:rm).with("/var/run/chef/chef-client.pid")
Chef::Daemon.remove_pid_file
end
end
describe "when the pid file exists and the process is forked" do
before do
File.stub!(:exists?).with("/var/run/chef/chef-client.pid").and_return(true)
Chef::Daemon.stub!(:forked?) { true }
end
it "should not remove the file" do
FileUtils.should_not_receive(:rm)
Chef::Daemon.remove_pid_file
end
end
describe "when the pid file exists and the process is not forked" do
before do
File.stub!(:exists?).with("/var/run/chef/chef-client.pid").and_return(true)
Chef::Daemon.stub!(:forked?) { false }
end
it "should remove the file" do
FileUtils.should_receive(:rm)
Chef::Daemon.remove_pid_file
end
end
describe "when the pid file does not exist" do
before do
File.stub!(:exists?).with("/var/run/chef/chef-client.pid").and_return(false)
end
it "should not remove the file" do
FileUtils.should_not_receive(:rm)
Chef::Daemon.remove_pid_file
end
end
end
describe ".change_privilege" do
before do
Chef::Application.stub!(:fatal!).and_return(true)
Chef::Config[:user] = 'aj'
Dir.stub!(:chdir)
end
it "changes the working directory to root" do
Dir.rspec_reset
Dir.should_receive(:chdir).with("/").and_return(0)
Chef::Daemon.change_privilege
end
describe "when the user and group options are supplied" do
before do
Chef::Config[:group] = 'staff'
end
it "should log an appropriate info message" do
Chef::Log.should_receive(:info).with("About to change privilege to aj:staff")
Chef::Daemon.change_privilege
end
it "should call _change_privilege with the user and group" do
Chef::Daemon.should_receive(:_change_privilege).with("aj", "staff")
Chef::Daemon.change_privilege
end
end
describe "when just the user option is supplied" do
it "should log an appropriate info message" do
Chef::Log.should_receive(:info).with("About to change privilege to aj")
Chef::Daemon.change_privilege
end
it "should call _change_privilege with just the user" do
Chef::Daemon.should_receive(:_change_privilege).with("aj")
Chef::Daemon.change_privilege
end
end
end
describe "._change_privilege" do
before do
Process.stub!(:euid).and_return(0)
Process.stub!(:egid).and_return(0)
Process::UID.stub!(:change_privilege).and_return(nil)
Process::GID.stub!(:change_privilege).and_return(nil)
@pw_user = mock("Struct::Passwd", :uid => 501)
@pw_group = mock("Struct::Group", :gid => 20)
Process.stub!(:initgroups).and_return(true)
Etc.stub!(:getpwnam).and_return(@pw_user)
Etc.stub!(:getgrnam).and_return(@pw_group)
end
describe "with sufficient privileges" do
before do
Process.stub!(:euid).and_return(0)
Process.stub!(:egid).and_return(0)
end
it "should initialize the supplemental group list" do
Process.should_receive(:initgroups).with("aj", 20)
Chef::Daemon._change_privilege("aj")
end
it "should attempt to change the process GID" do
Process::GID.should_receive(:change_privilege).with(20).and_return(20)
Chef::Daemon._change_privilege("aj")
end
it "should attempt to change the process UID" do
Process::UID.should_receive(:change_privilege).with(501).and_return(501)
Chef::Daemon._change_privilege("aj")
end
end
describe "with insufficient privileges" do
before do
Process.stub!(:euid).and_return(999)
Process.stub!(:egid).and_return(999)
end
it "should log an appropriate error message and fail miserably" do
Process.stub!(:initgroups).and_raise(Errno::EPERM)
error = "Operation not permitted"
if RUBY_PLATFORM.match("solaris2") || RUBY_PLATFORM.match("aix")
error = "Not owner"
end
Chef::Application.should_receive(:fatal!).with("Permission denied when trying to change 999:999 to 501:20. #{error}")
Chef::Daemon._change_privilege("aj")
end
end
end
end
|