blob: b68986d0a6525685e80279619ccda6a44d8ec746 [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 cjson = require('cjson')
26local consts = require('gse_constants')
27
28local gse = require('gse')
29local gse_policy = require('gse_policy')
30
31highest_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
72gse.add_cluster("heat", {'heat-api', 'controller'}, {'nova', 'glance', 'neutron', 'keystone', 'rabbitmq'}, 'member', highest_policy)
73gse.add_cluster("nova", {'nova-api', 'nova-ec2-api', 'nova-scheduler'}, {'glance', 'neutron', 'keystone', 'rabbitmq'}, 'member', highest_policy)
74gse.add_cluster("neutron", {'neutron-api'}, {'keystone', 'rabbitmq'}, 'member', highest_policy)
75gse.add_cluster("keystone", {'keystone-admin-api', 'keystone-public-api'}, {}, 'member', highest_policy)
76gse.add_cluster("glance", {'glance-api', 'glance-registry-api'}, {'keystone'}, 'member', highest_policy)
77gse.add_cluster("rabbitmq", {'rabbitmq-cluster', 'controller'}, {}, 'hostname', highest_policy)
78
79-- provision facts
80gse.set_member_status("neutron", "neutron-api", consts.DOWN, {{message="All neutron endpoints are down"}}, 'node-1')
81gse.set_member_status('keystone', 'keystone-admin-api', consts.OKAY, {}, 'node-1')
82gse.set_member_status('glance', "glance-api", consts.WARN, {{message="glance-api endpoint is down on node-1"}}, 'node-1')
83gse.set_member_status('glance', "glance-registry-api", consts.DOWN, {{message='glance-registry endpoints are down'}}, 'node-1')
84gse.set_member_status("rabbitmq", 'rabbitmq-cluster', consts.WARN, {{message="1 RabbitMQ node out of 3 is down"}}, 'node-2')
85gse.set_member_status("rabbitmq", 'rabbitmq-cluster', consts.OKAY, {}, 'node-1')
86gse.set_member_status("rabbitmq", 'rabbitmq-cluster', consts.OKAY, {}, 'node-3')
87gse.set_member_status('heat', "heat-api", consts.WARN, {{message='5xx errors detected'}}, 'node-1')
88gse.set_member_status('nova', "nova-api", consts.OKAY, {}, 'node-1')
89gse.set_member_status('nova', "nova-ec2_api", consts.OKAY, {}, 'node-1')
90gse.set_member_status('nova', "nova-scheduler", consts.OKAY, {}, 'node-1')
91gse.set_member_status('rabbitmq', "controller", consts.WARN, {{message='no space left'}}, 'node-1')
92gse.set_member_status('heat', "controller", consts.WARN, {{message='no space left'}}, 'node-1')
93
94for _, v in ipairs({'rabbitmq', 'keystone', 'glance', 'neutron', 'nova', 'heat'}) do
95 gse.resolve_status(v)
96end
97
98TestGse = {}
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()
Guillaume Thouvenin6faa8622017-02-28 16:40:42 +0100187 gse.inject_cluster_metric('nova', {key = "val"}, true, true, 'mail')
Éric Lemoine71272712016-11-08 12:53:51 +0000188 local metric = last_injected_msg
Éric Lemoine551218f2016-11-07 16:25:29 +0000189 assertEquals(metric.Type, 'gse_metric')
190 assertEquals(metric.Fields.member, 'nova')
191 assertEquals(metric.Fields.name, 'cluster_status')
Éric Lemoine71272712016-11-08 12:53:51 +0000192 assertEquals(metric.Fields.value, consts.OKAY)
Éric Lemoinefc2ae372016-11-08 13:55:03 +0000193 assertEquals(metric.Fields.key, 'val')
Guillaume Thouvenin6faa8622017-02-28 16:40:42 +0100194 assertEquals(metric.Fields.notification_handler, 'mail')
Éric Lemoine71272712016-11-08 12:53:51 +0000195 assertEquals(metric.Payload, '{"alarms":[]}')
196 end
197
198 function TestGse:test_inject_cluster_metric_for_glance()
Guillaume Thouvenin6faa8622017-02-28 16:40:42 +0100199 gse.inject_cluster_metric('glance', {key = "val"}, false, false, 'mail')
Éric Lemoine71272712016-11-08 12:53:51 +0000200 local metric = last_injected_msg
Éric Lemoine551218f2016-11-07 16:25:29 +0000201 assertEquals(metric.Type, 'gse_metric')
202 assertEquals(metric.Fields.member, 'glance')
203 assertEquals(metric.Fields.name, 'cluster_status')
Éric Lemoine71272712016-11-08 12:53:51 +0000204 assertEquals(metric.Fields.value, consts.DOWN)
Éric Lemoinefc2ae372016-11-08 13:55:03 +0000205 assertEquals(metric.Fields.key, 'val')
Guillaume Thouvenin6faa8622017-02-28 16:40:42 +0100206 assertEquals(metric.Fields.notification_handler, 'mail')
Éric Lemoine71272712016-11-08 12:53:51 +0000207 assert(metric.Payload:match("glance%-registry endpoints are down"))
208 assert(metric.Payload:match("glance%-api endpoint is down on node%-1"))
209 end
210
211 function TestGse:test_inject_cluster_metric_for_heat()
Guillaume Thouvenin6faa8622017-02-28 16:40:42 +0100212 gse.inject_cluster_metric('heat', {key = "val"}, true, true, 'mail')
Éric Lemoine71272712016-11-08 12:53:51 +0000213 local metric = last_injected_msg
Éric Lemoine551218f2016-11-07 16:25:29 +0000214 assertEquals(metric.Type, 'gse_metric')
215 assertEquals(metric.Fields.member, 'heat')
216 assertEquals(metric.Fields.name, 'cluster_status')
Éric Lemoine71272712016-11-08 12:53:51 +0000217 assertEquals(metric.Fields.value, consts.WARN)
Éric Lemoinefc2ae372016-11-08 13:55:03 +0000218 assertEquals(metric.Fields.key, 'val')
Guillaume Thouvenin6faa8622017-02-28 16:40:42 +0100219 assertEquals(metric.Fields.notification_handler, 'mail')
Éric Lemoine71272712016-11-08 12:53:51 +0000220 assert(metric.Payload:match("5xx errors detected"))
221 assert(metric.Payload:match("1 RabbitMQ node out of 3 is down"))
222 end
223
224 function TestGse:test_reverse_index()
225 local clusters = gse.find_cluster_memberships('controller')
226 assertEquals(#clusters, 2)
227 assertEquals(clusters[1], 'heat')
228 assertEquals(clusters[2], 'rabbitmq')
229 end
230
231lu = LuaUnit
232lu:setVerbosity( 1 )
233os.exit( lu:run() )