1
0

test_status.py 12 KB

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