Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 1 | -- 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 | |
| 15 | EXPORT_ASSERT_TO_GLOBALS=true |
| 16 | require('luaunit') |
| 17 | package.path = package.path .. ";../heka/files/lua/common/?.lua;lua/mocks/?.lua" |
| 18 | |
| 19 | -- mock the inject_message() function from the Heka sandbox library |
| 20 | local last_injected_msg |
| 21 | function inject_message(msg) |
| 22 | last_injected_msg = msg |
| 23 | end |
| 24 | |
| 25 | local afd = require('afd') |
| 26 | local consts = require('gse_constants') |
| 27 | local extra = require('extra_fields') |
| 28 | |
| 29 | TestAfd = {} |
| 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 Lemoine | 041383a | 2016-11-08 12:59:08 +0000 | [diff] [blame] | 49 | function TestAfd:test_inject_afd_metric_without_alarms() |
Guillaume Thouvenin | 6faa862 | 2017-02-28 16:40:42 +0100 | [diff] [blame] | 50 | afd.inject_afd_metric(consts.OKAY, 'node-1', 'foo', {}, false, false, nil) |
Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 51 | |
| 52 | local alarms = afd.get_alarms() |
| 53 | assertEquals(#alarms, 0) |
Éric Lemoine | 041383a | 2016-11-08 12:59:08 +0000 | [diff] [blame] | 54 | assertEquals(last_injected_msg.Type, 'afd_metric') |
Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 55 | assertEquals(last_injected_msg.Fields.value, consts.OKAY) |
| 56 | assertEquals(last_injected_msg.Fields.hostname, 'node-1') |
Simon Pasquier | c04539d | 2017-03-24 14:27:53 +0100 | [diff] [blame^] | 57 | assertEquals(last_injected_msg.Fields.notification_handler, nil) |
Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 58 | assertEquals(last_injected_msg.Payload, '{"alarms":[]}') |
| 59 | end |
| 60 | |
Éric Lemoine | 041383a | 2016-11-08 12:59:08 +0000 | [diff] [blame] | 61 | function TestAfd:test_inject_afd_metric_with_alarms() |
Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 62 | afd.add_to_alarms(consts.CRIT, 'last', 'metric_1', {}, {}, '==', 0, 0, nil, nil, "important message") |
Guillaume Thouvenin | 6faa862 | 2017-02-28 16:40:42 +0100 | [diff] [blame] | 63 | afd.inject_afd_metric(consts.CRIT, 'node-1', 'foo', {}, true, true, 'mail') |
Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 64 | |
| 65 | local alarms = afd.get_alarms() |
| 66 | assertEquals(#alarms, 0) |
Éric Lemoine | 041383a | 2016-11-08 12:59:08 +0000 | [diff] [blame] | 67 | assertEquals(last_injected_msg.Type, 'afd_metric') |
Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 68 | 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 Thouvenin | 6faa862 | 2017-02-28 16:40:42 +0100 | [diff] [blame] | 71 | assertEquals(last_injected_msg.Fields.notification_handler, 'mail') |
Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 72 | 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 | |
| 155 | lu = LuaUnit |
| 156 | lu:setVerbosity( 1 ) |
| 157 | os.exit( lu:run() ) |