proby 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/proby.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/proby/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "proby"
6
+ s.license = "MIT"
7
+ s.version = Proby::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["John Wood", "Doug Barth"]
10
+ s.email = ["john@signalhq.com", "doug@signalhq.com"]
11
+ s.homepage = "http://github.com/signal/proby-ruby"
12
+ s.summary = %Q{A simple library for working with the Proby task monitoring application.}
13
+ s.description = %Q{A simple library for working with the Proby task monitoring application.}
14
+
15
+ s.rubyforge_project = "proby"
16
+
17
+ s.required_rubygems_version = ">= 1.3.6"
18
+
19
+ s.add_dependency 'httparty', '~> 0.8.1'
20
+ s.add_dependency 'chronic', '~> 0.6.7'
21
+ s.add_dependency 'multi_json', '~> 1.2.0'
22
+
23
+ s.add_development_dependency "bundler", ">= 1.0.0"
24
+ s.add_development_dependency "rake", "~> 0.9.0"
25
+ s.add_development_dependency "yard", "~> 0.6.4"
26
+ s.add_development_dependency "bluecloth", "~> 2.1.0"
27
+ s.add_development_dependency "fakeweb", "~> 1.3.0"
28
+ s.add_development_dependency "shoulda", "~> 2.11.3"
29
+ s.add_development_dependency "json", "~> 1.6.6"
30
+
31
+ s.files = `git ls-files`.split("\n")
32
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
33
+ s.require_path = 'lib'
34
+ end
35
+
@@ -0,0 +1,60 @@
1
+ require 'test_helper'
2
+
3
+ class NotifierTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ ENV['PROBY_TASK_ID'] = nil
7
+ FakeWeb.clean_registry
8
+ FakeWeb.allow_net_connect = false
9
+ end
10
+
11
+ def teardown
12
+ FakeWeb.allow_net_connect = true
13
+ end
14
+
15
+ should "not send the notification if the api_key is not set" do
16
+ assert_nil Proby.send_start_notification("abc123")
17
+ end
18
+
19
+ context "with an api key set" do
20
+ setup do
21
+ Proby.api_key = '1234567890abcdefg'
22
+ end
23
+
24
+ should "not send the notification if a task id was not specified" do
25
+ assert_nil Proby.send_start_notification
26
+ end
27
+
28
+ should "not send the notification if a task id is blank" do
29
+ assert_nil Proby.send_start_notification(" ")
30
+ end
31
+
32
+ should "send a start notification if a task_id is specified in the call" do
33
+ FakeWeb.register_uri(:post, Proby::ProbyHttpApi.base_uri + "/api/v1/tasks/abc123xyz456/start.json", :status => ["200", "OK"])
34
+ assert_equal 200, Proby.send_start_notification("abc123xyz456")
35
+ end
36
+
37
+ should "send a start notification if a task_id is specified in an environment variable" do
38
+ ENV['PROBY_TASK_ID'] = "uuu777sss999"
39
+ FakeWeb.register_uri(:post, Proby::ProbyHttpApi.base_uri + "/api/v1/tasks/uuu777sss999/start.json", :status => ["200", "OK"])
40
+ assert_equal 200, Proby.send_start_notification
41
+ end
42
+
43
+ should "send a finish notification if a task_id is specified in the call" do
44
+ FakeWeb.register_uri(:post, Proby::ProbyHttpApi.base_uri + "/api/v1/tasks/abc123xyz456/finish.json", :status => ["200", "OK"])
45
+ assert_equal 200, Proby.send_finish_notification("abc123xyz456")
46
+ end
47
+
48
+ should "send a finish notification with options if options are specified" do
49
+ FakeWeb.register_uri(:post, Proby::ProbyHttpApi.base_uri + "/api/v1/tasks/abc123xyz456/finish.json", :status => ["200", "OK"])
50
+ assert_equal 200, Proby.send_finish_notification("abc123xyz456", :failed => true, :error_message => "something bad happened")
51
+ end
52
+
53
+ should "send a finish notification if a task_id is specified in an environment variable" do
54
+ ENV['PROBY_TASK_ID'] = "iii999ooo222"
55
+ FakeWeb.register_uri(:post, Proby::ProbyHttpApi.base_uri + "/api/v1/tasks/iii999ooo222/finish.json", :status => ["200", "OK"])
56
+ assert_equal 200, Proby.send_finish_notification
57
+ end
58
+ end
59
+
60
+ end
@@ -0,0 +1,375 @@
1
+ require 'test_helper'
2
+
3
+ class ProbyTaskTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ Proby.api_key = '1234567890abcdefg'
7
+ FakeWeb.clean_registry
8
+ FakeWeb.allow_net_connect = false
9
+ end
10
+
11
+ def teardown
12
+ FakeWeb.allow_net_connect = true
13
+ end
14
+
15
+ should "raise an error if the api key has not been set" do
16
+ Proby.api_key = nil
17
+ e = assert_raise(Proby::InvalidApiKeyException) do
18
+ Proby::ProbyTask.find(:all)
19
+ end
20
+ assert_equal "Your Proby API key has not been set. Set it using Proby.api_key = 'my_api_key'", e.message
21
+ end
22
+
23
+ should "be able to get a list of tasks" do
24
+ FakeWeb.register_uri(:get, Proby::ProbyHttpApi.base_uri + '/api/v1/tasks.json', :status => ['200', 'OK'], :body => <<-END)
25
+ {
26
+ "tasks": [
27
+ {
28
+ "name": "task 1",
29
+ "api_id": "abc123",
30
+ "crontab": "* * * * *",
31
+ "paused": false,
32
+ "time_zone": "UTC",
33
+ "machine": null,
34
+ "finish_alarms_enabled": true,
35
+ "maximum_run_time": null,
36
+ "start_notification_grace_period": 5,
37
+ "consecutive_alarmed_tasks": 0,
38
+ "consecutive_alarmed_tasks_required_to_trigger_alarm": 1,
39
+ "created_at": "2012-04-25 20:10:20 UTC",
40
+ "updated_at": "2012-04-25 20:10:20 UTC",
41
+ "status": {
42
+ "description": "OK",
43
+ "details": ""
44
+ }
45
+ },
46
+ {
47
+ "name": "task 2",
48
+ "api_id": "abc456",
49
+ "crontab": "* * * * *",
50
+ "paused": false,
51
+ "time_zone": "UTC",
52
+ "machine": "dopey",
53
+ "finish_alarms_enabled": true,
54
+ "maximum_run_time": 90,
55
+ "start_notification_grace_period": 5,
56
+ "consecutive_alarmed_tasks": 3,
57
+ "consecutive_alarmed_tasks_required_to_trigger_alarm": 1,
58
+ "created_at": "2012-04-25 20:10:20 UTC",
59
+ "updated_at": "2012-04-25 20:10:20 UTC",
60
+ "status": {
61
+ "description": "OK",
62
+ "details": "Wonderful"
63
+ }
64
+ },
65
+ {
66
+ "name": "task 3",
67
+ "api_id": "abc789",
68
+ "crontab": "1 2 3 4 5",
69
+ "paused": false,
70
+ "time_zone": "UTC",
71
+ "machine": null,
72
+ "finish_alarms_enabled": true,
73
+ "maximum_run_time": null,
74
+ "start_notification_grace_period": 5,
75
+ "consecutive_alarmed_tasks": 0,
76
+ "consecutive_alarmed_tasks_required_to_trigger_alarm": 1,
77
+ "created_at": "2012-04-25 20:10:20 UTC",
78
+ "updated_at": "2012-04-25 20:10:20 UTC",
79
+ "status": {
80
+ "description": "OK",
81
+ "details": ""
82
+ }
83
+ }
84
+ ]
85
+ }
86
+ END
87
+ tasks = Proby::ProbyTask.find(:all)
88
+ assert_equal 3, tasks.size
89
+
90
+ task = tasks[1]
91
+ assert_equal "task 2", task.name
92
+ assert_equal "abc456", task.api_id
93
+ assert_equal "* * * * *", task.crontab
94
+ assert_equal false, task.paused
95
+ assert_equal "UTC", task.time_zone
96
+ assert_equal "dopey", task.machine
97
+ assert_equal true, task.finish_alarms_enabled
98
+ assert_equal 90, task.maximum_run_time
99
+ assert_equal 5, task.start_notification_grace_period
100
+ assert_equal 3, task.consecutive_alarmed_tasks
101
+ assert_equal 1, task.consecutive_alarmed_tasks_required_to_trigger_alarm
102
+ assert_equal Time.utc(2012, 4, 25, 20, 10, 20), task.created_at
103
+ assert_equal Time.utc(2012, 4, 25, 20, 10, 20), task.updated_at
104
+ assert_equal "OK", task.status.description
105
+ assert_equal "Wonderful", task.status.details
106
+ end
107
+
108
+ should "raise an exception if unable to get a list of tasks" do
109
+ FakeWeb.register_uri(:get, Proby::ProbyHttpApi.base_uri + '/api/v1/tasks.json', :status => ['400', 'Bad request'], :body => <<-END)
110
+ {
111
+ "request" : "http://www.example.com/api/v1/tasks.json",
112
+ "message" : "Something bad happened"
113
+ }
114
+ END
115
+ e = assert_raise Proby::ApiException do
116
+ Proby::ProbyTask.find(:all)
117
+ end
118
+ assert e.message.include?("API request failed with a response code of 400")
119
+ assert e.message.include?("Something bad happened")
120
+ end
121
+
122
+ should "be able to display a specific task" do
123
+ FakeWeb.register_uri(:get, Proby::ProbyHttpApi.base_uri + '/api/v1/tasks/abc123.json', :status => ['200', 'OK'], :body => <<-END)
124
+ {
125
+ "task": {
126
+ "name": "task 1",
127
+ "api_id": "abc123",
128
+ "crontab": "* * * * *",
129
+ "paused": false,
130
+ "time_zone": "UTC",
131
+ "machine": "dopey",
132
+ "finish_alarms_enabled": true,
133
+ "maximum_run_time": 90,
134
+ "start_notification_grace_period": 5,
135
+ "consecutive_alarmed_tasks": 3,
136
+ "consecutive_alarmed_tasks_required_to_trigger_alarm": 1,
137
+ "created_at": "2012-04-25 20:10:20 UTC",
138
+ "updated_at": "2012-04-25 20:10:20 UTC",
139
+ "status": {
140
+ "description": "OK",
141
+ "details": "Wonderful"
142
+ }
143
+ }
144
+ }
145
+ END
146
+
147
+ task = Proby::ProbyTask.find("abc123")
148
+ assert_equal "task 1", task.name
149
+ assert_equal "abc123", task.api_id
150
+ assert_equal "* * * * *", task.crontab
151
+ assert_equal false, task.paused
152
+ assert_equal "UTC", task.time_zone
153
+ assert_equal "dopey", task.machine
154
+ assert_equal true, task.finish_alarms_enabled
155
+ assert_equal 90, task.maximum_run_time
156
+ assert_equal 5, task.start_notification_grace_period
157
+ assert_equal 3, task.consecutive_alarmed_tasks
158
+ assert_equal 1, task.consecutive_alarmed_tasks_required_to_trigger_alarm
159
+ assert_equal Time.utc(2012, 4, 25, 20, 10, 20), task.created_at
160
+ assert_equal Time.utc(2012, 4, 25, 20, 10, 20), task.updated_at
161
+ assert_equal "OK", task.status.description
162
+ assert_equal "Wonderful", task.status.details
163
+ end
164
+
165
+ should "return nil if the specific task could not be found" do
166
+ FakeWeb.register_uri(:get, Proby::ProbyHttpApi.base_uri + '/api/v1/tasks/abc123.json', :status => ['404', 'Not Found'])
167
+ assert_nil Proby::ProbyTask.find("abc123")
168
+ end
169
+
170
+ should "raise an exception if unable to fetch a specific task" do
171
+ FakeWeb.register_uri(:get, Proby::ProbyHttpApi.base_uri + '/api/v1/tasks/abc123.json', :status => ['400', 'Bad request'], :body => <<-END)
172
+ {
173
+ "request" : "http://www.example.com/api/v1/tasks/abc123.json",
174
+ "message" : "Something bad happened"
175
+ }
176
+ END
177
+ e = assert_raise Proby::ApiException do
178
+ Proby::ProbyTask.find("abc123")
179
+ end
180
+ assert e.message.include?("API request failed with a response code of 400")
181
+ assert e.message.include?("Something bad happened")
182
+ end
183
+
184
+ should "raise an error if trying to fetch a task without specifying the api_id" do
185
+ assert_raise(Proby::InvalidParameterException) { Proby::ProbyTask.find(nil) }
186
+ assert_raise(Proby::InvalidParameterException) { Proby::ProbyTask.find(" ") }
187
+ end
188
+
189
+ should "be able to create a new task" do
190
+ FakeWeb.register_uri(:post, Proby::ProbyHttpApi.base_uri + '/api/v1/tasks.json', :status => ['201', 'Created'], :body => <<-END)
191
+ {
192
+ "task": {
193
+ "name": "task 1",
194
+ "api_id": "abc123",
195
+ "crontab": "* * * * *",
196
+ "paused": false,
197
+ "time_zone": "UTC",
198
+ "machine": "dopey",
199
+ "finish_alarms_enabled": true,
200
+ "maximum_run_time": 90,
201
+ "start_notification_grace_period": 5,
202
+ "consecutive_alarmed_tasks": 3,
203
+ "consecutive_alarmed_tasks_required_to_trigger_alarm": 1,
204
+ "created_at": "2012-04-25 20:10:20 UTC",
205
+ "updated_at": "2012-04-25 20:10:20 UTC",
206
+ "status": {
207
+ "description": "OK",
208
+ "details": "Wonderful"
209
+ }
210
+ }
211
+ }
212
+ END
213
+
214
+ # Not specifying all attributes, for berivity
215
+ task = Proby::ProbyTask.create(:name => "abc123", :crontab => "* * * * *")
216
+
217
+ assert_equal "task 1", task.name
218
+ assert_equal "abc123", task.api_id
219
+ assert_equal "* * * * *", task.crontab
220
+ assert_equal false, task.paused
221
+ assert_equal "UTC", task.time_zone
222
+ assert_equal "dopey", task.machine
223
+ assert_equal true, task.finish_alarms_enabled
224
+ assert_equal 90, task.maximum_run_time
225
+ assert_equal 5, task.start_notification_grace_period
226
+ assert_equal 3, task.consecutive_alarmed_tasks
227
+ assert_equal 1, task.consecutive_alarmed_tasks_required_to_trigger_alarm
228
+ assert_equal Time.utc(2012, 4, 25, 20, 10, 20), task.created_at
229
+ assert_equal Time.utc(2012, 4, 25, 20, 10, 20), task.updated_at
230
+ assert_equal "OK", task.status.description
231
+ assert_equal "Wonderful", task.status.details
232
+ end
233
+
234
+ should "raise an exception if unable to create a task" do
235
+ FakeWeb.register_uri(:post, Proby::ProbyHttpApi.base_uri + '/api/v1/tasks.json', :status => ['400', 'Bad request'], :body => <<-END)
236
+ {
237
+ "request" : "http://www.example.com/api/v1/tasks.json",
238
+ "message" : "Something bad happened"
239
+ }
240
+ END
241
+ e = assert_raise Proby::ApiException do
242
+ Proby::ProbyTask.create(:name => "foo", :crontab => "invalid")
243
+ end
244
+ assert e.message.include?("API request failed with a response code of 400")
245
+ assert e.message.include?("Something bad happened")
246
+ end
247
+
248
+ should "raise an error if trying to create a task without specifying the attributes" do
249
+ assert_raise(Proby::InvalidParameterException) { Proby::ProbyTask.create(nil) }
250
+ assert_raise(Proby::InvalidParameterException) { Proby::ProbyTask.create({}) }
251
+ end
252
+
253
+ should "raise an error if trying to create a task without the necessary required attributes" do
254
+ assert_raise(Proby::InvalidParameterException) { Proby::ProbyTask.create(:name => "Foo") }
255
+ assert_raise(Proby::InvalidParameterException) { Proby::ProbyTask.create(:crontab => "* * * * *") }
256
+ end
257
+
258
+ should "be able to update a task" do
259
+ FakeWeb.register_uri(:put, Proby::ProbyHttpApi.base_uri + '/api/v1/tasks/abc123.json', :status => ['200', 'OK'])
260
+ proby_task = Proby::ProbyTask.new('api_id' => 'abc123', 'name' => 'Test task', 'crontab' => '* * * * *')
261
+ proby_task.name = "New name"
262
+ proby_task.crontab = "1 2 3 4 5"
263
+ proby_task.time_zone = "UTC"
264
+ proby_task.machine = "sleepy"
265
+ proby_task.finish_alarms_enabled = "false"
266
+ proby_task.maximum_run_time = 60
267
+ proby_task.start_notification_grace_period = 10
268
+ proby_task.consecutive_alarmed_tasks_required_to_trigger_alarm = 4
269
+ assert proby_task.save
270
+ end
271
+
272
+ should "raise an exception if unable to update a task" do
273
+ FakeWeb.register_uri(:put, Proby::ProbyHttpApi.base_uri + '/api/v1/tasks/abc123.json', :status => ['400', 'Bad request'], :body => <<-END)
274
+ {
275
+ "request" : "http://www.example.com/api/v1/tasks/abc123.json",
276
+ "message" : "Something bad happened"
277
+ }
278
+ END
279
+ proby_task = Proby::ProbyTask.new('api_id' => 'abc123', 'name' => 'Test task', 'crontab' => '* * * * *')
280
+ proby_task.name = "New name"
281
+ proby_task.crontab = "1 2 3 4 5"
282
+ e = assert_raise Proby::ApiException do
283
+ proby_task.save
284
+ end
285
+ assert e.message.include?("API request failed with a response code of 400")
286
+ assert e.message.include?("Something bad happened")
287
+ end
288
+
289
+ should "raise an error if trying to update a task without the necessary required attributes" do
290
+ proby_task = Proby::ProbyTask.new('api_id' => 'abc123', 'name' => 'Test task', 'crontab' => '* * * * *')
291
+
292
+ proby_task.name = nil
293
+ assert_raise(Proby::InvalidParameterException) { proby_task.save }
294
+
295
+ proby_task.name = " "
296
+ assert_raise(Proby::InvalidParameterException) { proby_task.save }
297
+
298
+ proby_task.crontab = nil
299
+ assert_raise(Proby::InvalidParameterException) { proby_task.save }
300
+
301
+ proby_task.crontab = " "
302
+ assert_raise(Proby::InvalidParameterException) { proby_task.save }
303
+ end
304
+
305
+ should "be able to delete a task" do
306
+ FakeWeb.register_uri(:delete, Proby::ProbyHttpApi.base_uri + '/api/v1/tasks/abc123.json', :status => ['200', 'OK'])
307
+ proby_task = Proby::ProbyTask.new('api_id' => 'abc123', 'name' => 'Test task', 'crontab' => '* * * * *')
308
+ assert proby_task.delete
309
+ assert proby_task.frozen?
310
+ e = assert_raise TypeError do
311
+ proby_task.name = "foo"
312
+ end
313
+ assert_equal "can't modify frozen object", e.message
314
+ end
315
+
316
+ should "raise an exception if unable to delete a task" do
317
+ FakeWeb.register_uri(:delete, Proby::ProbyHttpApi.base_uri + '/api/v1/tasks/abc123.json', :status => ['400', 'Bad request'], :body => <<-END)
318
+ {
319
+ "request" : "http://www.example.com/api/v1/tasks/abc123.json",
320
+ "message" : "Something bad happened"
321
+ }
322
+ END
323
+ proby_task = Proby::ProbyTask.new('api_id' => 'abc123', 'name' => 'Test task', 'crontab' => '* * * * *')
324
+ e = assert_raise Proby::ApiException do
325
+ proby_task.delete
326
+ end
327
+ assert e.message.include?("API request failed with a response code of 400")
328
+ assert e.message.include?("Something bad happened")
329
+ end
330
+
331
+ should "be able to pause a task" do
332
+ FakeWeb.register_uri(:post, Proby::ProbyHttpApi.base_uri + '/api/v1/tasks/abc123/pause.json', :status => ['200', 'OK'])
333
+ proby_task = Proby::ProbyTask.new('api_id' => 'abc123', 'name' => 'Test task', 'crontab' => '* * * * *', 'paused' => 'false')
334
+ assert proby_task.pause
335
+ assert_equal true, proby_task.paused
336
+ end
337
+
338
+ should "raise an exception if unable to pause a task" do
339
+ FakeWeb.register_uri(:post, Proby::ProbyHttpApi.base_uri + '/api/v1/tasks/abc123/pause.json', :status => ['400', 'Bad request'], :body => <<-END)
340
+ {
341
+ "request" : "http://www.example.com/api/v1/tasks/abc123/pause.json",
342
+ "message" : "Something bad happened"
343
+ }
344
+ END
345
+ proby_task = Proby::ProbyTask.new('api_id' => 'abc123', 'name' => 'Test task', 'crontab' => '* * * * *')
346
+ e = assert_raise Proby::ApiException do
347
+ proby_task.pause
348
+ end
349
+ assert e.message.include?("API request failed with a response code of 400")
350
+ assert e.message.include?("Something bad happened")
351
+ end
352
+
353
+ should "be able to unpause a task" do
354
+ FakeWeb.register_uri(:post, Proby::ProbyHttpApi.base_uri + '/api/v1/tasks/abc123/unpause.json', :status => ['200', 'OK'])
355
+ proby_task = Proby::ProbyTask.new('api_id' => 'abc123', 'name' => 'Test task', 'crontab' => '* * * * *', 'paused' => 'true')
356
+ assert proby_task.unpause
357
+ assert_equal false, proby_task.paused
358
+ end
359
+
360
+ should "raise an exception if unable to unpause a task" do
361
+ FakeWeb.register_uri(:post, Proby::ProbyHttpApi.base_uri + '/api/v1/tasks/abc123/unpause.json', :status => ['400', 'Bad request'], :body => <<-END)
362
+ {
363
+ "request" : "http://www.example.com/api/v1/tasks/abc123/unpause.json",
364
+ "message" : "Something bad happened"
365
+ }
366
+ END
367
+ proby_task = Proby::ProbyTask.new('api_id' => 'abc123', 'name' => 'Test task', 'crontab' => '* * * * *')
368
+ e = assert_raise Proby::ApiException do
369
+ proby_task.unpause
370
+ end
371
+ assert e.message.include?("API request failed with a response code of 400")
372
+ assert e.message.include?("Something bad happened")
373
+ end
374
+
375
+ end
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ require 'test/unit'
12
+ require 'fakeweb'
13
+ require 'shoulda'
14
+
15
+ require 'proby'
16
+