1
0

test_status.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import os
  5. # Import Salt Libs
  6. import salt.utils.platform
  7. import salt.modules.status as status
  8. from salt.exceptions import CommandExecutionError
  9. from salt.ext import six
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.unit import TestCase
  13. from tests.support.mock import (
  14. MagicMock,
  15. patch,
  16. mock_open,
  17. )
  18. class StatusTestCase(TestCase, LoaderModuleMockMixin):
  19. '''
  20. test modules.status functions
  21. '''
  22. def setup_loader_modules(self):
  23. return {status: {}}
  24. def _set_up_test_uptime(self):
  25. '''
  26. Define common mock data for status.uptime tests
  27. '''
  28. class MockData(object):
  29. '''
  30. Store mock data
  31. '''
  32. m = MockData()
  33. m.now = 1477004312
  34. m.ut = 1540154.00
  35. m.idle = 3047777.32
  36. m.ret = {
  37. 'users': 3,
  38. 'seconds': 1540154,
  39. 'since_t': 1475464158,
  40. 'days': 17,
  41. 'since_iso': '2016-10-03T03:09:18',
  42. 'time': '19:49',
  43. }
  44. return m
  45. def _set_up_test_uptime_sunos(self):
  46. '''
  47. Define common mock data for cmd.run_all for status.uptime on SunOS
  48. '''
  49. class MockData(object):
  50. '''
  51. Store mock data
  52. '''
  53. m = MockData()
  54. m.ret = {
  55. 'retcode': 0,
  56. 'stdout': 'unix:0:system_misc:boot_time 1475464158',
  57. }
  58. return m
  59. def test_uptime_linux(self):
  60. '''
  61. Test modules.status.uptime function for Linux
  62. '''
  63. m = self._set_up_test_uptime()
  64. with patch.multiple(salt.utils.platform,
  65. is_linux=MagicMock(return_value=True),
  66. is_sunos=MagicMock(return_value=False),
  67. is_darwin=MagicMock(return_value=False),
  68. is_freebsd=MagicMock(return_value=False),
  69. is_openbsd=MagicMock(return_value=False),
  70. is_netbsd=MagicMock(return_value=False)), \
  71. patch('salt.utils.path.which', MagicMock(return_value=True)), \
  72. patch.dict(status.__salt__, {'cmd.run': MagicMock(return_value=os.linesep.join(['1', '2', '3']))}), \
  73. patch('time.time', MagicMock(return_value=m.now)), \
  74. patch('os.path.exists', MagicMock(return_value=True)):
  75. proc_uptime = salt.utils.stringutils.to_str('{0} {1}'.format(m.ut, m.idle))
  76. with patch('salt.utils.files.fopen', mock_open(read_data=proc_uptime)):
  77. ret = status.uptime()
  78. self.assertDictEqual(ret, m.ret)
  79. with patch('os.path.exists', MagicMock(return_value=False)):
  80. with self.assertRaises(CommandExecutionError):
  81. status.uptime()
  82. def test_uptime_sunos(self):
  83. '''
  84. Test modules.status.uptime function for SunOS
  85. '''
  86. m = self._set_up_test_uptime()
  87. m2 = self._set_up_test_uptime_sunos()
  88. with patch.multiple(salt.utils.platform,
  89. is_linux=MagicMock(return_value=False),
  90. is_sunos=MagicMock(return_value=True),
  91. is_darwin=MagicMock(return_value=False),
  92. is_freebsd=MagicMock(return_value=False),
  93. is_openbsd=MagicMock(return_value=False),
  94. is_netbsd=MagicMock(return_value=False)), \
  95. patch('salt.utils.path.which', MagicMock(return_value=True)), \
  96. patch.dict(status.__salt__, {'cmd.run': MagicMock(return_value=os.linesep.join(['1', '2', '3'])),
  97. 'cmd.run_all': MagicMock(return_value=m2.ret)}), \
  98. patch('time.time', MagicMock(return_value=m.now)):
  99. ret = status.uptime()
  100. self.assertDictEqual(ret, m.ret)
  101. def test_uptime_macos(self):
  102. '''
  103. Test modules.status.uptime function for macOS
  104. '''
  105. m = self._set_up_test_uptime()
  106. kern_boottime = ('{{ sec = {0}, usec = {1:0<6} }} Mon Oct 03 03:09:18.23 2016'
  107. ''.format(*six.text_type(m.now - m.ut).split('.')))
  108. with patch.multiple(salt.utils.platform,
  109. is_linux=MagicMock(return_value=False),
  110. is_sunos=MagicMock(return_value=False),
  111. is_darwin=MagicMock(return_value=True),
  112. is_freebsd=MagicMock(return_value=False),
  113. is_openbsd=MagicMock(return_value=False),
  114. is_netbsd=MagicMock(return_value=False)), \
  115. patch('salt.utils.path.which', MagicMock(return_value=True)), \
  116. patch.dict(status.__salt__, {'cmd.run': MagicMock(return_value=os.linesep.join(['1', '2', '3'])),
  117. 'sysctl.get': MagicMock(return_value=kern_boottime)}), \
  118. patch('time.time', MagicMock(return_value=m.now)):
  119. ret = status.uptime()
  120. self.assertDictEqual(ret, m.ret)
  121. with patch.dict(status.__salt__, {'sysctl.get': MagicMock(return_value='')}):
  122. with self.assertRaises(CommandExecutionError):
  123. status.uptime()
  124. def test_uptime_return_success_not_supported(self):
  125. '''
  126. Test modules.status.uptime function for other platforms
  127. '''
  128. with patch.multiple(salt.utils.platform,
  129. is_linux=MagicMock(return_value=False),
  130. is_sunos=MagicMock(return_value=False),
  131. is_darwin=MagicMock(return_value=False),
  132. is_freebsd=MagicMock(return_value=False),
  133. is_openbsd=MagicMock(return_value=False),
  134. is_netbsd=MagicMock(return_value=False)):
  135. exc_mock = MagicMock(side_effect=CommandExecutionError)
  136. with self.assertRaises(CommandExecutionError):
  137. with patch.dict(status.__salt__, {'cmd.run': exc_mock}):
  138. status.uptime()
  139. def _set_up_test_cpustats_openbsd(self):
  140. '''
  141. Define mock data for status.cpustats on OpenBSD
  142. '''
  143. class MockData(object):
  144. '''
  145. Store mock data
  146. '''
  147. m = MockData()
  148. m.ret = {
  149. '0': {
  150. 'User': '0.0%',
  151. 'Nice': '0.0%',
  152. 'System': '4.5%',
  153. 'Interrupt': '0.5%',
  154. 'Idle': '95.0%',
  155. }
  156. }
  157. return m
  158. def test_cpustats_openbsd(self):
  159. '''
  160. Test modules.status.cpustats function for OpenBSD
  161. '''
  162. m = self._set_up_test_cpustats_openbsd()
  163. systat = '\n' \
  164. '\n' \
  165. ' 1 users Load 0.20 0.07 0.05 salt.localdomain 09:42:42\n' \
  166. 'CPU User Nice System Interrupt Idle\n' \
  167. '0 0.0% 0.0% 4.5% 0.5% 95.0%\n'
  168. with patch.multiple(salt.utils.platform,
  169. is_linux=MagicMock(return_value=False),
  170. is_sunos=MagicMock(return_value=False),
  171. is_darwin=MagicMock(return_value=False),
  172. is_freebsd=MagicMock(return_value=False),
  173. is_openbsd=MagicMock(return_value=True),
  174. is_netbsd=MagicMock(return_value=False)), \
  175. patch('salt.utils.path.which', MagicMock(return_value=True)), \
  176. patch.dict(status.__grains__, {'kernel': 'OpenBSD'}), \
  177. patch.dict(status.__salt__, {'cmd.run': MagicMock(return_value=systat)}):
  178. ret = status.cpustats()
  179. self.assertDictEqual(ret, m.ret)
  180. def _set_up_test_cpuinfo_bsd(self):
  181. class MockData(object):
  182. '''
  183. Store mock data
  184. '''
  185. m = MockData()
  186. m.ret = {
  187. 'hw.model': 'Intel(R) Core(TM) i5-7287U CPU @ 3.30GHz',
  188. 'hw.ncpu': '4',
  189. }
  190. return m
  191. def test_cpuinfo_freebsd(self):
  192. m = self._set_up_test_cpuinfo_bsd()
  193. sysctl = 'hw.model:Intel(R) Core(TM) i5-7287U CPU @ 3.30GHz\nhw.ncpu:4'
  194. with patch.dict(status.__grains__, {'kernel': 'FreeBSD'}):
  195. with patch.dict(status.__salt__, {'cmd.run': MagicMock(return_value=sysctl)}):
  196. ret = status.cpuinfo()
  197. self.assertDictEqual(ret, m.ret)
  198. def test_cpuinfo_openbsd(self):
  199. m = self._set_up_test_cpuinfo_bsd()
  200. sysctl = 'hw.model=Intel(R) Core(TM) i5-7287U CPU @ 3.30GHz\nhw.ncpu=4'
  201. for bsd in ['NetBSD', 'OpenBSD']:
  202. with patch.dict(status.__grains__, {'kernel': bsd}):
  203. with patch.dict(status.__salt__, {'cmd.run': MagicMock(return_value=sysctl)}):
  204. ret = status.cpuinfo()
  205. self.assertDictEqual(ret, m.ret)
  206. def _set_up_test_meminfo_openbsd(self):
  207. class MockData(object):
  208. '''
  209. Store mock data
  210. '''
  211. m = MockData()
  212. m.ret = {
  213. 'active virtual pages': '355M',
  214. 'free list size': '305M',
  215. 'page faults': '845',
  216. 'pages reclaimed': '1',
  217. 'pages paged in': '2',
  218. 'pages paged out': '3',
  219. 'pages freed': '4',
  220. 'pages scanned': '5'
  221. }
  222. return m
  223. def test_meminfo_openbsd(self):
  224. m = self._set_up_test_meminfo_openbsd()
  225. vmstat = ' procs memory page disks traps cpu\n' \
  226. ' r s avm fre flt re pi po fr sr cd0 sd0 int sys cs us sy id\n' \
  227. ' 2 103 355M 305M 845 1 2 3 4 5 0 1 21 682 86 1 1 98'
  228. with patch.dict(status.__grains__, {'kernel': 'OpenBSD'}):
  229. with patch.dict(status.__salt__, {'cmd.run': MagicMock(return_value=vmstat)}):
  230. ret = status.meminfo()
  231. self.assertDictEqual(ret, m.ret)
  232. def _set_up_test_w_linux(self):
  233. '''
  234. Define mock data for status.w on Linux
  235. '''
  236. class MockData(object):
  237. '''
  238. Store mock data
  239. '''
  240. m = MockData()
  241. m.ret = [{
  242. 'idle': '0s',
  243. 'jcpu': '0.24s',
  244. 'login': '13:42',
  245. 'pcpu': '0.16s',
  246. 'tty': 'pts/1',
  247. 'user': 'root',
  248. 'what': 'nmap -sV 10.2.2.2',
  249. }]
  250. return m
  251. def _set_up_test_w_bsd(self):
  252. '''
  253. Define mock data for status.w on Linux
  254. '''
  255. class MockData(object):
  256. '''
  257. Store mock data
  258. '''
  259. m = MockData()
  260. m.ret = [{
  261. 'idle': '0',
  262. 'from': '10.2.2.1',
  263. 'login': '1:42PM',
  264. 'tty': 'p1',
  265. 'user': 'root',
  266. 'what': 'nmap -sV 10.2.2.2',
  267. }]
  268. return m
  269. def test_w_linux(self):
  270. m = self._set_up_test_w_linux()
  271. w_output = 'root pts/1 13:42 0s 0.24s 0.16s nmap -sV 10.2.2.2'
  272. with patch.dict(status.__grains__, {'kernel': 'Linux'}):
  273. with patch.dict(status.__salt__, {'cmd.run': MagicMock(return_value=w_output)}):
  274. ret = status.w()
  275. self.assertListEqual(ret, m.ret)
  276. def test_w_bsd(self):
  277. m = self._set_up_test_w_bsd()
  278. w_output = 'root p1 10.2.2.1 1:42PM 0 nmap -sV 10.2.2.2'
  279. for bsd in ['Darwin', 'FreeBSD', 'OpenBSD']:
  280. with patch.dict(status.__grains__, {'kernel': bsd}):
  281. with patch.dict(status.__salt__, {'cmd.run': MagicMock(return_value=w_output)}):
  282. ret = status.w()
  283. self.assertListEqual(ret, m.ret)