← Back to team overview

nagios-charmers team mailing list archive

Re: [Merge] ~aluria/hw-health-charm/+git/hw-health-charm:unittests-engrot6 into hw-health-charm:master

 


Diff comments:

> diff --git a/src/tests/unit/test_check_mdadm.py b/src/tests/unit/test_check_mdadm.py
> new file mode 100644
> index 0000000..e9d36bf
> --- /dev/null
> +++ b/src/tests/unit/test_check_mdadm.py
> @@ -0,0 +1,119 @@
> +import mock
> +import unittest
> +import io
> +import os
> +import sys
> +
> +sys.path.append('files')
> +import check_mdadm
> +
> +
> +class TestCheckMegaCLI(unittest.TestCase):
> +    @mock.patch('os.path.exists')
> +    @mock.patch('subprocess.Popen')
> +    def test_get_devices_mdadm_exists(self, mdadm_details, path_exists):
> +        class Test_Popen(object):
> +            def __init__(cls):
> +                data = b'ARRAY /dev/md0 metadata=1.2 name=node00:0'
> +                data += b' UUID=dfaf7413:7551b000:56dd7442:5b020adb\n'
> +                data += b'ARRAY /dev/md1 metadata=1.2 name=node01:0'
> +                data += b' UUID=dfaf7413:7551b000:56dd7442:5b020adc\n'

Actually a better option would be to use implicit line joining here:

data = (
   b'...'
   b'...'
   b'...'
)

> +                cls.stdout = io.BufferedReader(io.BytesIO(data))
> +
> +        mdadm_details.return_value = Test_Popen()
> +        path_exists.return_value = True
> +
> +        real = check_mdadm.get_devices()
> +        expected = set(['/dev/md0', '/dev/md1'])
> +        self.assertEqual(real, expected)
> +
> +    @mock.patch('os.path.exists')
> +    def test_get_devices_mdadm_notfound(self, path_exists):
> +        path_exists.return_value = False
> +        real = check_mdadm.get_devices()
> +        expected = set()
> +        self.assertEqual(real, expected)
> +
> +    @mock.patch('os.path.exists')
> +    @mock.patch('subprocess.Popen')
> +    def test_get_devices_mdadm_exception(self, mdadm_details, path_exists):
> +        path_exists.return_value = True
> +        mdadm_details.side_effect = Exception
> +        real = check_mdadm.get_devices()
> +        expected = set()
> +        self.assertEqual(real, expected)
> +
> +    @mock.patch('check_mdadm.get_devices')
> +    @mock.patch('subprocess.Popen')
> +    def test_parse_output_ok(self, mdadm_details, devices):
> +        class Test_Popen(object):
> +            def __init__(cls):
> +                test_output = os.path.join(os.getcwd(),
> +                                           'tests',
> +                                           'hw-health-samples',
> +                                           'mdadm.output')
> +                cls.stdout = io.FileIO(test_output)
> +                cls.wait = lambda: 0
> +
> +        devices.return_value = set(['/dev/md0', '/dev/md1', '/dev/md2'])
> +        mdadm_details.return_value = Test_Popen()
> +        real = check_mdadm.parse_output()
> +        expected = ('OK: /dev/md0 ok; /dev/md1 ok; /dev/md3 ok; /dev/md2 ok',
> +                    0)
> +        self.assertEqual(real, expected)
> +
> +    @mock.patch('check_mdadm.get_devices')
> +    @mock.patch('subprocess.Popen')
> +    def test_parse_output_wait_warning(self, mdadm_details, devices):
> +        class Test_Popen(object):
> +            def __init__(cls):
> +                test_output = os.path.join(os.getcwd(),
> +                                           'tests',
> +                                           'hw-health-samples',
> +                                           'mdadm.output')
> +                cls.stdout = io.FileIO(test_output)
> +                cls.wait = lambda: 2
> +
> +        devices.return_value = set(['/dev/md0', '/dev/md1', '/dev/md2'])
> +        mdadm_details.return_value = Test_Popen()
> +        real = check_mdadm.parse_output()
> +        expected = ('WARNING: mdadm returned exit status 2',
> +                    1)
> +        self.assertEqual(real, expected)
> +
> +    @mock.patch('check_mdadm.get_devices')
> +    def test_parse_output_nodevices(self, devices):
> +        devices.return_value = set()
> +        real = check_mdadm.parse_output()
> +        expected = ('WARNING: unexpectedly checked no devices', 1)
> +        self.assertEqual(real, expected)
> +
> +    @mock.patch('check_mdadm.get_devices')
> +    @mock.patch('subprocess.Popen')
> +    def test_parse_output_mdadm_warning(self, mdadm_details, devices):
> +        devices.return_value = set(['/dev/md0', '/dev/md1', '/dev/md2'])
> +        mdadm_details.side_effect = Exception('ugly error')
> +        real = check_mdadm.parse_output()
> +        expected = ('WARNING: error executing mdadm: ugly error', 1)
> +        self.assertEqual(real, expected)
> +
> +    @mock.patch('check_mdadm.get_devices')
> +    @mock.patch('subprocess.Popen')
> +    def test_parse_output_degraded(self, mdadm_details, devices):
> +        class Test_Popen(object):
> +            def __init__(cls):
> +                test_output = os.path.join(os.getcwd(),
> +                                           'tests',
> +                                           'hw-health-samples',
> +                                           'mdadm.output.critical')
> +                cls.stdout = io.FileIO(test_output)
> +                cls.wait = lambda: 0
> +
> +        devices.return_value = set(['/dev/md0', '/dev/md1', '/dev/md2'])
> +        mdadm_details.return_value = Test_Popen()
> +        real = check_mdadm.parse_output()
> +        expected = ('CRITICAL: /dev/md0 ok; /dev/md1 degraded, Active[1],'
> +                    ' Failed[1], Spare[0], Working[1]; /dev/md3 ok;'
> +                    ' /dev/md2 ok',
> +                    2)
> +        self.assertEqual(real, expected)


-- 
https://code.launchpad.net/~aluria/hw-health-charm/+git/hw-health-charm/+merge/361504
Your team Nagios Charm developers is requested to review the proposed merge of ~aluria/hw-health-charm/+git/hw-health-charm:unittests-engrot6 into hw-health-charm:master.


References