É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 | local gse_policy = require('gse_policy') |
| 20 | local consts = require('gse_constants') |
| 21 | |
| 22 | local test_policy_down = gse_policy.new({ |
| 23 | status='down', |
| 24 | trigger={ |
| 25 | logical_operator='or', |
| 26 | rules={{ |
| 27 | ['function']='count', |
| 28 | arguments={'down'}, |
| 29 | relational_operator='>', |
| 30 | threshold=0 |
| 31 | }} |
| 32 | } |
| 33 | }) |
| 34 | |
| 35 | local test_policy_critical = gse_policy.new({ |
| 36 | status='critical', |
| 37 | trigger={ |
| 38 | logical_operator='and', |
| 39 | rules={{ |
| 40 | ['function']='count', |
| 41 | arguments={'critical'}, |
| 42 | relational_operator='>', |
| 43 | threshold=0 |
| 44 | }, { |
| 45 | ['function']='percent', |
| 46 | arguments={'okay', 'warning'}, |
| 47 | relational_operator='<', |
| 48 | threshold=50 |
| 49 | }} |
| 50 | } |
| 51 | }) |
| 52 | |
| 53 | local test_policy_warning = gse_policy.new({ |
| 54 | status='warning', |
| 55 | trigger={ |
| 56 | logical_operator='or', |
| 57 | rules={{ |
| 58 | ['function']='percent', |
| 59 | arguments={'okay'}, |
| 60 | relational_operator='<', |
| 61 | threshold=50 |
| 62 | }, { |
| 63 | ['function']='percent', |
| 64 | arguments={'warning'}, |
| 65 | relational_operator='>', |
| 66 | threshold=30 |
| 67 | }} |
| 68 | } |
| 69 | }) |
| 70 | |
| 71 | local test_policy_okay = gse_policy.new({ |
| 72 | status='okay' |
| 73 | }) |
| 74 | |
| 75 | TestGsePolicy = {} |
| 76 | |
| 77 | function TestGsePolicy:test_policy_down() |
| 78 | assertEquals(test_policy_down.status, consts.DOWN) |
| 79 | assertEquals(test_policy_down.logical_op, 'or') |
| 80 | assertEquals(#test_policy_down.rules, 1) |
| 81 | assertEquals(test_policy_down.rules[1]['function'], 'count') |
| 82 | assertEquals(#test_policy_down.rules[1].arguments, 1) |
| 83 | assertEquals(test_policy_down.rules[1].arguments[1], consts.DOWN) |
| 84 | assertEquals(test_policy_down.rules[1].relational_op, '>') |
| 85 | assertEquals(test_policy_down.rules[1].threshold, 0) |
| 86 | assertEquals(test_policy_down.require_percent, false) |
| 87 | end |
| 88 | |
| 89 | function TestGsePolicy:test_policy_okay_evaluate_true() |
| 90 | local facts = { |
| 91 | [consts.OKAY]=5, |
| 92 | [consts.WARN]=0, |
| 93 | [consts.CRIT]=0, |
| 94 | [consts.DOWN]=0, |
| 95 | [consts.UNKW]=0, |
| 96 | } |
| 97 | assertEquals(test_policy_okay:evaluate(facts), true) |
| 98 | end |
| 99 | |
| 100 | function TestGsePolicy:test_policy_okay_evaluate_true_again() |
| 101 | local facts = { |
| 102 | [consts.OKAY]=0, |
| 103 | [consts.WARN]=0, |
| 104 | [consts.CRIT]=0, |
| 105 | [consts.DOWN]=0, |
| 106 | [consts.UNKW]=0, |
| 107 | } |
| 108 | assertEquals(test_policy_okay:evaluate(facts), true) |
| 109 | end |
| 110 | |
| 111 | function TestGsePolicy:test_policy_warn_evaluate_true() |
| 112 | local facts = { |
| 113 | [consts.OKAY]=2, |
| 114 | [consts.WARN]=2, |
| 115 | [consts.CRIT]=0, |
| 116 | [consts.DOWN]=0, |
| 117 | [consts.UNKW]=1, |
| 118 | } |
| 119 | assertEquals(test_policy_warning:evaluate(facts), true) |
| 120 | end |
| 121 | |
| 122 | function TestGsePolicy:test_policy_warn_evaluate_false() |
| 123 | local facts = { |
| 124 | [consts.OKAY]=6, |
| 125 | [consts.WARN]=2, |
| 126 | [consts.CRIT]=0, |
| 127 | [consts.DOWN]=0, |
| 128 | [consts.UNKW]=1, |
| 129 | } |
| 130 | assertEquals(test_policy_warning:evaluate(facts), false) |
| 131 | end |
| 132 | |
| 133 | function TestGsePolicy:test_policy_warn_evaluate_true_again() |
| 134 | local facts = { |
| 135 | [consts.OKAY]=3, |
| 136 | [consts.WARN]=2, |
| 137 | [consts.CRIT]=0, |
| 138 | [consts.DOWN]=0, |
| 139 | [consts.UNKW]=0, |
| 140 | } |
| 141 | assertEquals(test_policy_warning:evaluate(facts), true) |
| 142 | end |
| 143 | |
| 144 | function TestGsePolicy:test_policy_crit_evaluate_true() |
| 145 | local facts = { |
| 146 | [consts.OKAY]=1, |
| 147 | [consts.WARN]=1, |
| 148 | [consts.CRIT]=3, |
| 149 | [consts.DOWN]=0, |
| 150 | [consts.UNKW]=0, |
| 151 | } |
| 152 | assertEquals(test_policy_critical:evaluate(facts), true) |
| 153 | end |
| 154 | |
| 155 | function TestGsePolicy:test_policy_crit_evaluate_false() |
| 156 | local facts = { |
| 157 | [consts.OKAY]=4, |
| 158 | [consts.WARN]=1, |
| 159 | [consts.CRIT]=3, |
| 160 | [consts.DOWN]=0, |
| 161 | [consts.UNKW]=0, |
| 162 | } |
| 163 | assertEquals(test_policy_critical:evaluate(facts), false) |
| 164 | end |
| 165 | |
| 166 | function TestGsePolicy:test_policy_crit_evaluate_false_again() |
| 167 | local facts = { |
| 168 | [consts.OKAY]=3, |
| 169 | [consts.WARN]=1, |
| 170 | [consts.CRIT]=0, |
| 171 | [consts.DOWN]=0, |
| 172 | [consts.UNKW]=0, |
| 173 | } |
| 174 | assertEquals(test_policy_critical:evaluate(facts), false) |
| 175 | end |
| 176 | |
| 177 | function TestGsePolicy:test_policy_down_evaluate_true() |
| 178 | local facts = { |
| 179 | [consts.OKAY]=2, |
| 180 | [consts.WARN]=2, |
| 181 | [consts.CRIT]=0, |
| 182 | [consts.DOWN]=1, |
| 183 | [consts.UNKW]=0, |
| 184 | } |
| 185 | assertEquals(test_policy_down:evaluate(facts), true) |
| 186 | end |
| 187 | |
| 188 | function TestGsePolicy:test_policy_down_evaluate_false() |
| 189 | local facts = { |
| 190 | [consts.OKAY]=2, |
| 191 | [consts.WARN]=3, |
| 192 | [consts.CRIT]=0, |
| 193 | [consts.DOWN]=0, |
| 194 | [consts.UNKW]=0, |
| 195 | } |
| 196 | assertEquals(test_policy_down:evaluate(facts), false) |
| 197 | end |
| 198 | |
| 199 | lu = LuaUnit |
| 200 | lu:setVerbosity( 1 ) |
| 201 | os.exit( lu:run() ) |