/
test_service_generators.py
executable file
·184 lines (151 loc) · 8.29 KB
/
test_service_generators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
# Copyright (C) 2009-2014:
# Gabes Jean, naparuba@gmail.com
# Gerhard Lausser, Gerhard.Lausser@consol.de
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.
#
# This file is used to test reading and processing of config files
#
from __future__ import absolute_import, division, print_function, unicode_literals
from shinken_test import *
class TestConfig(ShinkenTest):
def setUp(self):
self.setup_with_file('etc/shinken_service_generators.cfg')
def test_service_generators(self):
host = self.sched.hosts.find_by_name("test_host_0")
host.checks_in_progress = []
host.act_depend_of = [] # ignore the router
router = self.sched.hosts.find_by_name("test_router_0")
router.checks_in_progress = []
router.act_depend_of = [] # ignore the router
svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
print("All service of", "test_host_0")
for s in host.services:
print(s.get_name())
# We ask for 4 services with our disks :)
svc_c = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Generated Service C")
svc_d = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Generated Service D")
svc_e = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Generated Service E")
svc_f = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Generated Service F")
svc_g = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Generated Service G")
self.assertIsNot(svc_c, None)
self.assertIsNot(svc_d, None)
self.assertIsNot(svc_e, None)
self.assertIsNot(svc_f, None)
self.assertIsNot(svc_g, None)
# two classics
self.assertEqual(['C', '80%', '90%'], svc_c.check_command.args)
self.assertEqual(['D', '95%', '70%'], svc_d.check_command.args)
# a default parameters
self.assertEqual(['E', '38%', '24%'], svc_e.check_command.args)
# and another one
self.assertEqual(['F', '95%', '70%'], svc_f.check_command.args)
# and the tricky last one (with no value :) )
self.assertEqual(['G', '38%', '24%'], svc_g.check_command.args)
# Now check that the dependencies are also created as Generated Service C Dependant -> Generated Service C
svc_c_dep = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Generated Service C Dependant")
self.assertIsNot(svc_c_dep, None)
# Dep version should a child of svc
self.assertIn(svc_c_dep, svc_c.child_dependencies)
# But not on other of course
self.assertNotIn(svc_c_dep, svc_d.child_dependencies)
def test_service_generators_not(self):
host = self.sched.hosts.find_by_name("test_host_0")
host.checks_in_progress = []
host.act_depend_of = [] # ignore the router
router = self.sched.hosts.find_by_name("test_router_0")
router.checks_in_progress = []
router.act_depend_of = [] # ignore the router
svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
print("All service of", "test_host_0")
for s in host.services:
print(s.get_name())
# We ask for 4 services with our disks :)
svc_c = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Generated Service NOT C")
svc_d = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Generated Service NOT D")
svc_e = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Generated Service NOT E")
svc_f = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Generated Service NOT F")
svc_g = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Generated Service NOT G")
self.assertIsNot(svc_c, None)
self.assertIsNot(svc_d, None)
self.assertIs(None, svc_e)
self.assertIs(None, svc_f)
self.assertIsNot(svc_g, None)
def test_service_generators_key_generator(self):
host = self.sched.hosts.find_by_name("sw_0")
host.checks_in_progress = []
host.act_depend_of = [] # ignore the router
print("All service of", "sw_0")
for s in host.services:
print(s.get_name())
# We ask for our 6*46 + 6 services with our ports :)
# _ports Unit [1-6] Port [0-46]$(80%!90%)$,Unit [1-6] Port 47$(80%!90%)$
for unit_id in range(1, 7):
for port_id in range(0, 47):
n = "Unit %d Port %d" % (unit_id, port_id)
print("Look for port", 'Generated Service ' + n)
svc = self.sched.services.find_srv_by_name_and_hostname("sw_0", 'Generated Service ' + n)
self.assertIsNot(svc, None)
for unit_id in range(1, 7):
port_id = 47
n = "Unit %d Port %d" % (unit_id, port_id)
print("Look for port", 'Generated Service ' + n)
svc = self.sched.services.find_srv_by_name_and_hostname("sw_0", 'Generated Service ' + n)
self.assertIsNot(svc, None)
def test_service_generators_array(self):
host = self.sched.hosts.find_by_name("sw_1")
host.checks_in_progress = []
host.act_depend_of = [] # ignore the router
print("All service of", "sw_1")
for s in host.services:
print(s.get_name())
svc = self.sched.services.find_srv_by_name_and_hostname("sw_1", 'Generated Service Gigabit0/1')
self.assertIsNot(svc, None)
self.assertEqual('check_service!1!80%!90%', svc.check_command.call)
svc = self.sched.services.find_srv_by_name_and_hostname("sw_1", 'Generated Service Gigabit0/2')
self.assertIsNot(svc, None)
self.assertEqual('check_service!2!80%!90%', svc.check_command.call)
svc = self.sched.services.find_srv_by_name_and_hostname("sw_1", 'Generated Service Ethernet0/1')
self.assertIsNot(svc, None)
self.assertEqual('check_service!3!80%!95%', svc.check_command.call)
svc = self.sched.services.find_srv_by_name_and_hostname("sw_1", 'Generated Service ISDN1')
self.assertIsNot(svc, None)
self.assertEqual('check_service!4!80%!95%', svc.check_command.call)
def test_service_inherited_description(self):
s1 = self.sched.services.find_srv_by_name_and_hostname("r1", 'SSH')
s2 = self.sched.services.find_srv_by_name_and_hostname("r2", 'SSH')
self.assertIsNotNone(s1)
self.assertIsNotNone(s2)
def test_key_as_descr(self):
svc_h = self.sched.services.find_srv_by_name_and_hostname("test_host_1", "Generated Service H")
svc_i = self.sched.services.find_srv_by_name_and_hostname("test_host_1", "Generated Service I")
svc_j = self.sched.services.find_srv_by_name_and_hostname("test_host_2", "Generated Service J")
svc_k = self.sched.services.find_srv_by_name_and_hostname("test_host_2", "Generated Service K")
svc_l1 = self.sched.services.find_srv_by_name_and_hostname("test_host_1", "Generated Service L")
svc_m1 = self.sched.services.find_srv_by_name_and_hostname("test_host_1", "Generated Service M")
svc_l2 = self.sched.services.find_srv_by_name_and_hostname("test_host_2", "Generated Service L")
svc_m2 = self.sched.services.find_srv_by_name_and_hostname("test_host_2", "Generated Service M")
self.assertIsNotNone(svc_h)
self.assertIsNotNone(svc_i)
self.assertIsNotNone(svc_j)
self.assertIsNotNone(svc_k)
self.assertIsNotNone(svc_l1)
self.assertIsNotNone(svc_m1)
self.assertIsNotNone(svc_l2)
self.assertIsNotNone(svc_m2)
if __name__ == '__main__':
unittest.main()