File: test_god.rb

package info (click to toggle)
ruby-god 0.12.1-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 752 kB
  • sloc: ruby: 5,913; ansic: 217; makefile: 3
file content (648 lines) | stat: -rw-r--r-- 14,704 bytes parent folder | download
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
require File.dirname(__FILE__) + '/helper'

class TestGod < Test::Unit::TestCase
  def setup
    God::Socket.stubs(:new).returns(true)
    God.stubs(:setup).returns(true)
    God.stubs(:validater).returns(true)
    God.reset
    God.pid_file_directory = '/var/run/god'
  end

  def teardown
    God.main && God.main.kill
    if God.watches
      God.watches.each do |k, w|
        w.driver.thread.kill
      end
    end
  end

  # applog

  def test_applog
    LOG.expects(:log).with(nil, :debug, 'foo')
    applog(nil, :debug, 'foo')
  end

  # internal_init

  def test_init_should_initialize_watches_to_empty_array
    God.internal_init { }
    assert_equal Hash.new, God.watches
  end

  # init

  def test_pid_file_directory_should_abort_if_called_after_watch
    God.watch { |w| w.name = 'foo'; w.start = 'bar' }

    assert_abort do
      God.pid_file_directory = 'foo'
    end
  end

  # pid_file_directory

  def test_pid_file_directory_should_return_default_if_not_set_explicitly
    God.internal_init
    assert_equal '/var/run/god', God.pid_file_directory
  end

  def test_pid_file_directory_equals_should_set
    God.pid_file_directory = '/foo'
    God.internal_init
    assert_equal '/foo', God.pid_file_directory
  end

  # watch

  def test_watch_should_get_stored
    watch = nil
    God.watch do |w|
      w.name = 'foo'
      w.start = 'bar'
      watch = w
    end

    assert_equal 1, God.watches.size
    assert_equal watch, God.watches.values.first

    assert_equal 0, God.groups.size
  end

  def test_watch_should_get_stored_in_pending_watches
    watch = nil
    God.watch do |w|
      w.name = 'foo'
      w.start = 'bar'
      watch = w
    end

    assert_equal 1, God.pending_watches.size
    assert_equal watch, God.pending_watches.first
  end

  def test_watch_should_register_processes
    assert_nil God.registry['foo']
    God.watch do |w|
      w.name = 'foo'
      w.start = 'bar'
    end
    assert_kind_of God::Process, God.registry['foo']
  end

  def test_watch_should_get_stored_by_group
    a = nil

    God.watch do |w|
      a = w
      w.name = 'foo'
      w.start = 'bar'
      w.group = 'test'
    end

    assert_equal({'test' => [a]}, God.groups)
  end

  def test_watches_should_get_stored_by_group
    a = nil
    b = nil

    God.watch do |w|
      a = w
      w.name = 'foo'
      w.start = 'bar'
      w.group = 'test'
    end

    God.watch do |w|
      b = w
      w.name = 'bar'
      w.start = 'baz'
      w.group = 'test'
    end

    assert_equal({'test' => [a, b]}, God.groups)
  end

  def test_watch_should_allow_multiple_watches
    God.watch { |w| w.name = 'foo'; w.start = 'bar' }

    assert_nothing_raised do
      God.watch { |w| w.name = 'bar'; w.start = 'bar' }
    end
  end

  def test_watch_should_disallow_duplicate_watch_names
    God.watch { |w| w.name = 'foo'; w.start = 'bar' }

    assert_abort do
      God.watch { |w| w.name = 'foo'; w.start = 'bar' }
    end
  end

  def test_watch_should_disallow_identical_watch_and_group_names
    God.watch { |w| w.name = 'foo'; w.group = 'bar'; w.start = 'bar' }

    assert_abort do
      God.watch { |w| w.name = 'bar'; w.start = 'bar' }
    end
  end

  def test_watch_should_disallow_identical_watch_and_group_names_other_way
    God.watch { |w| w.name = 'bar'; w.start = 'bar' }

    assert_abort do
      God.watch { |w| w.name = 'foo'; w.group = 'bar'; w.start = 'bar' }
    end
  end

  def test_watch_should_unwatch_new_watch_if_running_and_duplicate_watch
    God.watch { |w| w.name = 'foo'; w.start = 'bar' }
    God.running = true

    assert_nothing_raised do
      God.watch { |w| w.name = 'foo'; w.start = 'bar' }
    end
  end

  # unwatch

  def test_unwatch_should_unmonitor_watch
    God.watch { |w| w.name = 'bar'; w.start = 'bar' }
    w = God.watches['bar']
    w.state = :up
    w.expects(:unmonitor)
    God.unwatch(w)
  end

  def test_unwatch_should_unregister_watch
    God.watch { |w| w.name = 'bar'; w.start = 'bar' }
    w = God.watches['bar']
    w.expects(:unregister!)
    God.unwatch(w)
  end

  def test_unwatch_should_remove_same_name_watches
    God.watch { |w| w.name = 'bar'; w.start = 'bar' }
    w = God.watches['bar']
    God.unwatch(w)
    assert_equal 0, God.watches.size
  end

  def test_unwatch_should_remove_from_group
    God.watch do |w|
      w.name = 'bar'
      w.start = 'baz'
      w.group = 'test'
    end
    w = God.watches['bar']
    God.unwatch(w)
    assert !God.groups[w.group].include?(w)
  end

  # contact

  def test_contact_should_ensure_init_is_called
    God.contact(:fake_contact) { |c| c.name = 'tom' }
    assert God.inited
  end

  def test_contact_should_abort_on_invalid_contact_kind
    assert_abort do
      God.contact(:foo) { |c| c.name = 'tom' }
    end
  end

  def test_contact_should_create_and_store_contact
    contact = nil
    God.contact(:fake_contact) { |c| c.name = 'tom'; contact = c }
    assert_equal({"tom" => contact}, God.contacts)
  end

  def test_contact_should_add_to_group
    God.contact(:fake_contact) { |c| c.name = 'tom'; c.group = 'devs' }
    God.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'devs' }
    assert_equal 2, God.contacts.size
    assert_equal 1, God.contact_groups.size
  end

  def test_contact_should_abort_on_no_name
    assert_abort do
      God.contact(:fake_contact) { |c| }
    end
  end

  def test_contact_should_abort_on_duplicate_contact_name
    God.contact(:fake_contact) { |c| c.name = 'tom' }
    assert_nothing_raised do
      God.contact(:fake_contact) { |c| c.name = 'tom' }
    end
  end

  def test_contact_should_abort_on_contact_with_same_name_as_group
    God.contact(:fake_contact) { |c| c.name = 'tom'; c.group = 'devs' }
    assert_nothing_raised do
      God.contact(:fake_contact) { |c| c.name = 'devs' }
    end
  end

  def test_contact_should_abort_on_contact_with_same_group_as_name
    God.contact(:fake_contact) { |c| c.name = 'tom' }
    assert_abort do
      God.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'tom' }
    end
  end

  def test_contact_should_abort_if_contact_is_invalid
    assert_abort do
      God.contact(:fake_contact) do |c|
        c.name = 'tom'
        c.stubs(:valid?).returns(false)
      end
    end
  end

  # control

  def test_control_should_monitor_on_start
    God.watch { |w| w.name = 'foo'; w.start = 'bar' }

    w = God.watches['foo']
    w.expects(:monitor)
    God.control('foo', 'start')
  end

  def test_control_should_move_to_restart_on_restart
    God.watch { |w| w.name = 'foo'; w.start = 'bar' }

    w = God.watches['foo']
    w.expects(:move).with(:restart)
    God.control('foo', 'restart')
  end

  def test_control_should_unmonitor_and_stop_on_stop
    God.watch { |w| w.name = 'foo'; w.start = 'bar' }

    w = God.watches['foo']
    w.state = :up
    w.expects(:unmonitor).returns(w)
    w.expects(:action).with(:stop)
    God.control('foo', 'stop')
  end

  def test_control_should_unmonitor_on_unmonitor
    God.watch { |w| w.name = 'foo'; w.start = 'bar' }

    w = God.watches['foo']
    w.state = :up
    w.expects(:unmonitor).returns(w)
    God.control('foo', 'unmonitor')
  end

  def test_control_should_unwatch_on_remove
    God.watch { |w| w.name = 'foo'; w.start = 'bar' }

    w = God.watches['foo']
    w.state = :up
    God.expects(:unwatch)
    God.control('foo', 'remove')
  end

  def test_control_should_raise_on_invalid_command
    God.watch { |w| w.name = 'foo'; w.start = 'bar' }

    assert_raise InvalidCommandError do
      God.control('foo', 'invalid')
    end
  end

  def test_control_should_operate_on_each_watch_in_group
    God.watch do |w|
      w.name = 'foo1'
      w.start = 'go'
      w.group = 'bar'
    end

    God.watch do |w|
      w.name = 'foo2'
      w.start = 'go'
      w.group = 'bar'
    end

    God.watches['foo1'].expects(:monitor)
    God.watches['foo2'].expects(:monitor)

    God.control('bar', 'start')
  end

  # stop_all

  # terminate

  def test_terminate_should_exit
    God.pid = nil
    FileUtils.expects(:rm_f).never
    God.expects(:exit!)
    God.terminate
  end

  def test_terminate_should_delete_pid
    God.pid = '/foo/bar'
    FileUtils.expects(:rm_f).with("/foo/bar")
    God.expects(:exit!)
    God.terminate
  end

  # status

  def test_status_should_show_state
    God.watch { |w| w.name = 'foo'; w.start = 'bar' }

    w = God.watches['foo']
    w.state = :up
    assert_equal({'foo' => {:state => :up, :group => nil}}, God.status)
  end

  def test_status_should_show_state_with_group
    God.watch { |w| w.name = 'foo'; w.start = 'bar'; w.group = 'g' }

    w = God.watches['foo']
    w.state = :up
    assert_equal({'foo' => {:state => :up, :group => 'g'}}, God.status)
  end

  def test_status_should_show_unmonitored_for_nil_state
    God.watch { |w| w.name = 'foo'; w.start = 'bar' }

    w = God.watches['foo']
    assert_equal({'foo' => {:state => :unmonitored, :group => nil}}, God.status)
  end

  # running_log

  def test_running_log_should_call_watch_log_since_on_main_log
    God.watch { |w| w.name = 'foo'; w.start = 'bar' }
    t = Time.now
    LOG.expects(:watch_log_since).with('foo', t)
    God.running_log('foo', t)
  end

  def test_running_log_should_raise_on_unknown_watch
    God.internal_init
    assert_raise(NoSuchWatchError) do
      God.running_log('foo', Time.now)
    end
  end

  # running_load

  def test_running_load_should_eval_code
    code = <<-EOF
      God.watch do |w|
        w.name = 'foo'
        w.start = 'go'
      end
    EOF

    God.running_load(code, '/foo/bar.god')

    assert_equal 1, God.watches.size
  end

  def test_running_load_should_monitor_new_watches
    code = <<-EOF
      God.watch do |w|
        w.name = 'foo'
        w.start = 'go'
      end
    EOF

    Watch.any_instance.expects(:monitor)
    God.running_load(code, '/foo/bar.god')
  end

  def test_running_load_should_not_monitor_new_watches_with_autostart_false
    code = <<-EOF
      God.watch do |w|
        w.name = 'foo'
        w.start = 'go'
        w.autostart = false
      end
    EOF

    Watch.any_instance.expects(:monitor).never
    God.running_load(code, '/foo/bar.god')
  end

  def test_running_load_should_return_array_of_affected_watches
    code = <<-EOF
      God.watch do |w|
        w.name = 'foo'
        w.start = 'go'
      end
    EOF

    w = nil
    w, e = *God.running_load(code, '/foo/bar.god')
    assert_equal 1, w.size
    assert_equal 'foo', w.first
  end

  def test_running_load_should_clear_pending_watches
    code = <<-EOF
      God.watch do |w|
        w.name = 'foo'
        w.start = 'go'
      end
    EOF

    God.running_load(code, '/foo/bar.god')
    assert_equal 0, God.pending_watches.size
  end

  def test_running_load_with_stop
    code_one = <<-EOF
      God.watch do |w|
        w.name = 'foo'
        w.start = 'go'
      end
    EOF

    code_two = <<-EOF
      God.watch do |w|
        w.name = 'bar'
        w.start = 'go'
      end
    EOF

    a, e, r = *God.running_load(code_one, '/foo/one.god', 'stop')

    assert_equal 1, a.size
    assert_equal 0, r.size

    a, e, r = *God.running_load(code_two, '/foo/two.god', 'stop')

    assert_equal 1, a.size
    assert_equal 1, r.size
  end

  def test_running_load_with_remove
    code_one = <<-EOF
      God.watch do |w|
        w.name = 'foo'
        w.start = 'go'
      end
    EOF

    code_two = <<-EOF
      God.watch do |w|
        w.name = 'bar'
        w.start = 'go'
      end
    EOF

    a, e, r = *God.running_load(code_one, '/foo/one.god', 'remove')

    assert_equal 1, a.size
    assert_equal 0, r.size

    a, e, r = *God.running_load(code_two, '/foo/two.god', 'remove')

    assert_equal 1, a.size
    assert_equal 1, r.size
  end

  def test_running_load_with_leave
    code_one = <<-EOF
      God.watch do |w|
        w.name = 'foo'
        w.start = 'go'
      end
    EOF

    code_two = <<-EOF
      God.watch do |w|
        w.name = 'bar'
        w.start = 'go'
      end
    EOF

    a, e, r = *God.running_load(code_one, '/foo/one.god', 'leave')

    assert_equal 1, a.size
    assert_equal 0, r.size

    a, e, r = *God.running_load(code_two, '/foo/two.god', 'leave')

    assert_equal 1, a.size
    assert_equal 0, r.size
  end

  # load

  def test_load_should_collect_and_load_globbed_path
    Dir.expects(:[]).with('/path/to/*.thing').returns(['a', 'b'])
    Kernel.expects(:load).with('a').once
    Kernel.expects(:load).with('b').once
    God.load('/path/to/*.thing')
  end

  # start

  def test_start_should_kick_off_a_server_instance
    God::Socket.expects(:new).returns(true)
    God.start
  end

  def test_start_should_begin_monitoring_autostart_watches
    God.watch do |w|
      w.name = 'foo'
      w.start = 'go'
    end

    Watch.any_instance.expects(:monitor).once
    God.start
  end

  def test_start_should_not_begin_monitoring_non_autostart_watches
    God.watch do |w|
      w.name = 'foo'
      w.start = 'go'
      w.autostart = false
    end

    Watch.any_instance.expects(:monitor).never
    God.start
  end

  def test_start_should_get_and_join_timer
    God.watch { |w| w.name = 'foo'; w.start = 'bar' }
    God.start
  end

  # at_exit

  def test_at_exit_should_call_start
    God.expects(:start).once
    God.at_exit
  end

  # pattern_match

  def test_pattern_match
    list = %w{ mongrel-3000 mongrel-3001 fuzed22 fuzed fuzed2 apache mysql}

    assert_equal %w{ mongrel-3000 }, God.pattern_match('m3000', list)
    assert_equal %w{ mongrel-3001 }, God.pattern_match('m31', list)
    assert_equal %w{ fuzed fuzed2 fuzed22}, God.pattern_match('fu', list)
    assert_equal %w{ mysql }, God.pattern_match('sql', list)
  end
end


# class TestGodOther < Test::Unit::TestCase
#   def setup
#     God::Socket.stubs(:new).returns(true)
#     God.internal_init
#     God.reset
#   end
#
#   def teardown
#     God.main && God.main.kill
#   end
#
#   # setup
#
#   def test_setup_should_create_pid_file_directory_if_it_doesnt_exist
#     God.expects(:test).returns(false)
#     FileUtils.expects(:mkdir_p).with(God.pid_file_directory)
#     God.setup
#   end
#
#   def test_setup_should_raise_if_no_permissions_to_create_pid_file_directory
#     God.expects(:test).returns(false)
#     FileUtils.expects(:mkdir_p).raises(Errno::EACCES)
#
#     assert_abort do
#       God.setup
#     end
#   end
#
#   # validate
#
#   def test_validate_should_abort_if_pid_file_directory_is_unwriteable
#     God.expects(:test).returns(false)
#     assert_abort do
#       God.validater
#     end
#   end
#
#   def test_validate_should_not_abort_if_pid_file_directory_is_writeable
#     God.expects(:test).returns(true)
#     assert_nothing_raised do
#       God.validater
#     end
#   end
# end