sysinfo_test.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. # Copyright (C) 2012 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. from isc.sysinfo import *
  16. import os
  17. import unittest
  18. import platform
  19. import subprocess
  20. import time
  21. # different fake 'number of processors' values used for the different
  22. # operating systems
  23. NPROCESSORS_LINUX = 42
  24. NPROCESSORS_OPENBSD = 43
  25. NPROCESSORS_FREEBSD = 44
  26. NPROCESSORS_OSX = 45
  27. def _my_testcase_platform_system():
  28. return 'BIND10Testcase'
  29. def _my_linux_platform_system():
  30. return 'Linux'
  31. def _my_linux_os_sysconf(key):
  32. if key == 'SC_NPROCESSORS_CONF':
  33. return NPROCESSORS_LINUX
  34. assert False, 'Unhandled key'
  35. class MyLinuxFile:
  36. def __init__(self, filename):
  37. self._filename = filename
  38. def read(self):
  39. if self._filename == '/proc/sys/kernel/hostname':
  40. return 'myhostname'
  41. elif self._filename == '/proc/version':
  42. return 'An SMP version string'
  43. elif self._filename == '/proc/uptime':
  44. return '172800.75 139993.71'
  45. elif self._filename == '/proc/loadavg':
  46. return '0.1 0.2 0.3 0.4'
  47. else:
  48. assert False, 'Unhandled filename'
  49. def readlines(self):
  50. if self._filename == '/proc/meminfo':
  51. return ['MemTotal: 3083872 kB',
  52. 'MemFree: 870492 kB',
  53. 'Buffers: 27412 kB',
  54. 'Cached: 1303860 kB',
  55. 'SwapTotal: 4194300 kB',
  56. 'SwapFree: 3999464 kB']
  57. else:
  58. assert False, 'Unhandled filename'
  59. def close(self):
  60. return
  61. def __enter__(self):
  62. return self
  63. def __exit__(self, type, value, traceback):
  64. return
  65. def _my_linux_open(filename):
  66. return MyLinuxFile(filename)
  67. def _my_linux_subprocess_check_output(command):
  68. assert type(command) == list, 'command argument is not a list'
  69. if command == ['lsb_release', '-a']:
  70. return b'Description: My Distribution\n'
  71. elif command == ['ip', 'addr']:
  72. return b'qB2osV6vUOjqm3P/+tQ4d92xoYz8/U8P9v3KWRpNwlI=\n'
  73. elif command == ['ip', 'route']:
  74. return b'VGWAS92AlS14Pl2xqENJs5P2Ihe6Nv9g181Mu6Zz+aQ=\n'
  75. elif command == ['ip', '-f', 'inet6', 'route']:
  76. return b'XfizswwNA9NkXz6K36ZExpjV08Y5IXkHI8jjDSV+5Nc=\n'
  77. elif command == ['netstat', '-s']:
  78. return b'osuxbrcc1g9VgaF4yf3FrtfodrfATrbSnjhqhuQSAs8=\n'
  79. elif command == ['netstat', '-apn']:
  80. return b'Z+w0lwa02/T+5+EIio84rrst/Dtizoz/aL9Im7J7ESA=\n'
  81. else:
  82. assert False, 'Unhandled command'
  83. def _my_openbsd_platform_system():
  84. return 'OpenBSD'
  85. def _my_openbsd_os_sysconf(key):
  86. if key == 'SC_NPROCESSORS_CONF':
  87. return NPROCESSORS_OPENBSD
  88. assert False, 'Unhandled key'
  89. def _my_openbsd_platform_uname():
  90. return ('OpenBSD', 'test.example.com', '5.0', '', 'amd64')
  91. # For the BSD types, there is a hierarchy that mostly resembles the
  92. # class hierarchy in the sysinfo library;
  93. # These are output strings of commands that sysinfo calls
  94. #
  95. # The test hierarchy is used as follows:
  96. # Each operating system has its own _my_<OS>_subprocess_check_output
  97. # call. If the call is not found, it calls it's 'parent' (e.g.
  98. # for openbsd that is my_bsd_subprocesses_check_output).
  99. #
  100. # If that returns None, the call had no test value and the test fails
  101. # (and needs to be updated).
  102. # The child classes are checked first so that they can override
  103. # output from the parents, if necessary.
  104. #
  105. # Some parents have their own parent
  106. # (e.g. _my_freebsd_osx_subprocess_check_output), in that case,
  107. # if they do not recognize the command, they simply return whatever
  108. # their parent returns
  109. def _my_bsd_subprocess_check_output(command):
  110. '''subprocess output for all bsd types'''
  111. assert type(command) == list, 'command argument is not a list'
  112. if command == ['hostname']:
  113. return b'test.example.com\n'
  114. elif command == ['sysctl', '-n', 'hw.physmem']:
  115. return b'543214321\n'
  116. elif command == ['ifconfig']:
  117. return b'qB2osV6vUOjqm3P/+tQ4d92xoYz8/U8P9v3KWRpNwlI=\n'
  118. elif command == ['netstat', '-s']:
  119. return b'osuxbrcc1g9VgaF4yf3FrtfodrfATrbSnjhqhuQSAs8=\n'
  120. elif command == ['netstat', '-an']:
  121. return b'Z+w0lwa02/T+5+EIio84rrst/Dtizoz/aL9Im7J7ESA=\n'
  122. elif command == ['netstat', '-nr']:
  123. return b'XfizswwNA9NkXz6K36ZExpjV08Y5IXkHI8jjDSV+5Nc=\n'
  124. else:
  125. return None
  126. def _my_openbsd_subprocess_check_output(command):
  127. assert type(command) == list, 'command argument is not a list'
  128. if command == ['sysctl', '-n', 'kern.boottime']:
  129. return bytes(str(int(time.time() - 76632)), 'utf-8')
  130. elif command == ['sysctl', '-n', 'vm.loadavg']:
  131. return b'0.7 0.9 0.8\n'
  132. elif command == ['vmstat']:
  133. 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'
  134. elif command == ['swapctl', '-s', '-k']:
  135. return b'total: 553507 1K-blocks allocated, 2 used, 553505 available'
  136. else:
  137. bsd_output = _my_bsd_subprocess_check_output(command)
  138. if bsd_output is not None:
  139. return bsd_output
  140. else:
  141. assert False, 'Unhandled command'
  142. def _my_freebsd_platform_system():
  143. return 'FreeBSD'
  144. def _my_freebsd_os_sysconf(key):
  145. if key == 'SC_NPROCESSORS_CONF':
  146. return NPROCESSORS_FREEBSD
  147. assert False, 'Unhandled key'
  148. def _my_freebsd_platform_uname():
  149. return ('FreeBSD', 'freebsd', '8.2-RELEASE', '', 'i386')
  150. def _my_freebsd_osx_subprocess_check_output(command):
  151. '''subprocess output shared for freebsd and osx'''
  152. assert type(command) == list, 'command argument is not a list'
  153. if command == ['sysctl', '-n', 'kern.boottime']:
  154. return bytes('{ sec = ' + str(int(time.time() - 76632)) + ', usec = 0 }\n', 'utf-8')
  155. elif command == ['sysctl', '-n', 'vm.loadavg']:
  156. return b'{ 0.2 0.4 0.6 }\n'
  157. else:
  158. return _my_bsd_subprocess_check_output(command)
  159. def _my_freebsd_subprocess_check_output(command, faked_output):
  160. assert type(command) == list, 'command argument is not a list'
  161. if command == ['sysctl', '-n', 'kern.smp.forward_signal_enabled']:
  162. output = faked_output['smp-sysctl']
  163. if isinstance(output, Exception):
  164. raise output
  165. return faked_output['smp-sysctl']
  166. elif command == ['vmstat', '-H']:
  167. 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 343434 123456 47 0 0 0 0 0 2 0 2 80 14 0 1 99\n'
  168. elif command == ['swapctl', '-s', '-k']:
  169. return b'Total: 1013216 0\n'
  170. else:
  171. freebsd_osx_output = _my_freebsd_osx_subprocess_check_output(command)
  172. if freebsd_osx_output is not None:
  173. return freebsd_osx_output
  174. else:
  175. assert False, 'Unhandled command'
  176. def _my_osx_platform_system():
  177. return 'Darwin'
  178. def _my_osx_platform_uname():
  179. return ('Darwin', 'test.example.com', '10.6.0', '', '')
  180. def _my_osx_os_sysconf(key):
  181. if key == 'SC_NPROCESSORS_CONF':
  182. return NPROCESSORS_OSX
  183. assert False, 'Unhandled key'
  184. def _my_osx_subprocess_check_output(command):
  185. assert type(command) == list, 'command argument is not a list'
  186. if command == ['sysctl', '-n', 'hw.memsize']:
  187. # Something different than physmem from bsd
  188. return b'123456789\n'
  189. elif command == ['vm_stat']:
  190. return b'Mach Virtual Memory Statistics: (page size of 4096 bytes)\nPages free: 12345.\nPages speculative: 11111.\n'
  191. elif command == ['sysctl', '-n', 'vm.swapusage']:
  192. return b'total = 18432.00M used = 17381.23M free = 1050.77M\n'
  193. else:
  194. freebsd_osx_output = _my_freebsd_osx_subprocess_check_output(command)
  195. if freebsd_osx_output is not None:
  196. return freebsd_osx_output
  197. else:
  198. assert False, 'Unhandled command'
  199. class SysInfoTest(unittest.TestCase):
  200. def setUp(self):
  201. # Save existing implementations of library functions
  202. # (they are replaced in the tests)
  203. self.old_platform_system = platform.system
  204. self.old_os_sysconf = os.sysconf
  205. self.old_open = __builtins__.open
  206. self.old_subprocess_check_output = subprocess.check_output
  207. def tearDown(self):
  208. # Restore the library functions
  209. platform.system = self.old_platform_system
  210. os.sysconf = self.old_os_sysconf
  211. __builtins__.open = self.old_open
  212. subprocess.check_output = self.old_subprocess_check_output
  213. def test_sysinfo(self):
  214. """Test that the various methods on SysInfo exist and return data."""
  215. s = SysInfo()
  216. self.assertEqual(None, s.get_num_processors())
  217. self.assertEqual('Unknown', s.get_endianness())
  218. self.assertEqual('', s.get_platform_hostname())
  219. self.assertEqual('Unknown', s.get_platform_name())
  220. self.assertEqual('Unknown', s.get_platform_version())
  221. self.assertEqual('Unknown', s.get_platform_machine())
  222. self.assertFalse(s.get_platform_is_smp())
  223. self.assertEqual(None, s.get_uptime())
  224. self.assertEqual(None, s.get_uptime_desc())
  225. self.assertEqual(None, s.get_loadavg())
  226. self.assertEqual(None, s.get_mem_total())
  227. self.assertEqual(None, s.get_mem_free())
  228. self.assertEqual(None, s.get_mem_cached())
  229. self.assertEqual(None, s.get_mem_buffers())
  230. self.assertEqual(None, s.get_mem_swap_total())
  231. self.assertEqual(None, s.get_mem_swap_free())
  232. self.assertEqual(None, s.get_platform_distro())
  233. self.assertEqual('Unknown\n', s.get_net_interfaces())
  234. self.assertEqual('Unknown\n', s.get_net_routing_table())
  235. self.assertEqual('Unknown\n', s.get_net_stats())
  236. self.assertEqual('Unknown\n', s.get_net_connections())
  237. def test_sysinfo_factory(self):
  238. """Test that SysInfoFromFactory returns a valid system-specific
  239. SysInfo implementation.
  240. See sysinfo.SysInfoTestcase() for some ofthe parameters.
  241. """
  242. old_platform_system = platform.system
  243. platform.system = _my_testcase_platform_system
  244. s = SysInfoFromFactory()
  245. self.assertEqual(None, s.get_num_processors())
  246. self.assertEqual('bigrastafarian', s.get_endianness())
  247. self.assertEqual('', s.get_platform_hostname())
  248. self.assertEqual('b10test', s.get_platform_name())
  249. self.assertEqual('Unknown', s.get_platform_version())
  250. self.assertEqual('Unknown', s.get_platform_machine())
  251. self.assertFalse(s.get_platform_is_smp())
  252. self.assertEqual(131072, s.get_uptime())
  253. # We check we do NOT add 's' to 'day' (because it's singular):
  254. self.assertEqual('1 day, 12:24', s.get_uptime_desc())
  255. self.assertEqual(None, s.get_loadavg())
  256. self.assertEqual(None, s.get_mem_total())
  257. self.assertEqual(None, s.get_mem_free())
  258. self.assertEqual(None, s.get_mem_cached())
  259. self.assertEqual(None, s.get_mem_buffers())
  260. self.assertEqual(None, s.get_mem_swap_total())
  261. self.assertEqual(None, s.get_mem_swap_free())
  262. self.assertEqual(None, s.get_platform_distro())
  263. self.assertEqual('Unknown\n', s.get_net_interfaces())
  264. self.assertEqual('Unknown\n', s.get_net_routing_table())
  265. self.assertEqual('Unknown\n', s.get_net_stats())
  266. self.assertEqual('Unknown\n', s.get_net_connections())
  267. platform.system = old_platform_system
  268. def test_sysinfo_linux(self):
  269. """Tests the Linux implementation of SysInfo. Note that this
  270. tests deep into the implementation, and not just the
  271. interfaces."""
  272. # Replace existing implementations of library functions
  273. # with mock ones for testing.
  274. platform.system = _my_linux_platform_system
  275. os.sysconf = _my_linux_os_sysconf
  276. __builtins__.open = _my_linux_open
  277. subprocess.check_output = _my_linux_subprocess_check_output
  278. s = SysInfoFromFactory()
  279. self.assertEqual(NPROCESSORS_LINUX, s.get_num_processors())
  280. self.assertEqual('myhostname', s.get_platform_hostname())
  281. self.assertTrue(s.get_platform_is_smp())
  282. self.assertEqual(172801, s.get_uptime())
  283. # We check we add 's' to 'day', and the mm part has an additional
  284. # 0, i.e., not '0:0' but '0:00':
  285. self.assertEqual('2 days, 0:00', s.get_uptime_desc())
  286. self.assertEqual((0.1, 0.2, 0.3), s.get_loadavg())
  287. self.assertEqual(3157884928, s.get_mem_total())
  288. self.assertEqual(891383808, s.get_mem_free())
  289. self.assertEqual(1335152640, s.get_mem_cached())
  290. self.assertEqual(28069888, s.get_mem_buffers())
  291. self.assertEqual(4294963200, s.get_mem_swap_total())
  292. self.assertEqual(4095451136, s.get_mem_swap_free())
  293. self.assertEqual('My Distribution', s.get_platform_distro())
  294. # These test that the corresponding tools are being called (and
  295. # no further processing is done on this data). Please see the
  296. # implementation functions at the top of this file.
  297. self.assertEqual('qB2osV6vUOjqm3P/+tQ4d92xoYz8/U8P9v3KWRpNwlI=\n', s.get_net_interfaces())
  298. self.assertEqual('VGWAS92AlS14Pl2xqENJs5P2Ihe6Nv9g181Mu6Zz+aQ=\n\nXfizswwNA9NkXz6K36ZExpjV08Y5IXkHI8jjDSV+5Nc=\n', s.get_net_routing_table())
  299. self.assertEqual('osuxbrcc1g9VgaF4yf3FrtfodrfATrbSnjhqhuQSAs8=\n', s.get_net_stats())
  300. self.assertEqual('Z+w0lwa02/T+5+EIio84rrst/Dtizoz/aL9Im7J7ESA=\n', s.get_net_connections())
  301. def check_bsd_values(self, s):
  302. # check values shared by all bsd implementations
  303. self.assertEqual('test.example.com', s.get_platform_hostname())
  304. self.assertLess(abs(76632 - s.get_uptime()), 4)
  305. self.assertEqual(None, s.get_mem_cached())
  306. self.assertEqual(None, s.get_mem_buffers())
  307. self.assertEqual(None, s.get_platform_distro())
  308. # These test that the corresponding tools are being called (and
  309. # no further processing is done on this data). Please see the
  310. # implementation functions at the top of this file.
  311. self.assertEqual('qB2osV6vUOjqm3P/+tQ4d92xoYz8/U8P9v3KWRpNwlI=\n', s.get_net_interfaces())
  312. self.assertEqual('XfizswwNA9NkXz6K36ZExpjV08Y5IXkHI8jjDSV+5Nc=\n', s.get_net_routing_table())
  313. self.assertEqual('osuxbrcc1g9VgaF4yf3FrtfodrfATrbSnjhqhuQSAs8=\n', s.get_net_stats())
  314. self.assertEqual('Z+w0lwa02/T+5+EIio84rrst/Dtizoz/aL9Im7J7ESA=\n', s.get_net_connections())
  315. def test_sysinfo_openbsd(self):
  316. """Tests the OpenBSD implementation of SysInfo. Note that this
  317. tests deep into the implementation, and not just the
  318. interfaces."""
  319. # Replace existing implementations of library functions
  320. # with mock ones for testing.
  321. platform.system = _my_openbsd_platform_system
  322. os.sysconf = _my_openbsd_os_sysconf
  323. subprocess.check_output = _my_openbsd_subprocess_check_output
  324. os.uname = _my_openbsd_platform_uname
  325. s = SysInfoFromFactory()
  326. self.assertEqual(NPROCESSORS_OPENBSD, s.get_num_processors())
  327. self.check_bsd_values(s)
  328. self.assertEqual((0.7, 0.9, 0.8), s.get_loadavg())
  329. self.assertFalse(s.get_platform_is_smp())
  330. self.assertEqual(543214321, s.get_mem_total())
  331. self.assertEqual(543214321 - (121212 * 1024), s.get_mem_free())
  332. self.assertEqual(566791168, s.get_mem_swap_total())
  333. self.assertEqual(566789120, s.get_mem_swap_free())
  334. def test_sysinfo_freebsd(self):
  335. """Tests the FreeBSD implementation of SysInfo. Note that this
  336. tests deep into the implementation, and not just the
  337. interfaces."""
  338. # Replace existing implementations of library functions
  339. # with mock ones for testing.
  340. platform.system = _my_freebsd_platform_system
  341. os.sysconf = _my_freebsd_os_sysconf
  342. # We use a lambda object so we can tweak the subprocess output during
  343. # the tests later.
  344. faked_process_output = { 'smp-sysctl': b'1\n' }
  345. subprocess.check_output = lambda command : \
  346. _my_freebsd_subprocess_check_output(command, faked_process_output)
  347. os.uname = _my_freebsd_platform_uname
  348. s = SysInfoFromFactory()
  349. self.assertEqual(NPROCESSORS_FREEBSD, s.get_num_processors())
  350. self.assertTrue(s.get_platform_is_smp())
  351. # We check the kernel SMP support by the availability of an sysctl
  352. # variable. The value (especiall a 0 value) shouldn't matter.
  353. faked_process_output['smp-sysctl'] = b'0\n'
  354. s = SysInfoFromFactory()
  355. self.assertTrue(s.get_platform_is_smp())
  356. # if the sysctl raises CalledProcessError, we treat it as non-SMP
  357. # kernel.
  358. faked_process_output['smp-sysctl'] = \
  359. subprocess.CalledProcessError(1, 'sysctl')
  360. s = SysInfoFromFactory()
  361. self.assertFalse(s.get_platform_is_smp())
  362. # if it results in OSError, no SMP information will be provided.
  363. faked_process_output['smp-sysctl'] = OSError()
  364. s = SysInfoFromFactory()
  365. self.assertIsNone(s.get_platform_is_smp())
  366. self.check_bsd_values(s)
  367. self.assertEqual((0.2, 0.4, 0.6), s.get_loadavg())
  368. self.assertEqual(543214321, s.get_mem_total())
  369. self.assertEqual(543214321 - (343434 * 1024), s.get_mem_free())
  370. self.assertEqual(1037533184, s.get_mem_swap_total())
  371. self.assertEqual(1037533184, s.get_mem_swap_free())
  372. def test_sysinfo_osx(self):
  373. """Tests the OS X implementation of SysInfo. Note that this
  374. tests deep into the implementation, and not just the
  375. interfaces."""
  376. # Replace existing implementations of library functions
  377. # with mock ones for testing.
  378. platform.system = _my_osx_platform_system
  379. os.sysconf = _my_osx_os_sysconf
  380. subprocess.check_output = _my_osx_subprocess_check_output
  381. os.uname = _my_osx_platform_uname
  382. s = SysInfoFromFactory()
  383. self.assertEqual(NPROCESSORS_OSX, s.get_num_processors())
  384. self.assertFalse(s.get_platform_is_smp())
  385. self.check_bsd_values(s)
  386. self.assertEqual((0.2, 0.4, 0.6), s.get_loadavg())
  387. self.assertEqual(123456789, s.get_mem_total())
  388. self.assertEqual((23456 * 4096), s.get_mem_free())
  389. self.assertEqual(18874368.0, s.get_mem_swap_total())
  390. self.assertEqual(1075988.48, s.get_mem_swap_free())
  391. if __name__ == "__main__":
  392. unittest.main()