aboutsummaryrefslogtreecommitdiff
path: root/test/tc_mauve_person.rb
blob: 0d4b69550ba17953c38ac319b57de1bc08bf5ee6 (plain)
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
$:.unshift "../lib"

require 'th_mauve'
require 'mauve/person'
require 'mauve/configuration'
require 'mauve/configuration_builder'
require 'mauve/configuration_builders'
require 'pp' 

class TcMauvePerson < Mauve::UnitTest 

  include Mauve

  def setup
    super
    setup_database
  end

  def teardown
    teardown_database
    super
  end

  def test_send_alert
    #
    # Allows us to pick up notifications sent.
    #
    notification_buffer = []

    config =<<EOF
notification_method("email") {
  debug!
  deliver_to_queue []
  disable_normal_delivery!
}

person ("test") {
  email "test@example.com"
  all { email }
  suppress_notifications_after( 6 => 60.seconds )
}

alert_group("default") {
  level URGENT

  notify("test") {
    every 10.seconds
  } 
}
EOF
  
    Configuration.current = ConfigurationBuilder.parse(config)
    notification_buffer = Configuration.current.notification_methods["email"].deliver_to_queue

    Server.instance.setup
    person = Configuration.current.people["test"]

    alert = Alert.new(
      :alert_id  => "test",
      :source    => "test",
      :subject   => "test"
    )
    alert.raise!
    assert_equal(false,    person.suppressed?, "Person suppressed before we even begin!")

    start_time = Time.now

    #
    # 6 alerts every 60 seconds.
    #
    [ [0, true],
      [5, true],
      [10, true],
      [15, true],
      [20, true],
      [25, true], # 6th alert -- suppress from now on
      [30, false], 
      [35, false],
      [40, false],
      [60, false], # One minute after starting -- should still be suppressed
      [65, false],
      [70, false],
      [75, false],
      [80, false],
      [85, true], # One minute after the last alert was sent, start sending again.
      [90, true]
    ].each do |offset, notification_sent|
      # 
      # Advance in to the future!
      #
      Timecop.freeze(start_time + offset)
      person.send_alert(alert.level, alert)

      if notification_sent 
        assert_equal(1, notification_buffer.length, "Notification not sent when it should have been at #{Time.now}.")
        #
        # Pop the notification off the buffer.
        #
        notification_buffer.pop
      else
        assert_equal(0, notification_buffer.length, "Notification sent when it should not have been at #{Time.now}.")
      end

      logger_pop
    end

  end

  def test_send_alert_when_only_one_blargh
    #
    # This configuration is a bit different.  We only want one alert per
    # minute.
    #
    config =<<EOF
notification_method("email") {
  debug!
  deliver_to_queue []
  disable_normal_delivery!
}

person ("test") {
  email "test@example.com"
  all { email }
  suppress_notifications_after( 1 => 1.minute )
}

alert_group("default") {
  level URGENT

  notify("test") {
    every 10.seconds
  } 
}
EOF
  
    Configuration.current = ConfigurationBuilder.parse(config)
    notification_buffer = Configuration.current.notification_methods["email"].deliver_to_queue
    Server.instance.setup

    person = Configuration.current.people["test"]

    alert = Alert.new(
      :alert_id  => "test",
      :source    => "test",
      :subject   => "test"
    )

    alert.raise!
    assert_equal(false,    person.suppressed?, "Person suppressed before we even begin!")

    start_time = Time.now

    #
    # 1 alerts every 60 seconds.
    #
    [ [0, true ],
      [5, false],
      [15, false],
      [30, false],
      [60, true ], # One minute after starting -- should send an alert.
      [90, false],
      [120, true] # Two minutes after starting -- should send an alert.
    ].each do |offset, notification_sent|
      # 
      # Advance in to the future!
      #
      Timecop.freeze(start_time + offset)

      person.send_alert(alert.level, alert)

      if notification_sent 
        assert_equal(1, notification_buffer.length, "Notification not sent when it should have been at #{Time.now}.")
        #
        # Pop the notification off the buffer.
        #
        notification_buffer.pop
      else
        assert_equal(0, notification_buffer.length, "Notification sent when it should not have been at #{Time.now}.")
      end

      logger_pop
    end

  end
  
  def test_send_alert_suppression_as_alerts_get_more_urgent
    #
    # This configuration is a bit different.  We only want one alert per
    # minute.
    #
    config =<<EOF
notification_method("email") {
  debug!
  deliver_to_queue []
  disable_normal_delivery!
}

person ("test") {
  email "test@example.com"
  all { email }
  suppress_notifications_after( 2 => 1.minute )
}

alert_group("low") {
  level LOW
  includes { alert_id =~ /^low-/  }

  notify("test") {
    every 10.seconds
  } 
}

alert_group("normal") {
  level NORMAL
  includes { alert_id =~ /^normal-/ } 

  notify("test") {
    every 10.seconds
  } 
}

alert_group("default") {
  level URGENT

  notify("test") {
    every 10.seconds
  } 
}
EOF
  
    Configuration.current = ConfigurationBuilder.parse(config)
    notification_buffer = Configuration.current.notification_methods["email"].deliver_to_queue
    Server.instance.setup

    person = Configuration.current.people["test"]

    alerts = [
      Alert.new(
        :alert_id  => "low-test",
        :source    => "test",
        :subject   => "test"
      ),
      Alert.new(
        :alert_id  => "normal-test",
        :source    => "test",
        :subject   => "test"
      ),
      Alert.new(
        :alert_id  => "urgent-test",
        :source    => "test",
        :subject   => "test"
      )
    ]

    #
    # Raise the alerts
    #
    alerts.each{|a| a.raise!}
    assert_equal(false,    person.suppressed?, "Person suppressed before we even begin!")

    assert_equal(:low, alerts[0].level)
    assert_equal(:normal, alerts[1].level)
    assert_equal(:urgent, alerts[2].level)

    start_time = Time.now

    #
    # 
    #
    [ [0, true, alerts.first ],
      [1, true, alerts.first ],
      [2, false, alerts.first ],
      [3, false, alerts.first ],
      [4, true, alerts[1]],
      [5, false, alerts.first],
      [6, true, alerts[1]],
      [7, false, alerts[1]],
      [8, false, alerts[1]],
      [9, false, alerts[1]],
      [10, true, alerts[2]],
      [11, true, alerts[2]],
      [12, false, alerts[2]],
      [13, false, alerts.first]
    ].each do |offset, notification_sent, alert|
      # 
      # Advance in to the future!
      #
      Timecop.freeze(start_time + offset)

      person.send_alert(alert.level, alert)

      if notification_sent 
        assert_equal(1, notification_buffer.length, "#{alert.level.to_s.capitalize} notification not sent when it should have been at #{Time.now}.")
        #
        # Pop the notification off the buffer.
        #
        notification_buffer.pop
      else
        assert_equal(0, notification_buffer.length, "#{alert.level.to_s.capitalize} notification sent when it should not have been at #{Time.now}.")
      end

      logger_pop
    end

  end

  def test_current_alerts

  end

  def test_is_on_holiday? 

  end
end