6
from testtools import TestCase
7
from mock import patch, call
11
from charmhelpers.contrib.charmsupport import nrpe
12
from charmhelpers.core.hookenv import Serializable
15
class NRPERelationTest(TestCase):
16
"""Tests for the update_nrpe_checks hook.
18
Half of this is already tested in the tests for charmsupport.nrpe, but
19
as the hook in the charm pre-dates that, the tests are left here to ensure
20
backwards-compatibility.
24
'config': {'object': nrpe},
25
'log': {'object': nrpe},
26
'getpwnam': {'object': pwd},
27
'getgrnam': {'object': grp},
28
'mkdir': {'object': os},
29
'chown': {'object': os},
30
'exists': {'object': os.path},
31
'listdir': {'object': os},
32
'remove': {'object': os},
33
'open': {'object': nrpe, 'create': True},
34
'isfile': {'object': os.path},
35
'call': {'object': subprocess},
36
'install_nrpe_scripts': {'object': hooks},
37
'relation_ids': {'object': nrpe},
38
'relation_set': {'object': nrpe},
42
super(NRPERelationTest, self).setUp()
45
for attr, data in self.patches.items():
46
create = data.get('create', False)
47
patcher = patch.object(data['object'], attr, create=create)
48
self.patched[attr] = patcher.start()
49
self.addCleanup(patcher.stop)
50
if not 'JUJU_UNIT_NAME' in os.environ:
51
os.environ['JUJU_UNIT_NAME'] = 'test'
53
def check_call_counts(self, **kwargs):
54
for attr, expected in kwargs.items():
55
patcher = self.patched[attr]
56
self.assertEqual(expected, patcher.call_count, attr)
58
def test_update_nrpe_no_nagios_bails(self):
59
config = {'nagios_context': 'test'}
60
self.patched['config'].return_value = Serializable(config)
61
self.patched['getpwnam'].side_effect = KeyError
63
self.assertEqual(None, hooks.update_nrpe_checks())
65
expected = 'Nagios user not set up, nrpe checks not updated'
66
self.patched['log'].assert_called_once_with(expected)
67
self.check_call_counts(log=1, config=1, getpwnam=1)
69
def test_update_nrpe_removes_existing_config(self):
71
'nagios_context': 'test',
72
'nagios_check_http_params': '-u http://example.com/url',
74
self.patched['config'].return_value = Serializable(config)
75
self.patched['exists'].return_value = True
76
self.patched['listdir'].return_value = [
77
'foo', 'bar.cfg', 'check_squidrp.cfg']
79
self.assertEqual(None, hooks.update_nrpe_checks())
81
expected = '/var/lib/nagios/export/check_squidrp.cfg'
82
self.patched['remove'].assert_called_once_with(expected)
83
self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
84
exists=5, remove=1, open=4, listdir=2)
86
def test_update_nrpe_uses_check_squidpeers(self):
88
'nagios_context': 'test',
90
self.patched['config'].return_value = Serializable(config)
91
self.patched['exists'].return_value = True
92
self.patched['isfile'].return_value = False
94
self.assertEqual(None, hooks.update_nrpe_checks())
95
self.assertEqual(2, self.patched['open'].call_count)
96
filename = 'check_squidpeers.cfg'
98
service_file_contents = """
99
#---------------------------------------------------
100
# This file is Juju managed
101
#---------------------------------------------------
105
service_description test-test[squidpeers] Check Squid Peers
106
check_command check_nrpe!check_squidpeers
110
self.patched['open'].assert_has_calls(
111
[call('/etc/nagios/nrpe.d/%s' % filename, 'w'),
112
call('/var/lib/nagios/export/service__test-test_%s' %
114
call().__enter__().write(service_file_contents),
115
call().__enter__().write('# check squidpeers\n'),
116
call().__enter__().write(
117
'command[check_squidpeers]='
118
'/check_squidpeers\n')],
121
self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
122
exists=3, open=2, listdir=1)
124
def test_update_nrpe_with_check_url(self):
126
'nagios_context': 'test',
127
'nagios_check_http_params': '-u foo -H bar',
129
self.patched['config'].return_value = Serializable(config)
130
self.patched['exists'].return_value = True
131
self.patched['isfile'].return_value = False
133
self.assertEqual(None, hooks.update_nrpe_checks())
134
self.assertEqual(4, self.patched['open'].call_count)
135
filename = 'check_squidrp.cfg'
137
service_file_contents = """
138
#---------------------------------------------------
139
# This file is Juju managed
140
#---------------------------------------------------
144
service_description test-test[squidrp] Check Squid
145
check_command check_nrpe!check_squidrp
149
self.patched['open'].assert_has_calls(
150
[call('/etc/nagios/nrpe.d/%s' % filename, 'w'),
151
call('/var/lib/nagios/export/service__test-test_%s' %
153
call().__enter__().write(service_file_contents),
154
call().__enter__().write('# check squidrp\n'),
155
call().__enter__().write(
156
'command[check_squidrp]=/check_http -u foo -H bar\n')],
159
self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
160
exists=5, open=4, listdir=2)
162
def test_update_nrpe_with_no_check_path(self):
164
'nagios_context': 'test',
165
'services': '- {service_name: i_ytimg_com}',
167
self.patched['config'].return_value = Serializable(config)
168
self.patched['exists'].return_value = True
170
self.assertEqual(None, hooks.update_nrpe_checks())
172
self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
173
exists=3, open=2, listdir=1)
175
def test_update_nrpe_with_services_and_host_header(self):
177
'nagios_context': 'test',
178
'services': '- {service_name: i_ytimg_com, nrpe_check_path: /}',
180
self.patched['config'].return_value = Serializable(config)
181
self.patched['exists'].return_value = True
183
self.assertEqual(None, hooks.update_nrpe_checks())
185
self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
186
exists=5, open=4, listdir=2)
187
expected = ('command[check_squid-i_ytimg_com]=/check_http '
188
'-I 127.0.0.1 -p 3128 --method=HEAD '
189
'-u http://i_ytimg_com/\n')
190
self.patched['open'].assert_has_calls(
191
[call('/etc/nagios/nrpe.d/check_squid-i_ytimg_com.cfg', 'w'),
192
call().__enter__().write(expected)],
195
def test_update_nrpe_with_dotted_service_name_and_host_header(self):
197
'nagios_context': 'test',
198
'services': '- {service_name: i.ytimg.com, nrpe_check_path: /}',
200
self.patched['config'].return_value = Serializable(config)
201
self.patched['exists'].return_value = True
203
self.assertEqual(None, hooks.update_nrpe_checks())
205
self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
206
exists=5, open=4, listdir=2)
207
expected = ('command[check_squid-i_ytimg_com]=/check_http '
208
'-I 127.0.0.1 -p 3128 --method=HEAD '
209
'-u http://i.ytimg.com/\n')
210
self.patched['open'].assert_has_calls(
211
[call('/etc/nagios/nrpe.d/check_squid-i_ytimg_com.cfg', 'w'),
212
call().__enter__().write(expected)],
215
def test_update_nrpe_with_services_and_balancer_name_header(self):
217
'nagios_context': 'test',
218
'x_balancer_name_allowed': True,
219
'services': '- {service_name: i_ytimg_com, nrpe_check_path: /}',
221
self.patched['config'].return_value = Serializable(config)
222
self.patched['exists'].return_value = True
224
self.assertEqual(None, hooks.update_nrpe_checks())
226
self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
227
exists=5, open=4, listdir=2)
229
expected = ('command[check_squid-i_ytimg_com]=/check_http '
230
'-I 127.0.0.1 -p 3128 --method=HEAD -u http://localhost/ '
231
"-k 'X-Balancer-Name: i_ytimg_com'\n")
232
self.patched['open'].assert_has_calls(
233
[call('/etc/nagios/nrpe.d/check_squid-i_ytimg_com.cfg', 'w'),
234
call().__enter__().write(expected)],
237
def test_update_nrpe_with_services_and_optional_path(self):
238
services = '- {nrpe_check_path: /foo.jpg, service_name: foo_com}\n'
240
'nagios_context': 'test',
241
'services': services,
243
self.patched['config'].return_value = Serializable(config)
244
self.patched['exists'].return_value = True
246
self.assertEqual(None, hooks.update_nrpe_checks())
248
self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
249
exists=5, open=4, listdir=2)
250
expected = ('command[check_squid-foo_com]=/check_http '
251
'-I 127.0.0.1 -p 3128 --method=HEAD '
252
'-u http://foo_com/foo.jpg\n')
253
self.patched['open'].assert_has_calls(
254
[call('/etc/nagios/nrpe.d/check_squid-foo_com.cfg', 'w'),
255
call().__enter__().write(expected)],
258
def test_update_nrpe_restarts_service(self):
260
'nagios_context': 'test',
261
'nagios_check_http_params': '-u foo -p 3128'
263
self.patched['config'].return_value = Serializable(config)
264
self.patched['exists'].return_value = True
266
self.assertEqual(None, hooks.update_nrpe_checks())
268
expected = ['service', 'nagios-nrpe-server', 'restart']
269
self.assertEqual(expected, self.patched['call'].call_args[0][0])
270
self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
271
exists=5, open=4, listdir=2, call=1)