Browse Source

[2121] Add testcases for OpenBSD implementation of SysInfo

Mukund Sivaraman 13 years ago
parent
commit
42674a0354
1 changed files with 82 additions and 0 deletions
  1. 82 0
      src/lib/python/isc/sysinfo/tests/sysinfo_test.py

+ 82 - 0
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'
@@ -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."""
@@ -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()