Revision 2d128717
Ajout de tests fonctionnels pour le formulaire de mise à jour des évènements corrélés.
git-svn-id: https://vigilo-dev.si.c-s.fr/svn@1946 b22e2e97-25c9-44ff-b637-2e5ceca36478
vigiboard/tests/functional/test_root.py | ||
---|---|---|
10 | 10 |
Please read http://pythonpaste.org/webtest/ for more information. |
11 | 11 |
|
12 | 12 |
""" |
13 |
from nose.tools import assert_true |
|
13 |
from nose.tools import assert_true, assert_false, assert_equal |
|
14 |
from datetime import datetime |
|
15 |
import transaction |
|
16 |
|
|
17 |
from vigilo.models.configure import DBSession |
|
18 |
from vigilo.models import Event, CorrEvent, \ |
|
19 |
Permission, StateName, \ |
|
20 |
Host, HostGroup, ServiceGroup, LowLevelService |
|
14 | 21 |
|
15 | 22 |
from vigiboard.tests import TestController |
16 | 23 |
|
24 |
def populate_DB(): |
|
25 |
""" Peuple la base de données. """ |
|
26 |
# On ajoute un groupe d'hôtes et un groupe de services. |
|
27 |
hostmanagers = HostGroup(name = u'managersgroup') |
|
28 |
DBSession.add(hostmanagers) |
|
29 |
servicemanagers = ServiceGroup(name = u'managersgroup') |
|
30 |
DBSession.add(servicemanagers) |
|
31 |
DBSession.flush() |
|
32 |
|
|
33 |
# On ajoute la permission 'manage' à ces deux groupes. |
|
34 |
manage_perm = Permission.by_permission_name(u'manage') |
|
35 |
hostmanagers.permissions.append(manage_perm) |
|
36 |
servicemanagers.permissions.append(manage_perm) |
|
37 |
DBSession.flush() |
|
38 |
|
|
39 |
# On crée un 2 hôtes, et on les ajoute au groupe d'hôtes. |
|
40 |
host1 = Host( |
|
41 |
name = u'host1', |
|
42 |
checkhostcmd = u'halt', |
|
43 |
snmpcommunity = u'public', |
|
44 |
hosttpl = u'/dev/null', |
|
45 |
mainip = u'192.168.1.1', |
|
46 |
snmpport = 42, |
|
47 |
weight = 42, |
|
48 |
) |
|
49 |
DBSession.add(host1) |
|
50 |
hostmanagers.hosts.append(host1) |
|
51 |
host2 = Host( |
|
52 |
name = u'host2', |
|
53 |
checkhostcmd = u'halt', |
|
54 |
snmpcommunity = u'public', |
|
55 |
hosttpl = u'/dev/null', |
|
56 |
mainip = u'192.168.1.1', |
|
57 |
snmpport = 42, |
|
58 |
weight = 42, |
|
59 |
) |
|
60 |
DBSession.add(host2) |
|
61 |
hostmanagers.hosts.append(host2) |
|
62 |
DBSession.flush() |
|
63 |
|
|
64 |
# On crée 2 services de bas niveau, et on les ajoute au groupe de services. |
|
65 |
service1 = LowLevelService( |
|
66 |
host = host1, |
|
67 |
servicename = u'service1', |
|
68 |
command = u'halt', |
|
69 |
op_dep = u'+', |
|
70 |
weight = 42, |
|
71 |
) |
|
72 |
DBSession.add(service1) |
|
73 |
servicemanagers.services.append(service1) |
|
74 |
service2 = LowLevelService( |
|
75 |
host = host2, |
|
76 |
servicename = u'service2', |
|
77 |
command = u'halt', |
|
78 |
op_dep = u'+', |
|
79 |
weight = 42, |
|
80 |
) |
|
81 |
DBSession.add(service2) |
|
82 |
servicemanagers.services.append(service2) |
|
83 |
DBSession.flush() |
|
84 |
|
|
85 |
return ([host1, host2], [service1, service2]) |
|
86 |
|
|
87 |
def add_correvent_caused_by(supitem, |
|
88 |
correvent_status=u"None", event_status=u"WARNING"): |
|
89 |
""" |
|
90 |
Ajoute dans la base de données un évènement corrélé causé |
|
91 |
par un incident survenu sur l'item passé en paramètre. |
|
92 |
Génère un historique pour les tests. |
|
93 |
""" |
|
94 |
|
|
95 |
# Ajout d'un événement |
|
96 |
event = Event( |
|
97 |
supitem = supitem, |
|
98 |
message = u'foo', |
|
99 |
current_state = StateName.statename_to_value(event_status), |
|
100 |
) |
|
101 |
DBSession.add(event) |
|
102 |
DBSession.flush() |
|
103 |
|
|
104 |
# Ajout d'un événement corrélé |
|
105 |
aggregate = CorrEvent( |
|
106 |
idcause = event.idevent, |
|
107 |
timestamp_active = datetime.now(), |
|
108 |
priority = 1, |
|
109 |
status = correvent_status) |
|
110 |
aggregate.events.append(event) |
|
111 |
DBSession.add(aggregate) |
|
112 |
DBSession.flush() |
|
113 |
|
|
114 |
return aggregate.idcorrevent |
|
115 |
|
|
17 | 116 |
|
18 | 117 |
class TestRootController(TestController): |
19 | 118 |
""" Classe de test du root controller """ |
... | ... | |
24 | 123 |
msg = 'Unauthorized' |
25 | 124 |
assert_true(msg in response) |
26 | 125 |
|
126 |
def test_update_host_correvents_status(self): |
|
127 |
"""Màj du statut d'évènements corrélés causés par des hôtes""" |
|
128 |
|
|
129 |
# On peuple la BDD avec 2 hôtes, 2 services de bas niveau, |
|
130 |
# et un groupe d'hôtes et de services associés à ces items. |
|
131 |
(hosts, services) = populate_DB() |
|
132 |
|
|
133 |
# On ajoute 2 évènements corrélés causés par ces hôtes |
|
134 |
correvent1_id = add_correvent_caused_by(hosts[0]) |
|
135 |
correvent2_id = add_correvent_caused_by(hosts[1]) |
|
136 |
|
|
137 |
transaction.commit() |
|
138 |
|
|
139 |
### 1er cas : L'utilisateur n'est pas connecté. |
|
140 |
# On vérifie que le plugin retourne bien une erreur 401. |
|
141 |
response = self.app.post( |
|
142 |
'/update', |
|
143 |
{"ids" : str(correvent1_id) + "," + str(correvent2_id), |
|
144 |
"ack" : u'NoChange', |
|
145 |
"trouble_ticket" : u"foo"}, |
|
146 |
status = 401) |
|
147 |
|
|
148 |
### 2ème cas : L'utilisateur utilisé pour |
|
149 |
# se connecter à Vigiboard est 'editor'. |
|
150 |
environ = {'REMOTE_USER': 'editor'} |
|
151 |
|
|
152 |
# On s'attend à ce qu'une erreur 302 soit renvoyée, et à |
|
153 |
# ce qu'un message d'erreur précise à l'utilisateur qu'il |
|
154 |
# n'a pas la permission de modifier ces évènements. |
|
155 |
response = self.app.post( |
|
156 |
'/update', |
|
157 |
{"id" : str(correvent1_id) + "," + str(correvent2_id), |
|
158 |
"ack" : u'NoChange', |
|
159 |
"trouble_ticket" : u"foo"}, |
|
160 |
status = 302, |
|
161 |
extra_environ = environ) |
|
162 |
|
|
163 |
response = response.follow( |
|
164 |
status=200, |
|
165 |
extra_environ = environ) |
|
166 |
assert_true(response.lxml.xpath( |
|
167 |
'//div[@id="flash"]/div[@class="error"]')) |
|
168 |
|
|
169 |
### 3ème cas : L'utilisateur utilisé pour |
|
170 |
# se connecter à Vigiboard est 'manager'. |
|
171 |
environ = {'REMOTE_USER': 'manager'} |
|
172 |
|
|
173 |
# On s'attend à ce que le statut de la requête soit 302, |
|
174 |
# et à ce qu'un message informe l'utilisateur que les |
|
175 |
# évènements corrélés sélectionnées ont bien été mis à jour. |
|
176 |
response = self.app.post( |
|
177 |
'/update', |
|
178 |
{"id" : str(correvent1_id) + "," + str(correvent2_id), |
|
179 |
"ack" : u'NoChange', |
|
180 |
"trouble_ticket" : u"foo"}, |
|
181 |
status = 302, |
|
182 |
extra_environ = environ) |
|
183 |
|
|
184 |
response = response.follow( |
|
185 |
status=200, |
|
186 |
extra_environ = environ) |
|
187 |
assert_false(response.lxml.xpath( |
|
188 |
'//div[@id="flash"]/div[@class="error"]')) |
|
189 |
assert_true(response.lxml.xpath( |
|
190 |
'//div[@id="flash"]/div[@class="ok"]')) |
|
191 |
|
|
192 |
# On s'assure que le ticket de l'évènement corrélé |
|
193 |
# a bien été mis à jour dans la base de données. |
|
194 |
correvents = DBSession.query( |
|
195 |
CorrEvent.trouble_ticket |
|
196 |
).filter(CorrEvent.idcorrevent.in_([correvent1_id, correvent2_id]) |
|
197 |
).all() |
|
198 |
|
|
199 |
assert_equal(correvents[0].trouble_ticket, u"foo") |
|
200 |
assert_equal(correvents[1].trouble_ticket, u"foo") |
|
201 |
|
|
202 |
def test_update_service_correvents_status(self): |
|
203 |
"""Màj du statut d'évènements corrélés causés par des SBN""" |
|
204 |
|
|
205 |
# On peuple la BDD avec 2 hôtes, 2 services de bas niveau, |
|
206 |
# et un groupe d'hôtes et de services associés à ces items. |
|
207 |
(hosts, services) = populate_DB() |
|
208 |
|
|
209 |
# On ajoute 2 évènements corrélés causés par ces hôtes |
|
210 |
correvent1_id = add_correvent_caused_by(services[0]) |
|
211 |
correvent2_id = add_correvent_caused_by(services[1]) |
|
212 |
|
|
213 |
transaction.commit() |
|
214 |
|
|
215 |
### 1er cas : L'utilisateur n'est pas connecté. |
|
216 |
# On vérifie que le plugin retourne bien une erreur 401. |
|
217 |
response = self.app.post( |
|
218 |
'/update', |
|
219 |
{"ids" : str(correvent1_id) + "," + str(correvent2_id), |
|
220 |
"ack" : u'NoChange', |
|
221 |
"trouble_ticket" : u"foo"}, |
|
222 |
status = 401) |
|
223 |
|
|
224 |
### 2ème cas : L'utilisateur utilisé pour |
|
225 |
# se connecter à Vigiboard est 'editor'. |
|
226 |
environ = {'REMOTE_USER': 'editor'} |
|
227 |
|
|
228 |
# On s'attend à ce qu'une erreur 302 soit renvoyée, et à |
|
229 |
# ce qu'un message d'erreur précise à l'utilisateur qu'il |
|
230 |
# n'a pas la permission de modifier ces évènements. |
|
231 |
response = self.app.post( |
|
232 |
'/update', |
|
233 |
{"id" : str(correvent1_id) + "," + str(correvent2_id), |
|
234 |
"ack" : u'NoChange', |
|
235 |
"trouble_ticket" : u"foo"}, |
|
236 |
status = 302, |
|
237 |
extra_environ = environ) |
|
238 |
|
|
239 |
response = response.follow( |
|
240 |
status=200, |
|
241 |
extra_environ = environ) |
|
242 |
assert_true(response.lxml.xpath( |
|
243 |
'//div[@id="flash"]/div[@class="error"]')) |
|
244 |
|
|
245 |
### 3ème cas : L'utilisateur utilisé pour |
|
246 |
# se connecter à Vigiboard est 'manager'. |
|
247 |
environ = {'REMOTE_USER': 'manager'} |
|
248 |
|
|
249 |
# On s'attend à ce que le statut de la requête soit 302, |
|
250 |
# et à ce qu'un message informe l'utilisateur que les |
|
251 |
# évènements corrélés sélectionnées ont bien été mis à jour. |
|
252 |
response = self.app.post( |
|
253 |
'/update', |
|
254 |
{"id" : str(correvent1_id) + "," + str(correvent2_id), |
|
255 |
"ack" : u'NoChange', |
|
256 |
"trouble_ticket" : u"foo"}, |
|
257 |
status = 302, |
|
258 |
extra_environ = environ) |
|
259 |
|
|
260 |
response = response.follow( |
|
261 |
status=200, |
|
262 |
extra_environ = environ) |
|
263 |
assert_false(response.lxml.xpath( |
|
264 |
'//div[@id="flash"]/div[@class="error"]')) |
|
265 |
assert_true(response.lxml.xpath( |
|
266 |
'//div[@id="flash"]/div[@class="ok"]')) |
|
267 |
|
|
268 |
# On s'assure que le ticket de l'évènement corrélé |
|
269 |
# a bien été mis à jour dans la base de données. |
|
270 |
correvents = DBSession.query( |
|
271 |
CorrEvent.trouble_ticket |
|
272 |
).filter(CorrEvent.idcorrevent.in_([correvent1_id, correvent2_id]) |
|
273 |
).all() |
|
274 |
assert_equal(correvents[0].trouble_ticket, u"foo") |
|
275 |
assert_equal(correvents[1].trouble_ticket, u"foo") |
|
276 |
|
|
277 |
def test_update_host_correvents_tickets(self): |
|
278 |
"""Màj de tickets d'évènements corrélés causés par des hôtes""" |
|
279 |
|
|
280 |
# On peuple la BDD avec 2 hôtes, 2 services de bas niveau, |
|
281 |
# et un groupe d'hôtes et de services associés à ces items. |
|
282 |
(hosts, services) = populate_DB() |
|
283 |
|
|
284 |
# On ajoute 2 évènements corrélés causés par ces hôtes |
|
285 |
correvent1_id = add_correvent_caused_by(hosts[0]) |
|
286 |
correvent2_id = add_correvent_caused_by(hosts[1]) |
|
287 |
|
|
288 |
transaction.commit() |
|
289 |
|
|
290 |
### 1er cas : L'utilisateur n'est pas connecté. |
|
291 |
# On vérifie que le plugin retourne bien une erreur 401. |
|
292 |
response = self.app.post( |
|
293 |
'/update', |
|
294 |
{"ids" : str(correvent1_id) + "," + str(correvent2_id), |
|
295 |
"ack" : u'Acknowledged', |
|
296 |
"trouble_ticket" : ""}, |
|
297 |
status = 401) |
|
298 |
|
|
299 |
### 2ème cas : L'utilisateur utilisé pour |
|
300 |
# se connecter à Vigiboard est 'editor'. |
|
301 |
environ = {'REMOTE_USER': 'editor'} |
|
302 |
|
|
303 |
# On s'attend à ce qu'une erreur 302 soit renvoyée, et à |
|
304 |
# ce qu'un message d'erreur précise à l'utilisateur qu'il |
|
305 |
# n'a pas la permission de modifier ces évènements. |
|
306 |
response = self.app.post( |
|
307 |
'/update', |
|
308 |
{"id" : str(correvent1_id) + "," + str(correvent2_id), |
|
309 |
"ack" : u'Acknowledged', |
|
310 |
"trouble_ticket" : ""}, |
|
311 |
status = 302, |
|
312 |
extra_environ = environ) |
|
313 |
|
|
314 |
response = response.follow( |
|
315 |
status=200, |
|
316 |
extra_environ = environ) |
|
317 |
assert_true(response.lxml.xpath( |
|
318 |
'//div[@id="flash"]/div[@class="error"]')) |
|
319 |
|
|
320 |
### 3ème cas : L'utilisateur utilisé pour |
|
321 |
# se connecter à Vigiboard est 'manager'. |
|
322 |
environ = {'REMOTE_USER': 'manager'} |
|
323 |
|
|
324 |
# On s'attend à ce que le statut de la requête soit 302, |
|
325 |
# et à ce qu'un message informe l'utilisateur que les |
|
326 |
# évènements corrélés sélectionnées ont bien été mis à jour. |
|
327 |
response = self.app.post( |
|
328 |
'/update', |
|
329 |
{"id" : str(correvent1_id) + "," + str(correvent2_id), |
|
330 |
"ack" : u'Acknowledged', |
|
331 |
"trouble_ticket" : ""}, |
|
332 |
status = 302, |
|
333 |
extra_environ = environ) |
|
334 |
|
|
335 |
response = response.follow( |
|
336 |
status=200, |
|
337 |
extra_environ = environ) |
|
338 |
assert_false(response.lxml.xpath( |
|
339 |
'//div[@id="flash"]/div[@class="error"]')) |
|
340 |
assert_true(response.lxml.xpath( |
|
341 |
'//div[@id="flash"]/div[@class="ok"]')) |
|
342 |
|
|
343 |
# On s'assure que le statut de l'évènement corrélé |
|
344 |
# a bien été mis à jour dans la base de données. |
|
345 |
correvents = DBSession.query( |
|
346 |
CorrEvent.status |
|
347 |
).filter(CorrEvent.idcorrevent.in_([correvent1_id, correvent2_id]) |
|
348 |
).all() |
|
349 |
assert_equal(correvents[0].status, u'Acknowledged') |
|
350 |
assert_equal(correvents[1].status, u'Acknowledged') |
|
351 |
|
|
352 |
|
|
353 |
def test_update_service_correvents_tickets(self): |
|
354 |
"""Màj de tickets d'évènements corrélés causés par des SBN""" |
|
355 |
|
|
356 |
# On peuple la BDD avec 2 hôtes, 2 services de bas niveau, |
|
357 |
# et un groupe d'hôtes et de services associés à ces items. |
|
358 |
(hosts, services) = populate_DB() |
|
359 |
|
|
360 |
# On ajoute 2 évènements corrélés causés par ces hôtes |
|
361 |
correvent1_id = add_correvent_caused_by(services[0]) |
|
362 |
correvent2_id = add_correvent_caused_by(services[1]) |
|
363 |
|
|
364 |
transaction.commit() |
|
365 |
|
|
366 |
### 1er cas : L'utilisateur n'est pas connecté. |
|
367 |
# On vérifie que le plugin retourne bien une erreur 401. |
|
368 |
response = self.app.post( |
|
369 |
'/update', |
|
370 |
{"ids" : str(correvent1_id) + "," + str(correvent2_id), |
|
371 |
"ack" : u'Acknowledged', |
|
372 |
"trouble_ticket" : ""}, |
|
373 |
status = 401) |
|
374 |
|
|
375 |
### 2ème cas : L'utilisateur utilisé pour |
|
376 |
# se connecter à Vigiboard est 'editor'. |
|
377 |
environ = {'REMOTE_USER': 'editor'} |
|
378 |
|
|
379 |
# On s'attend à ce qu'une erreur 302 soit renvoyée, et à |
|
380 |
# ce qu'un message d'erreur précise à l'utilisateur qu'il |
|
381 |
# n'a pas la permission de modifier ces évènements. |
|
382 |
response = self.app.post( |
|
383 |
'/update', |
|
384 |
{"id" : str(correvent1_id) + "," + str(correvent2_id), |
|
385 |
"ack" : u'Acknowledged', |
|
386 |
"trouble_ticket" : ""}, |
|
387 |
status = 302, |
|
388 |
extra_environ = environ) |
|
389 |
|
|
390 |
response = response.follow( |
|
391 |
status=200, |
|
392 |
extra_environ = environ) |
|
393 |
assert_true(response.lxml.xpath( |
|
394 |
'//div[@id="flash"]/div[@class="error"]')) |
|
395 |
|
|
396 |
### 3ème cas : L'utilisateur utilisé pour |
|
397 |
# se connecter à Vigiboard est 'manager'. |
|
398 |
environ = {'REMOTE_USER': 'manager'} |
|
399 |
|
|
400 |
# On s'attend à ce que le statut de la requête soit 302, |
|
401 |
# et à ce qu'un message informe l'utilisateur que les |
|
402 |
# évènements corrélés sélectionnées ont bien été mis à jour. |
|
403 |
response = self.app.post( |
|
404 |
'/update', |
|
405 |
{"id" : str(correvent1_id) + "," + str(correvent2_id), |
|
406 |
"ack" : u'Acknowledged', |
|
407 |
"trouble_ticket" : ""}, |
|
408 |
status = 302, |
|
409 |
extra_environ = environ) |
|
410 |
|
|
411 |
response = response.follow( |
|
412 |
status=200, |
|
413 |
extra_environ = environ) |
|
414 |
assert_false(response.lxml.xpath( |
|
415 |
'//div[@id="flash"]/div[@class="error"]')) |
|
416 |
assert_true(response.lxml.xpath( |
|
417 |
'//div[@id="flash"]/div[@class="ok"]')) |
|
418 |
|
|
419 |
# On s'assure que le statut de l'évènement corrélé |
|
420 |
# a bien été mis à jour dans la base de données. |
|
421 |
correvents = DBSession.query( |
|
422 |
CorrEvent.status |
|
423 |
).filter(CorrEvent.idcorrevent.in_([correvent1_id, correvent2_id]) |
|
424 |
).all() |
|
425 |
assert_equal(correvents[0].status, u'Acknowledged') |
|
426 |
assert_equal(correvents[1].status, u'Acknowledged') |
|
427 |
|
Also available in: Unified diff