python/psutil/test/_osx.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/python/psutil/test/_osx.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,156 @@
     1.4 +#!/usr/bin/env python
     1.5 +
     1.6 +# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
     1.7 +# Use of this source code is governed by a BSD-style license that can be
     1.8 +# found in the LICENSE file.
     1.9 +
    1.10 +"""OSX specific tests.  These are implicitly run by test_psutil.py."""
    1.11 +
    1.12 +import unittest
    1.13 +import subprocess
    1.14 +import time
    1.15 +import sys
    1.16 +import os
    1.17 +import re
    1.18 +
    1.19 +import psutil
    1.20 +
    1.21 +from psutil._compat import PY3
    1.22 +from test_psutil import *
    1.23 +
    1.24 +
    1.25 +PAGESIZE = os.sysconf("SC_PAGE_SIZE")
    1.26 +
    1.27 +
    1.28 +def sysctl(cmdline):
    1.29 +    """Expects a sysctl command with an argument and parse the result
    1.30 +    returning only the value of interest.
    1.31 +    """
    1.32 +    p = subprocess.Popen(cmdline, shell=1, stdout=subprocess.PIPE)
    1.33 +    result = p.communicate()[0].strip().split()[1]
    1.34 +    if PY3:
    1.35 +        result = str(result, sys.stdout.encoding)
    1.36 +    try:
    1.37 +        return int(result)
    1.38 +    except ValueError:
    1.39 +        return result
    1.40 +
    1.41 +def vm_stat(field):
    1.42 +    """Wrapper around 'vm_stat' cmdline utility."""
    1.43 +    out = sh('vm_stat')
    1.44 +    for line in out.split('\n'):
    1.45 +        if field in line:
    1.46 +            break
    1.47 +    else:
    1.48 +        raise ValueError("line not found")
    1.49 +    return int(re.search('\d+', line).group(0)) * PAGESIZE
    1.50 +
    1.51 +
    1.52 +class OSXSpecificTestCase(unittest.TestCase):
    1.53 +
    1.54 +    def setUp(self):
    1.55 +        self.pid = get_test_subprocess().pid
    1.56 +
    1.57 +    def tearDown(self):
    1.58 +        reap_children()
    1.59 +
    1.60 +    def test_process_create_time(self):
    1.61 +        cmdline = "ps -o lstart -p %s" %self.pid
    1.62 +        p = subprocess.Popen(cmdline, shell=1, stdout=subprocess.PIPE)
    1.63 +        output = p.communicate()[0]
    1.64 +        if PY3:
    1.65 +            output = str(output, sys.stdout.encoding)
    1.66 +        start_ps = output.replace('STARTED', '').strip()
    1.67 +        start_psutil = psutil.Process(self.pid).create_time
    1.68 +        start_psutil = time.strftime("%a %b %e %H:%M:%S %Y",
    1.69 +                                     time.localtime(start_psutil))
    1.70 +        self.assertEqual(start_ps, start_psutil)
    1.71 +
    1.72 +    def test_disks(self):
    1.73 +        # test psutil.disk_usage() and psutil.disk_partitions()
    1.74 +        # against "df -a"
    1.75 +        def df(path):
    1.76 +            out = sh('df -k "%s"' % path).strip()
    1.77 +            lines = out.split('\n')
    1.78 +            lines.pop(0)
    1.79 +            line = lines.pop(0)
    1.80 +            dev, total, used, free = line.split()[:4]
    1.81 +            if dev == 'none':
    1.82 +                dev = ''
    1.83 +            total = int(total) * 1024
    1.84 +            used = int(used) * 1024
    1.85 +            free = int(free) * 1024
    1.86 +            return dev, total, used, free
    1.87 +
    1.88 +        for part in psutil.disk_partitions(all=False):
    1.89 +            usage = psutil.disk_usage(part.mountpoint)
    1.90 +            dev, total, used, free = df(part.mountpoint)
    1.91 +            self.assertEqual(part.device, dev)
    1.92 +            self.assertEqual(usage.total, total)
    1.93 +            # 10 MB tollerance
    1.94 +            if abs(usage.free - free) > 10 * 1024 * 1024:
    1.95 +                self.fail("psutil=%s, df=%s" % usage.free, free)
    1.96 +            if abs(usage.used - used) > 10 * 1024 * 1024:
    1.97 +                self.fail("psutil=%s, df=%s" % usage.used, used)
    1.98 +
    1.99 +    # --- virtual mem
   1.100 +
   1.101 +    def test_vmem_total(self):
   1.102 +        sysctl_hwphymem = sysctl('sysctl hw.memsize')
   1.103 +        self.assertEqual(sysctl_hwphymem, psutil.TOTAL_PHYMEM)
   1.104 +
   1.105 +    @retry_before_failing()
   1.106 +    def test_vmem_free(self):
   1.107 +        num = vm_stat("free")
   1.108 +        self.assertAlmostEqual(psutil.virtual_memory().free, num,
   1.109 +                               delta=TOLERANCE)
   1.110 +
   1.111 +    @retry_before_failing()
   1.112 +    def test_vmem_active(self):
   1.113 +        num = vm_stat("active")
   1.114 +        self.assertAlmostEqual(psutil.virtual_memory().active, num,
   1.115 +                               delta=TOLERANCE)
   1.116 +
   1.117 +    @retry_before_failing()
   1.118 +    def test_vmem_inactive(self):
   1.119 +        num = vm_stat("inactive")
   1.120 +        self.assertAlmostEqual(psutil.virtual_memory().inactive, num,
   1.121 +                               delta=TOLERANCE)
   1.122 +
   1.123 +    @retry_before_failing()
   1.124 +    def test_vmem_wired(self):
   1.125 +        num = vm_stat("wired")
   1.126 +        self.assertAlmostEqual(psutil.virtual_memory().wired, num,
   1.127 +                               delta=TOLERANCE)
   1.128 +
   1.129 +    # --- swap mem
   1.130 +
   1.131 +    def test_swapmem_sin(self):
   1.132 +        num = vm_stat("Pageins")
   1.133 +        self.assertEqual(psutil.swap_memory().sin, num)
   1.134 +
   1.135 +    def test_swapmem_sout(self):
   1.136 +        num = vm_stat("Pageouts")
   1.137 +        self.assertEqual(psutil.swap_memory().sout, num)
   1.138 +
   1.139 +    def test_swapmem_total(self):
   1.140 +        tot1 = psutil.swap_memory().total
   1.141 +        tot2 = 0
   1.142 +        # OSX uses multiple cache files:
   1.143 +        # http://en.wikipedia.org/wiki/Paging#OS_X
   1.144 +        for name in os.listdir("/var/vm/"):
   1.145 +            file = os.path.join("/var/vm", name)
   1.146 +            if os.path.isfile(file):
   1.147 +                tot2 += os.path.getsize(file)
   1.148 +        self.assertEqual(tot1, tot2)
   1.149 +
   1.150 +
   1.151 +def test_main():
   1.152 +    test_suite = unittest.TestSuite()
   1.153 +    test_suite.addTest(unittest.makeSuite(OSXSpecificTestCase))
   1.154 +    result = unittest.TextTestRunner(verbosity=2).run(test_suite)
   1.155 +    return result.wasSuccessful()
   1.156 +
   1.157 +if __name__ == '__main__':
   1.158 +    if not test_main():
   1.159 +        sys.exit(1)

mercurial