blob: 101698430c8cf8ec5d5edada61bf8ad51cef6c0c [file] [log] [blame]
Éric Lemoine71272712016-11-08 12:53:51 +00001-- Copyright 2015 Mirantis, Inc.
2--
3-- Licensed under the Apache License, Version 2.0 (the "License");
4-- you may not use this file except in compliance with the License.
5-- You may obtain a copy of the License at
6--
7-- http://www.apache.org/licenses/LICENSE-2.0
8--
9-- Unless required by applicable law or agreed to in writing, software
10-- distributed under the License is distributed on an "AS IS" BASIS,
11-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-- See the License for the specific language governing permissions and
13-- limitations under the License.
14
15EXPORT_ASSERT_TO_GLOBALS=true
16require('luaunit')
17package.path = package.path .. ";../heka/files/lua/common/?.lua;lua/mocks/?.lua"
18
19-- mock the inject_message() function from the Heka sandbox library
20local last_injected_msg
21function inject_message(msg)
22 last_injected_msg = msg
23end
24
25local afd = require('afd')
26local consts = require('gse_constants')
27local extra = require('extra_fields')
28
29TestAfd = {}
30
31 function TestAfd:setUp()
32 afd.reset_alarms()
33 end
34
35 function TestAfd:test_add_to_alarms()
36 afd.add_to_alarms(consts.CRIT, 'last', 'metric_1', {}, {}, '==', 0, 0, nil, nil, "crit message")
37 local alarms = afd.get_alarms()
38 assertEquals(alarms[1].severity, 'CRITICAL')
39 assertEquals(alarms[1].metric, 'metric_1')
40 assertEquals(alarms[1].message, 'crit message')
41
42 afd.add_to_alarms(consts.WARN, 'last', 'metric_2', {}, {}, '>=', 10, 2, 5, 600, "warn message")
43 alarms = afd.get_alarms()
44 assertEquals(alarms[2].severity, 'WARN')
45 assertEquals(alarms[2].metric, 'metric_2')
46 assertEquals(alarms[2].message, 'warn message')
47 end
48
Éric Lemoine041383a2016-11-08 12:59:08 +000049 function TestAfd:test_inject_afd_metric_without_alarms()
Guillaume Thouvenin6faa8622017-02-28 16:40:42 +010050 afd.inject_afd_metric(consts.OKAY, 'node-1', 'foo', {}, false, false, nil)
Éric Lemoine71272712016-11-08 12:53:51 +000051
52 local alarms = afd.get_alarms()
53 assertEquals(#alarms, 0)
Éric Lemoine041383a2016-11-08 12:59:08 +000054 assertEquals(last_injected_msg.Type, 'afd_metric')
Éric Lemoine71272712016-11-08 12:53:51 +000055 assertEquals(last_injected_msg.Fields.value, consts.OKAY)
56 assertEquals(last_injected_msg.Fields.hostname, 'node-1')
Simon Pasquierc04539d2017-03-24 14:27:53 +010057 assertEquals(last_injected_msg.Fields.notification_handler, nil)
Éric Lemoine71272712016-11-08 12:53:51 +000058 assertEquals(last_injected_msg.Payload, '{"alarms":[]}')
59 end
60
Éric Lemoine041383a2016-11-08 12:59:08 +000061 function TestAfd:test_inject_afd_metric_with_alarms()
Éric Lemoine71272712016-11-08 12:53:51 +000062 afd.add_to_alarms(consts.CRIT, 'last', 'metric_1', {}, {}, '==', 0, 0, nil, nil, "important message")
Guillaume Thouvenin6faa8622017-02-28 16:40:42 +010063 afd.inject_afd_metric(consts.CRIT, 'node-1', 'foo', {}, true, true, 'mail')
Éric Lemoine71272712016-11-08 12:53:51 +000064
65 local alarms = afd.get_alarms()
66 assertEquals(#alarms, 0)
Éric Lemoine041383a2016-11-08 12:59:08 +000067 assertEquals(last_injected_msg.Type, 'afd_metric')
Éric Lemoine71272712016-11-08 12:53:51 +000068 assertEquals(last_injected_msg.Fields.value, consts.CRIT)
69 assertEquals(last_injected_msg.Fields.hostname, 'node-1')
70 assertEquals(last_injected_msg.Fields.environment_id, extra.environment_id)
Guillaume Thouvenin6faa8622017-02-28 16:40:42 +010071 assertEquals(last_injected_msg.Fields.notification_handler, 'mail')
Éric Lemoine71272712016-11-08 12:53:51 +000072 assert(last_injected_msg.Payload:match('"message":"important message"'))
73 assert(last_injected_msg.Payload:match('"severity":"CRITICAL"'))
74 end
75
76 function TestAfd:test_alarms_for_human_without_fields()
77 local alarms = afd.alarms_for_human({{
78 severity='WARNING',
79 ['function']='avg',
80 metric='load_longterm',
81 fields={},
82 tags={},
83 operator='>',
84 value=7,
85 threshold=5,
86 window=600,
87 periods=0,
88 message='load too high',
89 }})
90
91 assertEquals(#alarms, 1)
92 assertEquals(alarms[1], 'load too high (WARNING, rule=\'avg(load_longterm)>5\', current=7.00)')
93 end
94
95 function TestAfd:test_alarms_for_human_with_fields()
96 local alarms = afd.alarms_for_human({{
97 severity='CRITICAL',
98 ['function']='avg',
99 metric='fs_space_percent_free',
100 fields={fs='/'},
101 tags={},
102 operator='<=',
103 value=2,
104 threshold=5,
105 window=600,
106 periods=0,
107 message='free disk space too low'
108 }})
109
110 assertEquals(#alarms, 1)
111 assertEquals(alarms[1], 'free disk space too low (CRITICAL, rule=\'avg(fs_space_percent_free[fs="/"])<=5\', current=2.00)')
112 end
113
114 function TestAfd:test_alarms_for_human_with_hostname()
115 local alarms = afd.alarms_for_human({{
116 severity='WARNING',
117 ['function']='avg',
118 metric='load_longterm',
119 fields={},
120 tags={},
121 operator='>',
122 value=7,
123 threshold=5,
124 window=600,
125 periods=0,
126 message='load too high',
127 hostname='node-1'
128 }})
129
130 assertEquals(#alarms, 1)
131 assertEquals(alarms[1], 'load too high (WARNING, rule=\'avg(load_longterm)>5\', current=7.00, host=node-1)')
132 end
133
134 function TestAfd:test_alarms_for_human_with_hints()
135 local alarms = afd.alarms_for_human({{
136 severity='WARNING',
137 ['function']='avg',
138 metric='load_longterm',
139 fields={},
140 tags={dependency_level='hint',dependency_name='controller'},
141 operator='>',
142 value=7,
143 threshold=5,
144 window=600,
145 periods=0,
146 message='load too high',
147 hostname='node-1'
148 }})
149
150 assertEquals(#alarms, 2)
151 assertEquals(alarms[1], 'Other related alarms:')
152 assertEquals(alarms[2], 'load too high (WARNING, rule=\'avg(load_longterm)>5\', current=7.00, host=node-1)')
153 end
154
155lu = LuaUnit
156lu:setVerbosity( 1 )
157os.exit( lu:run() )