sysinfo_test.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 '86400.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. else:
  123. return None
  124. def _my_openbsd_subprocess_check_output(command):
  125. assert type(command) == list, 'command argument is not a list'
  126. bsd_output = _my_bsd_subprocess_check_output(command)
  127. if command == ['sysctl', '-n', 'kern.boottime']:
  128. return bytes(str(int(time.time() - 76632)), 'utf-8')
  129. elif command == ['sysctl', '-n', 'vm.loadavg']:
  130. return b'0.7 0.9 0.8\n'
  131. elif command == ['vmstat']:
  132. 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'
  133. elif command == ['swapctl', '-s', '-k']:
  134. return b'total: 553507 1K-blocks allocated, 2 used, 553505 available'
  135. elif command == ['route', '-n', 'show']:
  136. return b'XfizswwNA9NkXz6K36ZExpjV08Y5IXkHI8jjDSV+5Nc=\n'
  137. else:
  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. elif command == ['netstat', '-nr']:
  158. return b'XfizswwNA9NkXz6K36ZExpjV08Y5IXkHI8jjDSV+5Nc=\n'
  159. else:
  160. return _my_bsd_subprocess_check_output(command)
  161. def _my_freebsd_subprocess_check_output(command):
  162. assert type(command) == list, 'command argument is not a list'
  163. if command == ['sysctl', '-n', 'kern.smp.active']:
  164. return b'1\n'
  165. elif command == ['vmstat', '-H']:
  166. 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'
  167. elif command == ['swapctl', '-s', '-k']:
  168. return b'Total: 1013216 0\n'
  169. else:
  170. freebsd_osx_output = _my_freebsd_osx_subprocess_check_output(command)
  171. if freebsd_osx_output is not None:
  172. return freebsd_osx_output
  173. else:
  174. assert False, 'Unhandled command'
  175. def _my_osx_platform_system():
  176. return 'Darwin'
  177. def _my_osx_platform_uname():
  178. return ('Darwin', 'test.example.com', '10.6.0', '', '')
  179. def _my_osx_os_sysconf(key):
  180. if key == 'SC_NPROCESSORS_CONF':
  181. return NPROCESSORS_OSX
  182. assert False, 'Unhandled key'
  183. def _my_osx_subprocess_check_output(command):
  184. assert type(command) == list, 'command argument is not a list'
  185. if command == ['sysctl', '-n', 'hw.memsize']:
  186. # Something different than physmem from bsd
  187. return b'123456789\n'
  188. elif command == ['vm_stat']:
  189. return b'Mach Virtual Memory Statistics: (page size of 4096 bytes)\nPages free: 12345.\nPages speculative: 11111.\n'
  190. elif command == ['sysctl', '-n', 'vm.swapusage']:
  191. return b'total = 18432.00M used = 17381.23M free = 1050.77M\n'
  192. else:
  193. freebsd_osx_output = _my_freebsd_osx_subprocess_check_output(command)
  194. if freebsd_osx_output is not None:
  195. return freebsd_osx_output
  196. else:
  197. assert False, 'Unhandled command'
  198. class SysInfoTest(unittest.TestCase):
  199. def setUp(self):
  200. # Save existing implementations of library functions
  201. # (they are replaced in the tests)
  202. self.old_platform_system = platform.system
  203. self.old_os_sysconf = os.sysconf
  204. self.old_open = __builtins__.open
  205. self.old_subprocess_check_output = subprocess.check_output
  206. def tearDown(self):
  207. # Restore the library functions
  208. platform.system = self.old_platform_system
  209. os.sysconf = self.old_os_sysconf
  210. __builtins__.open = self.old_open
  211. subprocess.check_output = self.old_subprocess_check_output
  212. def test_sysinfo(self):
  213. """Test that the various methods on SysInfo exist and return data."""
  214. s = SysInfo()
  215. self.assertEqual(None, s.get_num_processors())
  216. self.assertEqual('Unknown', s.get_endianness())
  217. self.assertEqual('', s.get_platform_hostname())
  218. self.assertEqual('Unknown', s.get_platform_name())
  219. self.assertEqual('Unknown', s.get_platform_version())
  220. self.assertEqual('Unknown', s.get_platform_machine())
  221. self.assertFalse(s.get_platform_is_smp())
  222. self.assertEqual(None, s.get_uptime())
  223. self.assertEqual(None, s.get_loadavg())
  224. self.assertEqual(None, s.get_mem_total())
  225. self.assertEqual(None, s.get_mem_free())
  226. self.assertEqual(None, s.get_mem_cached())
  227. self.assertEqual(None, s.get_mem_buffers())
  228. self.assertEqual(None, s.get_mem_swap_total())
  229. self.assertEqual(None, s.get_mem_swap_free())
  230. self.assertEqual('Unknown', s.get_platform_distro())
  231. self.assertEqual('Unknown\n', s.get_net_interfaces())
  232. self.assertEqual('Unknown\n', s.get_net_routing_table())
  233. self.assertEqual('Unknown\n', s.get_net_stats())
  234. self.assertEqual('Unknown\n', s.get_net_connections())
  235. def test_sysinfo_factory(self):
  236. """Test that SysInfoFromFactory returns a valid system-specific
  237. SysInfo implementation."""
  238. old_platform_system = platform.system
  239. platform.system = _my_testcase_platform_system
  240. s = SysInfoFromFactory()
  241. self.assertEqual(None, s.get_num_processors())
  242. self.assertEqual('bigrastafarian', s.get_endianness())
  243. self.assertEqual('', s.get_platform_hostname())
  244. self.assertEqual('b10test', s.get_platform_name())
  245. self.assertEqual('Unknown', s.get_platform_version())
  246. self.assertEqual('Unknown', s.get_platform_machine())
  247. self.assertFalse(s.get_platform_is_smp())
  248. self.assertEqual(131072, s.get_uptime())
  249. self.assertEqual(None, s.get_loadavg())
  250. self.assertEqual(None, s.get_mem_total())
  251. self.assertEqual(None, s.get_mem_free())
  252. self.assertEqual(None, s.get_mem_cached())
  253. self.assertEqual(None, s.get_mem_buffers())
  254. self.assertEqual(None, s.get_mem_swap_total())
  255. self.assertEqual(None, s.get_mem_swap_free())
  256. self.assertEqual('Unknown', s.get_platform_distro())
  257. self.assertEqual('Unknown\n', s.get_net_interfaces())
  258. self.assertEqual('Unknown\n', s.get_net_routing_table())
  259. self.assertEqual('Unknown\n', s.get_net_stats())
  260. self.assertEqual('Unknown\n', s.get_net_connections())
  261. platform.system = old_platform_system
  262. def test_sysinfo_linux(self):
  263. """Tests the Linux implementation of SysInfo. Note that this
  264. tests deep into the implementation, and not just the
  265. interfaces."""
  266. # Replace existing implementations of library functions
  267. # with mock ones for testing.
  268. platform.system = _my_linux_platform_system
  269. os.sysconf = _my_linux_os_sysconf
  270. __builtins__.open = _my_linux_open
  271. subprocess.check_output = _my_linux_subprocess_check_output
  272. s = SysInfoFromFactory()
  273. self.assertEqual(NPROCESSORS_LINUX, s.get_num_processors())
  274. self.assertEqual('myhostname', s.get_platform_hostname())
  275. self.assertTrue(s.get_platform_is_smp())
  276. self.assertEqual(86401, s.get_uptime())
  277. self.assertEqual([0.1, 0.2, 0.3], s.get_loadavg())
  278. self.assertEqual(3157884928, s.get_mem_total())
  279. self.assertEqual(891383808, s.get_mem_free())
  280. self.assertEqual(1335152640, s.get_mem_cached())
  281. self.assertEqual(28069888, s.get_mem_buffers())
  282. self.assertEqual(4294963200, s.get_mem_swap_total())
  283. self.assertEqual(4095451136, s.get_mem_swap_free())
  284. self.assertEqual('My Distribution', s.get_platform_distro())
  285. # These test that the corresponding tools are being called (and
  286. # no further processing is done on this data). Please see the
  287. # implementation functions at the top of this file.
  288. self.assertEqual('qB2osV6vUOjqm3P/+tQ4d92xoYz8/U8P9v3KWRpNwlI=\n', s.get_net_interfaces())
  289. self.assertEqual('VGWAS92AlS14Pl2xqENJs5P2Ihe6Nv9g181Mu6Zz+aQ=\n\nXfizswwNA9NkXz6K36ZExpjV08Y5IXkHI8jjDSV+5Nc=\n', s.get_net_routing_table())
  290. self.assertEqual('osuxbrcc1g9VgaF4yf3FrtfodrfATrbSnjhqhuQSAs8=\n', s.get_net_stats())
  291. self.assertEqual('Z+w0lwa02/T+5+EIio84rrst/Dtizoz/aL9Im7J7ESA=\n', s.get_net_connections())
  292. def check_bsd_values(self, s):
  293. # check values shared by all bsd implementations
  294. self.assertEqual('test.example.com', s.get_platform_hostname())
  295. self.assertLess(abs(76632 - s.get_uptime()), 4)
  296. self.assertEqual(None, s.get_mem_cached())
  297. self.assertEqual(None, s.get_mem_buffers())
  298. # These test that the corresponding tools are being called (and
  299. # no further processing is done on this data). Please see the
  300. # implementation functions at the top of this file.
  301. self.assertEqual('qB2osV6vUOjqm3P/+tQ4d92xoYz8/U8P9v3KWRpNwlI=\n', s.get_net_interfaces())
  302. self.assertEqual('XfizswwNA9NkXz6K36ZExpjV08Y5IXkHI8jjDSV+5Nc=\n', s.get_net_routing_table())
  303. self.assertEqual('osuxbrcc1g9VgaF4yf3FrtfodrfATrbSnjhqhuQSAs8=\n', s.get_net_stats())
  304. self.assertEqual('Z+w0lwa02/T+5+EIio84rrst/Dtizoz/aL9Im7J7ESA=\n', s.get_net_connections())
  305. def test_sysinfo_openbsd(self):
  306. """Tests the OpenBSD implementation of SysInfo. Note that this
  307. tests deep into the implementation, and not just the
  308. interfaces."""
  309. # Replace existing implementations of library functions
  310. # with mock ones for testing.
  311. platform.system = _my_openbsd_platform_system
  312. os.sysconf = _my_openbsd_os_sysconf
  313. subprocess.check_output = _my_openbsd_subprocess_check_output
  314. os.uname = _my_openbsd_platform_uname
  315. s = SysInfoFromFactory()
  316. self.assertEqual(NPROCESSORS_OPENBSD, s.get_num_processors())
  317. self.check_bsd_values(s)
  318. self.assertEqual([0.7, 0.9, 0.8], s.get_loadavg())
  319. self.assertFalse(s.get_platform_is_smp())
  320. self.assertEqual(543214321, s.get_mem_total())
  321. self.assertEqual(543214321 - (121212 * 1024), s.get_mem_free())
  322. self.assertEqual(566791168, s.get_mem_swap_total())
  323. self.assertEqual(566789120, s.get_mem_swap_free())
  324. # Try new regex assertion (which replaced the deprecated
  325. # assertRegexpMatches. If it is not available, use the old one
  326. try:
  327. self.assertRegex(s.get_platform_distro(), '^OpenBSD\s+.*')
  328. except AttributeError:
  329. self.assertRegexpMatches(s.get_platform_distro(), '^OpenBSD\s+.*')
  330. def test_sysinfo_freebsd(self):
  331. """Tests the FreeBSD implementation of SysInfo. Note that this
  332. tests deep into the implementation, and not just the
  333. interfaces."""
  334. # Replace existing implementations of library functions
  335. # with mock ones for testing.
  336. platform.system = _my_freebsd_platform_system
  337. os.sysconf = _my_freebsd_os_sysconf
  338. subprocess.check_output = _my_freebsd_subprocess_check_output
  339. os.uname = _my_freebsd_platform_uname
  340. s = SysInfoFromFactory()
  341. self.assertEqual(NPROCESSORS_FREEBSD, s.get_num_processors())
  342. self.assertTrue(s.get_platform_is_smp())
  343. self.check_bsd_values(s)
  344. self.assertEqual([0.2, 0.4, 0.6], s.get_loadavg())
  345. self.assertEqual(543214321, s.get_mem_total())
  346. self.assertEqual(543214321 - (343434 * 1024), s.get_mem_free())
  347. self.assertEqual(1037533184, s.get_mem_swap_total())
  348. self.assertEqual(1037533184, s.get_mem_swap_free())
  349. # Try new regex assertion (which replaced the deprecated
  350. # assertRegexpMatches. If it is not available, use the old one
  351. try:
  352. self.assertRegex(s.get_platform_distro(), '^FreeBSD\s+.*')
  353. except AttributeError:
  354. self.assertRegexpMatches(s.get_platform_distro(), '^FreeBSD\s+.*')
  355. def test_sysinfo_osx(self):
  356. """Tests the OS X implementation of SysInfo. Note that this
  357. tests deep into the implementation, and not just the
  358. interfaces."""
  359. # Replace existing implementations of library functions
  360. # with mock ones for testing.
  361. platform.system = _my_osx_platform_system
  362. os.sysconf = _my_osx_os_sysconf
  363. subprocess.check_output = _my_osx_subprocess_check_output
  364. os.uname = _my_osx_platform_uname
  365. s = SysInfoFromFactory()
  366. self.assertEqual(NPROCESSORS_OSX, s.get_num_processors())
  367. self.assertFalse(s.get_platform_is_smp())
  368. self.check_bsd_values(s)
  369. self.assertEqual([0.2, 0.4, 0.6], s.get_loadavg())
  370. self.assertEqual(123456789, s.get_mem_total())
  371. self.assertEqual((23456 * 4096), s.get_mem_free())
  372. self.assertEqual(18874368.0, s.get_mem_swap_total())
  373. self.assertEqual(1075988.48, s.get_mem_swap_free())
  374. # Try new regex assertion (which replaced the deprecated
  375. # assertRegexpMatches. If it is not available, use the old one
  376. try:
  377. self.assertRegex(s.get_platform_distro(), '^Darwin\s+.*')
  378. except AttributeError:
  379. self.assertRegexpMatches(s.get_platform_distro(), '^Darwin\s+.*')
  380. if __name__ == "__main__":
  381. unittest.main()