test_sysmod.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. patch,
  12. )
  13. # Import Salt Libs
  14. import salt.modules.sysmod as sysmod
  15. class MockDocstringable(object):
  16. def __init__(self, docstr):
  17. self.__doc__ = docstr
  18. def set_module_docstring(self, docstr):
  19. self.__globals__ = {'__doc__': docstr}
  20. class Mockstate(object):
  21. """
  22. Mock of State
  23. """
  24. class State(object):
  25. """
  26. Mock state functions
  27. """
  28. states = {}
  29. def __init__(self, opts):
  30. pass
  31. class Mockrunner(object):
  32. """
  33. Mock of runner
  34. """
  35. class Runner(object):
  36. """
  37. Mock runner functions
  38. """
  39. def __init__(self, opts):
  40. pass
  41. @property
  42. def functions(self):
  43. return sysmod.__salt__
  44. class Mockloader(object):
  45. """
  46. Mock of loader
  47. """
  48. functions = [] # ? does not have any effect on existing tests
  49. def __init__(self):
  50. pass
  51. def returners(self, opts, lst):
  52. """
  53. Mock returner functions
  54. """
  55. return sysmod.__salt__
  56. def render(self, opts, lst):
  57. """
  58. Mock renderers
  59. """
  60. return sysmod.__salt__ # 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', 'sys.list_functions', 'sys.list_modules',
  73. 'sysctl.get', 'sysctl.show',
  74. 'system.halt', 'system.reboot',
  75. 'udev.name', 'udev.path',
  76. 'user.add', 'user.info', 'user.rename',
  77. ]
  78. cls._docstrings = {}
  79. cls._statedocstrings = {}
  80. cls.salt_dunder = {}
  81. for func in cls._functions:
  82. docstring = 'docstring for {0}'.format(func)
  83. cls.salt_dunder[func] = MockDocstringable(docstring)
  84. cls._docstrings[func] = docstring
  85. module = func.split('.')[0]
  86. cls._statedocstrings[func] = docstring
  87. cls._statedocstrings[module] = 'docstring for {0}'.format(module)
  88. cls._modules.add(func.split('.')[0])
  89. docstring = 'docstring for {0}'.format(func)
  90. mock = MockDocstringable(docstring)
  91. mock.set_module_docstring('docstring for {0}'.format(func.split('.')[0]))
  92. Mockstate.State.states[func] = mock
  93. cls._modules = sorted(list(cls._modules))
  94. cls.state_patcher = patch('salt.state', Mockstate())
  95. cls.state_patcher.start()
  96. cls.runner_patcher = patch('salt.runner', Mockrunner())
  97. cls.runner_patcher.start()
  98. cls.loader_patcher = patch('salt.loader', Mockloader())
  99. cls.loader_patcher.start()
  100. # 'doc' function tests: 2
  101. @classmethod
  102. def tearDownClass(cls):
  103. cls.runner_patcher.stop()
  104. cls.state_patcher.stop()
  105. cls.loader_patcher.stop()
  106. for attrname in ('_modules', '_functions', '_docstrings', '_statedocstrings', 'salt_dunder',
  107. 'runner_patcher', 'state_patcher', 'loader_patcher'):
  108. try:
  109. delattr(cls, attrname)
  110. except AttributeError:
  111. continue
  112. def test_doc(self):
  113. '''
  114. Test if it returns the docstrings for all modules.
  115. '''
  116. self.assertDictEqual(sysmod.doc(), self._docstrings)
  117. self.assertDictEqual(sysmod.doc('sys.doc'), {'sys.doc': 'docstring for sys.doc'})
  118. # 'state_doc' function tests: 2
  119. def test_state_doc(self):
  120. '''
  121. Test if it returns the docstrings for all states.
  122. '''
  123. self.assertDictEqual(sysmod.state_doc(), self._statedocstrings)
  124. self.assertDictEqual(sysmod.state_doc('sys.doc'), {'sys': 'docstring for sys', 'sys.doc': 'docstring for sys.doc'})
  125. # 'runner_doc' function tests: 2
  126. def test_runner_doc(self):
  127. '''
  128. Test if it returns the docstrings for all runners.
  129. '''
  130. self.assertDictEqual(sysmod.runner_doc(), self._docstrings)
  131. self.assertDictEqual(sysmod.runner_doc('sys.doc'), {'sys.doc': 'docstring for sys.doc'})
  132. # 'returner_doc' function tests: 2
  133. def test_returner_doc(self):
  134. '''
  135. Test if it returns the docstrings for all returners.
  136. '''
  137. self.assertDictEqual(sysmod.returner_doc(), self._docstrings)
  138. self.assertDictEqual(sysmod.returner_doc('sys.doc'), {'sys.doc': 'docstring for sys.doc'})
  139. # 'renderer_doc' function tests: 2
  140. def test_renderer_doc(self):
  141. '''
  142. Test if it returns the docstrings for all renderers.
  143. '''
  144. self.assertDictEqual(sysmod.renderer_doc(), self._docstrings)
  145. self.assertDictEqual(sysmod.renderer_doc('sys.doc'), {'sys.doc': 'docstring for sys.doc'})
  146. # 'list_functions' function tests: 7
  147. def test_list_functions(self):
  148. '''
  149. Test if it lists the functions for all modules.
  150. '''
  151. self.assertListEqual(sysmod.list_functions(), self._functions)
  152. self.assertListEqual(sysmod.list_functions('nonexist'), [])
  153. # list all functions in/given a specific module
  154. self.assertListEqual(sysmod.list_functions('sys'), ['sys.doc', 'sys.list_functions', 'sys.list_modules'])
  155. # globs can be used for both module names and function names:
  156. self.assertListEqual(sysmod.list_functions('sys*'), ['sys.doc', 'sys.list_functions', 'sys.list_modules', 'sysctl.get', 'sysctl.show', 'system.halt', 'system.reboot'])
  157. self.assertListEqual(sysmod.list_functions('sys.list*'), ['sys.list_functions', 'sys.list_modules'])
  158. # "list", or check for a specific function:
  159. self.assertListEqual(sysmod.list_functions('sys.list'), [])
  160. self.assertListEqual(sysmod.list_functions('exist.exist'), ['exist.exist'])
  161. # 'list_modules' function tests: 4
  162. def test_list_modules(self):
  163. '''
  164. Test if it lists the modules loaded on the minion
  165. '''
  166. self.assertListEqual(sysmod.list_modules(), self._modules)
  167. self.assertListEqual(sysmod.list_modules('nonexist'), [])
  168. self.assertListEqual(sysmod.list_modules('user'), ['user'])
  169. self.assertListEqual(sysmod.list_modules('s*'), ['sys', 'sysctl', 'system'])
  170. # 'reload_modules' function tests: 1
  171. def test_reload_modules(self):
  172. '''
  173. Test if it tell the minion to reload the execution modules
  174. '''
  175. self.assertTrue(sysmod.reload_modules())
  176. # 'argspec' function tests: 1
  177. def test_argspec(self):
  178. '''
  179. Test if it return the argument specification
  180. of functions in Salt execution modules.
  181. '''
  182. self.assertDictEqual(sysmod.argspec(), {})
  183. # 'state_argspec' function tests: 1
  184. def test_state_argspec(self):
  185. '''
  186. Test if it return the argument specification
  187. of functions in Salt state modules.
  188. '''
  189. self.assertDictEqual(sysmod.state_argspec(), {})
  190. # 'returner_argspec' function tests: 1
  191. def test_returner_argspec(self):
  192. '''
  193. Test if it return the argument specification
  194. of functions in Salt returner modules.
  195. '''
  196. self.assertDictEqual(sysmod.returner_argspec(), {})
  197. # 'runner_argspec' function tests: 1
  198. def test_runner_argspec(self):
  199. '''
  200. Test if it return the argument specification of functions in Salt runner
  201. modules.
  202. '''
  203. self.assertDictEqual(sysmod.runner_argspec(), {})
  204. # 'list_state_functions' function tests: 7
  205. def test_list_state_functions(self):
  206. '''
  207. Test if it lists the functions for all state modules.
  208. '''
  209. self.assertListEqual(sysmod.list_state_functions(), self._functions)
  210. self.assertListEqual(sysmod.list_state_functions('nonexist'), [])
  211. # list all functions in/given a specific module
  212. self.assertListEqual(sysmod.list_state_functions('sys'), ['sys.doc', 'sys.list_functions', 'sys.list_modules'])
  213. # globs can be used for both module names and function names:
  214. self.assertListEqual(sysmod.list_state_functions('sys*'), ['sys.doc', 'sys.list_functions', 'sys.list_modules', 'sysctl.get', 'sysctl.show', 'system.halt', 'system.reboot'])
  215. self.assertListEqual(sysmod.list_state_functions('sys.list*'), ['sys.list_functions', 'sys.list_modules'])
  216. # "list", or check for a specific function:
  217. self.assertListEqual(sysmod.list_state_functions('sys.list'), [])
  218. self.assertListEqual(sysmod.list_state_functions('exist.exist'), ['exist.exist'])
  219. # 'list_state_modules' function tests: 4
  220. def test_list_state_modules(self):
  221. '''
  222. Test if it lists the modules loaded on the minion.
  223. '''
  224. self.assertListEqual(sysmod.list_state_modules(), self._modules)
  225. self.assertListEqual(sysmod.list_state_modules('nonexist'), [])
  226. self.assertListEqual(sysmod.list_state_modules('user'), ['user'])
  227. self.assertListEqual(sysmod.list_state_modules('s*'), ['sys', 'sysctl', 'system'])
  228. # 'list_runners' function tests: 4
  229. def test_list_runners(self):
  230. '''
  231. Test if it list the runners loaded on the minion.
  232. '''
  233. self.assertListEqual(sysmod.list_runners(), self._modules)
  234. self.assertListEqual(sysmod.list_runners('nonexist'), [])
  235. self.assertListEqual(sysmod.list_runners('user'), ['user'])
  236. self.assertListEqual(sysmod.list_runners('s*'), ['sys', 'sysctl', 'system'])
  237. # 'list_runner_functions' function tests: 7
  238. def test_list_runner_functions(self):
  239. '''
  240. Test if it lists the functions for all runner modules.
  241. '''
  242. self.assertListEqual(sysmod.list_runner_functions(), self._functions)
  243. self.assertListEqual(sysmod.list_runner_functions('nonexist'), [])
  244. # list all functions in/given a specific module
  245. self.assertListEqual(sysmod.list_runner_functions('sys'), ['sys.doc', 'sys.list_functions', 'sys.list_modules'])
  246. # globs can be used for both module names and function names:
  247. self.assertListEqual(sysmod.list_runner_functions('sys*'), ['sys.doc', 'sys.list_functions', 'sys.list_modules', 'sysctl.get', 'sysctl.show', 'system.halt', 'system.reboot'])
  248. self.assertListEqual(sysmod.list_runner_functions('sys.list*'), ['sys.list_functions', 'sys.list_modules'])
  249. # "list", or check for a specific function:
  250. self.assertListEqual(sysmod.list_runner_functions('sys.list'), [])
  251. self.assertListEqual(sysmod.list_runner_functions('exist.exist'), ['exist.exist'])
  252. # 'list_returners' function tests: 4
  253. def test_list_returners(self):
  254. '''
  255. Test if it lists the returners loaded on the minion
  256. '''
  257. self.assertListEqual(sysmod.list_returners(), self._modules)
  258. self.assertListEqual(sysmod.list_returners('nonexist'), [])
  259. self.assertListEqual(sysmod.list_returners('user'), ['user'])
  260. self.assertListEqual(sysmod.list_returners('s*'), ['sys', 'sysctl', 'system'])
  261. # 'list_returner_functions' function tests: 7
  262. def test_list_returner_functions(self):
  263. '''
  264. Test if it lists the functions for all returner modules.
  265. '''
  266. self.assertListEqual(sysmod.list_returner_functions(), self._functions)
  267. self.assertListEqual(sysmod.list_returner_functions('nonexist'), [])
  268. # list all functions in/given a specific module
  269. self.assertListEqual(sysmod.list_returner_functions('sys'), ['sys.doc', 'sys.list_functions', 'sys.list_modules'])
  270. # globs can be used for both module names and function names:
  271. self.assertListEqual(sysmod.list_returner_functions('sys*'), ['sys.doc', 'sys.list_functions', 'sys.list_modules', 'sysctl.get', 'sysctl.show', 'system.halt', 'system.reboot'])
  272. self.assertListEqual(sysmod.list_returner_functions('sys.list*'), ['sys.list_functions', 'sys.list_modules'])
  273. # "list", or check for a specific function:
  274. self.assertListEqual(sysmod.list_returner_functions('sys.list'), [])
  275. self.assertListEqual(sysmod.list_returner_functions('exist.exist'), ['exist.exist'])
  276. # 'list_renderers' function tests: 4
  277. def test_list_renderers(self):
  278. '''
  279. Test if it list the renderers loaded on the minion.
  280. '''
  281. self.assertListEqual(sysmod.list_renderers(), self._functions)
  282. self.assertListEqual(sysmod.list_renderers('nonexist'), [])
  283. self.assertListEqual(sysmod.list_renderers('user.info'), ['user.info'])
  284. self.assertListEqual(sysmod.list_renderers('syst*'), ['system.halt', 'system.reboot'])