Commit e26eb608 authored by Mukund Sivaraman's avatar Mukund Sivaraman
Browse files

[2062] Add testcases for SysInfo Linux implementation

parent 304e2b97
......@@ -150,11 +150,13 @@ class SysInfoLinux(SysInfo):
with open('/proc/uptime') as f:
u = f.read().strip().split(' ')
self._uptime = int(round(float(u[0])))
if len(u) > 1:
self._uptime = int(round(float(u[0])))
with open('/proc/loadavg') as f:
l = f.read().strip().split(' ')
self._loadavg = [float(l[0]), float(l[1]), float(l[2])]
if len(l) >= 3:
self._loadavg = [float(l[0]), float(l[1]), float(l[2])]
with open('/proc/meminfo') as f:
m = f.readlines()
......
......@@ -14,12 +14,78 @@
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from isc.sysinfo import *
import os
import unittest
import platform
import subprocess
def _my_system():
def _my_testcase_platform_system():
return 'BIND10Testcase'
def _my_linux_platform_system():
return 'Linux'
def _my_linux_os_sysconf(key):
if key == 'SC_NPROCESSORS_CONF':
return 42
assert False, 'Unhandled key'
class MyFile:
def __init__(self, filename):
self._filename = filename
def read(self):
if self._filename == '/proc/sys/kernel/hostname':
return 'myhostname'
elif self._filename == '/proc/version':
return 'An SMP version string'
elif self._filename == '/proc/uptime':
return '86400.75 139993.71'
elif self._filename == '/proc/loadavg':
return '0.1 0.2 0.3 0.4'
else:
assert False, 'Unhandled filename'
def readlines(self):
if self._filename == '/proc/meminfo':
return ['MemTotal: 3083872 kB',
'MemFree: 870492 kB',
'Buffers: 27412 kB',
'Cached: 1303860 kB',
'SwapTotal: 4194300 kB',
'SwapFree: 3999464 kB']
else:
assert False, 'Unhandled filename'
def close(self):
return
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
return
def _my_linux_open(filename):
return MyFile(filename)
def _my_linux_subprocess_check_output(command):
assert type(command) == list, 'command argument is not a list'
if command == ['lsb_release', '-a']:
return b'Description: My Distribution\n'
elif command == ['ip', 'addr']:
return b'qB2osV6vUOjqm3P/+tQ4d92xoYz8/U8P9v3KWRpNwlI=\n'
elif command == ['ip', 'route']:
return b'VGWAS92AlS14Pl2xqENJs5P2Ihe6Nv9g181Mu6Zz+aQ=\n'
elif command == ['ip', '-f', 'inet6', 'route']:
return b'XfizswwNA9NkXz6K36ZExpjV08Y5IXkHI8jjDSV+5Nc=\n'
elif command == ['netstat', '-s']:
return b'osuxbrcc1g9VgaF4yf3FrtfodrfATrbSnjhqhuQSAs8=\n'
elif command == ['netstat', '-apn']:
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."""
......@@ -50,8 +116,8 @@ class SysInfoTest(unittest.TestCase):
"""Test that SysInfoFromFactory returns a valid system-specific
SysInfo implementation."""
old_system = platform.system
platform.system = _my_system
old_platform_system = platform.system
platform.system = _my_testcase_platform_system
s = SysInfoFromFactory()
self.assertEqual(-1, s.get_num_processors())
......@@ -75,7 +141,50 @@ class SysInfoTest(unittest.TestCase):
self.assertEqual('Unknown', s.get_net_stats())
self.assertEqual('Unknown', s.get_net_connections())
platform.system = old_system
platform.system = old_platform_system
def test_sysinfo_linux(self):
"""Tests the Linux 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 Linux as some
# system calls may not even be available.
osname = platform.system()
if osname != 'Linux':
return
old_platform_system = platform.system
platform.system = _my_linux_platform_system
old_os_sysconf = os.sysconf
os.sysconf = _my_linux_os_sysconf
old_open = __builtins__.open
__builtins__.open = _my_linux_open
old_subprocess_check_output = subprocess.check_output
subprocess.check_output = _my_linux_subprocess_check_output
s = SysInfoFromFactory()
self.assertEqual(42, s.get_num_processors())
self.assertEqual('myhostname', s.get_platform_hostname())
self.assertTrue(s.get_platform_is_smp())
self.assertEqual(86401, s.get_uptime())
self.assertEqual([0.1, 0.2, 0.3], s.get_loadavg())
self.assertEqual(3157884928, s.get_mem_total())
self.assertEqual(891383808, s.get_mem_free())
self.assertEqual(1335152640, s.get_mem_cached())
self.assertEqual(28069888, s.get_mem_buffers())
self.assertEqual(4294963200, s.get_mem_swap_total())
self.assertEqual(4095451136, s.get_mem_swap_free())
self.assertEqual('My Distribution', s.get_platform_distro())
self.assertEqual('qB2osV6vUOjqm3P/+tQ4d92xoYz8/U8P9v3KWRpNwlI=\n', s.get_net_interfaces())
self.assertEqual('VGWAS92AlS14Pl2xqENJs5P2Ihe6Nv9g181Mu6Zz+aQ=\n\nXfizswwNA9NkXz6K36ZExpjV08Y5IXkHI8jjDSV+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())
platform.system = old_platform_system
os.sysconf = old_os_sysconf
__builtins__.open = old_open
subprocess.check_output = old_subprocess_check_output
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment