~joeborg/charm-helpers/charm-helpers

« back to all changes in this revision

Viewing changes to tests/contrib/charmsupport/test_nrpe.py

  • Committer: Stuart Bishop
  • Date: 2017-03-09 07:22:08 UTC
  • mfrom: (708.1.3 lp1670223)
  • Revision ID: stuart.bishop@canonical.com-20170309072208-eo8ey9160fyjs37a
[freyes,r=stub] Verify that the nagios user exists before attempting to run a check

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
import subprocess
4
4
 
5
5
from testtools import TestCase
6
 
from mock import patch, call
 
6
from mock import patch, call, MagicMock
7
7
 
8
8
from charmhelpers.contrib.charmsupport import nrpe
9
9
from charmhelpers.core import host
295
295
        self.patched['relations_of_type'].return_value = []
296
296
        self.assertEqual(nrpe.get_nagios_unit_name(), 'testunit')
297
297
 
298
 
    def test_add_init_service_checks(self):
 
298
    @patch.object(os.path, 'isdir')
 
299
    def test_add_init_service_checks(self, mock_isdir):
299
300
        def _exists(init_file):
300
301
            files = ['/etc/init/apache2.conf',
301
302
                     '/usr/lib/nagios/plugins/check_upstart_job',
309
310
 
310
311
        self.patched['exists'].side_effect = _exists
311
312
 
312
 
        # Test without systemd
 
313
        # Test without systemd and /var/lib/nagios does not exist
313
314
        self.patched['init_is_systemd'].return_value = False
 
315
        mock_isdir.return_value = False
314
316
        bill = nrpe.NRPE()
315
317
        services = ['apache2', 'haproxy']
316
318
        nrpe.add_init_service_checks(bill, services, 'testunit')
 
319
        mock_isdir.assert_called_with('/var/lib/nagios')
 
320
        self.patched['call'].assert_not_called()
317
321
        expect_cmds = {
318
322
            'apache2': '/usr/lib/nagios/plugins/check_upstart_job apache2',
319
323
            'haproxy': '/usr/lib/nagios/plugins/check_status_file.py -f '
324
328
        self.assertEqual(bill.checks[1].shortname, 'haproxy')
325
329
        self.assertEqual(bill.checks[1].check_cmd, expect_cmds['haproxy'])
326
330
 
 
331
        # without systemd and /var/lib/nagios does exist
 
332
        mock_isdir.return_value = True
 
333
        f = MagicMock()
 
334
        self.patched['open'].return_value = f
 
335
        bill = nrpe.NRPE()
 
336
        services = ['apache2', 'haproxy']
 
337
        nrpe.add_init_service_checks(bill, services, 'testunit')
 
338
        mock_isdir.assert_called_with('/var/lib/nagios')
 
339
        self.patched['call'].assert_called_with(
 
340
            ['/usr/local/lib/nagios/plugins/check_exit_status.pl', '-s',
 
341
             '/etc/init.d/haproxy', 'status'], stdout=f,
 
342
            stderr=subprocess.STDOUT)
 
343
 
327
344
        # Test with systemd
328
345
        self.patched['init_is_systemd'].return_value = True
329
346
        nrpe.add_init_service_checks(bill, services, 'testunit')