BIND 10 master, updated. af3a7eee9a18263f41e0ea8b5f6e0e3e49c11714 Merge branch 'master' into trac2121
BIND 10 source code commits
bind10-changes at lists.isc.org
Sun Jul 15 18:36:24 UTC 2012
The branch, master has been updated
via af3a7eee9a18263f41e0ea8b5f6e0e3e49c11714 (commit)
via a9e21b463c8c2e7301e21e05787b25406d53d067 (commit)
via 5255dd6b43a8469d9614ff86adb38dee3b8d630b (commit)
via b1507181e9176e068e58dc863395870f562c5c36 (commit)
via 42674a0354067c011d2c8915ff71d8ccf9a15340 (commit)
via 90792ad4ddb26af360ca65a690020d07c21d2d20 (commit)
via d4748711299a76785c434dacbab8e8445cd3b4da (commit)
from 49ad6346f574d00cfbd1d12905915fd0dd6a0bac (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit af3a7eee9a18263f41e0ea8b5f6e0e3e49c11714
Merge: a9e21b4 49ad634
Author: Mukund Sivaraman <muks at isc.org>
Date: Sun Jul 15 23:59:08 2012 +0530
Merge branch 'master' into trac2121
-----------------------------------------------------------------------
Summary of changes:
src/lib/python/isc/sysinfo/sysinfo.py | 142 +++++++++++++++++-----
src/lib/python/isc/sysinfo/tests/sysinfo_test.py | 102 ++++++++++++++--
2 files changed, 204 insertions(+), 40 deletions(-)
-----------------------------------------------------------------------
diff --git a/src/lib/python/isc/sysinfo/sysinfo.py b/src/lib/python/isc/sysinfo/sysinfo.py
index efba0f6..a484749 100644
--- a/src/lib/python/isc/sysinfo/sysinfo.py
+++ b/src/lib/python/isc/sysinfo/sysinfo.py
@@ -21,6 +21,7 @@ import re
import subprocess
import os.path
import platform
+import time
class SysInfo:
def __init__(self):
@@ -40,10 +41,10 @@ class SysInfo:
self._mem_swap_total = -1
self._mem_swap_free = -1
self._platform_distro = 'Unknown'
- self._net_interfaces = 'Unknown'
- self._net_routing_table = 'Unknown'
- self._net_stats = 'Unknown'
- self._net_connections = 'Unknown'
+ self._net_interfaces = 'Unknown\n'
+ self._net_routing_table = 'Unknown\n'
+ self._net_stats = 'Unknown\n'
+ self._net_connections = 'Unknown\n'
def get_num_processors(self):
"""Returns the number of processors. This is the number of
@@ -127,9 +128,9 @@ class SysInfo:
"""Returns network connection information (as a multi-line string)."""
return self._net_connections
-class SysInfoLinux(SysInfo):
- """Linux implementation of the SysInfo class.
- See the base class documentation for more information.
+class SysInfoPOSIX(SysInfo):
+ """Common POSIX implementation of the SysInfo class.
+ See the SysInfo class documentation for more information.
"""
def __init__(self):
super().__init__()
@@ -137,14 +138,21 @@ class SysInfoLinux(SysInfo):
self._num_processors = os.sysconf('SC_NPROCESSORS_CONF')
self._endianness = sys.byteorder
- with open('/proc/sys/kernel/hostname') as f:
- self._hostname = f.read().strip()
-
u = os.uname()
self._platform_name = u[0]
self._platform_version = u[2]
self._platform_machine = u[4]
+class SysInfoLinux(SysInfoPOSIX):
+ """Linux implementation of the SysInfo class.
+ See the SysInfo class documentation for more information.
+ """
+ def __init__(self):
+ super().__init__()
+
+ with open('/proc/sys/kernel/hostname') as f:
+ self._hostname = f.read().strip()
+
with open('/proc/version') as f:
self._platform_is_smp = ' SMP ' in f.read().strip()
@@ -222,18 +230,11 @@ class SysInfoLinux(SysInfo):
if self._platform_distro is None:
self._platform_distro = 'Unknown'
- self._net_interfaces = None
-
try:
s = subprocess.check_output(['ip', 'addr'])
self._net_interfaces = s.decode('utf-8')
except (subprocess.CalledProcessError, OSError):
- pass
-
- if self._net_interfaces is None:
- self._net_interfaces = 'Unknown'
-
- self._net_routing_table = None
+ self._net_interfaces = 'Warning: "ip addr" command failed.\n'
try:
s = subprocess.check_output(['ip', 'route'])
@@ -242,32 +243,111 @@ class SysInfoLinux(SysInfo):
s = subprocess.check_output(['ip', '-f', 'inet6', 'route'])
self._net_routing_table += s.decode('utf-8')
except (subprocess.CalledProcessError, OSError):
+ self._net_routing_table = 'Warning: "ip route" or "ip -f inet6 route" command failed.\n'
+
+ try:
+ s = subprocess.check_output(['netstat', '-s'])
+ self._net_stats = s.decode('utf-8')
+ except (subprocess.CalledProcessError, OSError):
+ self._net_stats = 'Warning: "netstat -s" command failed.\n'
+
+ try:
+ s = subprocess.check_output(['netstat', '-apn'])
+ self._net_connections = s.decode('utf-8')
+ except (subprocess.CalledProcessError, OSError):
+ self._net_connections = 'Warning: "netstat -apn" command failed.\n'
+
+class SysInfoBSD(SysInfoPOSIX):
+ """Common BSD implementation of the SysInfo class.
+ See the SysInfo class documentation for more information.
+ """
+ def __init__(self):
+ super().__init__()
+
+ try:
+ s = subprocess.check_output(['hostname'])
+ self._hostname = s.decode('utf-8').strip()
+ except (subprocess.CalledProcessError, OSError):
+ pass
+
+ try:
+ s = subprocess.check_output(['sysctl', '-n', 'kern.boottime'])
+ t = s.decode('utf-8').strip()
+ sec = time.time() - int(t)
+ self._uptime = int(round(sec))
+ except (subprocess.CalledProcessError, OSError):
pass
- if self._net_routing_table is None:
- self._net_routing_table = 'Unknown'
+ try:
+ s = subprocess.check_output(['sysctl', '-n', 'vm.loadavg'])
+ l = s.decode('utf-8').strip().split(' ')
+ if len(l) >= 3:
+ self._loadavg = [float(l[0]), float(l[1]), float(l[2])]
+ except (subprocess.CalledProcessError, OSError):
+ pass
- self._net_stats = None
+ try:
+ s = subprocess.check_output(['sysctl', '-n', 'hw.physmem'])
+ self._mem_total = int(s.decode('utf-8').strip())
+ except (subprocess.CalledProcessError, OSError):
+ pass
try:
- s = subprocess.check_output(['netstat', '-s'])
- self._net_stats = s.decode('utf-8')
+ s = subprocess.check_output(['vmstat'])
+ lines = s.decode('utf-8').split('\n')
+ v = re.split('\s+', lines[2])
+ used = int(v[4]) * 1024
+ self._mem_free = self._mem_total - used
except (subprocess.CalledProcessError, OSError):
pass
- if self._net_stats is None:
- self._net_stats = 'Unknown'
+ self._platform_distro = self._platform_name + ' ' + self._platform_version
- self._net_connections = None
+class SysInfoOpenBSD(SysInfoBSD):
+ """OpenBSD implementation of the SysInfo class.
+ See the SysInfo class documentation for more information.
+ """
+ def __init__(self):
+ super().__init__()
+
+ # Don't know how to gather these
+ self._platform_is_smp = False
+ self._mem_cached = -1
+ self._mem_buffers = -1
try:
- s = subprocess.check_output(['netstat', '-apn'])
- self._net_connections = s.decode('utf-8')
+ s = subprocess.check_output(['swapctl', '-s', '-k'])
+ l = s.decode('utf-8').strip()
+ r = re.match('^total: (\d+) 1K-blocks allocated, (\d+) used, (\d+) available', l)
+ if r:
+ self._mem_swap_total = int(r.group(1).strip()) * 1024
+ self._mem_swap_free = int(r.group(3).strip()) * 1024
except (subprocess.CalledProcessError, OSError):
pass
- if self._net_connections is None:
- self._net_connections = 'Unknown'
+ try:
+ s = subprocess.check_output(['ifconfig'])
+ self._net_interfaces = s.decode('utf-8')
+ except (subprocess.CalledProcessError, OSError):
+ self._net_interfaces = 'Warning: "ifconfig" command failed.\n'
+
+ try:
+ s = subprocess.check_output(['route', '-n', 'show'])
+ self._net_routing_table = s.decode('utf-8')
+ except (subprocess.CalledProcessError, OSError):
+ self._net_routing_table = 'Warning: "route -n show" command failed.\n'
+
+ try:
+ s = subprocess.check_output(['netstat', '-s'])
+ self._net_stats = s.decode('utf-8')
+ except (subprocess.CalledProcessError, OSError):
+ self._net_stats = 'Warning: "netstat -s" command failed.\n'
+
+ try:
+ s = subprocess.check_output(['netstat', '-an'])
+ self._net_connections = s.decode('utf-8')
+ except (subprocess.CalledProcessError, OSError):
+ self._net_connections = 'Warning: "netstat -an" command failed.\n'
class SysInfoTestcase(SysInfo):
def __init__(self):
@@ -280,6 +360,8 @@ def SysInfoFromFactory():
osname = platform.system()
if osname == 'Linux':
return SysInfoLinux()
+ elif osname == 'OpenBSD':
+ return SysInfoOpenBSD()
elif osname == 'BIND10Testcase':
return SysInfoTestcase()
else:
diff --git a/src/lib/python/isc/sysinfo/tests/sysinfo_test.py b/src/lib/python/isc/sysinfo/tests/sysinfo_test.py
index 93985c7..0cbf655 100644
--- a/src/lib/python/isc/sysinfo/tests/sysinfo_test.py
+++ b/src/lib/python/isc/sysinfo/tests/sysinfo_test.py
@@ -18,6 +18,7 @@ import os
import unittest
import platform
import subprocess
+import time
def _my_testcase_platform_system():
return 'BIND10Testcase'
@@ -30,7 +31,7 @@ def _my_linux_os_sysconf(key):
return 42
assert False, 'Unhandled key'
-class MyFile:
+class MyLinuxFile:
def __init__(self, filename):
self._filename = filename
@@ -67,7 +68,7 @@ class MyFile:
return
def _my_linux_open(filename):
- return MyFile(filename)
+ return MyLinuxFile(filename)
def _my_linux_subprocess_check_output(command):
assert type(command) == list, 'command argument is not a list'
@@ -86,6 +87,39 @@ def _my_linux_subprocess_check_output(command):
else:
assert False, 'Unhandled command'
+def _my_openbsd_platform_system():
+ return 'OpenBSD'
+
+def _my_openbsd_os_sysconf(key):
+ if key == 'SC_NPROCESSORS_CONF':
+ return 53
+ assert False, 'Unhandled key'
+
+def _my_openbsd_subprocess_check_output(command):
+ assert type(command) == list, 'command argument is not a list'
+ if command == ['hostname']:
+ return b'blowfish.example.com\n'
+ elif command == ['sysctl', '-n', 'kern.boottime']:
+ return bytes(str(int(time.time() - 76632)), 'utf-8')
+ elif command == ['sysctl', '-n', 'vm.loadavg']:
+ return b'0.7 0.9 0.8'
+ elif command == ['sysctl', '-n', 'hw.physmem']:
+ return b'543214321'
+ elif command == ['vmstat']:
+ return b' procs memory page disks traps cpu\n r b w avm fre flt re pi po fr sr wd0 cd0 int sys cs us sy id\n 0 0 0 121212 123456 47 0 0 0 0 0 2 0 2 80 14 0 1 99\n'
+ elif command == ['swapctl', '-s', '-k']:
+ return b'total: 553507 1K-blocks allocated, 2 used, 553505 available'
+ elif command == ['ifconfig']:
+ return b'qB2osV6vUOjqm3P/+tQ4d92xoYz8/U8P9v3KWRpNwlI=\n'
+ elif command == ['route', '-n', 'show']:
+ return b'XfizswwNA9NkXz6K36ZExpjV08Y5IXkHI8jjDSV+5Nc=\n'
+ elif command == ['netstat', '-s']:
+ return b'osuxbrcc1g9VgaF4yf3FrtfodrfATrbSnjhqhuQSAs8=\n'
+ elif command == ['netstat', '-an']:
+ return b'Z+w0lwa02/T+5+EIio84rrst/Dtizoz/aL9Im7J7ESA=\n'
+ else:
+ assert False, 'Unhandled command'
+
class SysInfoTest(unittest.TestCase):
def test_sysinfo(self):
"""Test that the various methods on SysInfo exist and return data."""
@@ -107,10 +141,10 @@ class SysInfoTest(unittest.TestCase):
self.assertEqual(-1, s.get_mem_swap_total())
self.assertEqual(-1, s.get_mem_swap_free())
self.assertEqual('Unknown', s.get_platform_distro())
- self.assertEqual('Unknown', s.get_net_interfaces())
- self.assertEqual('Unknown', s.get_net_routing_table())
- self.assertEqual('Unknown', s.get_net_stats())
- self.assertEqual('Unknown', s.get_net_connections())
+ self.assertEqual('Unknown\n', s.get_net_interfaces())
+ self.assertEqual('Unknown\n', s.get_net_routing_table())
+ self.assertEqual('Unknown\n', s.get_net_stats())
+ self.assertEqual('Unknown\n', s.get_net_connections())
def test_sysinfo_factory(self):
"""Test that SysInfoFromFactory returns a valid system-specific
@@ -136,10 +170,10 @@ class SysInfoTest(unittest.TestCase):
self.assertEqual(-1, s.get_mem_swap_total())
self.assertEqual(-1, s.get_mem_swap_free())
self.assertEqual('Unknown', s.get_platform_distro())
- self.assertEqual('Unknown', s.get_net_interfaces())
- self.assertEqual('Unknown', s.get_net_routing_table())
- self.assertEqual('Unknown', s.get_net_stats())
- self.assertEqual('Unknown', s.get_net_connections())
+ self.assertEqual('Unknown\n', s.get_net_interfaces())
+ self.assertEqual('Unknown\n', s.get_net_routing_table())
+ self.assertEqual('Unknown\n', s.get_net_stats())
+ self.assertEqual('Unknown\n', s.get_net_connections())
platform.system = old_platform_system
@@ -193,5 +227,53 @@ class SysInfoTest(unittest.TestCase):
__builtins__.open = old_open
subprocess.check_output = old_subprocess_check_output
+ def test_sysinfo_openbsd(self):
+ """Tests the OpenBSD implementation of SysInfo. Note that this
+ tests deep into the implementation, and not just the
+ interfaces."""
+
+ # Don't run this test on platform other than Openbsd as some
+ # system calls may not even be available.
+ osname = platform.system()
+ if osname != 'OpenBSD':
+ return
+
+ # Save and replace existing implementations of library functions
+ # with mock ones for testing.
+ old_platform_system = platform.system
+ platform.system = _my_openbsd_platform_system
+ old_os_sysconf = os.sysconf
+ os.sysconf = _my_openbsd_os_sysconf
+ old_subprocess_check_output = subprocess.check_output
+ subprocess.check_output = _my_openbsd_subprocess_check_output
+
+ s = SysInfoFromFactory()
+ self.assertEqual(53, s.get_num_processors())
+ self.assertEqual('blowfish.example.com', s.get_platform_hostname())
+ self.assertFalse(s.get_platform_is_smp())
+
+ self.assertLess(abs(76632 - s.get_uptime()), 4)
+ self.assertEqual([0.7, 0.9, 0.8], s.get_loadavg())
+ self.assertEqual(543214321, s.get_mem_total())
+ self.assertEqual(543214321 - (121212 * 1024), s.get_mem_free())
+ self.assertEqual(-1, s.get_mem_cached())
+ self.assertEqual(-1, s.get_mem_buffers())
+ self.assertEqual(566791168, s.get_mem_swap_total())
+ self.assertEqual(566789120, s.get_mem_swap_free())
+ self.assertRegexpMatches(s.get_platform_distro(), '^OpenBSD\s+.*')
+
+ # These test that the corresponding tools are being called (and
+ # no further processing is done on this data). Please see the
+ # implementation functions at the top of this file.
+ self.assertEqual('qB2osV6vUOjqm3P/+tQ4d92xoYz8/U8P9v3KWRpNwlI=\n', s.get_net_interfaces())
+ self.assertEqual('XfizswwNA9NkXz6K36ZExpjV08Y5IXkHI8jjDSV+5Nc=\n', s.get_net_routing_table())
+ self.assertEqual('osuxbrcc1g9VgaF4yf3FrtfodrfATrbSnjhqhuQSAs8=\n', s.get_net_stats())
+ self.assertEqual('Z+w0lwa02/T+5+EIio84rrst/Dtizoz/aL9Im7J7ESA=\n', s.get_net_connections())
+
+ # Restore original implementations.
+ platform.system = old_platform_system
+ os.sysconf = old_os_sysconf
+ subprocess.check_output = old_subprocess_check_output
+
if __name__ == "__main__":
unittest.main()
More information about the bind10-changes
mailing list