Beispiel #1
0
    def test_run_smartctl_with_failure(self):
        output = factory.make_string().encode('utf-8')
        storage = factory.make_name('storage')
        cmd = ['sudo', '-n', 'smartctl', '--xall', storage]
        self.patch(smartctl, "check_SMART_support")
        self.patch(smartctl, "run_smartctl_selftest")
        mock_print = self.patch(smartctl, "print")
        mock_popen = self.patch(smartctl, "Popen")
        mock_popen.return_value = Popen('echo -n %s; exit 1' %
                                        output.decode('utf-8'),
                                        stdout=PIPE,
                                        shell=True)

        self.assertEquals(1, smartctl.run_smartctl(storage))
        self.assertThat(
            mock_print,
            MockCallsMatch(
                call('INFO: Verifying and/or validating SMART tests...'),
                call('INFO: Running command: %s\n' % ' '.join(cmd)),
                call('FAILURE: SMART tests have FAILED for: %s' % storage),
                call('The test exited with return code %d! See the smarctl '
                     'manpage for information on the return code meaning. For '
                     'more information on the test failures, review the test '
                     'output provided below.' % 1),
                call('---------------------------------------------------\n'),
                call(output.decode('utf-8'))))
Beispiel #2
0
 def test__default(self):
     self.assertEquals(self.output,
                       smartctl.run_smartctl(self.blockdevice, self.args))
     self.assertThat(
         self.mock_check_output,
         MockCalledOnceWith(['sudo', '-n', 'smartctl'] + self.args +
                            [self.blockdevice],
                            timeout=smartctl.TIMEOUT))
     self.assertThat(self.mock_print, MockNotCalled())
Beispiel #3
0
 def test__device(self):
     device = factory.make_name('device')
     self.assertEquals(
         self.output,
         smartctl.run_smartctl(self.blockdevice, self.args, device=device))
     self.assertThat(
         self.mock_check_output,
         MockCalledOnceWith(['sudo', '-n', 'smartctl', '-d', device] +
                            self.args + [self.blockdevice],
                            timeout=smartctl.TIMEOUT))
     self.assertThat(self.mock_print, MockNotCalled())
Beispiel #4
0
 def test_default(self):
     self.assertEquals(self.output,
                       smartctl.run_smartctl(self.blockdevice, self.args))
     self.assertThat(
         self.mock_check_output,
         MockCalledOnceWith(
             ["sudo", "-n", "smartctl"] + self.args + [self.blockdevice],
             timeout=smartctl.TIMEOUT,
         ),
     )
     self.assertThat(self.mock_print, MockNotCalled())
Beispiel #5
0
 def test_device(self):
     device = factory.make_name("device")
     self.assertEquals(
         self.output,
         smartctl.run_smartctl(self.blockdevice, self.args, device=device),
     )
     self.assertThat(
         self.mock_check_output,
         MockCalledOnceWith(
             ["sudo", "-n", "smartctl", "-d", device] + self.args +
             [self.blockdevice],
             timeout=smartctl.TIMEOUT,
         ),
     )
     self.assertThat(self.mock_print, MockNotCalled())
Beispiel #6
0
    def test_run_smartctl(self):
        self.patch(smartctl, 'list_supported_drives').return_value = [
            ['/dev/sda', '-d', 'sat'],
        ]
        output = factory.make_string()
        mock_popen = self.patch(smartctl, 'Popen')
        mock_popen.return_value = Popen(['echo', '-n', output], stdout=PIPE)
        mock_print = self.patch(smartctl, 'print')

        self.assertEquals(0, smartctl.run_smartctl())

        dashes = '-' * int((80.0 - (2 + len('/dev/sda'))) / 2)
        header = '%s /dev/sda %s' % (dashes, dashes)
        self.assertThat(mock_print,
                        MockCallsMatch(call(header), call(), call(output)))
Beispiel #7
0
    def test_run_smartctl_timedout(self):
        smartctl.TIMEOUT = 1
        storage = factory.make_name('storage')
        cmd = ['sudo', '-n', 'smartctl', '--xall', storage]
        self.patch(smartctl, "check_SMART_support")
        self.patch(smartctl, "run_smartctl_selftest")
        mock_print = self.patch(smartctl, "print")
        mock_popen = self.patch(smartctl, "Popen")
        mock_popen.return_value = Popen(['sleep', '60'], stdout=PIPE)

        self.assertEquals(1, smartctl.run_smartctl(storage))
        self.assertThat(
            mock_print,
            MockCallsMatch(
                call('INFO: Verifying and/or validating SMART tests...'),
                call('INFO: Running command: %s\n' % ' '.join(cmd)),
                call('INFO: Running `smartctl --xall %s` timed out!' %
                     storage)))
Beispiel #8
0
    def test_run_smartctl_timedout(self):
        self.patch(smartctl, 'list_supported_drives').return_value = [
            ['/dev/sda', '-d', 'sat'],
        ]
        smartctl.TIMEOUT = 1
        mock_popen = self.patch(smartctl, 'Popen')
        mock_popen.return_value = Popen(['sleep', '60'], stdout=PIPE)
        mock_print = self.patch(smartctl, 'print')

        self.assertEquals(1, smartctl.run_smartctl())

        dashes = '-' * int((80.0 - (2 + len('/dev/sda'))) / 2)
        header = '%s /dev/sda %s' % (dashes, dashes)
        self.assertThat(
            mock_print,
            MockCallsMatch(
                call(header), call(),
                call('Running `smartctl --xall /dev/sda -d sat` timed out!')))
Beispiel #9
0
    def test_run_smartctl_with_success(self):
        output = factory.make_string().encode('utf-8')
        storage = factory.make_name('storage')
        cmd = ['sudo', '-n', 'smartctl', '--xall', storage]
        self.patch(smartctl, "check_SMART_support")
        self.patch(smartctl, "run_smartctl_selftest")
        mock_print = self.patch(smartctl, "print")
        mock_popen = self.patch(smartctl, "Popen")
        mock_popen.return_value = Popen(['echo', '-n', output], stdout=PIPE)

        self.assertEquals(0, smartctl.run_smartctl(storage))
        self.assertThat(
            mock_print,
            MockCallsMatch(
                call('INFO: Verifying and/or validating SMART tests...'),
                call('INFO: Running command: %s\n' % ' '.join(cmd)),
                call('SUCCESS: SMART tests have PASSED for: %s\n' % storage),
                call('---------------------------------------------------\n'),
                call(output.decode('utf-8'))))
Beispiel #10
0
    def test_run_smartctl_failure(self):
        self.patch(smartctl, 'list_supported_drives').return_value = [
            ['/dev/sda', '-d', 'sat'],
        ]
        output = factory.make_string()
        mock_popen = self.patch(smartctl, 'Popen')
        mock_popen.return_value = Popen('echo -n %s; exit 1' % output,
                                        stdout=PIPE,
                                        shell=True)
        mock_print = self.patch(smartctl, 'print')

        self.assertEquals(1, smartctl.run_smartctl())

        dashes = '-' * int((80.0 - (2 + len('/dev/sda'))) / 2)
        header = '%s /dev/sda %s' % (dashes, dashes)
        self.assertThat(
            mock_print,
            MockCallsMatch(
                call(header), call(),
                call('Error, `smartctl --xall /dev/sda -d sat` returned 1!'),
                call('See the smartctl man page for return code meaning'),
                call(), call(output)))
Beispiel #11
0
    def test_run_smartctl_with_smartctl_failure(self):
        self.patch(smartctl, 'list_supported_drives').return_value = [
            ['/dev/sda', '-d', 'sat'],
        ]
        output = factory.make_string()
        self.patch(smartctl, 'check_call').side_effect = CalledProcessError(
            1, 'smartctl'),
        mock_popen = self.patch(smartctl, 'Popen')
        mock_popen.return_value = Popen(['echo', '-n', output], stdout=PIPE)
        mock_print = self.patch(smartctl, 'print')
        test = factory.make_name('test')

        self.assertEquals(1, smartctl.run_smartctl(test))

        dashes = '-' * int((80.0 - (2 + len('/dev/sda'))) / 2)
        header = '%s /dev/sda %s' % (dashes, dashes)
        self.assertThat(
            mock_print,
            MockCallsMatch(
                call(header), call(),
                call('Failed to start and wait for smartctl self-test: %s' %
                     test), call(), call(output)))