diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2015-11-25 03:24:48 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2015-11-25 03:24:48 +0100 |
commit | 22c4200f377130eeb53b9ba077fe2323fcf035b6 (patch) | |
tree | 71c9509dbe9de3a4943ca65441e8ebb62bbd24d7 | |
parent | a5f9fbca765e5e1bdffa2a973d95e8210c0848e3 (diff) | |
download | psutil-22c4200f377130eeb53b9ba077fe2323fcf035b6.tar.gz |
add openbsd specific tests
-rw-r--r-- | test/_openbsd.py | 105 | ||||
-rw-r--r-- | test/test_psutil.py | 2 |
2 files changed, 107 insertions, 0 deletions
diff --git a/test/_openbsd.py b/test/_openbsd.py new file mode 100644 index 00000000..b50b66a6 --- /dev/null +++ b/test/_openbsd.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python + +# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""OpenBSD specific tests. These are implicitly run by test_psutil.py.""" + +import datetime +import subprocess +import sys +import time + +import psutil +from psutil._compat import PY3 +from test_psutil import get_test_subprocess +from test_psutil import OPENBSD +from test_psutil import reap_children +from test_psutil import sh +from test_psutil import unittest + + +def sysctl(cmdline): + """Expects a sysctl command with an argument and parse the result + returning only the value of interest. + """ + result = sh("sysctl " + cmdline) + result = result[result.find("=") + 1:] + try: + return int(result) + except ValueError: + return result + + +@unittest.skipUnless(OPENBSD, "not an OpenBSD system") +class OpenBSDSpecificTestCase(unittest.TestCase): + + @classmethod + def setUpClass(cls): + cls.pid = get_test_subprocess().pid + + @classmethod + def tearDownClass(cls): + reap_children() + + def test_boot_time(self): + s = sysctl('kern.boottime') + sys_bt = datetime.datetime.strptime(s, "%a %b %d %H:%M:%S %Y") + psutil_bt = datetime.datetime.fromtimestamp(psutil.boot_time()) + self.assertEqual(sys_bt, psutil_bt) + + def test_process_create_time(self): + cmdline = "ps -o lstart -p %s" % self.pid + p = subprocess.Popen(cmdline, shell=1, stdout=subprocess.PIPE) + output = p.communicate()[0] + if PY3: + output = str(output, sys.stdout.encoding) + start_ps = output.replace('STARTED', '').strip() + start_psutil = psutil.Process(self.pid).create_time() + start_psutil = time.strftime("%a %b %e %H:%M:%S %Y", + time.localtime(start_psutil)) + self.assertEqual(start_ps, start_psutil) + + def test_disks(self): + # test psutil.disk_usage() and psutil.disk_partitions() + # against "df -a" + def df(path): + out = sh('df -k "%s"' % path).strip() + lines = out.split('\n') + lines.pop(0) + line = lines.pop(0) + dev, total, used, free = line.split()[:4] + if dev == 'none': + dev = '' + total = int(total) * 1024 + used = int(used) * 1024 + free = int(free) * 1024 + return dev, total, used, free + + for part in psutil.disk_partitions(all=False): + usage = psutil.disk_usage(part.mountpoint) + dev, total, used, free = df(part.mountpoint) + self.assertEqual(part.device, dev) + self.assertEqual(usage.total, total) + # 10 MB tollerance + if abs(usage.free - free) > 10 * 1024 * 1024: + self.fail("psutil=%s, df=%s" % (usage.free, free)) + if abs(usage.used - used) > 10 * 1024 * 1024: + self.fail("psutil=%s, df=%s" % (usage.used, used)) + + def test_cpu_count_logical(self): + syst = sysctl("hw.ncpu") + self.assertEqual(psutil.cpu_count(logical=True), syst) + + +def main(): + test_suite = unittest.TestSuite() + test_suite.addTest(unittest.makeSuite(OpenBSDSpecificTestCase)) + result = unittest.TextTestRunner(verbosity=2).run(test_suite) + return result.wasSuccessful() + + +if __name__ == '__main__': + if not main(): + sys.exit(1) diff --git a/test/test_psutil.py b/test/test_psutil.py index 9ca4d175..d2a85713 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -3228,6 +3228,8 @@ def main(): from _osx import OSXSpecificTestCase as stc elif FREEBSD: from _freebsd import FreeBSDSpecificTestCase as stc + elif OPENBSD: + from _openbsd import OpenBSDSpecificTestCase as stc elif SUNOS: from _sunos import SunOSSpecificTestCase as stc if stc is not None: |