created unittest for file_helpers
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 12 Jan 2016 12:27:18 +0000 (13:27 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 12 Jan 2016 12:27:18 +0000 (13:27 +0100)
test/file_helper_unittest.py [new file with mode: 0644]

diff --git a/test/file_helper_unittest.py b/test/file_helper_unittest.py
new file mode 100644 (file)
index 0000000..db672f0
--- /dev/null
@@ -0,0 +1,73 @@
+""" file_helper_unittest.py: unit tests for file_helpers
+
+Tests classes and functions in file_helpers
+
+Should be able to run from python2 and python3!
+
+For help see :py:mod:`unittest`
+
+.. codeauthor:: Christian Herdtweck, christian.herdtweck@intra2net.com
+"""
+
+import unittest
+
+import file_helpers
+
+from call_helpers import call_and_capture
+import os
+
+
+class FileHelperTester(unittest.TestCase):
+
+    def test_cd(self):
+        """ tests the cd context manager """
+
+        test_dir = '/'
+
+        start_cwd = os.getcwd()
+
+        with file_helpers.cd(test_dir):
+            self.assertEqual(os.getcwd(), test_dir)
+        self.assertEqual(os.getcwd(), start_cwd)
+
+
+    def test_disc_stats(self):
+        """ tests get_disc_stats """
+
+        stats = file_helpers.get_disc_stats()
+
+        # check number
+        code, out, err = call_and_capture(file_helpers.DF_CMD)
+        self.assertEqual(code, 0)
+        self.assertEqual(len(err), 0)
+        self.assertEqual(len(out)-1, len(stats))
+
+        for stat in stats:
+            # do numbers make sense?
+            self.assertGreaterEqual(stat.size, 0)
+            self.assertGreaterEqual(stat.used, 0)
+            self.assertLessEqual(stat.used, stat.size)
+            self.assertGreaterEqual(stat.available, 0)
+            self.assertLessEqual(stat.available, stat.size)
+            self.assertGreaterEqual(stat.capacity, 0)
+            self.assertLessEqual(stat.capacity, 100)
+
+            # are strings non-empty
+            self.assertGreater(len(stat.name), 0)
+            self.assertGreater(len(stat.mount_point), 0)
+
+            # does match capacity?
+            capacity = 100. * stat.used / stat.size
+            self.assertLess(abs(capacity - stat.capacity), 5.,
+                            'capacity deviates from used/size by >5%!')
+
+            # is size approx equal to used + available?
+            size = stat.used + stat.available
+            self.assertLess(float(abs(stat.size - size))
+                            / float(max(stat.size, size)),
+                            0.10,
+                            'size deviates from used+free by more than 10%!')
+
+
+if __name__ == '__main__':
+    unittest.main()