blob: addc4eca3c3eb56730c8c65ec09df3686f363f48 [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
49 function TestAfd:test_inject_afd_service_metric_without_alarms()
50 afd.inject_afd_service_metric('nova-scheduler', consts.OKAY, 'node-1', 10, 'some_source')
51
52 local alarms = afd.get_alarms()
53 assertEquals(#alarms, 0)
54 assertEquals(last_injected_msg.Type, 'afd_service_metric')
55 assertEquals(last_injected_msg.Fields.value, consts.OKAY)
56 assertEquals(last_injected_msg.Fields.hostname, 'node-1')
57 assertEquals(last_injected_msg.Payload, '{"alarms":[]}')
58 end
59
60 function TestAfd:test_inject_afd_service_metric_with_alarms()
61 afd.add_to_alarms(consts.CRIT, 'last', 'metric_1', {}, {}, '==', 0, 0, nil, nil, "important message")
62 afd.inject_afd_service_metric('nova-scheduler', consts.CRIT, 'node-1', 10, 'some_source')
63
64 local alarms = afd.get_alarms()
65 assertEquals(#alarms, 0)
66 assertEquals(last_injected_msg.Type, 'afd_service_metric')
67 assertEquals(last_injected_msg.Fields.value, consts.CRIT)
68 assertEquals(last_injected_msg.Fields.hostname, 'node-1')
69 assertEquals(last_injected_msg.Fields.environment_id, extra.environment_id)
70 assert(last_injected_msg.Payload:match('"message":"important message"'))
71 assert(last_injected_msg.Payload:match('"severity":"CRITICAL"'))
72 end
73
74 function TestAfd:test_alarms_for_human_without_fields()
75 local alarms = afd.alarms_for_human({{
76 severity='WARNING',
77 ['function']='avg',
78 metric='load_longterm',
79 fields={},
80 tags={},
81 operator='>',
82 value=7,
83 threshold=5,
84 window=600,
85 periods=0,
86 message='load too high',
87 }})
88
89 assertEquals(#alarms, 1)
90 assertEquals(alarms[1], 'load too high (WARNING, rule=\'avg(load_longterm)>5\', current=7.00)')
91 end
92
93 function TestAfd:test_alarms_for_human_with_fields()
94 local alarms = afd.alarms_for_human({{
95 severity='CRITICAL',
96 ['function']='avg',
97 metric='fs_space_percent_free',
98 fields={fs='/'},
99 tags={},
100 operator='<=',
101 value=2,
102 threshold=5,
103 window=600,
104 periods=0,
105 message='free disk space too low'
106 }})
107
108 assertEquals(#alarms, 1)
109 assertEquals(alarms[1], 'free disk space too low (CRITICAL, rule=\'avg(fs_space_percent_free[fs="/"])<=5\', current=2.00)')
110 end
111
112 function TestAfd:test_alarms_for_human_with_hostname()
113 local alarms = afd.alarms_for_human({{
114 severity='WARNING',
115 ['function']='avg',
116 metric='load_longterm',
117 fields={},
118 tags={},
119 operator='>',
120 value=7,
121 threshold=5,
122 window=600,
123 periods=0,
124 message='load too high',
125 hostname='node-1'
126 }})
127
128 assertEquals(#alarms, 1)
129 assertEquals(alarms[1], 'load too high (WARNING, rule=\'avg(load_longterm)>5\', current=7.00, host=node-1)')
130 end
131
132 function TestAfd:test_alarms_for_human_with_hints()
133 local alarms = afd.alarms_for_human({{
134 severity='WARNING',
135 ['function']='avg',
136 metric='load_longterm',
137 fields={},
138 tags={dependency_level='hint',dependency_name='controller'},
139 operator='>',
140 value=7,
141 threshold=5,
142 window=600,
143 periods=0,
144 message='load too high',
145 hostname='node-1'
146 }})
147
148 assertEquals(#alarms, 2)
149 assertEquals(alarms[1], 'Other related alarms:')
150 assertEquals(alarms[2], 'load too high (WARNING, rule=\'avg(load_longterm)>5\', current=7.00, host=node-1)')
151 end
152
153lu = LuaUnit
154lu:setVerbosity( 1 )
155os.exit( lu:run() )