~abentley/charms/trusty/apache2/apache-website

« back to all changes in this revision

Viewing changes to hooks/tests/test_nrpe_hooks.py

  • Committer: Aaron Bentley
  • Date: 2015-04-15 20:23:01 UTC
  • mfrom: (56.4.7 apache2)
  • Revision ID: aaron.bentley@canonical.com-20150415202301-8sbei1eiol8ywp6a
Merged trunk into apache2-website-interface.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import os
2
 
import grp
3
 
import pwd
4
 
import subprocess
5
1
from testtools import TestCase
6
 
from mock import patch, call
 
2
from mock import patch
7
3
 
8
4
import hooks
9
 
from charmhelpers.contrib.charmsupport import nrpe
10
 
from charmhelpers.core.hookenv import Serializable
11
5
 
12
6
 
13
7
class NRPERelationTest(TestCase):
14
 
    """Tests for the update_nrpe_checks hook.
15
 
 
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.
19
 
 
20
 
    """
21
 
    patches = {
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},
36
 
    }
37
 
 
38
 
    def setUp(self):
39
 
        super(NRPERelationTest, self).setUp()
40
 
        self.patched = {}
41
 
        # Mock the universe.
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'
49
 
 
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)
54
 
 
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
59
 
 
60
 
        self.assertEqual(None, hooks.update_nrpe_checks())
61
 
 
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)
65
 
 
66
 
    def test_update_nrpe_removes_existing_config(self):
67
 
        config = {
68
 
            'nagios_context': 'test',
69
 
            'nagios_check_http_params': '-u http://example.com/url',
70
 
        }
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']
75
 
 
76
 
        self.assertEqual(None, hooks.update_nrpe_checks())
77
 
 
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)
82
 
 
83
 
    def test_update_nrpe_with_check_url(self):
84
 
        config = {
85
 
            'nagios_context': 'test',
 
8
    """Tests for the update_nrpe_checks hook."""
 
9
 
 
10
    @patch('hooks.nrpe.NRPE')
 
11
    def test_update_nrpe_with_check(self, mock_nrpe):
 
12
        nrpe = mock_nrpe.return_value
 
13
        nrpe.config = {
86
14
            'nagios_check_http_params': '-u foo -H bar',
87
15
        }
88
 
        self.patched['config'].return_value = Serializable(config)
89
 
        self.patched['exists'].return_value = True
90
 
        self.patched['isfile'].return_value = False
91
 
 
92
 
        self.assertEqual(None, hooks.update_nrpe_checks())
93
 
        self.assertEqual(2, self.patched['open'].call_count)
94
 
        filename = 'check_vhost.cfg'
95
 
 
96
 
        service_file_contents = """
97
 
#---------------------------------------------------
98
 
# This file is Juju managed
99
 
#---------------------------------------------------
100
 
define service {
101
 
    use                             active-service
102
 
    host_name                       test-test
103
 
    service_description             test-test[vhost] Check Virtual Host
104
 
    check_command                   check_nrpe!check_vhost
105
 
    servicegroups                   test
106
 
}
107
 
"""
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' %
111
 
                  filename, 'w'),
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')],
116
 
            any_order=True)
117
 
 
118
 
        self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
119
 
                               exists=3, open=2, listdir=1)
120
 
 
121
 
    def test_update_nrpe_restarts_service(self):
122
 
        config = {
123
 
            'nagios_context': 'test',
124
 
            'nagios_check_http_params': '-u foo -p 3128'
125
 
        }
126
 
        self.patched['config'].return_value = Serializable(config)
127
 
        self.patched['exists'].return_value = True
128
 
 
129
 
        self.assertEqual(None, hooks.update_nrpe_checks())
130
 
 
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(
 
18
            shortname='vhost',
 
19
            description='Check Virtual Host',
 
20
            check_cmd='check_http -u foo -H bar'
 
21
        )
 
22
        nrpe.write.assert_called_once_with()
 
23
 
 
24
    @patch('hooks.nrpe.NRPE')
 
25
    def test_update_nrpe_no_check(self, mock_nrpe):
 
26
        nrpe = mock_nrpe.return_value
 
27
        nrpe.config = {}
 
28
        hooks.update_nrpe_checks()
 
29
        self.assertFalse(nrpe.add_check.called)
 
30
        nrpe.write.assert_called_once_with()