5
1
from testtools import TestCase
6
from mock import patch, call
9
from charmhelpers.contrib.charmsupport import nrpe
10
from charmhelpers.core.hookenv import Serializable
13
7
class NRPERelationTest(TestCase):
14
"""Tests for the update_nrpe_checks hook.
16
Half of this is already tested in the tests for charmsupport.nrpe, but
17
as the hook in the charm pre-dates that, the tests are left here to ensure
18
backwards-compatibility.
22
'config': {'object': nrpe},
23
'log': {'object': nrpe},
24
'getpwnam': {'object': pwd},
25
'getgrnam': {'object': grp},
26
'mkdir': {'object': os},
27
'chown': {'object': os},
28
'exists': {'object': os.path},
29
'listdir': {'object': os},
30
'remove': {'object': os},
31
'open': {'object': nrpe, 'create': True},
32
'isfile': {'object': os.path},
33
'call': {'object': subprocess},
34
'relation_ids': {'object': nrpe},
35
'relation_set': {'object': nrpe},
39
super(NRPERelationTest, self).setUp()
42
for attr, data in self.patches.items():
43
create = data.get('create', False)
44
patcher = patch.object(data['object'], attr, create=create)
45
self.patched[attr] = patcher.start()
46
self.addCleanup(patcher.stop)
47
if 'JUJU_UNIT_NAME' not in os.environ:
48
os.environ['JUJU_UNIT_NAME'] = 'test'
50
def check_call_counts(self, **kwargs):
51
for attr, expected in kwargs.items():
52
patcher = self.patched[attr]
53
self.assertEqual(expected, patcher.call_count, attr)
55
def test_update_nrpe_no_nagios_bails(self):
56
config = {'nagios_context': 'test'}
57
self.patched['config'].return_value = Serializable(config)
58
self.patched['getpwnam'].side_effect = KeyError
60
self.assertEqual(None, hooks.update_nrpe_checks())
62
expected = 'Nagios user not set up, nrpe checks not updated'
63
self.patched['log'].assert_called_once_with(expected)
64
self.check_call_counts(log=1, config=1, getpwnam=1)
66
def test_update_nrpe_removes_existing_config(self):
68
'nagios_context': 'test',
69
'nagios_check_http_params': '-u http://example.com/url',
71
self.patched['config'].return_value = Serializable(config)
72
self.patched['exists'].return_value = True
73
self.patched['listdir'].return_value = [
74
'foo', 'bar.cfg', 'check_vhost.cfg']
76
self.assertEqual(None, hooks.update_nrpe_checks())
78
expected = '/var/lib/nagios/export/check_vhost.cfg'
79
self.patched['remove'].assert_called_once_with(expected)
80
self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
81
exists=3, remove=1, open=2, listdir=1)
83
def test_update_nrpe_with_check_url(self):
85
'nagios_context': 'test',
8
"""Tests for the update_nrpe_checks hook."""
10
@patch('hooks.nrpe.NRPE')
11
def test_update_nrpe_with_check(self, mock_nrpe):
12
nrpe = mock_nrpe.return_value
86
14
'nagios_check_http_params': '-u foo -H bar',
88
self.patched['config'].return_value = Serializable(config)
89
self.patched['exists'].return_value = True
90
self.patched['isfile'].return_value = False
92
self.assertEqual(None, hooks.update_nrpe_checks())
93
self.assertEqual(2, self.patched['open'].call_count)
94
filename = 'check_vhost.cfg'
96
service_file_contents = """
97
#---------------------------------------------------
98
# This file is Juju managed
99
#---------------------------------------------------
103
service_description test-test[vhost] Check Virtual Host
104
check_command check_nrpe!check_vhost
108
self.patched['open'].assert_has_calls(
109
[call('/etc/nagios/nrpe.d/%s' % filename, 'w'),
110
call('/var/lib/nagios/export/service__test-test_%s' %
112
call().__enter__().write(service_file_contents),
113
call().__enter__().write('# check vhost\n'),
114
call().__enter__().write(
115
'command[check_vhost]=/check_http -u foo -H bar\n')],
118
self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
119
exists=3, open=2, listdir=1)
121
def test_update_nrpe_restarts_service(self):
123
'nagios_context': 'test',
124
'nagios_check_http_params': '-u foo -p 3128'
126
self.patched['config'].return_value = Serializable(config)
127
self.patched['exists'].return_value = True
129
self.assertEqual(None, hooks.update_nrpe_checks())
131
expected = ['service', 'nagios-nrpe-server', 'restart']
132
self.assertEqual(expected, self.patched['call'].call_args[0][0])
133
self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
134
exists=3, open=2, listdir=1, call=1)
16
hooks.update_nrpe_checks()
17
nrpe.add_check.assert_called_once_with(
19
description='Check Virtual Host',
20
check_cmd='check_http -u foo -H bar'
22
nrpe.write.assert_called_once_with()
24
@patch('hooks.nrpe.NRPE')
25
def test_update_nrpe_no_check(self, mock_nrpe):
26
nrpe = mock_nrpe.return_value
28
hooks.update_nrpe_checks()
29
self.assertFalse(nrpe.add_check.called)
30
nrpe.write.assert_called_once_with()