aboutsummaryrefslogtreecommitdiff
path: root/test/tc_mauve_person.rb
blob: 30051b028ba4efa474427379bf0706941a390811 (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
$:.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.
    #
    $sent_notifications = []

    config =<<EOF
person ("test") {
  all { $sent_notifications << Time.now ; true }
  suppress_notifications_after( 6 => 60.seconds )
}

alert_group("default") {
  level URGENT

  notify("test") {
    every 10.seconds
  } 
}
EOF
  
    Configuration.current = ConfigurationBuilder.parse(config)
    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, false],
      [5, true, false],
      [10, true, false],
      [15, true, false],
      [20, true, false],
      [25, true, true], # 6th alert -- suppress from now on
      [30, false, true], 
      [35, false, true],
      [40, false, true],
      [60, false, true], # One minute after starting -- should still be suppressed
      [65, false, true],
      [70, false, true],
      [75, false, true],
      [80, false, true],
      [85, true, false], # One minute after the last alert was sent, start sending again.
      [90, true, false]
    ].each do |offset, notification_sent, suppressed|
      # 
      # Advance in to the future!
      #
      Timecop.freeze(start_time + offset)

      person.send_alert(alert.level, alert)

      assert_equal(suppressed,    person.suppressed?, "Suppressed (or not) when it should (or shouldn't) be at #{Time.now}.")

      if notification_sent 
        assert_equal(1, $sent_notifications.length, "Notification not sent when it should have been at #{Time.now}.")
        #
        # Pop the notification off the buffer.
        #
        last_notification_sent_at = $sent_notifications.pop
        assert_equal(Time.now, person.notification_thresholds[60][-1], "Notification thresholds not updated at #{Time.now}.")
      else
        assert_equal(0, $sent_notifications.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
    #
    # Allows us to pick up notifications sent.
    #
    $sent_notifications = []

    #
    # This configuration is a bit different.  We only want one alert per
    # minute.
    #
    config =<<EOF
person ("test") {
  all { $sent_notifications << Time.now ; true }
  suppress_notifications_after( 1 => 1.minute )
}

alert_group("default") {
  level URGENT

  notify("test") {
    every 10.seconds
  } 
}
EOF
  
    Configuration.current = ConfigurationBuilder.parse(config)
    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,   true],
      [5, false,  true],
      [15, false, true],
      [30, false, true],
      [60, true,  true], # One minute after starting -- should send an alert, but still be suppressed.
      [90, false, true],
      [120, true, true] # Two minutes after starting -- should send an alert, but still be suppressed.
    ].each do |offset, notification_sent, suppressed|
      # 
      # Advance in to the future!
      #
      Timecop.freeze(start_time + offset)

      person.send_alert(alert.level, alert)

      assert_equal(suppressed,    person.should_suppress?, "Suppressed (or not) when it should (or shouldn't) be at #{Time.now}.")

      if notification_sent 
        assert_equal(1, $sent_notifications.length, "Notification not sent when it should have been at #{Time.now}.")
        #
        # Pop the notification off the buffer.
        #
        last_notification_sent_at = $sent_notifications.pop
        assert_equal(Time.now, person.notification_thresholds[60][-1], "Notification thresholds not updated at #{Time.now}.")
      else
        assert_equal(0, $sent_notifications.length, "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