Improve hdparm error handling (should fix #6)

This commit is contained in:
desbma
2017-02-24 22:05:54 +01:00
parent 3238a097fd
commit 6e13ecd613
2 changed files with 46 additions and 8 deletions
+32 -2
View File
@@ -36,11 +36,11 @@ class TestDrive(unittest.TestCase):
def setUp(self):
with unittest.mock.patch("hddfancontrol.os.stat") as os_stat_mock, \
unittest.mock.patch("hddfancontrol.stat") as stat_mock, \
unittest.mock.patch("hddfancontrol.subprocess") as subprocess_check_call_mock, \
unittest.mock.patch("hddfancontrol.subprocess.check_output") as subprocess_check_output_mock, \
unittest.mock.patch("hddfancontrol.Drive.getPrettyName") as drive_getPrettyName:
os_stat_mock.return_value = os.stat_result
stat_mock.stat.S_IFBLK.return_value = True
subprocess_check_call_mock.side_effect = subprocess.CalledProcessError(0, "")
subprocess_check_output_mock.side_effect = subprocess.CalledProcessError(0, "")
drive_getPrettyName.return_value = "drive_name"
self.drive = hddfancontrol.Drive("/dev/sdz", None)
self.hddtemp_daemon = None
@@ -60,6 +60,36 @@ class TestDrive(unittest.TestCase):
stderr=subprocess.DEVNULL,
universal_newlines=True)
def test_supportsHitachiTempQuery(self):
with unittest.mock.patch("hddfancontrol.subprocess.check_output") as subprocess_check_output_mock:
subprocess_check_output_mock.return_value = "\n/dev/sdz:\n drive temperature (celsius) is: 30\n drive temperature in range: yes"
self.assertEqual(self.drive.supportsHitachiTempQuery(), True)
subprocess_check_output_mock.assert_called_once_with(("hdparm", "-H", "/dev/sdz"),
stdin=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
universal_newlines=True)
with unittest.mock.patch("hddfancontrol.subprocess.check_output") as subprocess_check_output_mock:
subprocess_check_output_mock.return_value = "\n/dev/sdz:\nSG_IO: questionable sense data, results may be incorrect\n drive temperature (celsius) is: -18\n drive temperature in range: yes"
self.assertEqual(self.drive.supportsHitachiTempQuery(), False)
subprocess_check_output_mock.assert_called_once_with(("hdparm", "-H", "/dev/sdz"),
stdin=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
universal_newlines=True)
with unittest.mock.patch("hddfancontrol.subprocess.check_output") as subprocess_check_output_mock:
subprocess_check_output_mock.return_value = "\n/dev/sdz:\nSG_IO: missing sense data, results may be incorrect\n drive temperature (celsius) is: -18\n drive temperature in range: yes"
self.assertEqual(self.drive.supportsHitachiTempQuery(), False)
subprocess_check_output_mock.assert_called_once_with(("hdparm", "-H", "/dev/sdz"),
stdin=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
universal_newlines=True)
with unittest.mock.patch("hddfancontrol.subprocess.check_output") as subprocess_check_output_mock:
subprocess_check_output_mock.return_value = "\n/dev/sdz:\nSG_IO: bad/missing sense data, sb[]: 70 00 05 00 00 00 00 0a 04 51 40 00 21 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00\n drive temperature (celsius) is: -18\n drive temperature in range: yes"
self.assertEqual(self.drive.supportsHitachiTempQuery(), False)
subprocess_check_output_mock.assert_called_once_with(("hdparm", "-H", "/dev/sdz"),
stdin=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
universal_newlines=True)
def test_getState(self):
with unittest.mock.patch("hddfancontrol.subprocess.check_output") as subprocess_check_output_mock:
subprocess_check_output_mock.return_value = "\n/dev/sdz:\n drive state is: active/idle\n"