From 2fa891ab836add35df4d0317f6c5f16a3fef11f5 Mon Sep 17 00:00:00 2001 From: desbma Date: Thu, 29 Jan 2015 13:20:36 +0100 Subject: [PATCH] Improve Drive class test coverage - 2 --- hddfancontrol/__init__.py | 2 ++ tests/__init__.py | 59 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/hddfancontrol/__init__.py b/hddfancontrol/__init__.py index 807e6e3..e84e5c2 100755 --- a/hddfancontrol/__init__.py +++ b/hddfancontrol/__init__.py @@ -145,6 +145,8 @@ class Drive: stats = stat_file.read() stats = filter(None, map(str.strip, stats.strip().split(" "))) stats = tuple(map(int, stats)) + if not stats: + raise RuntimeError("Unable to get stats for drive %s" % (self)) return stats @staticmethod diff --git a/tests/__init__.py b/tests/__init__.py index b08008b..4ca17b3 100755 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -5,6 +5,7 @@ import logging import os import socketserver import subprocess +import tempfile import threading import unittest import unittest.mock @@ -88,6 +89,45 @@ class TestDrive(unittest.TestCase): stderr=subprocess.DEVNULL, universal_newlines=True) + def test_isSleeping(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" + self.assertFalse(self.drive.isSleeping()) + subprocess_check_output_mock.assert_called_once_with(("hdparm", "-C", "/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:\n drive state is: standby\n" + self.assertTrue(self.drive.isSleeping()) + subprocess_check_output_mock.assert_called_once_with(("hdparm", "-C", "/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:\n drive state is: sleeping\n" + self.assertTrue(self.drive.isSleeping()) + subprocess_check_output_mock.assert_called_once_with(("hdparm", "-C", "/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.side_effect = subprocess.CalledProcessError(0, "") + with self.assertRaises(Exception): + self.drive.isSleeping() + subprocess_check_output_mock.assert_called_once_with(("hdparm", "-C", "/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 = "/dev/sdz: No such file or directory\n" + with self.assertRaises(Exception): + self.drive.isSleeping() + subprocess_check_output_mock.assert_called_once_with(("hdparm", "-C", "/dev/sdz"), + stdin=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + universal_newlines=True) + def test_getTemperature(self): # # Temperature querying can be done in 3 different way: @@ -173,6 +213,25 @@ class TestDrive(unittest.TestCase): with self.assertRaises(Exception): self.drive.getTemperature() + def test_spinDown(self): + with unittest.mock.patch("hddfancontrol.subprocess.check_call") as subprocess_check_call_mock: + self.drive.spinDown() + subprocess_check_call_mock.assert_called_once_with(("hdparm", "-y", "/dev/sdz"), + stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) + + def test_getActivityStats(self): + with self.assertRaises(Exception): + self.drive.getActivityStats() + with tempfile.NamedTemporaryFile("wt") as stat_file: + self.drive.stat_filepath = stat_file.name + with self.assertRaises(Exception): + self.drive.getActivityStats() + stat_file.write(" 21695 7718 2913268 95136 13986 754 932032 55820 0 19032 150940\n") + stat_file.flush() + self.assertEqual(self.drive.getActivityStats(), (21695, 7718, 2913268, 95136, 13986, 754, 932032, 55820, 0, 19032, 150940)) + if __name__ == "__main__": # disable logging