OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # |
| 3 # $Id: _osx.py 664 2010-10-09 16:14:34Z g.rodola $ |
| 4 # |
| 5 |
| 6 import unittest |
| 7 import subprocess |
| 8 import time |
| 9 import re |
| 10 import sys |
| 11 |
| 12 import psutil |
| 13 |
| 14 from test_psutil import reap_children, get_test_subprocess |
| 15 #from _posix import ps |
| 16 |
| 17 |
| 18 def sysctl(cmdline): |
| 19 """Expects a sysctl command with an argument and parse the result |
| 20 returning only the value of interest. |
| 21 """ |
| 22 p = subprocess.Popen(cmdline, shell=1, stdout=subprocess.PIPE) |
| 23 result = p.communicate()[0].strip().split()[1] |
| 24 if sys.version_info >= (3,): |
| 25 result = str(result, sys.stdout.encoding) |
| 26 try: |
| 27 return int(result) |
| 28 except ValueError: |
| 29 return result |
| 30 |
| 31 |
| 32 class OSXSpecificTestCase(unittest.TestCase): |
| 33 |
| 34 def setUp(self): |
| 35 self.pid = get_test_subprocess().pid |
| 36 |
| 37 def tearDown(self): |
| 38 reap_children() |
| 39 |
| 40 def test_TOTAL_PHYMEM(self): |
| 41 sysctl_hwphymem = sysctl('sysctl hw.physmem') |
| 42 self.assertEqual(sysctl_hwphymem, psutil.TOTAL_PHYMEM) |
| 43 |
| 44 def test_process_create_time(self): |
| 45 cmdline = "ps -o lstart -p %s" %self.pid |
| 46 p = subprocess.Popen(cmdline, shell=1, stdout=subprocess.PIPE) |
| 47 output = p.communicate()[0] |
| 48 if sys.version_info >= (3,): |
| 49 output = str(output, sys.stdout.encoding) |
| 50 start_ps = output.replace('STARTED', '').strip() |
| 51 start_psutil = psutil.Process(self.pid).create_time |
| 52 start_psutil = time.strftime("%a %b %e %H:%M:%S %Y", |
| 53 time.localtime(start_psutil)) |
| 54 self.assertEqual(start_ps, start_psutil) |
| 55 |
| 56 |
| 57 if __name__ == '__main__': |
| 58 test_suite = unittest.TestSuite() |
| 59 test_suite.addTest(unittest.makeSuite(OSXSpecificTestCase)) |
| 60 unittest.TextTestRunner(verbosity=2).run(test_suite) |
| 61 |
| 62 |
| 63 |
| 64 |
OLD | NEW |