dante 0.2.0 → 0.4.0

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.
data/test/runner_test.rb CHANGED
@@ -1,4 +1,6 @@
1
+ require 'stringio'
1
2
  require File.expand_path('../test_helper', __FILE__)
3
+ require 'socket'
2
4
 
3
5
  describe "dante runner" do
4
6
 
@@ -22,128 +24,1022 @@ describe "dante runner" do
22
24
 
23
25
  describe "with no daemonize" do
24
26
  before do
25
- @process = TestingProcess.new('a')
27
+ @process = TestingProcess.new('a', @tmpdir)
26
28
  @runner = Dante::Runner.new('test-process') { @process.run_a! }
27
29
  @stdout = capture_stdout { @runner.execute }
28
30
  end
29
31
 
30
32
  it "prints correct stdout" do
31
- assert_match /Starting test-process/, @stdout
33
+ assert_match(/Starting test-process/, @stdout)
32
34
  end
33
35
 
34
36
  it "starts successfully when executed" do
35
37
  @output = File.read(@process.tmp_path)
36
- assert_match /Started/, @output
38
+ assert_match(/Started/, @output)
37
39
  end
38
40
  end # no daemonize
39
41
 
42
+ describe "with daemonize set to false and a log file" do
43
+ it "writes its output to the log file" do
44
+ logfile = tmp_path('dante-foreground.log')
45
+ capture_stdout {
46
+ pid = fork do
47
+ Dante::Runner.new('test-process', :daemonize => false, :log_path => logfile) { puts "Started" }.execute
48
+ exit!(0)
49
+ end
50
+ Process.wait(pid) }
51
+
52
+ assert_equal "Started\n", (File.read(logfile) if File.exist?(logfile))
53
+ end
54
+ end # daemonize false
55
+
40
56
  describe "with daemonize flag" do
41
57
  before do
42
- @process = TestingProcess.new('b')
43
- @run_options = { :daemonize => true, :pid_path => "/tmp/dante.pid", :port => 8080, :log_path => false }
58
+ @process = TestingProcess.new('b', @tmpdir)
59
+ @pid_path = tmp_path('dante.pid')
60
+ @run_options = { :daemonize => true, :pid_path => @pid_path, :port => 8080, :log_path => tmp_path('dante-daemon.log') }
44
61
  @runner = Dante::Runner.new('test-process-2', @run_options) { |opts|
45
62
  @process.run_b!(opts[:port]) }
46
63
  @stdout = capture_stdout { @runner.execute }
47
- sleep(1)
64
+ wait_for_output(@process.tmp_path)
48
65
  end
49
66
 
50
67
  it "can properly handles aborts and starts / stops on INT" do
51
- refute_equal 0, @pid = `cat /tmp/dante.pid`.to_i
68
+ refute_equal 0, @pid = daemon_pid(@pid_path)
52
69
  Process.kill "INT", @pid
53
- sleep(1) # Wait to complete
70
+ wait_for_exit(@pid)
54
71
  @output = File.read(@process.tmp_path)
55
72
 
56
- assert_match /Started on 8080!!/, @output
57
- assert_match /Interrupt!!/, @output
58
- assert_match /Closing!!/, @output
73
+ assert_match(/Started on 8080!!/, @output)
74
+ assert_match(/Interrupt!!/, @output)
75
+ assert_match(/Closing!!/, @output)
59
76
  end
60
77
 
61
78
  it "can properly handles aborts and starts / stops on TERM" do
62
- refute_equal 0, @pid = `cat /tmp/dante.pid`.to_i
79
+ refute_equal 0, @pid = daemon_pid(@pid_path)
63
80
  Process.kill "TERM", @pid
64
- sleep(1) # Wait to complete
81
+ wait_for_exit(@pid)
65
82
  @output = File.read(@process.tmp_path)
66
83
 
67
- assert_match /Started on 8080!!/, @output
68
- assert_match /Closing!!/, @output
69
- refute_match /Interrupt!!/, @output
84
+ assert_match(/Started on 8080!!/, @output)
85
+ assert_match(/Closing!!/, @output)
86
+ refute_match(/Interrupt!!/, @output)
87
+ end
88
+
89
+ it "can be stopped with #stop, which removes the pid file" do
90
+ refute_equal 0, @pid = daemon_pid(@pid_path)
91
+ @stop_output = capture_stdout { @runner.stop }
92
+
93
+ assert_match(/Daemonized process killed after term/, @stop_output)
94
+ refute File.exist?(@pid_path), "pid file left behind after stop"
95
+ assert_match(/Closing!!/, File.read(@process.tmp_path))
96
+ refute_match(/Interrupt!!/, File.read(@process.tmp_path)) # stopped with TERM, not INT
70
97
  end
71
98
  end # daemonize
72
99
 
100
+ describe "daemonize with block on sigterm" do
101
+ before do
102
+ @logfile = tmp_path('dante-logging.log')
103
+ @pid_path = tmp_path('dante.pid')
104
+ @process = TestingProcess.new('f1', @tmpdir)
105
+ @run_options = { :daemonize => true, :pid_path => @pid_path, :log_path => @logfile }
106
+ @runner = Dante::Runner.new('test-process-f1', @run_options) { |opts|
107
+ @process.run_d! }
108
+ @stdout = capture_stdout { @runner.execute }
109
+ wait_for_output(@logfile, /The rescuer/)
110
+ end
111
+
112
+ it "cannot be stopped without force flag" do
113
+ refute_equal 0, @pid = daemon_pid(@pid_path)
114
+ @stop_output = capture_stdout { @runner.stop }
115
+ @output = File.read(@logfile)
116
+
117
+ assert_match(/Trying to stop test-process/, @output)
118
+ assert_match(/Failed to kill daemonized process/, @stop_output)
119
+ refute_match(/Daemonized process killed after kill/, @stop_output)
120
+ assert File.exist?(@pid_path), "pid file removed although the daemon is still running"
121
+ end
122
+ end
123
+
124
+ describe "daemonize with block on sigkill" do
125
+ before do
126
+ @logfile = tmp_path('dante-logging.log')
127
+ @pid_path = tmp_path('dante.pid')
128
+ @process = TestingProcess.new('f2', @tmpdir)
129
+ @run_options = { :daemonize => true, :pid_path => @pid_path, :log_path => @logfile, force: true }
130
+ @runner = Dante::Runner.new('test-process-f2', @run_options) { |opts|
131
+ @process.run_d! }
132
+ @stdout = capture_stdout { @runner.execute }
133
+ wait_for_output(@logfile, /The rescuer/)
134
+ end
135
+
136
+ it "can be stopped without force flag" do
137
+ refute_equal 0, @pid = daemon_pid(@pid_path)
138
+ @stop_output = capture_stdout { @runner.stop }
139
+ @output = File.read(@logfile)
140
+
141
+ assert_match(/Trying to stop test-process/, @output)
142
+ assert_match(/Failed to kill daemonized process/, @stop_output)
143
+ assert_match(/Daemonized process killed after kill/, @stop_output)
144
+ refute File.exist?(@pid_path), "pid file left behind after a forced stop"
145
+ end
146
+ end
147
+
73
148
  describe "with daemonize flag and log file specified" do
74
149
  before do
75
- @logfile = '/tmp/dante-logging.log'
76
- FileUtils.rm(@logfile) if File.exist?(@logfile)
77
- @process = TestingProcess.new('c')
78
- @run_options = { :daemonize => true, :pid_path => "/tmp/dante.pid", :port => 8081, :log_path => @logfile }
150
+ @logfile = tmp_path('dante-logging.log')
151
+ @pid_path = tmp_path('dante.pid')
152
+ @process = TestingProcess.new('c', @tmpdir)
153
+ @run_options = { :daemonize => true, :pid_path => @pid_path, :port => 8081, :log_path => @logfile }
79
154
  @runner = Dante::Runner.new('test-process-2', @run_options) { |opts|
80
155
  @process.run_c!(opts[:port]) }
81
- @runner.execute
82
- sleep(1)
156
+ @stdout = capture_stdout { @runner.execute }
157
+ wait_for_output(@logfile, /Started on 8081!!/)
83
158
  end
84
159
 
85
160
  it "can properly handles log to file and aborts on INT" do
86
- refute_equal 0, @pid = `cat /tmp/dante.pid`.to_i
161
+ refute_equal 0, @pid = daemon_pid(@pid_path)
87
162
  Process.kill "INT", @pid
88
- sleep(1) # Wait to complete
163
+ wait_for_exit(@pid)
89
164
  @output = File.read(@logfile)
90
165
 
91
- assert_match /Started on 8081!!/, @output
92
- assert_match /Interrupt!!/, @output
93
- assert_match /Closing!!/, @output
166
+ assert_match(/Started on 8081!!/, @output)
167
+ assert_match(/Interrupt!!/, @output)
168
+ assert_match(/Closing!!/, @output)
94
169
  end
95
170
  end
96
171
 
97
172
  describe "with daemonize flag and logging flag enabled, and debug flag disabled" do
98
173
  before do
99
- @logfile = '/tmp/dante-logging.log'
100
- FileUtils.rm(@logfile) if File.exist?(@logfile)
101
- @process = TestingProcess.new('c')
102
- @run_options = { :daemonize => true, :pid_path => "/tmp/dante.pid", :port => 8081, :log_path => @logfile, :debug => false }
174
+ @logfile = tmp_path('dante-logging.log')
175
+ @pid_path = tmp_path('dante.pid')
176
+ @process = TestingProcess.new('c', @tmpdir)
177
+ @run_options = { :daemonize => true, :pid_path => @pid_path, :port => 8081, :log_path => @logfile, :debug => false }
103
178
  @runner = Dante::Runner.new('test-process-2', @run_options) { |opts|
104
179
  @process.run_c!(opts[:port]) }
105
- @runner.execute
106
- sleep(1)
180
+ @stdout = capture_stdout { @runner.execute }
181
+ wait_for_output(@logfile, /Started on 8081!!/)
107
182
  end
108
183
 
109
184
  it "can properly handles log to file and aborts on INT" do
110
- refute_equal 0, @pid = `cat /tmp/dante.pid`.to_i
185
+ refute_equal 0, @pid = daemon_pid(@pid_path)
111
186
  Process.kill "INT", @pid
112
- sleep(1) # Wait to complete
187
+ wait_for_exit(@pid)
113
188
  @output = File.read(@logfile)
114
189
 
115
- assert_match /Started on 8081!!/, @output
116
- assert_match /Interrupt received/, @output
117
- assert_match /Closing!!/, @output
190
+ assert_match(/Started on 8081!!/, @output)
191
+ assert_match(/Interrupt received/, @output)
192
+ assert_match(/Closing!!/, @output)
118
193
  end
119
194
  end
120
195
 
121
196
 
122
197
  describe "with execute accepting block" do
123
198
  before do
124
- @process = TestingProcess.new('b')
125
- @run_options = { :daemonize => true, :pid_path => "/tmp/dante.pid", :port => 8080, :log_path => false }
199
+ @process = TestingProcess.new('b', @tmpdir)
200
+ @pid_path = tmp_path('dante.pid')
201
+ @run_options = { :daemonize => true, :pid_path => @pid_path, :port => 8080, :log_path => tmp_path('dante-daemon.log') }
126
202
  @runner = Dante::Runner.new('test-process-2', @run_options)
127
203
  @stdout = capture_stdout { @runner.execute { |opts| @process.run_b!(opts[:port]) } }
128
- sleep(1)
204
+ wait_for_output(@process.tmp_path)
129
205
  end
130
206
 
131
207
  it "can properly handles aborts and starts / stops on INT" do
132
- refute_equal 0, @pid = `cat /tmp/dante.pid`.to_i
208
+ refute_equal 0, @pid = daemon_pid(@pid_path)
133
209
  Process.kill "INT", @pid
134
- sleep(1) # Wait to complete
210
+ wait_for_exit(@pid)
135
211
  @output = File.read(@process.tmp_path)
136
212
 
137
- assert_match /Started on 8080!!/, @output
138
- assert_match /Interrupt!!/, @output
139
- assert_match /Closing!!/, @output
213
+ assert_match(/Started on 8080!!/, @output)
214
+ assert_match(/Interrupt!!/, @output)
215
+ assert_match(/Closing!!/, @output)
140
216
  end
141
217
  end # execute with block
142
218
 
219
+ describe "with daemonize flag, stopped while starting up" do
220
+ before do
221
+ # Holds the daemon at its "Starting" message, after its pid file is written and
222
+ # before its startup block runs, and shows that it got there by creating @starting.
223
+ # (It holds before writing the message, so the TERM below never lands mid-write.)
224
+ starting = @starting = tmp_path('starting')
225
+ slow_runner = Class.new(Dante::Runner) do
226
+ define_method(:log) do |message|
227
+ if message.start_with?("Starting")
228
+ File.write(starting, "")
229
+ sleep 10
230
+ end
231
+ super(message)
232
+ end
233
+ end
234
+ @logfile = tmp_path('dante-starting.log')
235
+ @pid_path = tmp_path('dante.pid')
236
+ @runner = slow_runner.new('test-process', :daemonize => true, :pid_path => @pid_path, :log_path => @logfile) { sleep 100 }
237
+ @stdout = capture_stdout { @runner.execute }
238
+ end
239
+
240
+ it "stops through its TERM handler once it has reported starting" do
241
+ assert_match(/Daemon has started successfully/, @stdout)
242
+ @pid = daemon_pid(@pid_path)
243
+ wait_for_output(@starting)
244
+ Process.kill "TERM", @pid
245
+ wait_for_exit(@pid)
246
+
247
+ assert_match(/Trying to stop test-process/, File.read(@logfile))
248
+ end
249
+ end # stopped while starting up
250
+
251
+ describe "with daemonize flag and a umask" do
252
+ before do
253
+ @original_umask = File.umask(0027)
254
+ @logfile = tmp_path('dante-umask.log')
255
+ @pid_path = tmp_path('dante.pid')
256
+ @created = tmp_path('created-by-daemon')
257
+ created = @created
258
+ @runner = Dante::Runner.new('test-process', :daemonize => true, :pid_path => @pid_path, :log_path => @logfile) {
259
+ File.write(created, "x")
260
+ sleep 100 }
261
+ @stdout = capture_stdout { @runner.execute }
262
+ wait_for_output(@created)
263
+ end
264
+
265
+ after do
266
+ File.umask(@original_umask)
267
+ end
268
+
269
+ it "creates files with the umask of the process that started it" do
270
+ assert_equal "640", "%o" % (File.stat(@created).mode & 0777)
271
+ end
272
+
273
+ it "creates the log file with mode 0644 less the umask" do
274
+ assert_equal "640", "%o" % (File.stat(@logfile).mode & 0777)
275
+ end
276
+ end # umask
277
+
278
+ describe "with daemonize flag and something at the pid file's path already" do
279
+ before do
280
+ @pid_path = tmp_path('dante.pid')
281
+ @other = tmp_path('other-file')
282
+ File.write(@other, "not a pid\n")
283
+ end
284
+
285
+ # Starts a daemon on @pid_path and returns its pid.
286
+ def start_daemon
287
+ started = tmp_path('started')
288
+ capture_stdout {
289
+ Dante::Runner.new('test-process', :daemonize => true, :pid_path => @pid_path, :log_path => tmp_path('dante.log')) {
290
+ File.write(started, ""); sleep 100 }.execute }
291
+ wait_for_output(started)
292
+ daemon_pid(@pid_path)
293
+ end
294
+
295
+ it "replaces a symlink with the pid file instead of writing through it" do
296
+ File.symlink(@other, @pid_path)
297
+ pid = start_daemon
298
+
299
+ refute File.symlink?(@pid_path), "the pid file is still a symlink"
300
+ assert_equal "#{pid}\n", File.read(@pid_path)
301
+ assert_equal "not a pid\n", File.read(@other)
302
+ assert_empty Dir[File.join(@tmpdir, '*.tmp')], "temporary file left behind"
303
+ end
304
+
305
+ it "replaces a hard link to another file instead of writing into it" do
306
+ File.link(@other, @pid_path)
307
+ pid = start_daemon
308
+
309
+ assert_equal "#{pid}\n", File.read(@pid_path)
310
+ assert_equal "not a pid\n", File.read(@other)
311
+ end
312
+ end # pid file path taken
313
+
314
+ describe "with a log file there already" do
315
+ # Runs a runner in the foreground in a forked child, writing to log_path.
316
+ def run_with_log(log_path)
317
+ capture_stdout {
318
+ pid = fork do
319
+ Dante::Runner.new('test-process', :log_path => log_path) { puts "Started" }.execute
320
+ exit!(0)
321
+ end
322
+ Process.wait(pid) }
323
+ end
324
+
325
+ it "appends to it and keeps its mode" do
326
+ logfile = tmp_path('dante.log')
327
+ File.write(logfile, "earlier line\n")
328
+ File.chmod(0600, logfile)
329
+ run_with_log(logfile)
330
+
331
+ assert_equal "earlier line\nStarted\n", File.read(logfile)
332
+ assert_equal "600", "%o" % (File.stat(logfile).mode & 0777)
333
+ end
334
+
335
+ it "follows a symlink to it, and keeps the mode of the file it points to" do
336
+ target = tmp_path('target.log')
337
+ File.write(target, "")
338
+ File.chmod(0600, target)
339
+ logfile = tmp_path('dante.log')
340
+ File.symlink(target, logfile)
341
+ run_with_log(logfile)
342
+
343
+ assert File.symlink?(logfile)
344
+ assert_equal "Started\n", File.read(target)
345
+ assert_equal "600", "%o" % (File.stat(target).mode & 0777)
346
+ end
347
+ end # existing log file
348
+
349
+ describe "with the standard streams connected to a socket" do
350
+ # Runs the block in a forked child whose stdin, stdout and stderr are one end of a
351
+ # socket pair, as under Spring or Zeus. Returns the child's exit status and the
352
+ # error it raised, if any. The error goes through a file rather than a pipe, which
353
+ # a daemon started by the block would hold open.
354
+ def run_with_socket_streams
355
+ error_path = tmp_path('child-error')
356
+ pid = fork do
357
+ begin
358
+ socket, _peer = UNIXSocket.pair
359
+ [STDIN, STDOUT, STDERR].each { |io| io.reopen(socket) }
360
+ $stdin, $stdout, $stderr = STDIN, STDOUT, STDERR
361
+ yield
362
+ rescue Exception => e
363
+ File.write(error_path, "#{e.class}: #{e.message}")
364
+ exit!(1)
365
+ end
366
+ exit!(0)
367
+ end
368
+ Process.wait(pid)
369
+ [$?.exitstatus, File.exist?(error_path) ? File.read(error_path) : ""]
370
+ end
371
+
372
+ def null_device?(io)
373
+ io.stat.chardev? && io.stat.rdev == File.stat(File::NULL).rdev
374
+ end
375
+
376
+ it "appends to the log file when run in the foreground" do
377
+ @logfile = tmp_path('dante-socket.log')
378
+ File.write(@logfile, "earlier line\n")
379
+ status, error = run_with_socket_streams do
380
+ Dante::Runner.new('test-process', :log_path => @logfile) { puts "Started" }.execute
381
+ end
382
+
383
+ assert_equal [0, ""], [status, error]
384
+ assert_equal "earlier line\nStarted\n", File.read(@logfile)
385
+ assert_equal 0644, File.stat(@logfile).mode & 0777
386
+ end
387
+
388
+ it "logs to the log file when daemonized" do
389
+ @logfile = tmp_path('dante-socket.log')
390
+ @pid_path = tmp_path('dante.pid')
391
+ @process = TestingProcess.new('c', @tmpdir)
392
+ status, error = run_with_socket_streams do
393
+ Dante::Runner.new('test-process', :daemonize => true, :pid_path => @pid_path, :log_path => @logfile) {
394
+ @process.run_c!(8082) }.execute
395
+ end
396
+
397
+ assert_equal [0, ""], [status, error]
398
+ wait_for_output(@logfile, /Started on 8082!!/)
399
+ assert_equal 0644, File.stat(@logfile).mode & 0777
400
+ end
401
+
402
+ it "gives the daemon /dev/null as its standard input" do
403
+ stdin_path = tmp_path('daemon-stdin')
404
+ status, error = run_with_socket_streams do
405
+ Dante::Runner.new('test-process', :daemonize => true, :pid_path => tmp_path('dante.pid'), :log_path => tmp_path('dante.log')) {
406
+ File.write("#{stdin_path}.tmp", null_device?($stdin) ? "/dev/null" : $stdin.stat.ftype)
407
+ File.rename("#{stdin_path}.tmp", stdin_path)
408
+ sleep 100 }.execute
409
+ end
410
+
411
+ assert_equal [0, ""], [status, error]
412
+ wait_for_output(stdin_path)
413
+ assert_equal "/dev/null", File.read(stdin_path)
414
+ end
415
+
416
+ it "gives the daemon /dev/null as its standard input when $stdin is not STDIN" do
417
+ stdin_path = tmp_path('daemon-stdin')
418
+ status, error = run_with_socket_streams do
419
+ $stdin = StringIO.new("typed")
420
+ started = Dante::Runner.new('test-process', :daemonize => true, :pid_path => tmp_path('dante.pid'), :log_path => tmp_path('dante.log')) {
421
+ File.write("#{stdin_path}.tmp", null_device?(STDIN) ? "/dev/null" : STDIN.stat.ftype)
422
+ File.rename("#{stdin_path}.tmp", stdin_path)
423
+ sleep 100 }.execute
424
+ raise "not started" unless started
425
+ end
426
+
427
+ assert_equal [0, ""], [status, error]
428
+ wait_for_output(stdin_path)
429
+ assert_equal "/dev/null", File.read(stdin_path)
430
+ end
431
+
432
+ it "gives the daemon /dev/null as its standard input when STDIN is closed" do
433
+ stdin_path = tmp_path('daemon-stdin')
434
+ status, error = run_with_socket_streams do
435
+ STDIN.close
436
+ started = Dante::Runner.new('test-process', :daemonize => true, :pid_path => tmp_path('dante.pid'), :log_path => tmp_path('dante.log')) {
437
+ fd0 = IO.for_fd(0, autoclose: false)
438
+ File.write("#{stdin_path}.tmp", null_device?(fd0) ? "/dev/null" : fd0.stat.ftype)
439
+ File.rename("#{stdin_path}.tmp", stdin_path)
440
+ sleep 100 }.execute
441
+ raise "not started" unless started
442
+ end
443
+
444
+ assert_equal [0, ""], [status, error]
445
+ wait_for_output(stdin_path)
446
+ assert_equal "/dev/null", File.read(stdin_path)
447
+ end
448
+
449
+ it "redirects all three streams to /dev/null without a log file" do
450
+ status, error = run_with_socket_streams do
451
+ Dante::Runner.new('test-process', :log_path => false).send(:redirect_output!)
452
+ streams = [STDIN, STDOUT, STDERR]
453
+ raise "not redirected to /dev/null: #{streams.reject { |io| null_device?(io) }.map(&:fileno)}" unless streams.all? { |io| null_device?(io) }
454
+ end
455
+
456
+ assert_equal [0, ""], [status, error]
457
+ end
458
+ end # socket streams
459
+
460
+ describe "with the pid file naming a process of another user" do
461
+ before do
462
+ @pid_path = tmp_path('dante.pid')
463
+ if Process.euid == 0
464
+ # A process of root's; the blocks below run as nobody.
465
+ @foreign_pid = @sleeper = spawn('sleep', '60')
466
+ else
467
+ @foreign_pid = 1
468
+ begin
469
+ Process.kill(0, @foreign_pid)
470
+ skip "pid #{@foreign_pid} can be signalled, so there is no process of another user to name"
471
+ rescue Errno::EPERM
472
+ end
473
+ end
474
+ File.write(@pid_path, "#{@foreign_pid}\n")
475
+ File.chmod(0755, @tmpdir)
476
+ File.chmod(0644, @pid_path)
477
+ end
478
+
479
+ after do
480
+ FileUtils.rm_f(@pid_path) if @pid_path
481
+ if @sleeper
482
+ Process.kill('KILL', @sleeper)
483
+ Process.wait(@sleeper)
484
+ end
485
+ end
486
+
487
+ # Runs the block in a forked child that may not signal @foreign_pid (as nobody, when
488
+ # the tests run as root). Returns what the block returned or raised, and what it
489
+ # printed.
490
+ def run_without_permission_to_signal
491
+ out_path = tmp_path('out')
492
+ reader, writer = IO.pipe
493
+ pid = fork do
494
+ reader.close
495
+ $stdout = File.open(out_path, 'w')
496
+ $stdout.sync = true
497
+ result = begin
498
+ if Process.euid == 0
499
+ nobody = Etc.getpwnam('nobody')
500
+ Process.groups = [nobody.gid]
501
+ Process::GID.change_privilege(nobody.gid)
502
+ Process::UID.change_privilege(nobody.uid)
503
+ end
504
+ raise "still root" if Process.euid == 0
505
+ [:returned, yield]
506
+ rescue Exception => e
507
+ [:raised, "#{e.class}: #{e.message}"]
508
+ end
509
+ writer.write(Marshal.dump(result))
510
+ exit!(0)
511
+ end
512
+ writer.close
513
+ Process.wait(pid)
514
+ [Marshal.load(reader.read), File.read(out_path)]
515
+ ensure
516
+ reader.close if reader && !reader.closed?
517
+ end
518
+
519
+ it "reports the daemon as running" do
520
+ result, _output = run_without_permission_to_signal do
521
+ Dante::Runner.new('test-process', :pid_path => @pid_path).daemon_running?
522
+ end
523
+
524
+ assert_equal [:returned, true], result
525
+ end
526
+
527
+ it "does not start a second daemon" do
528
+ result, output = run_without_permission_to_signal do
529
+ Dante::Runner.new('test-process', :daemonize => true, :pid_path => @pid_path, :log_path => tmp_path('dante.log')) {
530
+ raise "should not get here" }.execute
531
+ nil
532
+ end
533
+
534
+ assert_equal [:returned, nil], result
535
+ assert_equal "Process is already started\n", output
536
+ end
537
+
538
+ it "reports that it could not stop it with -k, and keeps the pid file" do
539
+ result, output = run_without_permission_to_signal do
540
+ ARGV.replace(['-k', '-P', @pid_path])
541
+ Dante::Runner.new('test-process').execute
542
+ nil
543
+ end
544
+
545
+ assert_equal [:returned, nil], result
546
+ assert_match(/Failed to stop! : Operation not permitted/, output)
547
+ assert_match(/Failed to kill daemonized process/, output)
548
+ assert_equal "#{@foreign_pid}\n", File.read(@pid_path)
549
+ end
550
+
551
+ it "stops waiting at once when it cannot signal it, even with :force" do
552
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
553
+ result, output = run_without_permission_to_signal do
554
+ Dante::Runner.new('test-process', :pid_path => @pid_path, :force => true).stop
555
+ nil
556
+ end
557
+ elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
558
+
559
+ assert_equal [:returned, nil], result
560
+ assert_match(/Failed to kill daemonized process/, output)
561
+ refute_match(/Sent SIG/, output)
562
+ assert_operator elapsed, :<, Dante::Runner::MAX_START_TRIES / 2.0, "waited for a daemon it could not signal"
563
+ assert_equal "#{@foreign_pid}\n", File.read(@pid_path)
564
+ end
565
+ end # another user's process
566
+
567
+ describe "with an empty pid file, as left by a crash between creating and writing it" do
568
+ before do
569
+ @pid_path = tmp_path('dante.pid')
570
+ File.write(@pid_path, "")
571
+ end
572
+
573
+ it "does not count as a running daemon, nor does a pid file naming no process" do
574
+ ["", "0\n", "-1\n"].each do |content|
575
+ File.write(@pid_path, content)
576
+ assert_equal false, Dante::Runner.new('test-process', :pid_path => @pid_path).daemon_running?, "pid file #{content.inspect}"
577
+ end
578
+ end
579
+
580
+ it "does not count a number too large for a pid as a daemon, running or to stop" do
581
+ ["2147483648\n", "99999999999999999999\n"].each do |content|
582
+ File.write(@pid_path, content)
583
+ runner = Dante::Runner.new('test-process', :pid_path => @pid_path)
584
+ assert_equal false, runner.daemon_running?, "pid file #{content.inspect}"
585
+ assert_equal "No test-process processes are running\n", capture_stdout { runner.stop }, "pid file #{content.inspect}"
586
+ end
587
+ end
588
+
589
+ it "starts a daemon in its place" do
590
+ started = tmp_path('started')
591
+ result = nil
592
+ output = capture_stdout {
593
+ result = Dante::Runner.new('test-process', :daemonize => true, :pid_path => @pid_path, :log_path => tmp_path('dante.log')) {
594
+ File.write(started, ""); sleep 100 }.execute }
595
+
596
+ assert_equal true, result
597
+ assert_match(/Daemon has started successfully/, output)
598
+ wait_for_output(started)
599
+ assert process_alive?(daemon_pid(@pid_path))
600
+ end
601
+
602
+ it "reports that no daemon is running when asked to stop, and signals nothing" do
603
+ # The stop runs in a child leading a process group of its own, which is all that a
604
+ # signal to "pid 0" (the caller's process group) could reach.
605
+ out_path = tmp_path('out')
606
+ pid = fork do
607
+ Process.setpgid(0, 0)
608
+ $stdout = File.open(out_path, 'w')
609
+ $stdout.sync = true
610
+ p Dante::Runner.new('test-process', :pid_path => @pid_path).stop
611
+ exit!(0)
612
+ end
613
+ Process.wait(pid)
614
+
615
+ assert_equal "No test-process processes are running\nfalse\n", File.read(out_path)
616
+ assert_equal 0, $?.exitstatus, "the stop ended with #{$?.inspect}"
617
+ assert_equal "", File.read(@pid_path)
618
+ end
619
+ end # empty pid file
620
+
621
+ describe "with daemonize flag, controlling a running daemon" do
622
+ before do
623
+ @pid_path = tmp_path('dante.pid')
624
+ @logfile = tmp_path('dante.log')
625
+ @first_started = tmp_path('first-started')
626
+ @run_options = { :daemonize => true, :pid_path => @pid_path, :log_path => @logfile }
627
+ first_started = @first_started
628
+ @start_result = nil
629
+ @stdout = capture_stdout {
630
+ @start_result = Dante::Runner.new('test-process', @run_options) { File.write(first_started, ""); sleep 100 }.execute }
631
+ wait_for_output(@first_started)
632
+ @pid = daemon_pid(@pid_path)
633
+ end
634
+
635
+ it "runs the daemon in a session of its own, which it does not lead" do
636
+ assert_equal true, @start_result
637
+ refute_equal Process.getsid, Process.getsid(@pid)
638
+ refute_equal @pid, Process.getsid(@pid)
639
+ end
640
+
641
+ it "does not start a second daemon on the same pid file" do
642
+ second_started = tmp_path('second-started')
643
+ result = :not_returned
644
+ output = capture_stdout {
645
+ result = Dante::Runner.new('test-process', @run_options) { File.write(second_started, "") }.execute }
646
+
647
+ assert_equal ["Process is already started\n"], output.lines.grep_v(/: warning: /) # Ruby 2.7 -w warns on stderr
648
+ assert_nil result
649
+ assert_equal "#{@pid}\n", File.read(@pid_path)
650
+ assert process_alive?(@pid)
651
+ end
652
+
653
+ it "stops the daemon and starts a new one with :restart" do
654
+ second_started = tmp_path('second-started')
655
+ result = nil
656
+ output = capture_stdout {
657
+ result = Dante::Runner.new('test-process', @run_options.merge(:restart => true)) {
658
+ File.write(second_started, ""); sleep 100 }.execute }
659
+
660
+ assert_equal true, result
661
+ assert_match(/Daemonized process killed after term.*Daemon has started successfully/m, output)
662
+ refute process_alive?(@pid), "the first daemon is still running"
663
+ wait_for_output(second_started)
664
+ new_pid = daemon_pid(@pid_path)
665
+ refute_equal @pid, new_pid
666
+ assert process_alive?(new_pid)
667
+ end
668
+
669
+ it "restarts the daemon as a daemon with #restart" do
670
+ ran_in = tmp_path('ran-in')
671
+ test_pid = Process.pid
672
+ runner = Dante::Runner.new('test-process', @run_options) {
673
+ File.write("#{ran_in}.tmp", Process.pid.to_s)
674
+ File.rename("#{ran_in}.tmp", ran_in)
675
+ sleep 100 unless Process.pid == test_pid } # returns if it runs in this process
676
+ result = nil
677
+ output = capture_stdout { result = runner.restart }
678
+
679
+ wait_for_output(ran_in)
680
+ refute_equal test_pid.to_s, File.read(ran_in), "the service ran in the caller"
681
+ assert_equal true, result
682
+ assert_match(/Daemonized process killed after term.*Daemon has started successfully/m, output)
683
+ refute process_alive?(@pid), "the first daemon is still running"
684
+ assert_equal daemon_pid(@pid_path).to_s, File.read(ran_in)
685
+ end
686
+
687
+ it "neither stops nor restarts the daemon with :kill or :restart given as false" do
688
+ [{ :kill => false }, { :restart => false }].each do |off|
689
+ output = capture_stdout {
690
+ Dante::Runner.new('test-process', @run_options.merge(off)) { sleep 100 }.execute }
691
+
692
+ assert_equal ["Process is already started\n"], output.lines.grep_v(/: warning: /), off.inspect
693
+ assert_equal "#{@pid}\n", File.read(@pid_path), off.inspect
694
+ assert process_alive?(@pid), off.inspect
695
+ end
696
+ end
697
+
698
+ it "stops the daemon with -k and removes its pid file" do
699
+ ARGV.replace(['-k', '-P', @pid_path])
700
+ output = capture_stdout { Dante::Runner.new('test-process').execute }
701
+
702
+ assert_match(/Sent SIGTERM to PID: #{@pid}/, output)
703
+ assert_match(/Daemonized process killed after term/, output)
704
+ refute process_alive?(@pid), "daemon still running after -k"
705
+ refute File.exist?(@pid_path), "pid file left behind after -k"
706
+ end
707
+ end # controlling a running daemon
708
+
709
+ describe "with daemonize flag, stopping a daemon while its pid file changes" do
710
+ before do
711
+ @pid_path = tmp_path('dante.pid')
712
+ @ready = tmp_path('ready')
713
+ end
714
+
715
+ after do
716
+ if @replacement
717
+ Process.kill('KILL', @replacement)
718
+ Process.wait(@replacement)
719
+ end
720
+ end
721
+
722
+ # Starts a daemon that runs on_term when it gets SIGTERM, and returns its pid.
723
+ def start_daemon(pid_path = @pid_path, &on_term)
724
+ ready = @ready
725
+ capture_stdout {
726
+ Dante::Runner.new('test-process', :daemonize => true, :pid_path => pid_path, :log_path => tmp_path('dante.log')) {
727
+ trap('TERM', &on_term) if on_term
728
+ File.write(ready, "")
729
+ sleep 100 }.execute }
730
+ wait_for_output(ready)
731
+ daemon_pid(pid_path)
732
+ end
733
+
734
+ it "stops it from two stops at once, neither of them failing" do
735
+ pid = start_daemon { sleep 0.5; exit }
736
+ output = capture_stdout {
737
+ stoppers = 2.times.map { Thread.new { Dante::Runner.new('test-process', :pid_path => @pid_path).stop } }
738
+ stoppers.each { |t| t.report_on_exception = false }
739
+ stoppers.each(&:value) }
740
+
741
+ assert_equal 2, output.scan(/Daemonized process killed after term/).size, output
742
+ refute process_alive?(pid)
743
+ refute File.exist?(@pid_path), "pid file left behind"
744
+ end
745
+
746
+ it "keeps the pid file when a new daemon recorded its pid there meanwhile" do
747
+ replacement = @replacement = spawn('sleep', '60')
748
+ pid_path = @pid_path
749
+ pid = start_daemon { File.write(pid_path, "#{replacement}\n"); exit }
750
+ output = capture_stdout { Dante::Runner.new('test-process', :pid_path => @pid_path).stop }
751
+
752
+ assert_match(/Daemonized process killed after term/, output)
753
+ refute process_alive?(pid)
754
+ assert_equal "#{replacement}\n", File.read(@pid_path)
755
+ end
756
+
757
+ it "sends SIGKILL with :force to the daemon it signalled, not to a new one in the pid file" do
758
+ replacement = @replacement = spawn('sleep', '60')
759
+ pid_path = @pid_path
760
+ pid = start_daemon { File.write(pid_path, "#{replacement}\n") } # survives SIGTERM
761
+ output = capture_stdout { Dante::Runner.new('test-process', :pid_path => @pid_path, :force => true).stop }
762
+
763
+ assert_match(/Sent SIGKILL to PID: #{pid} /, output)
764
+ assert_match(/Daemonized process killed after kill/, output)
765
+ refute process_alive?(pid)
766
+ assert process_alive?(replacement), "the new daemon was killed"
767
+ assert_equal "#{replacement}\n", File.read(@pid_path)
768
+ end
769
+
770
+ it "returns soon after the daemon stops, well within the second it used to wait first" do
771
+ # A process of this test's that takes 0.2 seconds to stop on SIGTERM, reaped as soon
772
+ # as it exits (a daemon is reaped by init), stands in for the daemon.
773
+ script = "trap('TERM') { sleep 0.2; exit }; File.write(ARGV[0], ''); sleep 100"
774
+ pid = spawn(RbConfig.ruby, '-e', script, @ready)
775
+ reaper = Process.detach(pid)
776
+ wait_for_output(@ready)
777
+ File.write(@pid_path, "#{pid}\n")
778
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
779
+ output = capture_stdout { Dante::Runner.new('test-process', :pid_path => @pid_path).stop }
780
+ elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
781
+
782
+ assert_match(/Daemonized process killed after term/, output)
783
+ assert_operator elapsed, :<, 0.8
784
+ ensure
785
+ if reaper && reaper.alive?
786
+ Process.kill('KILL', pid)
787
+ reaper.join
788
+ end
789
+ end
790
+
791
+ it "stops a daemon whose pid file path has glob characters in it" do
792
+ pid_path = tmp_path('run[1]/dante.pid')
793
+ pid = start_daemon(pid_path)
794
+ output = capture_stdout { Dante::Runner.new('test-process', :pid_path => pid_path).stop }
795
+
796
+ assert_match(/Daemonized process killed after term/, output)
797
+ refute process_alive?(pid)
798
+ refute File.exist?(pid_path), "pid file left behind"
799
+ end
800
+ end # pid file changes
801
+
802
+ describe "with daemonize flag and no log path" do
803
+ before do
804
+ # Records the log path the daemon would redirect its output to, then sends the
805
+ # output to a file of this test's instead of writing to /var/log.
806
+ recorded = @recorded = tmp_path('recorded-log-path')
807
+ logfile = tmp_path('dante.log')
808
+ recording_runner = Class.new(Dante::Runner) do
809
+ define_method(:redirect_output!) do
810
+ File.write("#{recorded}.tmp", options[:log_path].inspect)
811
+ File.rename("#{recorded}.tmp", recorded)
812
+ options[:log_path] = logfile
813
+ super()
814
+ end
815
+ end
816
+ @pid_path = tmp_path('dante.pid')
817
+ @stdout = capture_stdout {
818
+ recording_runner.new('test-process', :daemonize => true, :pid_path => @pid_path) { sleep 100 }.execute }
819
+ daemon_pid(@pid_path)
820
+ wait_for_output(@recorded)
821
+ end
822
+
823
+ it "logs to /var/log/<name>.log" do
824
+ assert_equal '"/var/log/test-process.log"', File.read(@recorded)
825
+ end
826
+ end # default log path
827
+
828
+ describe "with daemonize flag, when the daemon dies before recording its pid" do
829
+ it "reports that it could not be started" do
830
+ # The daemon fails to create the pid file's directory, which is a device.
831
+ pid_path = File.join(File::NULL, 'dante.pid')
832
+ result = nil
833
+ output = capture_stdout {
834
+ result = Dante::Runner.new('test-process', :daemonize => true, :pid_path => pid_path,
835
+ :log_path => tmp_path('dante.log')) { raise "should not get here" }.execute }
836
+
837
+ assert_equal false, result
838
+ assert_match(/Daemonized process couldn't be started/, output)
839
+ refute_match(/started successfully/, output)
840
+ end
841
+ end # dies at once
842
+
843
+ describe "with daemonize flag, when the daemon dies right after recording its pid" do
844
+ it "reports that it could not be started" do
845
+ pid_path = tmp_path('dante.pid')
846
+ # After the check made before starting, checks for the daemon only once its pid is
847
+ # recorded, so the check falls in the moment between the daemon starting up and dying.
848
+ checking_runner = Class.new(Dante::Runner) do
849
+ define_method(:daemon_running?) do
850
+ if @checked_before_starting
851
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 10
852
+ sleep 0.01 until (File.exist?(pid_path) && File.read(pid_path).to_i > 0) ||
853
+ Process.clock_gettime(Process::CLOCK_MONOTONIC) > deadline
854
+ end
855
+ @checked_before_starting = true
856
+ super()
857
+ end
858
+ end
859
+ result = nil
860
+ output = capture_stdout {
861
+ result = checking_runner.new('test-process', :daemonize => true, :pid_path => pid_path,
862
+ :log_path => tmp_path('dante.log')) { sleep 0.2; raise "boom" }.execute }
863
+
864
+ assert_equal false, result
865
+ assert_match(/Daemonized process couldn't be started/, output)
866
+ refute_match(/started successfully/, output)
867
+ assert_match(/Starting test-process/, File.read(tmp_path('dante.log'))) # it did start, then died
868
+ end
869
+
870
+ it "removes the pid file the daemon left" do
871
+ pid_path = tmp_path('dante.pid')
872
+ result = nil
873
+ capture_stdout {
874
+ result = Dante::Runner.new('test-process', :daemonize => true, :pid_path => pid_path,
875
+ :log_path => tmp_path('dante.log')) { raise "boom" }.execute }
876
+
877
+ assert_equal false, result
878
+ assert_match(/Starting test-process/, File.read(tmp_path('dante.log'))) # it recorded its pid and started
879
+ refute File.exist?(pid_path), "the dead daemon's pid file was left behind"
880
+ end
881
+
882
+ it "keeps the pid file when it names another process" do
883
+ pid_path = tmp_path('dante.pid')
884
+ other = spawn('sleep', '30')
885
+ # The daemon records another process's pid, as if another start had replaced the
886
+ # pid file after this daemon wrote it.
887
+ other_runner = Class.new(Dante::Runner) do
888
+ define_method(:store_pid) { |_pid| super(other) }
889
+ end
890
+ result = nil
891
+ capture_stdout {
892
+ result = other_runner.new('test-process', :daemonize => true, :pid_path => pid_path,
893
+ :log_path => tmp_path('dante.log')) { raise "boom" }.execute }
894
+
895
+ assert_equal false, result
896
+ assert_equal "#{other}\n", File.read(pid_path)
897
+ ensure
898
+ if other
899
+ Process.kill('KILL', other)
900
+ Process.wait(other)
901
+ end
902
+ end
903
+ end # dies after recording its pid
904
+
905
+ describe "with daemonize flag, interrupted while the daemon starts up" do
906
+ it "leaves no zombie behind" do
907
+ pid_path = tmp_path('dante.pid')
908
+ leader_path = tmp_path('leader')
909
+ leader = nil
910
+ # The daemon records its parent, the session leader, which then waits out the
911
+ # grace period while the caller waits for it; that wait is interrupted.
912
+ interrupter = Thread.new(Thread.current) do |caller|
913
+ leader = wait_until { File.exist?(leader_path) && (n = File.read(leader_path).to_i) > 0 && n }
914
+ caller.raise(Interrupt) if leader
915
+ end
916
+ capture_stdout {
917
+ assert_raises(Interrupt) {
918
+ Dante::Runner.new('test-process', :daemonize => true, :pid_path => pid_path, :log_path => tmp_path('dante.log')) {
919
+ File.write("#{leader_path}.tmp", Process.ppid.to_s)
920
+ File.rename("#{leader_path}.tmp", leader_path)
921
+ sleep 100 }.execute } }
922
+ interrupter.join
923
+ assert leader, "the daemon never recorded its parent"
924
+
925
+ # A zombie still answers signal 0; a reaped process does not.
926
+ gone = wait_until(10) do
927
+ begin
928
+ Process.kill(0, leader)
929
+ false
930
+ rescue Errno::ESRCH
931
+ true
932
+ end
933
+ end
934
+ assert gone, "the session leader #{leader} was left as a zombie"
935
+ ensure
936
+ begin
937
+ Process.waitpid(leader) if leader
938
+ rescue Errno::ECHILD
939
+ end
940
+ end
941
+ end # interrupted while starting
942
+
943
+ describe "with daemonize flag, in a program of its own" do
944
+ # Runs script with dante loaded in a separate Ruby process, passing it args, and
945
+ # returns what it printed, without Ruby 2.7's -w warnings. A process of its own,
946
+ # because the test harness keeps forked processes from running at_exit handlers.
947
+ # It runs outside Bundler's environment: it needs only dante, and bundler/setup
948
+ # would print git's errors where the tree is not a git checkout.
949
+ def run_program(script, *args)
950
+ lib = File.expand_path('../../lib', __FILE__)
951
+ run = lambda { IO.popen([RbConfig.ruby, '-I', lib, '-rdante', '-e', script, *args], :err => [:child, :out], &:read) }
952
+ output = defined?(Bundler) ? Bundler.with_unbundled_env(&run) : run.call
953
+ output.lines.grep_v(/: warning: /).join
954
+ end
955
+
956
+ before do
957
+ @pid_path = tmp_path('dante.pid')
958
+ @logfile = tmp_path('dante.log')
959
+ end
960
+
961
+ it "reports a running daemon as started whatever the program's at_exit handlers do" do
962
+ output = run_program(<<-RUBY, @pid_path, @logfile)
963
+ main = Process.pid
964
+ at_exit { exit 1 if Process.pid != main }
965
+ p Dante::Runner.new('test-process', :daemonize => true, :pid_path => ARGV[0], :log_path => ARGV[1]) { sleep 30 }.execute
966
+ RUBY
967
+
968
+ assert_equal "Daemon has started successfully\ntrue\n", output
969
+ pid = daemon_pid(@pid_path)
970
+ Process.kill('TERM', pid)
971
+ wait_for_exit(pid)
972
+ end
973
+
974
+ it "reports whether the daemon started when the program ignores SIGCHLD" do
975
+ script = <<-RUBY
976
+ trap('CHLD', 'IGNORE')
977
+ p Dante::Runner.new('test-process', :daemonize => true, :pid_path => ARGV[0], :log_path => ARGV[1]) {
978
+ raise "boom" if ARGV[2] == 'crash'
979
+ sleep 30 }.execute
980
+ RUBY
981
+
982
+ assert_equal "Daemon has started successfully\ntrue\n", run_program(script, @pid_path, @logfile, 'run')
983
+ pid = daemon_pid(@pid_path)
984
+ Process.kill('TERM', pid)
985
+ wait_for_exit(pid)
986
+
987
+ crash_pid_path = tmp_path('crash.pid')
988
+ assert_equal "Daemonized process couldn't be started\nfalse\n", run_program(script, crash_pid_path, @logfile, 'crash')
989
+ refute File.exist?(crash_pid_path), "the dead daemon's pid file was left behind"
990
+ end
991
+ end # program of its own
992
+
993
+ describe "with the -p option" do
994
+ it "passes the port to the startup block as an Integer" do
995
+ ARGV.replace(['-p', '1234'])
996
+ port = nil
997
+ capture_stdout { Dante::Runner.new('test-process').execute { |opts| port = opts[:port] } }
998
+
999
+ assert_equal 1234, port
1000
+ assert_kind_of Integer, port
1001
+ end
1002
+ end # -p
1003
+
1004
+ describe "with the :argv option" do
1005
+ before do
1006
+ ARGV.replace(['--host-program-flag', 'file.rb']) # options dante does not know
1007
+ end
1008
+
1009
+ # The port the startup block of runner gets, and ARGV afterwards.
1010
+ def port_from(runner, opts = {})
1011
+ port = :not_run
1012
+ capture_stdout { runner.execute(opts) { |o| port = o[:port] } }
1013
+ [port, ARGV]
1014
+ end
1015
+
1016
+ it "parses the given options instead of ARGV, and leaves both arrays as they were" do
1017
+ argv = ['-p', '1234', 'extra'].freeze
1018
+
1019
+ assert_equal [1234, ['--host-program-flag', 'file.rb']], port_from(Dante::Runner.new('test-process', :argv => argv))
1020
+ assert_equal ['-p', '1234', 'extra'], argv
1021
+ end
1022
+
1023
+ it "parses nothing when given an empty array" do
1024
+ assert_equal [nil, ['--host-program-flag', 'file.rb']], port_from(Dante::Runner.new('test-process', :argv => []))
1025
+ end
1026
+
1027
+ it "takes the options to parse from execute too" do
1028
+ assert_equal [4321, ['--host-program-flag', 'file.rb']], port_from(Dante::Runner.new('test-process'), :argv => ['-p', '4321'])
1029
+ end
1030
+
1031
+ it "takes the options to parse from Dante.run" do
1032
+ port = :not_run
1033
+ capture_stdout { Dante.run('test-process', :argv => ['-p', '1234']) { |o| port = o[:port] } }
1034
+
1035
+ assert_equal 1234, port
1036
+ end
1037
+ end # :argv
1038
+
143
1039
  describe "with parsing options" do
144
1040
  before do
145
- Object.send(:remove_const, 'ARGV'); ARGV = ['-t test_text']
146
- @process = TestingProcess.new('a')
1041
+ ARGV.replace(['-t test_text'])
1042
+ @process = TestingProcess.new('a', @tmpdir)
147
1043
  @runner = Dante::Runner.new('test-process')
148
1044
  @runner.with_options do |opts|
149
1045
  opts.on("-t", "--test TEST", String, "Test this thing") { |test| options[:test] = test }
@@ -152,32 +1048,33 @@ describe "dante runner" do
152
1048
  end
153
1049
 
154
1050
  it "prints correct stdout" do
155
- assert_match /Starting test-process/, @stdout
1051
+ assert_match(/Starting test-process/, @stdout)
156
1052
  end
157
1053
 
158
1054
  it "prints correct data" do
159
1055
  @output = File.read(@process.tmp_path)
160
- assert_match /test_text/, @output
1056
+ assert_match(/test_text/, @output)
161
1057
  end
162
1058
 
163
1059
  it "starts successfully when executed" do
164
1060
  @output = File.read(@process.tmp_path)
165
- assert_match /Started/, @output
1061
+ assert_match(/Started/, @output)
166
1062
  end
167
1063
  end # options parsing
168
1064
 
169
1065
  describe "with help command" do
170
1066
  before do
171
- Object.send(:remove_const, 'ARGV'); ARGV = ['--help']
172
- @process = TestingProcess.new('a')
1067
+ ARGV.replace(['--help'])
1068
+ @process = TestingProcess.new('a', @tmpdir)
173
1069
  @runner = Dante::Runner.new('test-process')
174
1070
  @runner.description = "Test process banana"
175
- @runner.expects(:exit).once.returns(true)
176
- @stdout = capture_stdout { @runner.execute { @process.run_a! } }
1071
+ @stdout = capture_stdout {
1072
+ assert_raises(SystemExit) { @runner.execute { @process.run_a! } }
1073
+ }
177
1074
  end
178
1075
 
179
1076
  it "prints correct stdout" do
180
- assert_match /test-process - Test process banana/, @stdout
1077
+ assert_match(/test-process - Test process banana/, @stdout)
181
1078
  end
182
1079
  end # help
183
- end
1080
+ end