watch_list 0.1.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.
@@ -0,0 +1,303 @@
1
+ describe 'monitor' do
2
+ it do
3
+ watch_list do
4
+ <<-RUBY
5
+ monitor "http monitor" do
6
+ target "http://example.com"
7
+ interval 5
8
+ paused false
9
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
10
+ type :http
11
+ end
12
+
13
+ monitor "http monitor (basic auth)" do
14
+ target "http://example.com"
15
+ interval 5
16
+ paused false
17
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
18
+
19
+ type :http do
20
+ httpusername "username"
21
+ httppassword "password"
22
+ end
23
+ end
24
+
25
+ monitor "keyword monitor" do
26
+ target "http://example.com"
27
+ interval 5
28
+ paused false
29
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
30
+
31
+ type :keyword do
32
+ keywordtype :exists
33
+ keywordvalue "Example Domain"
34
+ end
35
+ end
36
+
37
+ monitor "keyword monitor (basic auth)" do
38
+ target "http://example.com"
39
+ interval 5
40
+ paused false
41
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
42
+
43
+ type :keyword do
44
+ keywordtype :exists
45
+ keywordvalue "(Example Domain)"
46
+ httpusername "(username)"
47
+ httppassword "(password)"
48
+ end
49
+ end
50
+
51
+ monitor "ping monitor" do
52
+ target "127.0.0.1"
53
+ interval 5
54
+ paused false
55
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
56
+ type :ping
57
+ end
58
+
59
+ monitor "port monitor" do
60
+ target "example.com"
61
+ interval 5
62
+ paused false
63
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
64
+
65
+ type :port do
66
+ subtype :http
67
+ port 80
68
+ end
69
+ end
70
+
71
+ monitor "port monitor (custom)" do
72
+ target "example.com"
73
+ interval 5
74
+ paused false
75
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
76
+
77
+ type :port do
78
+ subtype :custom
79
+ port 8080
80
+ end
81
+ end
82
+
83
+ alert_contact do
84
+ type :email
85
+ value "#{WATCH_LIST_TEST_EMAIL}"
86
+ end
87
+ RUBY
88
+ end
89
+
90
+ expect(watch_list_export {|h| h[:monitors].length > 0 }).to eq(
91
+ {:monitors=>
92
+ {"http monitor"=>
93
+ {:FriendlyName=>"http monitor",
94
+ :URL=>"http://example.com",
95
+ :Type=>1,
96
+ :HTTPUsername=>nil,
97
+ :HTTPPassword=>nil,
98
+ :AlertContacts=>[{:Type=>2, :Value=>WATCH_LIST_TEST_EMAIL}],
99
+ :Interval=>5},
100
+ "http monitor (basic auth)"=>
101
+ {:FriendlyName=>"http monitor (basic auth)",
102
+ :URL=>"http://example.com",
103
+ :Type=>1,
104
+ :HTTPUsername=>"username",
105
+ :HTTPPassword=>"password",
106
+ :AlertContacts=>[{:Type=>2, :Value=>WATCH_LIST_TEST_EMAIL}],
107
+ :Interval=>5},
108
+ "keyword monitor"=>
109
+ {:FriendlyName=>"keyword monitor",
110
+ :URL=>"http://example.com",
111
+ :Type=>2,
112
+ :KeywordType=>1,
113
+ :KeywordValue=>"Example Domain",
114
+ :HTTPUsername=>nil,
115
+ :HTTPPassword=>nil,
116
+ :AlertContacts=>[{:Type=>2, :Value=>WATCH_LIST_TEST_EMAIL}],
117
+ :Interval=>5},
118
+ "keyword monitor (basic auth)"=>
119
+ {:FriendlyName=>"keyword monitor (basic auth)",
120
+ :URL=>"http://example.com",
121
+ :Type=>2,
122
+ :KeywordType=>1,
123
+ :KeywordValue=>"(Example Domain)",
124
+ :HTTPUsername=>"(username)",
125
+ :HTTPPassword=>"(password)",
126
+ :AlertContacts=>[{:Type=>2, :Value=>WATCH_LIST_TEST_EMAIL}],
127
+ :Interval=>5},
128
+ "ping monitor"=>
129
+ {:FriendlyName=>"ping monitor",
130
+ :URL=>"127.0.0.1",
131
+ :Type=>3,
132
+ :AlertContacts=>[{:Type=>2, :Value=>WATCH_LIST_TEST_EMAIL}],
133
+ :Interval=>5},
134
+ "port monitor"=>
135
+ {:FriendlyName=>"port monitor",
136
+ :URL=>"example.com",
137
+ :Type=>4,
138
+ :SubType=>1,
139
+ :Port=>80,
140
+ :AlertContacts=>[{:Type=>2, :Value=>WATCH_LIST_TEST_EMAIL}],
141
+ :Interval=>5},
142
+ "port monitor (custom)"=>
143
+ {:FriendlyName=>"port monitor (custom)",
144
+ :URL=>"example.com",
145
+ :Type=>4,
146
+ :SubType=>99,
147
+ :Port=>8080,
148
+ :AlertContacts=>[{:Type=>2, :Value=>WATCH_LIST_TEST_EMAIL}],
149
+ :Interval=>5}},
150
+ :alert_contacts=>[{:Type=>2, :Value=>WATCH_LIST_TEST_EMAIL}]}
151
+ )
152
+
153
+ watch_list do
154
+ <<-RUBY
155
+ monitor "http monitor" do
156
+ target "http://example.com/test"
157
+ interval 6
158
+ paused false
159
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
160
+ type :http
161
+ end
162
+
163
+ monitor "http monitor2 (basic auth)" do
164
+ target "http://example.com/test"
165
+ interval 7
166
+ paused false
167
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
168
+
169
+ type :http do
170
+ httpusername "username2"
171
+ httppassword "password2"
172
+ end
173
+ end
174
+
175
+ monitor "keyword monitor" do
176
+ target "http://example.com/test"
177
+ interval 8
178
+ paused false
179
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
180
+
181
+ type :keyword do
182
+ keywordtype :not_exists
183
+ keywordvalue "Example Domain2"
184
+ end
185
+ end
186
+
187
+ monitor "keyword monitor (basic auth)" do
188
+ target "http://example.com/test"
189
+ interval 9
190
+ paused false
191
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
192
+
193
+ type :keyword do
194
+ keywordtype :not_exists
195
+ keywordvalue "(Example Domain2)"
196
+ httpusername "(username2)"
197
+ httppassword "(password2)"
198
+ end
199
+ end
200
+
201
+ monitor "ping monitor" do
202
+ target "127.0.0.2"
203
+ interval 10
204
+ paused false
205
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
206
+ type :ping
207
+ end
208
+
209
+ monitor "port monitor" do
210
+ target "google.com"
211
+ interval 11
212
+ paused false
213
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
214
+
215
+ type :port do
216
+ subtype :https
217
+ port 443
218
+ end
219
+ end
220
+
221
+ monitor "port monitor (custom)" do
222
+ target "google.com"
223
+ interval 12
224
+ paused false
225
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
226
+
227
+ type :port do
228
+ subtype :custom
229
+ port 18080
230
+ end
231
+ end
232
+
233
+ alert_contact do
234
+ type :email
235
+ value "#{WATCH_LIST_TEST_EMAIL}"
236
+ end
237
+ RUBY
238
+ end
239
+
240
+ expect(watch_list_export {|h| h[:monitors]['port monitor (custom)'][:URL] != 'example.com' }).to eq(
241
+ {:monitors=>
242
+ {"http monitor"=>
243
+ {:FriendlyName=>"http monitor",
244
+ :URL=>"http://example.com/test",
245
+ :Type=>1,
246
+ :HTTPUsername=>nil,
247
+ :HTTPPassword=>nil,
248
+ :AlertContacts=>[{:Type=>2, :Value=>WATCH_LIST_TEST_EMAIL}],
249
+ :Interval=>6},
250
+ "keyword monitor"=>
251
+ {:FriendlyName=>"keyword monitor",
252
+ :URL=>"http://example.com/test",
253
+ :Type=>2,
254
+ :KeywordType=>2,
255
+ :KeywordValue=>"Example Domain2",
256
+ :HTTPUsername=>nil,
257
+ :HTTPPassword=>nil,
258
+ :AlertContacts=>[{:Type=>2, :Value=>WATCH_LIST_TEST_EMAIL}],
259
+ :Interval=>8},
260
+ "keyword monitor (basic auth)"=>
261
+ {:FriendlyName=>"keyword monitor (basic auth)",
262
+ :URL=>"http://example.com/test",
263
+ :Type=>2,
264
+ :KeywordType=>2,
265
+ :KeywordValue=>"(Example Domain2)",
266
+ :HTTPUsername=>"(username2)",
267
+ :HTTPPassword=>"(password2)",
268
+ :AlertContacts=>[{:Type=>2, :Value=>WATCH_LIST_TEST_EMAIL}],
269
+ :Interval=>9},
270
+ "ping monitor"=>
271
+ {:FriendlyName=>"ping monitor",
272
+ :URL=>"127.0.0.2",
273
+ :Type=>3,
274
+ :AlertContacts=>[{:Type=>2, :Value=>WATCH_LIST_TEST_EMAIL}],
275
+ :Interval=>10},
276
+ "port monitor"=>
277
+ {:FriendlyName=>"port monitor",
278
+ :URL=>"google.com",
279
+ :Type=>4,
280
+ :SubType=>2,
281
+ :Port=>443,
282
+ :AlertContacts=>[{:Type=>2, :Value=>WATCH_LIST_TEST_EMAIL}],
283
+ :Interval=>11},
284
+ "port monitor (custom)"=>
285
+ {:FriendlyName=>"port monitor (custom)",
286
+ :URL=>"google.com",
287
+ :Type=>4,
288
+ :SubType=>99,
289
+ :Port=>18080,
290
+ :AlertContacts=>[{:Type=>2, :Value=>WATCH_LIST_TEST_EMAIL}],
291
+ :Interval=>12},
292
+ "http monitor2 (basic auth)"=>
293
+ {:FriendlyName=>"http monitor2 (basic auth)",
294
+ :URL=>"http://example.com/test",
295
+ :Type=>1,
296
+ :HTTPUsername=>"username2",
297
+ :HTTPPassword=>"password2",
298
+ :AlertContacts=>[{:Type=>2, :Value=>WATCH_LIST_TEST_EMAIL}],
299
+ :Interval=>7}},
300
+ :alert_contacts=>[{:Type=>2, :Value=>WATCH_LIST_TEST_EMAIL}]}
301
+ )
302
+ end
303
+ end
@@ -0,0 +1,69 @@
1
+ describe 'monitor paused' do
2
+ it do
3
+ watch_list do
4
+ <<-RUBY
5
+ monitor "http monitor" do
6
+ target "http://example.com"
7
+ interval 5
8
+ paused false
9
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
10
+ type :http
11
+ end
12
+
13
+ alert_contact do
14
+ type :email
15
+ value "#{WATCH_LIST_TEST_EMAIL}"
16
+ end
17
+ RUBY
18
+ end
19
+
20
+ exported = watch_list_export(skip_normalize: true) {|h| h[:monitors].length > 0 }
21
+ expect(exported[:monitors]['http monitor'][:Status]).to_not eq UptimeRobot::Monitor::Status::Paused
22
+
23
+ watch_list do
24
+ <<-RUBY
25
+ monitor "http monitor" do
26
+ target "http://example.com"
27
+ interval 5
28
+ paused true
29
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
30
+ type :http
31
+ end
32
+
33
+ alert_contact do
34
+ type :email
35
+ value "#{WATCH_LIST_TEST_EMAIL}"
36
+ end
37
+ RUBY
38
+ end
39
+
40
+ exported = watch_list_export(skip_normalize: true) {|h|
41
+ h[:monitors]['http monitor'][:Status] == UptimeRobot::Monitor::Status::Paused
42
+ }
43
+
44
+ expect(exported[:monitors]['http monitor'][:Status]).to eq UptimeRobot::Monitor::Status::Paused
45
+
46
+ watch_list do
47
+ <<-RUBY
48
+ monitor "http monitor" do
49
+ target "http://example.com"
50
+ interval 5
51
+ paused false
52
+ alert_contact :email, "#{WATCH_LIST_TEST_EMAIL}"
53
+ type :http
54
+ end
55
+
56
+ alert_contact do
57
+ type :email
58
+ value "#{WATCH_LIST_TEST_EMAIL}"
59
+ end
60
+ RUBY
61
+ end
62
+
63
+ exported = watch_list_export(skip_normalize: true) {|h|
64
+ h[:monitors]['http monitor'][:Status] != UptimeRobot::Monitor::Status::Paused
65
+ }
66
+
67
+ expect(exported[:monitors]['http monitor'][:Status]).to_not eq UptimeRobot::Monitor::Status::Paused
68
+ end
69
+ end
@@ -0,0 +1,149 @@
1
+ require 'watch_list'
2
+ require 'tempfile'
3
+
4
+ WATCH_LIST_TEST_API_KEY = ENV['WATCH_LIST_TEST_API_KEY']
5
+ WATCH_LIST_TEST_PRIMARY_ALERT_CONTACT_ID = ENV['WATCH_LIST_TEST_PRIMARY_ALERT_CONTACT_ID']
6
+ WATCH_LIST_TEST_EMAIL = ENV['WATCH_LIST_TEST_EMAIL']
7
+ WATCH_LIST_TEST_EMAIL2 = ENV['WATCH_LIST_TEST_EMAIL2']
8
+ LOOP_LIMIT = 60
9
+
10
+ RSpec.configure do |config|
11
+ config.before(:each) do
12
+ cleanup_uptimerobot
13
+ end
14
+
15
+ config.after(:all) do
16
+ cleanup_uptimerobot(skip_wait: true)
17
+ end
18
+ end
19
+
20
+ def watch_list_client(options = {})
21
+ options = {
22
+ apiKey: WATCH_LIST_TEST_API_KEY,
23
+ }.merge(options)
24
+
25
+ if ENV['DEBUG'] == '1'
26
+ options[:debug] = true
27
+ else
28
+ options[:logger] = Logger.new('/dev/null')
29
+ end
30
+
31
+ WatchList::Client.new(options)
32
+ end
33
+
34
+ def watch_list(options = {})
35
+ client = watch_list_client(options)
36
+
37
+ tempfile(yield) do |f|
38
+ client.apply(f.path)
39
+ end
40
+ end
41
+
42
+ def wait_until(options = {}, client = uptimerobot_client)
43
+ export = proc do
44
+ WatchList::Exporter.export(client, options)
45
+ end
46
+
47
+ exported = nil
48
+
49
+ loop_until do
50
+ exported = export.call
51
+ yield(exported)
52
+ end
53
+
54
+ exported
55
+ end
56
+
57
+ def watch_list_export(options = {}, &block)
58
+ exported = wait_until(options, &block)
59
+
60
+ unless options[:skip_normalize]
61
+ exported[:monitors].each do |name, attrs|
62
+ attrs.delete(:ID)
63
+ attrs.delete(:Status)
64
+ end
65
+
66
+ exported[:alert_contacts].each do |alert_contact|
67
+ alert_contact.delete(:ID)
68
+ end
69
+ end
70
+
71
+ exported
72
+ end
73
+
74
+ def tempfile(content, options = {})
75
+ basename = "#{File.basename __FILE__}.#{$$}"
76
+ basename = [basename, options[:ext]] if options[:ext]
77
+
78
+ Tempfile.open(basename) do |f|
79
+ f.puts(content)
80
+ f.flush
81
+ f.rewind
82
+ yield(f)
83
+ end
84
+ end
85
+
86
+ def uptimerobot_client(options = {})
87
+ options = {
88
+ apiKey: WATCH_LIST_TEST_API_KEY,
89
+ }.merge(options)
90
+
91
+ UptimeRobot::Client.new(apiKey: WATCH_LIST_TEST_API_KEY)
92
+ end
93
+
94
+ def cleanup_uptimerobot(options = {})
95
+ client = uptimerobot_client
96
+ cleanup_uptimerobot_monitor(client, options)
97
+ cleanup_uptimerobot_alert_contact(client, options)
98
+ end
99
+
100
+ def cleanup_uptimerobot_monitor(client, options = {})
101
+ get_monitors = proc do
102
+ client.getMonitors['monitors']['monitor']
103
+ end
104
+
105
+ monitors = get_monitors.call
106
+ return if monitors.empty?
107
+
108
+ monitors.each do |monitor|
109
+ client.deleteMonitor(monitorID: monitor['id'])
110
+ end
111
+
112
+ return if options[:skip_wait]
113
+
114
+ loop_until do
115
+ get_monitors.call.length.zero?
116
+ end
117
+ end
118
+
119
+ def cleanup_uptimerobot_alert_contact(client, options = {})
120
+ get_alert_contacts = proc do
121
+ alert_contacts = client.getAlertContacts['alertcontacts']['alertcontact']
122
+ alert_contacts.select {|i| i['id'] != WATCH_LIST_TEST_PRIMARY_ALERT_CONTACT_ID }
123
+ end
124
+
125
+ alert_contacts = get_alert_contacts.call
126
+ return if alert_contacts.empty?
127
+
128
+ alert_contacts.each do |alert_contact|
129
+ client.deleteAlertContact(alertContactID: alert_contact['id'])
130
+ end
131
+
132
+ return if options[:skip_wait]
133
+
134
+ loop_until do
135
+ get_alert_contacts.call.length.zero?
136
+ end
137
+ end
138
+
139
+ def loop_until
140
+ break_loop = false
141
+
142
+ LOOP_LIMIT.times do
143
+ break_loop = yield
144
+ break if break_loop
145
+ sleep 1
146
+ end
147
+
148
+ raise 'wait timeout' unless break_loop
149
+ end