test_saltcheck.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. # -*- coding: utf-8 -*-
  2. '''Unit test for saltcheck execution module'''
  3. # Import Python libs
  4. from __future__ import absolute_import, unicode_literals, print_function
  5. import os.path
  6. try:
  7. import salt.modules.saltcheck as saltcheck
  8. import salt.config
  9. import salt.syspaths as syspaths
  10. except:
  11. raise
  12. # Import Salt Testing Libs
  13. try:
  14. from tests.support.mixins import LoaderModuleMockMixin
  15. from tests.support.unit import TestCase
  16. from tests.support.mock import (
  17. MagicMock,
  18. patch,
  19. )
  20. except:
  21. raise
  22. class LinuxSysctlTestCase(TestCase, LoaderModuleMockMixin):
  23. '''
  24. TestCase for salt.modules.saltcheck module
  25. '''
  26. def setup_loader_modules(self):
  27. # Setting the environment to be local
  28. local_opts = salt.config.minion_config(
  29. os.path.join(syspaths.CONFIG_DIR, 'minion'))
  30. local_opts['file_client'] = 'local'
  31. patcher = patch('salt.config.minion_config',
  32. MagicMock(return_value=local_opts))
  33. patcher.start()
  34. self.addCleanup(patcher.stop)
  35. return {saltcheck: {}}
  36. def test_call_salt_command(self):
  37. '''test simple test.echo module'''
  38. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  39. 'sys.list_modules': MagicMock(return_value=['module1']),
  40. 'cp.cache_master': MagicMock(return_value=[True])
  41. }):
  42. sc_instance = saltcheck.SaltCheck()
  43. returned = sc_instance.call_salt_command(fun="test.echo", args=['hello'], kwargs=None)
  44. self.assertEqual(returned, 'hello')
  45. def test_update_master_cache(self):
  46. '''test master cache'''
  47. self.assertTrue(saltcheck.update_master_cache)
  48. def test_call_salt_command2(self):
  49. '''test simple test.echo module again'''
  50. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  51. 'sys.list_modules': MagicMock(return_value=['module1']),
  52. 'cp.cache_master': MagicMock(return_value=[True])
  53. }):
  54. sc_instance = saltcheck.SaltCheck()
  55. returned = sc_instance.call_salt_command(fun="test.echo", args=['hello'], kwargs=None)
  56. self.assertNotEqual(returned, 'not-hello')
  57. def test__assert_equal1(self):
  58. '''test'''
  59. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  60. 'cp.cache_master': MagicMock(return_value=[True])
  61. }):
  62. sc_instance = saltcheck.SaltCheck()
  63. aaa = {'a': 1, 'b': 2}
  64. bbb = {'a': 1, 'b': 2}
  65. mybool = sc_instance._SaltCheck__assert_equal(aaa, bbb)
  66. self.assertTrue(mybool)
  67. def test__assert_equal2(self):
  68. '''test'''
  69. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  70. 'cp.cache_master': MagicMock(return_value=[True])
  71. }):
  72. sc_instance = saltcheck.SaltCheck()
  73. mybool = sc_instance._SaltCheck__assert_equal(False, True)
  74. self.assertNotEqual(mybool, True)
  75. def test__assert_not_equal1(self):
  76. '''test'''
  77. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  78. 'cp.cache_master': MagicMock(return_value=[True])
  79. }):
  80. sc_instance = saltcheck.SaltCheck()
  81. aaa = {'a': 1, 'b': 2}
  82. bbb = {'a': 1, 'b': 2, 'c': 3}
  83. mybool = sc_instance._SaltCheck__assert_not_equal(aaa, bbb)
  84. self.assertTrue(mybool)
  85. def test__assert_not_equal2(self):
  86. '''test'''
  87. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  88. 'cp.cache_master': MagicMock(return_value=[True])
  89. }):
  90. sc_instance = saltcheck.SaltCheck()
  91. aaa = {'a': 1, 'b': 2}
  92. bbb = {'a': 1, 'b': 2}
  93. mybool = sc_instance._SaltCheck__assert_not_equal(aaa, bbb)
  94. self.assertNotEqual(mybool, True)
  95. def test__assert_true1(self):
  96. '''test'''
  97. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  98. 'cp.cache_master': MagicMock(return_value=[True])
  99. }):
  100. sc_instance = saltcheck.SaltCheck()
  101. mybool = sc_instance._SaltCheck__assert_equal(True, True)
  102. self.assertTrue(mybool)
  103. def test__assert_true2(self):
  104. '''test'''
  105. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  106. 'cp.cache_master': MagicMock(return_value=[True])
  107. }):
  108. sc_instance = saltcheck.SaltCheck()
  109. mybool = sc_instance._SaltCheck__assert_equal(False, True)
  110. self.assertNotEqual(mybool, True)
  111. def test__assert_false1(self):
  112. '''test'''
  113. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  114. 'cp.cache_master': MagicMock(return_value=[True])
  115. }):
  116. sc_instance = saltcheck.SaltCheck()
  117. mybool = sc_instance._SaltCheck__assert_false(False)
  118. self.assertTrue(mybool)
  119. def test__assert_false2(self):
  120. '''test'''
  121. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  122. 'cp.cache_master': MagicMock(return_value=[True])
  123. }):
  124. sc_instance = saltcheck.SaltCheck()
  125. mybool = sc_instance._SaltCheck__assert_false(True)
  126. self.assertNotEqual(mybool, True)
  127. def test__assert_in1(self):
  128. '''test'''
  129. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  130. 'cp.cache_master': MagicMock(return_value=[True])
  131. }):
  132. sc_instance = saltcheck.SaltCheck()
  133. aaa = "bob"
  134. mylist = ['alice', 'bob', 'charles', 'dana']
  135. mybool = sc_instance._SaltCheck__assert_in(aaa, mylist)
  136. self.assertTrue(mybool, True)
  137. def test__assert_in2(self):
  138. '''test'''
  139. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  140. 'cp.cache_master': MagicMock(return_value=[True])
  141. }):
  142. sc_instance = saltcheck.SaltCheck()
  143. aaa = "elaine"
  144. mylist = ['alice', 'bob', 'charles', 'dana']
  145. mybool = sc_instance._SaltCheck__assert_in(aaa, mylist)
  146. self.assertNotEqual(mybool, True)
  147. def test__assert_not_in1(self):
  148. '''test'''
  149. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  150. 'cp.cache_master': MagicMock(return_value=[True])
  151. }):
  152. sc_instance = saltcheck.SaltCheck()
  153. aaa = "elaine"
  154. mylist = ['alice', 'bob', 'charles', 'dana']
  155. mybool = sc_instance._SaltCheck__assert_not_in(aaa, mylist)
  156. self.assertTrue(mybool, True)
  157. def test__assert_not_in2(self):
  158. '''test'''
  159. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  160. 'cp.cache_master': MagicMock(return_value=[True])
  161. }):
  162. sc_instance = saltcheck.SaltCheck()
  163. aaa = "bob"
  164. mylist = ['alice', 'bob', 'charles', 'dana']
  165. mybool = sc_instance._SaltCheck__assert_not_in(aaa, mylist)
  166. self.assertNotEqual(mybool, True)
  167. def test__assert_greater1(self):
  168. '''test'''
  169. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  170. 'cp.cache_master': MagicMock(return_value=[True])
  171. }):
  172. sc_instance = saltcheck.SaltCheck()
  173. aaa = 110
  174. bbb = 100
  175. mybool = sc_instance._SaltCheck__assert_greater(aaa, bbb)
  176. self.assertTrue(mybool, True)
  177. def test__assert_greater2(self):
  178. '''test'''
  179. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  180. 'cp.cache_master': MagicMock(return_value=[True])
  181. }):
  182. sc_instance = saltcheck.SaltCheck()
  183. aaa = 100
  184. bbb = 110
  185. mybool = sc_instance._SaltCheck__assert_greater(aaa, bbb)
  186. self.assertNotEqual(mybool, True)
  187. def test__assert_greater3(self):
  188. '''test'''
  189. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  190. 'cp.cache_master': MagicMock(return_value=[True])
  191. }):
  192. sc_instance = saltcheck.SaltCheck()
  193. aaa = 100
  194. bbb = 100
  195. mybool = sc_instance._SaltCheck__assert_greater(aaa, bbb)
  196. self.assertNotEqual(mybool, True)
  197. def test__assert_greater_equal1(self):
  198. '''test'''
  199. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  200. 'cp.cache_master': MagicMock(return_value=[True])
  201. }):
  202. sc_instance = saltcheck.SaltCheck()
  203. aaa = 110
  204. bbb = 100
  205. mybool = sc_instance._SaltCheck__assert_greater_equal(aaa, bbb)
  206. self.assertTrue(mybool, True)
  207. def test__assert_greater_equal2(self):
  208. '''test'''
  209. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  210. 'cp.cache_master': MagicMock(return_value=[True])
  211. }):
  212. sc_instance = saltcheck.SaltCheck()
  213. aaa = 100
  214. bbb = 110
  215. mybool = sc_instance._SaltCheck__assert_greater_equal(aaa, bbb)
  216. self.assertNotEqual(mybool, True)
  217. def test__assert_greater_equal3(self):
  218. '''test'''
  219. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  220. 'cp.cache_master': MagicMock(return_value=[True])
  221. }):
  222. sc_instance = saltcheck.SaltCheck()
  223. aaa = 100
  224. bbb = 100
  225. mybool = sc_instance._SaltCheck__assert_greater_equal(aaa, bbb)
  226. self.assertEqual(mybool, 'Pass')
  227. def test__assert_less1(self):
  228. '''test'''
  229. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  230. 'cp.cache_master': MagicMock(return_value=[True])
  231. }):
  232. sc_instance = saltcheck.SaltCheck()
  233. aaa = 99
  234. bbb = 100
  235. mybool = sc_instance._SaltCheck__assert_less(aaa, bbb)
  236. self.assertTrue(mybool, True)
  237. def test__assert_less2(self):
  238. '''test'''
  239. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  240. 'cp.cache_master': MagicMock(return_value=[True])
  241. }):
  242. sc_instance = saltcheck.SaltCheck()
  243. aaa = 110
  244. bbb = 99
  245. mybool = sc_instance._SaltCheck__assert_less(aaa, bbb)
  246. self.assertNotEqual(mybool, True)
  247. def test__assert_less3(self):
  248. '''test'''
  249. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  250. 'cp.cache_master': MagicMock(return_value=[True])
  251. }):
  252. sc_instance = saltcheck.SaltCheck()
  253. aaa = 100
  254. bbb = 100
  255. mybool = sc_instance._SaltCheck__assert_less(aaa, bbb)
  256. self.assertNotEqual(mybool, True)
  257. def test__assert_less_equal1(self):
  258. '''test'''
  259. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  260. 'cp.cache_master': MagicMock(return_value=[True])
  261. }):
  262. sc_instance = saltcheck.SaltCheck()
  263. aaa = 99
  264. bbb = 100
  265. mybool = sc_instance._SaltCheck__assert_less_equal(aaa, bbb)
  266. self.assertTrue(mybool, True)
  267. def test__assert_less_equal2(self):
  268. '''test'''
  269. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  270. 'cp.cache_master': MagicMock(return_value=[True])
  271. }):
  272. sc_instance = saltcheck.SaltCheck()
  273. aaa = 110
  274. bbb = 99
  275. mybool = sc_instance._SaltCheck__assert_less_equal(aaa, bbb)
  276. self.assertNotEqual(mybool, True)
  277. def test__assert_less_equal3(self):
  278. '''test'''
  279. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  280. 'cp.cache_master': MagicMock(return_value=[True])
  281. }):
  282. sc_instance = saltcheck.SaltCheck()
  283. aaa = 100
  284. bbb = 100
  285. mybool = sc_instance._SaltCheck__assert_less_equal(aaa, bbb)
  286. self.assertEqual(mybool, 'Pass')
  287. def test_run_test_1(self):
  288. '''test'''
  289. with patch.dict(saltcheck.__salt__, {'config.get': MagicMock(return_value=True),
  290. 'sys.list_modules': MagicMock(return_value=['test']),
  291. 'sys.list_functions': MagicMock(return_value=['test.echo']),
  292. 'cp.cache_master': MagicMock(return_value=[True])}):
  293. returned = saltcheck.run_test(test={"module_and_function": "test.echo",
  294. "assertion": "assertEqual",
  295. "expected-return": "This works!",
  296. "args": ["This works!"]
  297. })
  298. self.assertEqual(returned, 'Pass')