-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathalerts_test.rb
177 lines (140 loc) · 5.37 KB
/
alerts_test.rb
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
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
class AlertsTest < Test::Unit::TestCase
include TestHelpers::AuthorizeAssertions
include TestHelpers::Fixtures
include TestHelpers::Integration
include TestHelpers::StorageKeys
include Backend::Alerts
def setup
@storage = Storage.instance(true)
@storage.flushdb
Resque.reset!
Memoizer.reset!
setup_provider_fixtures
@application_id1 = next_id
Application.save(:service_id => @service_id,
:id => @application_id1,
:plan_id => @plan_id,
:state => :active)
@application_id2 = next_id
Application.save(:service_id => @service_id,
:id => @application_id2,
:plan_id => @plan_id,
:state => :active)
@foos_id = next_id
Metric.save(:service_id => @service_id, :id => @foos_id, :name => 'foos')
UsageLimit.save(:service_id => @service_id,
:plan_id => @plan_id,
:metric_id => @foos_id,
:month => 100)
Alerts::ALERT_BINS.each do |val|
AlertLimit.save(@service_id, val)
end
end
test 'only one event for each application is stored per alert_ttl changing the alert limits' do
timestamp = Time.utc(2010, 5, 14, 12, 00, 00)
assert_equal 0, EventStorage.list().size
Timecop.freeze(timestamp) do
Transactor.report(@provider_key,
@service_id,
0 => {'app_id' => @application_id1, 'usage' => {'foos' => 90}})
Backend::Transactor.process_full_batch
Resque.run!
end
Timecop.freeze(timestamp + Alerts::ALERT_TTL*0.5) do
Transactor.report(@provider_key,
@service_id,
0 => {'app_id' => @application_id1, 'usage' => {'foos' => 1}})
Backend::Transactor.process_full_batch
Resque.run!
end
## now we are 3.0 days later, this should report a violation again
tmp_res = @storage.del("alerts/service_id:#{@service_id}/app_id:#{@application_id1}/#{90}/already_notified")
assert_equal tmp_res, 1
Timecop.freeze(timestamp + Alerts::ALERT_TTL*3.0) do
Transactor.report(@provider_key,
@service_id,
0 => {'app_id' => @application_id1, 'usage' => {'foos' => 1}})
Backend::Transactor.process_full_batch
Resque.run!
AlertLimit.delete(@service_id, 100)
## this one should over 100 and below 120 should not happen since
Transactor.report(@provider_key,
@service_id,
0 => {'app_id' => @application_id1, 'usage' => {'foos' => 10}})
Backend::Transactor.process_full_batch
Resque.run!
AlertLimit.save(@service_id, 100)
Transactor.report(@provider_key,
@service_id,
0 => {'app_id' => @application_id1, 'usage' => {'foos' => 20}})
Backend::Transactor.process_full_batch
Resque.run!
end
v = []
EventStorage.list.each do |event|
v << event if event[:type] == 'alert'
end
assert_equal 3, v.size
## again redis-2.6 yeilds a different order
cont = 0
v.each do |item|
assert_equal true, [1,2,3].include?(item[:object][:id])
if item[:object][:id] == 1
assert_equal 90, item[:object][:utilization]
cont = cont + 1
end
if item[:object][:id] == 2
assert_equal 90, item[:object][:utilization]
cont = cont + 1
end
if item[:object][:id] == 3
assert_equal 120, item[:object][:utilization]
cont = cont + 1
end
end
assert_equal 3, cont
end
test 'only one event for each application is stored per alert_ttl' do
timestamp = Time.utc(2010, 5, 14, 12, 00, 00)
assert_equal 0, EventStorage.list.size
Timecop.freeze(timestamp) do
Transactor.report(@provider_key,
@service_id,
0 => {'app_id' => @application_id1, 'usage' => {'foos' => 90}})
Backend::Transactor.process_full_batch
Resque.run!
end
Timecop.freeze(timestamp + Alerts::ALERT_TTL*0.5) do
Transactor.report(@provider_key,
@service_id,
0 => {'app_id' => @application_id1, 'usage' => {'foos' => 1}})
Backend::Transactor.process_full_batch
Resque.run!
end
## now we are 3.0 days later, this should report a violation again
tmp_res = @storage.del("alerts/service_id:#{@service_id}/app_id:#{@application_id1}/#{90}/already_notified")
assert_equal tmp_res, 1
Timecop.freeze(timestamp + Alerts::ALERT_TTL*3.0) do
Transactor.report(@provider_key,
@service_id,
0 => {'app_id' => @application_id1, 'usage' => {'foos' => 1}})
Backend::Transactor.process_full_batch
Resque.run!
Transactor.report(@provider_key,
@service_id,
0 => {'app_id' => @application_id1, 'usage' => {'foos' => 10}})
Backend::Transactor.process_full_batch
Resque.run!
end
v = []
EventStorage.list().each do |event|
if event[:type] == 'alert'
v << event[:object][:utilization]
end
end
assert_equal 3, v.size
assert v.include? 100
assert v.include? 90
end
end