1
0

test_sysmod.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.modules.sysmod as sysmod
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import patch
  12. from tests.support.unit import TestCase
  13. class MockDocstringable(object):
  14. def __init__(self, docstr):
  15. self.__doc__ = docstr
  16. def set_module_docstring(self, docstr):
  17. self.__globals__ = {"__doc__": docstr}
  18. class Mockstate(object):
  19. """
  20. Mock of State
  21. """
  22. class State(object):
  23. """
  24. Mock state functions
  25. """
  26. states = {}
  27. def __init__(self, opts):
  28. pass
  29. class Mockrunner(object):
  30. """
  31. Mock of runner
  32. """
  33. class Runner(object):
  34. """
  35. Mock runner functions
  36. """
  37. def __init__(self, opts):
  38. pass
  39. @property
  40. def functions(self):
  41. return sysmod.__salt__
  42. class Mockloader(object):
  43. """
  44. Mock of loader
  45. """
  46. functions = [] # ? does not have any effect on existing tests
  47. def __init__(self):
  48. pass
  49. def returners(self, opts, lst):
  50. """
  51. Mock returner functions
  52. """
  53. return sysmod.__salt__
  54. def render(self, opts, lst):
  55. """
  56. Mock renderers
  57. """
  58. return (
  59. sysmod.__salt__
  60. ) # renderers do not have '.'s; but whatever. This is for convenience
  61. class SysmodTestCase(TestCase, LoaderModuleMockMixin):
  62. """
  63. Test cases for salt.modules.sysmod
  64. """
  65. def setup_loader_modules(self):
  66. return {sysmod: {"__salt__": self.salt_dunder}}
  67. @classmethod
  68. def setUpClass(cls):
  69. cls._modules = set()
  70. cls._functions = [
  71. "exist.exist",
  72. "sys.doc",
  73. "sys.list_functions",
  74. "sys.list_modules",
  75. "sysctl.get",
  76. "sysctl.show",
  77. "system.halt",
  78. "system.reboot",
  79. "udev.name",
  80. "udev.path",
  81. "user.add",
  82. "user.info",
  83. "user.rename",
  84. ]
  85. cls._docstrings = {}
  86. cls._statedocstrings = {}
  87. cls.salt_dunder = {}
  88. for func in cls._functions:
  89. docstring = "docstring for {0}".format(func)
  90. cls.salt_dunder[func] = MockDocstringable(docstring)
  91. cls._docstrings[func] = docstring
  92. module = func.split(".")[0]
  93. cls._statedocstrings[func] = docstring
  94. cls._statedocstrings[module] = "docstring for {0}".format(module)
  95. cls._modules.add(func.split(".")[0])
  96. docstring = "docstring for {0}".format(func)
  97. mock = MockDocstringable(docstring)
  98. mock.set_module_docstring("docstring for {0}".format(func.split(".")[0]))
  99. Mockstate.State.states[func] = mock
  100. cls._modules = sorted(list(cls._modules))
  101. cls.state_patcher = patch("salt.state", Mockstate())
  102. cls.state_patcher.start()
  103. cls.runner_patcher = patch("salt.runner", Mockrunner())
  104. cls.runner_patcher.start()
  105. cls.loader_patcher = patch("salt.loader", Mockloader())
  106. cls.loader_patcher.start()
  107. # 'doc' function tests: 2
  108. @classmethod
  109. def tearDownClass(cls):
  110. cls.runner_patcher.stop()
  111. cls.state_patcher.stop()
  112. cls.loader_patcher.stop()
  113. for attrname in (
  114. "_modules",
  115. "_functions",
  116. "_docstrings",
  117. "_statedocstrings",
  118. "salt_dunder",
  119. "runner_patcher",
  120. "state_patcher",
  121. "loader_patcher",
  122. ):
  123. try:
  124. delattr(cls, attrname)
  125. except AttributeError:
  126. continue
  127. def test_doc(self):
  128. """
  129. Test if it returns the docstrings for all modules.
  130. """
  131. self.assertDictEqual(sysmod.doc(), self._docstrings)
  132. self.assertDictEqual(
  133. sysmod.doc("sys.doc"), {"sys.doc": "docstring for sys.doc"}
  134. )
  135. # 'state_doc' function tests: 2
  136. def test_state_doc(self):
  137. """
  138. Test if it returns the docstrings for all states.
  139. """
  140. self.assertDictEqual(sysmod.state_doc(), self._statedocstrings)
  141. self.assertDictEqual(
  142. sysmod.state_doc("sys.doc"),
  143. {"sys": "docstring for sys", "sys.doc": "docstring for sys.doc"},
  144. )
  145. # 'runner_doc' function tests: 2
  146. def test_runner_doc(self):
  147. """
  148. Test if it returns the docstrings for all runners.
  149. """
  150. self.assertDictEqual(sysmod.runner_doc(), self._docstrings)
  151. self.assertDictEqual(
  152. sysmod.runner_doc("sys.doc"), {"sys.doc": "docstring for sys.doc"}
  153. )
  154. # 'returner_doc' function tests: 2
  155. def test_returner_doc(self):
  156. """
  157. Test if it returns the docstrings for all returners.
  158. """
  159. self.assertDictEqual(sysmod.returner_doc(), self._docstrings)
  160. self.assertDictEqual(
  161. sysmod.returner_doc("sys.doc"), {"sys.doc": "docstring for sys.doc"}
  162. )
  163. # 'renderer_doc' function tests: 2
  164. def test_renderer_doc(self):
  165. """
  166. Test if it returns the docstrings for all renderers.
  167. """
  168. self.assertDictEqual(sysmod.renderer_doc(), self._docstrings)
  169. self.assertDictEqual(
  170. sysmod.renderer_doc("sys.doc"), {"sys.doc": "docstring for sys.doc"}
  171. )
  172. # 'list_functions' function tests: 7
  173. def test_list_functions(self):
  174. """
  175. Test if it lists the functions for all modules.
  176. """
  177. self.assertListEqual(sysmod.list_functions(), self._functions)
  178. self.assertListEqual(sysmod.list_functions("nonexist"), [])
  179. # list all functions in/given a specific module
  180. self.assertListEqual(
  181. sysmod.list_functions("sys"),
  182. ["sys.doc", "sys.list_functions", "sys.list_modules"],
  183. )
  184. # globs can be used for both module names and function names:
  185. self.assertListEqual(
  186. sysmod.list_functions("sys*"),
  187. [
  188. "sys.doc",
  189. "sys.list_functions",
  190. "sys.list_modules",
  191. "sysctl.get",
  192. "sysctl.show",
  193. "system.halt",
  194. "system.reboot",
  195. ],
  196. )
  197. self.assertListEqual(
  198. sysmod.list_functions("sys.list*"),
  199. ["sys.list_functions", "sys.list_modules"],
  200. )
  201. # "list", or check for a specific function:
  202. self.assertListEqual(sysmod.list_functions("sys.list"), [])
  203. self.assertListEqual(sysmod.list_functions("exist.exist"), ["exist.exist"])
  204. # 'list_modules' function tests: 4
  205. def test_list_modules(self):
  206. """
  207. Test if it lists the modules loaded on the minion
  208. """
  209. self.assertListEqual(sysmod.list_modules(), self._modules)
  210. self.assertListEqual(sysmod.list_modules("nonexist"), [])
  211. self.assertListEqual(sysmod.list_modules("user"), ["user"])
  212. self.assertListEqual(sysmod.list_modules("s*"), ["sys", "sysctl", "system"])
  213. # 'reload_modules' function tests: 1
  214. def test_reload_modules(self):
  215. """
  216. Test if it tell the minion to reload the execution modules
  217. """
  218. self.assertTrue(sysmod.reload_modules())
  219. # 'argspec' function tests: 1
  220. def test_argspec(self):
  221. """
  222. Test if it return the argument specification
  223. of functions in Salt execution modules.
  224. """
  225. self.assertDictEqual(sysmod.argspec(), {})
  226. # 'state_argspec' function tests: 1
  227. def test_state_argspec(self):
  228. """
  229. Test if it return the argument specification
  230. of functions in Salt state modules.
  231. """
  232. self.assertDictEqual(sysmod.state_argspec(), {})
  233. # 'returner_argspec' function tests: 1
  234. def test_returner_argspec(self):
  235. """
  236. Test if it return the argument specification
  237. of functions in Salt returner modules.
  238. """
  239. self.assertDictEqual(sysmod.returner_argspec(), {})
  240. # 'runner_argspec' function tests: 1
  241. def test_runner_argspec(self):
  242. """
  243. Test if it return the argument specification of functions in Salt runner
  244. modules.
  245. """
  246. self.assertDictEqual(sysmod.runner_argspec(), {})
  247. # 'list_state_functions' function tests: 7
  248. def test_list_state_functions(self):
  249. """
  250. Test if it lists the functions for all state modules.
  251. """
  252. self.assertListEqual(sysmod.list_state_functions(), self._functions)
  253. self.assertListEqual(sysmod.list_state_functions("nonexist"), [])
  254. # list all functions in/given a specific module
  255. self.assertListEqual(
  256. sysmod.list_state_functions("sys"),
  257. ["sys.doc", "sys.list_functions", "sys.list_modules"],
  258. )
  259. # globs can be used for both module names and function names:
  260. self.assertListEqual(
  261. sysmod.list_state_functions("sys*"),
  262. [
  263. "sys.doc",
  264. "sys.list_functions",
  265. "sys.list_modules",
  266. "sysctl.get",
  267. "sysctl.show",
  268. "system.halt",
  269. "system.reboot",
  270. ],
  271. )
  272. self.assertListEqual(
  273. sysmod.list_state_functions("sys.list*"),
  274. ["sys.list_functions", "sys.list_modules"],
  275. )
  276. # "list", or check for a specific function:
  277. self.assertListEqual(sysmod.list_state_functions("sys.list"), [])
  278. self.assertListEqual(
  279. sysmod.list_state_functions("exist.exist"), ["exist.exist"]
  280. )
  281. # 'list_state_modules' function tests: 4
  282. def test_list_state_modules(self):
  283. """
  284. Test if it lists the modules loaded on the minion.
  285. """
  286. self.assertListEqual(sysmod.list_state_modules(), self._modules)
  287. self.assertListEqual(sysmod.list_state_modules("nonexist"), [])
  288. self.assertListEqual(sysmod.list_state_modules("user"), ["user"])
  289. self.assertListEqual(
  290. sysmod.list_state_modules("s*"), ["sys", "sysctl", "system"]
  291. )
  292. # 'list_runners' function tests: 4
  293. def test_list_runners(self):
  294. """
  295. Test if it list the runners loaded on the minion.
  296. """
  297. self.assertListEqual(sysmod.list_runners(), self._modules)
  298. self.assertListEqual(sysmod.list_runners("nonexist"), [])
  299. self.assertListEqual(sysmod.list_runners("user"), ["user"])
  300. self.assertListEqual(sysmod.list_runners("s*"), ["sys", "sysctl", "system"])
  301. # 'list_runner_functions' function tests: 7
  302. def test_list_runner_functions(self):
  303. """
  304. Test if it lists the functions for all runner modules.
  305. """
  306. self.assertListEqual(sysmod.list_runner_functions(), self._functions)
  307. self.assertListEqual(sysmod.list_runner_functions("nonexist"), [])
  308. # list all functions in/given a specific module
  309. self.assertListEqual(
  310. sysmod.list_runner_functions("sys"),
  311. ["sys.doc", "sys.list_functions", "sys.list_modules"],
  312. )
  313. # globs can be used for both module names and function names:
  314. self.assertListEqual(
  315. sysmod.list_runner_functions("sys*"),
  316. [
  317. "sys.doc",
  318. "sys.list_functions",
  319. "sys.list_modules",
  320. "sysctl.get",
  321. "sysctl.show",
  322. "system.halt",
  323. "system.reboot",
  324. ],
  325. )
  326. self.assertListEqual(
  327. sysmod.list_runner_functions("sys.list*"),
  328. ["sys.list_functions", "sys.list_modules"],
  329. )
  330. # "list", or check for a specific function:
  331. self.assertListEqual(sysmod.list_runner_functions("sys.list"), [])
  332. self.assertListEqual(
  333. sysmod.list_runner_functions("exist.exist"), ["exist.exist"]
  334. )
  335. # 'list_returners' function tests: 4
  336. def test_list_returners(self):
  337. """
  338. Test if it lists the returners loaded on the minion
  339. """
  340. self.assertListEqual(sysmod.list_returners(), self._modules)
  341. self.assertListEqual(sysmod.list_returners("nonexist"), [])
  342. self.assertListEqual(sysmod.list_returners("user"), ["user"])
  343. self.assertListEqual(sysmod.list_returners("s*"), ["sys", "sysctl", "system"])
  344. # 'list_returner_functions' function tests: 7
  345. def test_list_returner_functions(self):
  346. """
  347. Test if it lists the functions for all returner modules.
  348. """
  349. self.assertListEqual(sysmod.list_returner_functions(), self._functions)
  350. self.assertListEqual(sysmod.list_returner_functions("nonexist"), [])
  351. # list all functions in/given a specific module
  352. self.assertListEqual(
  353. sysmod.list_returner_functions("sys"),
  354. ["sys.doc", "sys.list_functions", "sys.list_modules"],
  355. )
  356. # globs can be used for both module names and function names:
  357. self.assertListEqual(
  358. sysmod.list_returner_functions("sys*"),
  359. [
  360. "sys.doc",
  361. "sys.list_functions",
  362. "sys.list_modules",
  363. "sysctl.get",
  364. "sysctl.show",
  365. "system.halt",
  366. "system.reboot",
  367. ],
  368. )
  369. self.assertListEqual(
  370. sysmod.list_returner_functions("sys.list*"),
  371. ["sys.list_functions", "sys.list_modules"],
  372. )
  373. # "list", or check for a specific function:
  374. self.assertListEqual(sysmod.list_returner_functions("sys.list"), [])
  375. self.assertListEqual(
  376. sysmod.list_returner_functions("exist.exist"), ["exist.exist"]
  377. )
  378. # 'list_renderers' function tests: 4
  379. def test_list_renderers(self):
  380. """
  381. Test if it list the renderers loaded on the minion.
  382. """
  383. self.assertListEqual(sysmod.list_renderers(), self._functions)
  384. self.assertListEqual(sysmod.list_renderers("nonexist"), [])
  385. self.assertListEqual(sysmod.list_renderers("user.info"), ["user.info"])
  386. self.assertListEqual(
  387. sysmod.list_renderers("syst*"), ["system.halt", "system.reboot"]
  388. )