É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 cjson = require('cjson') |
| 26 | local consts = require('gse_constants') |
| 27 | |
| 28 | local gse = require('gse') |
| 29 | local gse_policy = require('gse_policy') |
| 30 | |
| 31 | highest_policy = { |
| 32 | gse_policy.new({ |
| 33 | status='down', |
| 34 | trigger={ |
| 35 | logical_operator='or', |
| 36 | rules={{ |
| 37 | ['function']='count', |
| 38 | arguments={'down'}, |
| 39 | relational_operator='>', |
| 40 | threshold=0 |
| 41 | }} |
| 42 | } |
| 43 | }), |
| 44 | gse_policy.new({ |
| 45 | status='critical', |
| 46 | trigger={ |
| 47 | logical_operator='or', |
| 48 | rules={{ |
| 49 | ['function']='count', |
| 50 | arguments={'critical'}, |
| 51 | relational_operator='>', |
| 52 | threshold=0 |
| 53 | }} |
| 54 | } |
| 55 | }), |
| 56 | gse_policy.new({ |
| 57 | status='warning', |
| 58 | trigger={ |
| 59 | logical_operator='or', |
| 60 | rules={{ |
| 61 | ['function']='count', |
| 62 | arguments={'warning'}, |
| 63 | relational_operator='>', |
| 64 | threshold=0 |
| 65 | }} |
| 66 | } |
| 67 | }), |
| 68 | gse_policy.new({status='okay'}) |
| 69 | } |
| 70 | |
| 71 | -- define clusters |
| 72 | gse.add_cluster("heat", {'heat-api', 'controller'}, {'nova', 'glance', 'neutron', 'keystone', 'rabbitmq'}, 'member', highest_policy) |
| 73 | gse.add_cluster("nova", {'nova-api', 'nova-ec2-api', 'nova-scheduler'}, {'glance', 'neutron', 'keystone', 'rabbitmq'}, 'member', highest_policy) |
| 74 | gse.add_cluster("neutron", {'neutron-api'}, {'keystone', 'rabbitmq'}, 'member', highest_policy) |
| 75 | gse.add_cluster("keystone", {'keystone-admin-api', 'keystone-public-api'}, {}, 'member', highest_policy) |
| 76 | gse.add_cluster("glance", {'glance-api', 'glance-registry-api'}, {'keystone'}, 'member', highest_policy) |
| 77 | gse.add_cluster("rabbitmq", {'rabbitmq-cluster', 'controller'}, {}, 'hostname', highest_policy) |
| 78 | |
| 79 | -- provision facts |
| 80 | gse.set_member_status("neutron", "neutron-api", consts.DOWN, {{message="All neutron endpoints are down"}}, 'node-1') |
| 81 | gse.set_member_status('keystone', 'keystone-admin-api', consts.OKAY, {}, 'node-1') |
| 82 | gse.set_member_status('glance', "glance-api", consts.WARN, {{message="glance-api endpoint is down on node-1"}}, 'node-1') |
| 83 | gse.set_member_status('glance', "glance-registry-api", consts.DOWN, {{message='glance-registry endpoints are down'}}, 'node-1') |
| 84 | gse.set_member_status("rabbitmq", 'rabbitmq-cluster', consts.WARN, {{message="1 RabbitMQ node out of 3 is down"}}, 'node-2') |
| 85 | gse.set_member_status("rabbitmq", 'rabbitmq-cluster', consts.OKAY, {}, 'node-1') |
| 86 | gse.set_member_status("rabbitmq", 'rabbitmq-cluster', consts.OKAY, {}, 'node-3') |
| 87 | gse.set_member_status('heat', "heat-api", consts.WARN, {{message='5xx errors detected'}}, 'node-1') |
| 88 | gse.set_member_status('nova', "nova-api", consts.OKAY, {}, 'node-1') |
| 89 | gse.set_member_status('nova', "nova-ec2_api", consts.OKAY, {}, 'node-1') |
| 90 | gse.set_member_status('nova', "nova-scheduler", consts.OKAY, {}, 'node-1') |
| 91 | gse.set_member_status('rabbitmq', "controller", consts.WARN, {{message='no space left'}}, 'node-1') |
| 92 | gse.set_member_status('heat', "controller", consts.WARN, {{message='no space left'}}, 'node-1') |
| 93 | |
| 94 | for _, v in ipairs({'rabbitmq', 'keystone', 'glance', 'neutron', 'nova', 'heat'}) do |
| 95 | gse.resolve_status(v) |
| 96 | end |
| 97 | |
| 98 | TestGse = {} |
| 99 | |
| 100 | function TestGse:test_ordered_clusters() |
| 101 | local ordered_clusters = gse.get_ordered_clusters() |
| 102 | assertEquals(#ordered_clusters, 6) |
| 103 | assertEquals(ordered_clusters[1], 'rabbitmq') |
| 104 | assertEquals(ordered_clusters[2], 'keystone') |
| 105 | assertEquals(ordered_clusters[3], 'glance') |
| 106 | assertEquals(ordered_clusters[4], 'neutron') |
| 107 | assertEquals(ordered_clusters[5], 'nova') |
| 108 | assertEquals(ordered_clusters[6], 'heat') |
| 109 | end |
| 110 | |
| 111 | function TestGse:test_01_rabbitmq_is_warning() |
| 112 | local status, alarms = gse.resolve_status('rabbitmq') |
| 113 | assertEquals(status, consts.WARN) |
| 114 | assertEquals(#alarms, 2) |
| 115 | assertEquals(alarms[1].hostname, 'node-1') |
| 116 | assertEquals(alarms[1].tags.dependency_name, 'controller') |
| 117 | assertEquals(alarms[1].tags.dependency_level, 'direct') |
| 118 | assertEquals(alarms[2].hostname, 'node-2') |
| 119 | assertEquals(alarms[2].tags.dependency_name, 'rabbitmq-cluster') |
| 120 | assertEquals(alarms[2].tags.dependency_level, 'direct') |
| 121 | end |
| 122 | |
| 123 | function TestGse:test_02_keystone_is_okay() |
| 124 | local status, alarms = gse.resolve_status('keystone') |
| 125 | assertEquals(status, consts.OKAY) |
| 126 | assertEquals(#alarms, 0) |
| 127 | end |
| 128 | |
| 129 | function TestGse:test_03_glance_is_down() |
| 130 | local status, alarms = gse.resolve_status('glance') |
| 131 | assertEquals(status, consts.DOWN) |
| 132 | assertEquals(#alarms, 2) |
| 133 | assert(alarms[1].hostname == nil) |
| 134 | assertEquals(alarms[1].tags.dependency_name, 'glance-api') |
| 135 | assertEquals(alarms[1].tags.dependency_level, 'direct') |
| 136 | assert(alarms[2].hostname == nil) |
| 137 | assertEquals(alarms[2].tags.dependency_name, 'glance-registry-api') |
| 138 | assertEquals(alarms[2].tags.dependency_level, 'direct') |
| 139 | end |
| 140 | |
| 141 | function TestGse:test_04_neutron_is_down() |
| 142 | local status, alarms = gse.resolve_status('neutron') |
| 143 | assertEquals(status, consts.DOWN) |
| 144 | assertEquals(#alarms, 3) |
| 145 | assertEquals(alarms[1].tags.dependency_name, 'neutron-api') |
| 146 | assertEquals(alarms[1].tags.dependency_level, 'direct') |
| 147 | assert(alarms[1].hostname == nil) |
| 148 | assertEquals(alarms[2].tags.dependency_name, 'rabbitmq') |
| 149 | assertEquals(alarms[2].tags.dependency_level, 'hint') |
| 150 | assertEquals(alarms[2].hostname, 'node-1') |
| 151 | assertEquals(alarms[3].tags.dependency_name, 'rabbitmq') |
| 152 | assertEquals(alarms[3].tags.dependency_level, 'hint') |
| 153 | assertEquals(alarms[3].hostname, 'node-2') |
| 154 | end |
| 155 | |
| 156 | function TestGse:test_05_nova_is_okay() |
| 157 | local status, alarms = gse.resolve_status('nova') |
| 158 | assertEquals(status, consts.OKAY) |
| 159 | assertEquals(#alarms, 0) |
| 160 | end |
| 161 | |
| 162 | function TestGse:test_06_heat_is_warning_with_hints() |
| 163 | local status, alarms = gse.resolve_status('heat') |
| 164 | assertEquals(status, consts.WARN) |
| 165 | assertEquals(#alarms, 6) |
| 166 | assertEquals(alarms[1].tags.dependency_name, 'controller') |
| 167 | assertEquals(alarms[1].tags.dependency_level, 'direct') |
| 168 | assert(alarms[1].hostname == nil) |
| 169 | assertEquals(alarms[2].tags.dependency_name, 'heat-api') |
| 170 | assertEquals(alarms[2].tags.dependency_level, 'direct') |
| 171 | assert(alarms[2].hostname == nil) |
| 172 | assertEquals(alarms[3].tags.dependency_name, 'glance') |
| 173 | assertEquals(alarms[3].tags.dependency_level, 'hint') |
| 174 | assert(alarms[3].hostname == nil) |
| 175 | assertEquals(alarms[4].tags.dependency_name, 'glance') |
| 176 | assertEquals(alarms[4].tags.dependency_level, 'hint') |
| 177 | assert(alarms[4].hostname == nil) |
| 178 | assertEquals(alarms[5].tags.dependency_name, 'neutron') |
| 179 | assertEquals(alarms[5].tags.dependency_level, 'hint') |
| 180 | assert(alarms[5].hostname == nil) |
| 181 | assertEquals(alarms[6].tags.dependency_name, 'rabbitmq') |
| 182 | assertEquals(alarms[6].tags.dependency_level, 'hint') |
| 183 | assertEquals(alarms[6].hostname, 'node-2') |
| 184 | end |
| 185 | |
| 186 | function TestGse:test_inject_cluster_metric_for_nova() |
Éric Lemoine | fc2ae37 | 2016-11-08 13:55:03 +0000 | [diff] [blame] | 187 | gse.inject_cluster_metric('nova', {key = "val"}, true) |
Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 188 | local metric = last_injected_msg |
Éric Lemoine | 551218f | 2016-11-07 16:25:29 +0000 | [diff] [blame] | 189 | assertEquals(metric.Type, 'gse_metric') |
| 190 | assertEquals(metric.Fields.member, 'nova') |
| 191 | assertEquals(metric.Fields.name, 'cluster_status') |
Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 192 | assertEquals(metric.Fields.value, consts.OKAY) |
Éric Lemoine | fc2ae37 | 2016-11-08 13:55:03 +0000 | [diff] [blame] | 193 | assertEquals(metric.Fields.key, 'val') |
Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 194 | assertEquals(metric.Payload, '{"alarms":[]}') |
| 195 | end |
| 196 | |
| 197 | function TestGse:test_inject_cluster_metric_for_glance() |
Éric Lemoine | fc2ae37 | 2016-11-08 13:55:03 +0000 | [diff] [blame] | 198 | gse.inject_cluster_metric('glance', {key = "val"}, true) |
Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 199 | local metric = last_injected_msg |
Éric Lemoine | 551218f | 2016-11-07 16:25:29 +0000 | [diff] [blame] | 200 | assertEquals(metric.Type, 'gse_metric') |
| 201 | assertEquals(metric.Fields.member, 'glance') |
| 202 | assertEquals(metric.Fields.name, 'cluster_status') |
Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 203 | assertEquals(metric.Fields.value, consts.DOWN) |
Éric Lemoine | fc2ae37 | 2016-11-08 13:55:03 +0000 | [diff] [blame] | 204 | assertEquals(metric.Fields.key, 'val') |
Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 205 | assert(metric.Payload:match("glance%-registry endpoints are down")) |
| 206 | assert(metric.Payload:match("glance%-api endpoint is down on node%-1")) |
| 207 | end |
| 208 | |
| 209 | function TestGse:test_inject_cluster_metric_for_heat() |
Éric Lemoine | fc2ae37 | 2016-11-08 13:55:03 +0000 | [diff] [blame] | 210 | gse.inject_cluster_metric('heat', {key = "val"}, true) |
Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 211 | local metric = last_injected_msg |
Éric Lemoine | 551218f | 2016-11-07 16:25:29 +0000 | [diff] [blame] | 212 | assertEquals(metric.Type, 'gse_metric') |
| 213 | assertEquals(metric.Fields.member, 'heat') |
| 214 | assertEquals(metric.Fields.name, 'cluster_status') |
Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 215 | assertEquals(metric.Fields.value, consts.WARN) |
Éric Lemoine | fc2ae37 | 2016-11-08 13:55:03 +0000 | [diff] [blame] | 216 | assertEquals(metric.Fields.key, 'val') |
Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 217 | assert(metric.Payload:match("5xx errors detected")) |
| 218 | assert(metric.Payload:match("1 RabbitMQ node out of 3 is down")) |
| 219 | end |
| 220 | |
| 221 | function TestGse:test_reverse_index() |
| 222 | local clusters = gse.find_cluster_memberships('controller') |
| 223 | assertEquals(#clusters, 2) |
| 224 | assertEquals(clusters[1], 'heat') |
| 225 | assertEquals(clusters[2], 'rabbitmq') |
| 226 | end |
| 227 | |
| 228 | lu = LuaUnit |
| 229 | lu:setVerbosity( 1 ) |
| 230 | os.exit( lu:run() ) |