firenxis-god 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. data/Announce.txt +135 -0
  2. data/History.txt +393 -0
  3. data/README.txt +59 -0
  4. data/Rakefile +142 -0
  5. data/bin/god +132 -0
  6. data/ext/god/.gitignore +5 -0
  7. data/ext/god/extconf.rb +55 -0
  8. data/ext/god/kqueue_handler.c +125 -0
  9. data/ext/god/netlink_handler.c +168 -0
  10. data/god.gemspec +164 -0
  11. data/lib/god.rb +701 -0
  12. data/lib/god/behavior.rb +52 -0
  13. data/lib/god/behaviors/clean_pid_file.rb +21 -0
  14. data/lib/god/behaviors/clean_unix_socket.rb +21 -0
  15. data/lib/god/behaviors/notify_when_flapping.rb +51 -0
  16. data/lib/god/cli/command.rb +256 -0
  17. data/lib/god/cli/run.rb +172 -0
  18. data/lib/god/cli/version.rb +23 -0
  19. data/lib/god/compat19.rb +36 -0
  20. data/lib/god/condition.rb +96 -0
  21. data/lib/god/conditions/always.rb +23 -0
  22. data/lib/god/conditions/complex.rb +86 -0
  23. data/lib/god/conditions/cpu_usage.rb +80 -0
  24. data/lib/god/conditions/degrading_lambda.rb +52 -0
  25. data/lib/god/conditions/disk_usage.rb +32 -0
  26. data/lib/god/conditions/file_mtime.rb +28 -0
  27. data/lib/god/conditions/flapping.rb +128 -0
  28. data/lib/god/conditions/http_response_code.rb +168 -0
  29. data/lib/god/conditions/lambda.rb +25 -0
  30. data/lib/god/conditions/memory_usage.rb +82 -0
  31. data/lib/god/conditions/process_exits.rb +72 -0
  32. data/lib/god/conditions/process_running.rb +74 -0
  33. data/lib/god/conditions/tries.rb +44 -0
  34. data/lib/god/configurable.rb +57 -0
  35. data/lib/god/contact.rb +114 -0
  36. data/lib/god/contacts/campfire.rb +121 -0
  37. data/lib/god/contacts/email.rb +136 -0
  38. data/lib/god/contacts/jabber.rb +75 -0
  39. data/lib/god/contacts/prowl.rb +57 -0
  40. data/lib/god/contacts/scout.rb +55 -0
  41. data/lib/god/contacts/twitter.rb +51 -0
  42. data/lib/god/contacts/webhook.rb +73 -0
  43. data/lib/god/dependency_graph.rb +41 -0
  44. data/lib/god/diagnostics.rb +37 -0
  45. data/lib/god/driver.rb +206 -0
  46. data/lib/god/errors.rb +24 -0
  47. data/lib/god/event_handler.rb +108 -0
  48. data/lib/god/event_handlers/dummy_handler.rb +13 -0
  49. data/lib/god/event_handlers/kqueue_handler.rb +17 -0
  50. data/lib/god/event_handlers/netlink_handler.rb +13 -0
  51. data/lib/god/logger.rb +109 -0
  52. data/lib/god/metric.rb +59 -0
  53. data/lib/god/process.rb +363 -0
  54. data/lib/god/registry.rb +32 -0
  55. data/lib/god/simple_logger.rb +59 -0
  56. data/lib/god/socket.rb +107 -0
  57. data/lib/god/sugar.rb +47 -0
  58. data/lib/god/sys_logger.rb +45 -0
  59. data/lib/god/system/portable_poller.rb +42 -0
  60. data/lib/god/system/process.rb +50 -0
  61. data/lib/god/system/slash_proc_poller.rb +92 -0
  62. data/lib/god/task.rb +503 -0
  63. data/lib/god/timeline.rb +25 -0
  64. data/lib/god/trigger.rb +43 -0
  65. data/lib/god/watch.rb +188 -0
  66. data/test/configs/child_events/child_events.god +44 -0
  67. data/test/configs/child_events/simple_server.rb +3 -0
  68. data/test/configs/child_polls/child_polls.god +37 -0
  69. data/test/configs/child_polls/simple_server.rb +12 -0
  70. data/test/configs/complex/complex.god +59 -0
  71. data/test/configs/complex/simple_server.rb +3 -0
  72. data/test/configs/contact/contact.god +108 -0
  73. data/test/configs/contact/simple_server.rb +3 -0
  74. data/test/configs/daemon_events/daemon_events.god +37 -0
  75. data/test/configs/daemon_events/simple_server.rb +8 -0
  76. data/test/configs/daemon_events/simple_server_stop.rb +11 -0
  77. data/test/configs/daemon_polls/daemon_polls.god +17 -0
  78. data/test/configs/daemon_polls/simple_server.rb +6 -0
  79. data/test/configs/degrading_lambda/degrading_lambda.god +31 -0
  80. data/test/configs/degrading_lambda/tcp_server.rb +15 -0
  81. data/test/configs/lifecycle/lifecycle.god +25 -0
  82. data/test/configs/matias/matias.god +50 -0
  83. data/test/configs/real.rb +59 -0
  84. data/test/configs/running_load/running_load.god +16 -0
  85. data/test/configs/stop_options/simple_server.rb +12 -0
  86. data/test/configs/stop_options/stop_options.god +39 -0
  87. data/test/configs/stress/simple_server.rb +3 -0
  88. data/test/configs/stress/stress.god +15 -0
  89. data/test/configs/task/logs/.placeholder +0 -0
  90. data/test/configs/task/task.god +26 -0
  91. data/test/configs/test.rb +61 -0
  92. data/test/helper.rb +141 -0
  93. data/test/suite.rb +6 -0
  94. data/test/test_behavior.rb +18 -0
  95. data/test/test_campfire.rb +23 -0
  96. data/test/test_condition.rb +50 -0
  97. data/test/test_conditions_disk_usage.rb +50 -0
  98. data/test/test_conditions_http_response_code.rb +109 -0
  99. data/test/test_conditions_process_running.rb +40 -0
  100. data/test/test_conditions_tries.rb +67 -0
  101. data/test/test_contact.rb +109 -0
  102. data/test/test_dependency_graph.rb +62 -0
  103. data/test/test_driver.rb +11 -0
  104. data/test/test_email.rb +34 -0
  105. data/test/test_event_handler.rb +80 -0
  106. data/test/test_god.rb +570 -0
  107. data/test/test_handlers_kqueue_handler.rb +16 -0
  108. data/test/test_jabber.rb +29 -0
  109. data/test/test_logger.rb +55 -0
  110. data/test/test_metric.rb +72 -0
  111. data/test/test_process.rb +247 -0
  112. data/test/test_prowl.rb +15 -0
  113. data/test/test_registry.rb +15 -0
  114. data/test/test_socket.rb +34 -0
  115. data/test/test_sugar.rb +42 -0
  116. data/test/test_system_portable_poller.rb +17 -0
  117. data/test/test_system_process.rb +30 -0
  118. data/test/test_task.rb +246 -0
  119. data/test/test_timeline.rb +37 -0
  120. data/test/test_trigger.rb +59 -0
  121. data/test/test_watch.rb +279 -0
  122. data/test/test_webhook.rb +15 -0
  123. metadata +362 -0
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ if God::EventHandler.event_system == "kqueue"
4
+
5
+ class TestHandlersKqueueHandler < Test::Unit::TestCase
6
+ def test_register_process
7
+ KQueueHandler.expects(:monitor_process).with(1234, 2147483648)
8
+ KQueueHandler.register_process(1234, [:proc_exit])
9
+ end
10
+
11
+ def test_events_mask
12
+ assert_equal 2147483648, KQueueHandler.events_mask([:proc_exit])
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ require File.dirname(__FILE__) + '/helper'
3
+
4
+ class TestJabber < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @jabber = God::Contacts::Jabber.new
8
+ end
9
+
10
+ def test_notify
11
+ @jabber.host = 'talk.google.com'
12
+ @jabber.from_jid = 'god@jabber.org'
13
+ @jabber.password = 'secret'
14
+ @jabber.to_jid = 'dev@jabber.org'
15
+
16
+ time = Time.now
17
+ body = God::Contacts::Jabber.format.call('msg', time, 'prio', 'cat', 'host')
18
+
19
+ assert_equal "Message: msg\nHost: host\nPriority: prio\nCategory: cat\n", body
20
+
21
+ Jabber::Client.any_instance.expects(:connect).with('talk.google.com', 5222)
22
+ Jabber::Client.any_instance.expects(:auth).with('secret')
23
+ Jabber::Client.any_instance.expects(:send)
24
+ Jabber::Client.any_instance.expects(:close)
25
+
26
+ @jabber.notify('msg', Time.now, 'prio', 'cat', 'host')
27
+ assert_equal "sent jabber message to dev@jabber.org", @jabber.info
28
+ end
29
+ end
@@ -0,0 +1,55 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestLogger < Test::Unit::TestCase
4
+ def setup
5
+ @log = God::Logger.new(StringIO.new('/dev/null'))
6
+ end
7
+
8
+ # log
9
+
10
+ def test_log_should_keep_logs_when_wanted
11
+ @log.watch_log_since('foo', Time.now)
12
+ @log.expects(:info).with("qux")
13
+
14
+ @log.log(stub(:name => 'foo'), :info, "qux")
15
+
16
+ assert_equal 1, @log.logs.size
17
+ assert_instance_of Time, @log.logs['foo'][0][0]
18
+ assert_match(/qux/, @log.logs['foo'][0][1])
19
+ end
20
+
21
+ def test_log_should_send_to_syslog
22
+ SysLogger.expects(:log).with(:fatal, 'foo')
23
+ @log.log(stub(:name => 'foo'), :fatal, "foo")
24
+ end
25
+
26
+ # watch_log_since
27
+
28
+ def test_watch_log_since
29
+ t1 = Time.now
30
+
31
+ @log.watch_log_since('foo', t1)
32
+
33
+ @log.log(stub(:name => 'foo'), :info, "one")
34
+ @log.log(stub(:name => 'foo'), :info, "two")
35
+
36
+ assert_match(/one.*two/m, @log.watch_log_since('foo', t1))
37
+
38
+ t2 = Time.now
39
+
40
+ @log.log(stub(:name => 'foo'), :info, "three")
41
+
42
+ out = @log.watch_log_since('foo', t2)
43
+
44
+ assert_no_match(/one/, out)
45
+ assert_no_match(/two/, out)
46
+ assert_match(/three/, out)
47
+ end
48
+
49
+ # regular methods
50
+
51
+ def test_fatal
52
+ @log.fatal('foo')
53
+ assert_equal 0, @log.logs.size
54
+ end
55
+ end
@@ -0,0 +1,72 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestMetric < Test::Unit::TestCase
4
+ def setup
5
+ @metric = Metric.new(stub(:interval => 10), nil)
6
+ end
7
+
8
+ # watch
9
+
10
+ def test_watch
11
+ w = stub()
12
+ m = Metric.new(w, nil)
13
+ assert_equal w, m.watch
14
+ end
15
+
16
+ # destination
17
+
18
+ def test_destination
19
+ d = stub()
20
+ m = Metric.new(nil, d)
21
+ assert_equal d, m.destination
22
+ end
23
+
24
+ # condition
25
+
26
+ def test_condition_should_be_block_optional
27
+ @metric.condition(:fake_poll_condition)
28
+ assert_equal 1, @metric.conditions.size
29
+ end
30
+
31
+ def test_poll_condition_should_inherit_interval_from_watch_if_not_specified
32
+ @metric.condition(:fake_poll_condition)
33
+ assert_equal 10, @metric.conditions.first.interval
34
+ end
35
+
36
+ def test_poll_condition_should_abort_if_no_interval_and_no_watch_interval
37
+ metric = Metric.new(stub(:name => 'foo', :interval => nil), nil)
38
+
39
+ assert_abort do
40
+ metric.condition(:fake_poll_condition)
41
+ end
42
+ end
43
+
44
+ def test_condition_should_allow_generation_of_subclasses_of_poll_or_event
45
+ metric = Metric.new(stub(:name => 'foo', :interval => 10), nil)
46
+
47
+ assert_nothing_raised do
48
+ metric.condition(:fake_poll_condition)
49
+ metric.condition(:fake_event_condition)
50
+ end
51
+ end
52
+
53
+ def test_condition_should_abort_if_not_subclass_of_poll_or_event
54
+ metric = Metric.new(stub(:name => 'foo', :interval => 10), nil)
55
+
56
+ assert_abort do
57
+ metric.condition(:fake_condition) { |c| }
58
+ end
59
+ end
60
+
61
+ def test_condition_should_abort_on_invalid_condition
62
+ assert_abort do
63
+ @metric.condition(:fake_poll_condition) { |c| c.stubs(:valid?).returns(false) }
64
+ end
65
+ end
66
+
67
+ def test_condition_should_abort_on_no_such_condition
68
+ assert_abort do
69
+ @metric.condition(:invalid) { }
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,247 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ module God
4
+ class Process
5
+ # def fork
6
+ # raise "You forgot to stub fork"
7
+ # end
8
+
9
+ def exec(*args)
10
+ raise "You forgot to stub exec"
11
+ end
12
+ end
13
+ end
14
+
15
+ class TestProcessChild < Test::Unit::TestCase
16
+ def setup
17
+ God.internal_init
18
+ @p = God::Process.new
19
+ @p.name = 'foo'
20
+ @p.stubs(:test).returns true # so we don't try to mkdir_p
21
+ Process.stubs(:detach) # because we stub fork
22
+
23
+ ::Process::Sys.stubs(:setuid).returns(true)
24
+ ::Process::Sys.stubs(:setgid).returns(true)
25
+ end
26
+
27
+ # valid?
28
+
29
+ def test_valid_should_return_true_if_auto_daemonized_and_log
30
+ @p.start = 'qux'
31
+ @p.log = 'bar'
32
+
33
+ assert @p.valid?
34
+ end
35
+
36
+ def test_valid_should_return_true_if_auto_daemonized_and_no_stop
37
+ @p.start = 'qux'
38
+ @p.log = 'bar'
39
+
40
+ assert @p.valid?
41
+ end
42
+
43
+ def test_valid_should_return_true_if_uid_exists
44
+ @p.start = 'qux'
45
+ @p.log = '/tmp/foo.log'
46
+ @p.uid = 'root'
47
+
48
+ assert @p.valid?
49
+ end
50
+
51
+ def test_valid_should_return_true_if_uid_does_not_exists
52
+ @p.start = 'qux'
53
+ @p.log = '/tmp/foo.log'
54
+ @p.uid = 'foobarbaz'
55
+
56
+ assert !@p.valid?
57
+ end
58
+
59
+ def test_valid_should_return_true_if_gid_exists
60
+ @p.start = 'qux'
61
+ @p.log = '/tmp/foo.log'
62
+ @p.gid = 'wheel'
63
+
64
+ assert @p.valid?
65
+ end
66
+
67
+ def test_valid_should_return_false_if_gid_does_not_exists
68
+ @p.start = 'qux'
69
+ @p.log = '/tmp/foo.log'
70
+ @p.gid = 'foobarbaz'
71
+
72
+ assert !@p.valid?
73
+ end
74
+
75
+ def test_valid_should_return_true_if_dir_exists
76
+ @p.start = 'qux'
77
+ @p.log = '/tmp/foo.log'
78
+ @p.dir = '/tmp'
79
+
80
+ assert @p.valid?
81
+ end
82
+
83
+ def test_valid_should_return_false_if_dir_does_not_exists
84
+ @p.start = 'qux'
85
+ @p.log = '/tmp/foo.log'
86
+ @p.dir = '/tmp/doesnotexist'
87
+
88
+ assert !@p.valid?
89
+ end
90
+
91
+ def test_valid_should_return_false_if_dir_is_not_a_dir
92
+ @p.start = 'qux'
93
+ @p.log = '/tmp/foo.log'
94
+ @p.dir = '/etc/passwd'
95
+
96
+ assert !@p.valid?
97
+ end
98
+
99
+ def test_valid_should_return_false_with_bogus_chroot
100
+ @p.chroot = '/bogusroot'
101
+
102
+ assert !@p.valid?
103
+ end
104
+
105
+ def test_valid_should_return_true_with_chroot_and_valid_log
106
+ @p.start = 'qux'
107
+ @p.chroot = '/tmp'
108
+ @p.log = '/tmp/foo.log'
109
+
110
+ File.expects(:exist?).with('/tmp').returns(true)
111
+ File.expects(:exist?).with('/tmp/foo.log').returns(true)
112
+ File.expects(:exist?).with('/tmp/dev/null').returns(true)
113
+
114
+ assert @p.valid?
115
+ end
116
+
117
+ # call_action
118
+
119
+ def test_call_action_should_write_pid
120
+ # Only for start, restart
121
+ [:start, :restart].each do |action|
122
+ @p.stubs(:test).returns true
123
+ IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
124
+ @p.expects(:fork)
125
+ Process.expects(:waitpid)
126
+ File.expects(:open).with(@p.default_pid_file, 'w')
127
+ @p.send("#{action}=", "run")
128
+ @p.call_action(action)
129
+ end
130
+ end
131
+ end
132
+
133
+ ###############################################################################
134
+ #
135
+ # Daemon
136
+ #
137
+ ###############################################################################
138
+
139
+ class TestProcessDaemon < Test::Unit::TestCase
140
+ def setup
141
+ God.internal_init
142
+ @p = God::Process.new
143
+ @p.name = 'foo'
144
+ @p.pid_file = 'blah.pid'
145
+ @p.stubs(:test).returns true # so we don't try to mkdir_p
146
+ Process.stubs(:detach) # because we stub fork
147
+ end
148
+
149
+ # alive?
150
+
151
+ def test_alive_should_call_system_process_exists
152
+ File.expects(:read).with('blah.pid').times(2).returns('1234')
153
+ System::Process.any_instance.expects(:exists?).returns(false)
154
+ assert !@p.alive?
155
+ end
156
+
157
+ def test_alive_should_return_false_if_no_such_file
158
+ File.expects(:read).with('blah.pid').raises(Errno::ENOENT)
159
+ assert !@p.alive?
160
+ end
161
+
162
+ # valid?
163
+
164
+ def test_valid_should_return_false_if_no_start
165
+ @p.name = 'foo'
166
+ @p.stop = 'baz'
167
+ assert !@p.valid?
168
+ end
169
+
170
+ # pid
171
+
172
+ def test_pid_should_return_integer_for_valid_pid_files
173
+ File.stubs(:read).returns("123")
174
+ assert_equal 123, @p.pid
175
+ end
176
+
177
+ def test_pid_should_return_nil_for_missing_files
178
+ @p.pid_file = ''
179
+ assert_equal nil, @p.pid
180
+ end
181
+
182
+ def test_pid_should_return_nil_for_invalid_pid_files
183
+ File.stubs(:read).returns("four score and seven years ago")
184
+ assert_equal nil, @p.pid
185
+ end
186
+
187
+ def test_pid_should_retain_last_pid_value_if_pid_file_is_removed
188
+ File.stubs(:read).returns("123")
189
+ assert_equal 123, @p.pid
190
+
191
+ File.stubs(:read).raises(Errno::ENOENT)
192
+ assert_equal 123, @p.pid
193
+
194
+ File.stubs(:read).returns("246")
195
+ assert_equal 246, @p.pid
196
+ end
197
+
198
+ # default_pid_file
199
+
200
+ def test_default_pid_file
201
+ assert_equal File.join(God.pid_file_directory, 'foo.pid'), @p.default_pid_file
202
+ end
203
+
204
+ # unix socket
205
+
206
+ def test_unix_socket_should_return_path_specified
207
+ @p.unix_socket = '/path/to-socket'
208
+ assert_equal '/path/to-socket', @p.unix_socket
209
+ end
210
+
211
+ # call_action
212
+ # These actually excercise call_action in the back at this point - Kev
213
+
214
+ def test_call_action_with_string_should_call_system
215
+ @p.start = "do something"
216
+ @p.expects(:fork)
217
+ Process.expects(:waitpid2).returns([123, 0])
218
+ @p.call_action(:start)
219
+ end
220
+
221
+ def test_call_action_with_lambda_should_call
222
+ cmd = lambda { puts "Hi" }
223
+ cmd.expects(:call)
224
+ @p.start = cmd
225
+ @p.call_action(:start)
226
+ end
227
+
228
+ def test_call_action_with_invalid_command_class_should_raise
229
+ @p.start = 5
230
+ @p.stop = 'baz'
231
+
232
+ assert @p.valid?
233
+
234
+ assert_raise NotImplementedError do
235
+ @p.call_action(:start)
236
+ end
237
+ end
238
+
239
+ # start!/stop!/restart!
240
+
241
+ def test_start_stop_restart_bang
242
+ [:start, :stop, :restart].each do |x|
243
+ @p.expects(:call_action).with(x)
244
+ @p.send("#{x}!")
245
+ end
246
+ end
247
+ end
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ require File.dirname(__FILE__) + '/helper'
3
+
4
+ class TestProwl < Test::Unit::TestCase
5
+ def test_live_notify
6
+ prowl = God::Contacts::Prowl.new
7
+ prowl.name = "Prowly"
8
+ prowl.apikey = 'put_your_apikey_here'
9
+
10
+ Prowly.expects(:notify).returns(mock(:succeeded? => true))
11
+
12
+ prowl.notify("Test", Time.now, "Test", "Test", "")
13
+ assert_equal "sent prowl notification to #{prowl.name}", prowl.info
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestRegistry < Test::Unit::TestCase
4
+ def setup
5
+ God.registry.reset
6
+ end
7
+
8
+ def test_add
9
+ foo = God::Process.new
10
+ foo.name = 'foo'
11
+ God.registry.add(foo)
12
+ assert_equal 1, God.registry.size
13
+ assert_equal foo, God.registry['foo']
14
+ end
15
+ end