~bloodearnest/charms/precise/squid-reverseproxy/trunk

« back to all changes in this revision

Viewing changes to hooks/tests/test_nrpe_hooks.py

  • Committer: Marco Ceppi
  • Date: 2013-11-07 01:40:09 UTC
  • mfrom: (28.1.85 master)
  • Revision ID: marco@ceppi.net-20131107014009-lqqg63wkyt6ot2ou
[sidnei] Greatly improve test coverage
[sidnei] Allow the use of an X-Balancer-Name header to select which cache_peer backend will be used for a specific request.
[sidnei] Support 'all-services' being set in the relation, in the way that the haproxy sets it, in addition to the previously supported 'sitenames' setting. Makes it compatible with the haproxy charm.
[sidnei] When the list of supported 'sitenames' (computed from dstdomain acls) changes, notify services related via the 'cached-website' relation. This allows to add new services in the haproxy service (or really, any other service related), which notifies the squid service, which then bubbles up to services related via cached-website.

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
 
 
6
from testtools import TestCase
 
7
from mock import patch, call
 
8
 
 
9
import hooks
 
10
 
 
11
from charmhelpers.contrib.charmsupport import nrpe
 
12
from charmhelpers.core.hookenv import Serializable
 
13
 
 
14
 
 
15
class NRPERelationTest(TestCase):
 
16
    """Tests for the update_nrpe_checks hook.
 
17
 
 
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.
 
21
 
 
22
    """
 
23
    patches = {
 
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},
 
39
    }
 
40
 
 
41
    def setUp(self):
 
42
        super(NRPERelationTest, self).setUp()
 
43
        self.patched = {}
 
44
        # Mock the universe.
 
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'
 
52
 
 
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)
 
57
 
 
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
 
62
 
 
63
        self.assertEqual(None, hooks.update_nrpe_checks())
 
64
 
 
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)
 
68
 
 
69
    def test_update_nrpe_removes_existing_config(self):
 
70
        config = {
 
71
            'nagios_context': 'test',
 
72
            'nagios_check_http_params': '-u http://example.com/url',
 
73
        }
 
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']
 
78
 
 
79
        self.assertEqual(None, hooks.update_nrpe_checks())
 
80
 
 
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)
 
85
 
 
86
    def test_update_nrpe_uses_check_squidpeers(self):
 
87
        config = {
 
88
            'nagios_context': 'test',
 
89
        }
 
90
        self.patched['config'].return_value = Serializable(config)
 
91
        self.patched['exists'].return_value = True
 
92
        self.patched['isfile'].return_value = False
 
93
 
 
94
        self.assertEqual(None, hooks.update_nrpe_checks())
 
95
        self.assertEqual(2, self.patched['open'].call_count)
 
96
        filename = 'check_squidpeers.cfg'
 
97
 
 
98
        service_file_contents = """
 
99
#---------------------------------------------------
 
100
# This file is Juju managed
 
101
#---------------------------------------------------
 
102
define service {
 
103
    use                             active-service
 
104
    host_name                       test-test
 
105
    service_description             test-test[squidpeers] Check Squid Peers
 
106
    check_command                   check_nrpe!check_squidpeers
 
107
    servicegroups                   test
 
108
}
 
109
"""
 
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' %
 
113
                  filename, 'w'),
 
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')],
 
119
            any_order=True)
 
120
 
 
121
        self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
 
122
                               exists=3, open=2, listdir=1)
 
123
 
 
124
    def test_update_nrpe_with_check_url(self):
 
125
        config = {
 
126
            'nagios_context': 'test',
 
127
            'nagios_check_http_params': '-u foo -H bar',
 
128
        }
 
129
        self.patched['config'].return_value = Serializable(config)
 
130
        self.patched['exists'].return_value = True
 
131
        self.patched['isfile'].return_value = False
 
132
 
 
133
        self.assertEqual(None, hooks.update_nrpe_checks())
 
134
        self.assertEqual(4, self.patched['open'].call_count)
 
135
        filename = 'check_squidrp.cfg'
 
136
 
 
137
        service_file_contents = """
 
138
#---------------------------------------------------
 
139
# This file is Juju managed
 
140
#---------------------------------------------------
 
141
define service {
 
142
    use                             active-service
 
143
    host_name                       test-test
 
144
    service_description             test-test[squidrp] Check Squid
 
145
    check_command                   check_nrpe!check_squidrp
 
146
    servicegroups                   test
 
147
}
 
148
"""
 
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' %
 
152
                  filename, 'w'),
 
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')],
 
157
            any_order=True)
 
158
 
 
159
        self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
 
160
                               exists=5, open=4, listdir=2)
 
161
 
 
162
    def test_update_nrpe_with_no_check_path(self):
 
163
        config = {
 
164
            'nagios_context': 'test',
 
165
            'services': '- {service_name: i_ytimg_com}',
 
166
        }
 
167
        self.patched['config'].return_value = Serializable(config)
 
168
        self.patched['exists'].return_value = True
 
169
 
 
170
        self.assertEqual(None, hooks.update_nrpe_checks())
 
171
 
 
172
        self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
 
173
                               exists=3, open=2, listdir=1)
 
174
 
 
175
    def test_update_nrpe_with_services_and_host_header(self):
 
176
        config = {
 
177
            'nagios_context': 'test',
 
178
            'services': '- {service_name: i_ytimg_com, nrpe_check_path: /}',
 
179
        }
 
180
        self.patched['config'].return_value = Serializable(config)
 
181
        self.patched['exists'].return_value = True
 
182
 
 
183
        self.assertEqual(None, hooks.update_nrpe_checks())
 
184
 
 
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)],
 
193
            any_order=True)
 
194
 
 
195
    def test_update_nrpe_with_dotted_service_name_and_host_header(self):
 
196
        config = {
 
197
            'nagios_context': 'test',
 
198
            'services': '- {service_name: i.ytimg.com, nrpe_check_path: /}',
 
199
        }
 
200
        self.patched['config'].return_value = Serializable(config)
 
201
        self.patched['exists'].return_value = True
 
202
 
 
203
        self.assertEqual(None, hooks.update_nrpe_checks())
 
204
 
 
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)],
 
213
            any_order=True)
 
214
 
 
215
    def test_update_nrpe_with_services_and_balancer_name_header(self):
 
216
        config = {
 
217
            'nagios_context': 'test',
 
218
            'x_balancer_name_allowed': True,
 
219
            'services': '- {service_name: i_ytimg_com, nrpe_check_path: /}',
 
220
        }
 
221
        self.patched['config'].return_value = Serializable(config)
 
222
        self.patched['exists'].return_value = True
 
223
 
 
224
        self.assertEqual(None, hooks.update_nrpe_checks())
 
225
 
 
226
        self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
 
227
                               exists=5, open=4, listdir=2)
 
228
 
 
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)],
 
235
            any_order=True)
 
236
 
 
237
    def test_update_nrpe_with_services_and_optional_path(self):
 
238
        services = '- {nrpe_check_path: /foo.jpg, service_name: foo_com}\n'
 
239
        config = {
 
240
            'nagios_context': 'test',
 
241
            'services': services,
 
242
        }
 
243
        self.patched['config'].return_value = Serializable(config)
 
244
        self.patched['exists'].return_value = True
 
245
 
 
246
        self.assertEqual(None, hooks.update_nrpe_checks())
 
247
 
 
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)],
 
256
            any_order=True)
 
257
 
 
258
    def test_update_nrpe_restarts_service(self):
 
259
        config = {
 
260
            'nagios_context': 'test',
 
261
            'nagios_check_http_params': '-u foo -p 3128'
 
262
        }
 
263
        self.patched['config'].return_value = Serializable(config)
 
264
        self.patched['exists'].return_value = True
 
265
 
 
266
        self.assertEqual(None, hooks.update_nrpe_checks())
 
267
 
 
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)