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
|
#
# Author:: Daniel DeLeo (<dan@opscode.com>)
# Copyright:: Copyright (c) 2012 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 File.expand_path('../../spec_helper', __FILE__)
require 'chef/client'
describe Chef::RunLock do
# This behavior works on windows, but the tests use fork :(
describe "when locking the chef-client run", :unix_only => true do
##
# Lockfile location and helpers
let(:random_temp_root) do
Kernel.srand(Time.now.to_i + Process.pid)
"/tmp/#{Kernel.rand(Time.now.to_i + Process.pid)}"
end
let(:lockfile){ "#{random_temp_root}/this/long/path/does/not/exist/chef-client-running.pid" }
# make sure to start with a clean slate.
before(:each){ FileUtils.rm_r(random_temp_root) if File.exist?(random_temp_root) }
after(:each){ FileUtils.rm_r(random_temp_root) }
def wait_on_lock
tries = 0
until File.exist?(lockfile)
raise "Lockfile never created, abandoning test" if tries > 10
tries += 1
sleep 0.1
end
end
##
# Side channel via a pipe allows child processes to send errors to the parent
# Don't lazy create the pipe or else we might not share it with subprocesses
let!(:error_pipe) do
r,w = IO.pipe
w.sync = true
[r,w]
end
let(:error_read) { error_pipe[0] }
let(:error_write) { error_pipe[1] }
after do
error_read.close unless error_read.closed?
error_write.close unless error_write.closed?
end
# Send a RuntimeError from the child process to the parent process. Also
# prints error to $stdout, just in case something goes wrong with the error
# marshaling stuff.
def send_side_channel_error(message)
$stderr.puts(message)
$stderr.puts(caller)
e = RuntimeError.new(message)
error_write.print(Marshal.dump(e))
end
# Read the error (if any) from the error channel. If a marhaled error is
# present, it is unmarshaled and raised (which will fail the test)
def raise_side_channel_error!
error_write.close
err = error_read.read
error_read.close
begin
# ArgumentError from Marshal.load indicates no data, which we assume
# means no error in child process.
raise Marshal.load(err)
rescue ArgumentError
nil
end
end
##
# Interprocess synchronization via a pipe. This allows us to control the
# state of the processes competing over the lock without relying on sleep.
let!(:sync_pipe) do
r,w = IO.pipe
w.sync = true
[r,w]
end
let(:sync_read) { sync_pipe[0] }
let(:sync_write) { sync_pipe[1] }
after do
sync_read.close unless sync_read.closed?
sync_write.close unless sync_write.closed?
end
# Wait on synchronization signal. If not received within the timeout, an
# error is sent via the error channel, and the process exits.
def sync_wait
if IO.select([sync_read], nil, nil, 20).nil?
# timeout reading from the sync pipe.
send_side_channel_error("Error syncing processes in run lock test (timeout)")
exit!(1)
else
sync_read.getc
end
end
# Sends a character in the sync pipe, which wakes ("unlocks") another
# process that is waiting on the sync signal
def sync_send
sync_write.putc("!")
sync_write.flush
end
##
# IPC to record test results in a pipe. Tests can read pipe contents to
# check that operations occur in the expected order.
let!(:results_pipe) do
r,w = IO.pipe
w.sync = true
[r,w]
end
let(:results_read) { results_pipe[0] }
let(:results_write) { results_pipe[1] }
after do
results_read.close unless results_read.closed?
results_write.close unless results_write.closed?
end
# writes the message to the results pipe for later checking.
# note that nothing accounts for the pipe filling and waiting forever on a
# read or write call, so don't put too much data in.
def record(message)
results_write.puts(message)
results_write.flush
end
def results
results_write.flush
results_write.close
message = results_read.read
results_read.close
message
end
##
# Run lock is the system under test
let!(:run_lock) { Chef::RunLock.new(lockfile) }
it "creates the full path to the lockfile" do
lambda { run_lock.acquire }.should_not raise_error
File.should exist(lockfile)
end
it "sets FD_CLOEXEC on the lockfile", :supports_cloexec => true do
run_lock.acquire
(run_lock.runlock.fcntl(Fcntl::F_GETFD, 0) & Fcntl::FD_CLOEXEC).should == Fcntl::FD_CLOEXEC
end
it "allows only one chef client run per lockfile" do
# First process, gets the lock and keeps it.
p1 = fork do
run_lock.acquire
record "p1 has lock"
# Wait until the other process is trying to get the lock:
sync_wait
# sleep a little bit to make process p2 wait on the lock
sleep 2
record "p1 releasing lock"
run_lock.release
exit!(0)
end
# Wait until p1 creates the lockfile
wait_on_lock
p2 = fork do
# inform process p1 that we're trying to get the lock
sync_send
run_lock.acquire
record "p2 has lock"
run_lock.release
exit!(0)
end
Process.waitpid2(p1)
Process.waitpid2(p2)
raise_side_channel_error!
expected=<<-E
p1 has lock
p1 releasing lock
p2 has lock
E
results.should == expected
end
it "clears the lock if the process dies unexpectedly" do
p1 = fork do
run_lock.acquire
record "p1 has lock"
sleep 60
record "p1 still has lock"
exit! 1
end
wait_on_lock
Process.kill(:KILL, p1)
Process.waitpid2(p1)
p2 = fork do
run_lock.acquire
record "p2 has lock"
run_lock.release
exit! 0
end
Process.waitpid2(p2)
results.should =~ /p2 has lock\Z/
end
it "test returns true and acquires the lock" do
p1 = fork do
run_lock.test.should == true
sleep 2
exit! 1
end
wait_on_lock
p2 = fork do
run_lock.test.should == false
exit! 0
end
Process.waitpid2(p2)
Process.waitpid2(p1)
end
it "test returns without waiting when the lock is acquired" do
p1 = fork do
run_lock.acquire
sleep 2
exit! 1
end
wait_on_lock
run_lock.test.should == false
Process.waitpid2(p1)
end
it "doesn't truncate the lock file so that contents can be read" do
p1 = fork do
run_lock.acquire
run_lock.save_pid
sleep 2
exit! 1
end
wait_on_lock
File.read(lockfile).should == p1.to_s
Process.waitpid2(p1)
end
end
end
|