test_grains.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. # -*- coding: utf-8 -*-
  2. """
  3. Test the grains module
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import logging
  7. import os
  8. import pprint
  9. import time
  10. import pytest
  11. from salt.ext.six.moves import range
  12. from tests.support.case import ModuleCase
  13. from tests.support.unit import skipIf
  14. log = logging.getLogger(__name__)
  15. @pytest.mark.windows_whitelisted
  16. class TestModulesGrains(ModuleCase):
  17. """
  18. Test the grains module
  19. """
  20. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  21. def test_items(self):
  22. """
  23. grains.items
  24. """
  25. opts = self.minion_opts
  26. self.assertEqual(
  27. self.run_function("grains.items")["test_grain"],
  28. opts["grains"]["test_grain"],
  29. )
  30. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  31. def test_item(self):
  32. """
  33. grains.item
  34. """
  35. opts = self.minion_opts
  36. self.assertEqual(
  37. self.run_function("grains.item", ["test_grain"])["test_grain"],
  38. opts["grains"]["test_grain"],
  39. )
  40. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  41. def test_ls(self):
  42. """
  43. grains.ls
  44. """
  45. check_for = (
  46. "cpu_flags",
  47. "cpu_model",
  48. "cpuarch",
  49. "domain",
  50. "fqdn",
  51. "fqdns",
  52. "gid",
  53. "groupname",
  54. "host",
  55. "kernel",
  56. "kernelrelease",
  57. "kernelversion",
  58. "localhost",
  59. "mem_total",
  60. "num_cpus",
  61. "os",
  62. "os_family",
  63. "path",
  64. "pid",
  65. "ps",
  66. "pythonpath",
  67. "pythonversion",
  68. "saltpath",
  69. "saltversion",
  70. "uid",
  71. "username",
  72. "virtual",
  73. )
  74. lsgrains = self.run_function("grains.ls")
  75. os = self.run_function("grains.get", ["os"])
  76. for grain in check_for:
  77. if os == "Windows" and grain in ["cpu_flags", "gid", "groupname", "uid"]:
  78. continue
  79. self.assertTrue(grain in lsgrains)
  80. @skipIf(
  81. os.environ.get("TRAVIS_PYTHON_VERSION", None) is not None,
  82. "Travis environment can't keep up with salt refresh",
  83. )
  84. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  85. def test_set_val(self):
  86. """
  87. test grains.set_val
  88. """
  89. self.assertEqual(
  90. self.run_function("grains.setval", ["setgrain", "grainval"]),
  91. {"setgrain": "grainval"},
  92. )
  93. time.sleep(5)
  94. ret = self.run_function("grains.item", ["setgrain"])
  95. if not ret:
  96. # Sleep longer, sometimes test systems get bogged down
  97. time.sleep(20)
  98. ret = self.run_function("grains.item", ["setgrain"])
  99. self.assertTrue(ret)
  100. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  101. def test_get(self):
  102. """
  103. test grains.get
  104. """
  105. self.assertEqual(self.run_function("grains.get", ["level1:level2"]), "foo")
  106. @pytest.mark.slow_test(seconds=120) # Test takes >60 and <=120 seconds
  107. def test_get_core_grains(self):
  108. """
  109. test to ensure some core grains are returned
  110. """
  111. grains = ("os", "os_family", "osmajorrelease", "osrelease", "osfullname", "id")
  112. os = self.run_function("grains.get", ["os"])
  113. for grain in grains:
  114. get_grain = self.run_function("grains.get", [grain])
  115. log.debug("Value of '%s' grain: '%s'", grain, get_grain)
  116. if os == "Arch" and grain in ["osmajorrelease"]:
  117. self.assertEqual(get_grain, "")
  118. continue
  119. if os == "Windows" and grain in ["osmajorrelease"]:
  120. self.assertEqual(get_grain, "")
  121. continue
  122. self.assertTrue(get_grain)
  123. @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
  124. def test_get_grains_int(self):
  125. """
  126. test to ensure int grains
  127. are returned as integers
  128. """
  129. grains = ["num_cpus", "mem_total", "num_gpus", "uid"]
  130. os = self.run_function("grains.get", ["os"])
  131. for grain in grains:
  132. get_grain = self.run_function("grains.get", [grain])
  133. if os == "Windows" and grain in ["uid"]:
  134. self.assertEqual(get_grain, "")
  135. continue
  136. self.assertIsInstance(
  137. get_grain, int, msg="grain: {0} is not an int or empty".format(grain)
  138. )
  139. @pytest.mark.windows_whitelisted
  140. class GrainsAppendTestCase(ModuleCase):
  141. """
  142. Tests written specifically for the grains.append function.
  143. """
  144. GRAIN_KEY = "salttesting-grain-key"
  145. GRAIN_VAL = "my-grain-val"
  146. def setUp(self):
  147. # Start off with an empty list
  148. self.run_function("grains.setval", [self.GRAIN_KEY, []])
  149. if not self.wait_for_grain(self.GRAIN_KEY, []):
  150. raise Exception("Failed to set grain")
  151. self.addCleanup(self.cleanup_grain)
  152. def cleanup_grain(self):
  153. self.run_function("grains.setval", [self.GRAIN_KEY, []])
  154. if not self.wait_for_grain(self.GRAIN_KEY, []):
  155. raise Exception("Failed to set grain")
  156. @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
  157. def test_grains_append(self):
  158. """
  159. Tests the return of a simple grains.append call.
  160. """
  161. ret = self.run_function("grains.append", [self.GRAIN_KEY, self.GRAIN_VAL])
  162. self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL])
  163. @pytest.mark.slow_test(seconds=120) # Test takes >60 and <=120 seconds
  164. def test_grains_append_val_already_present(self):
  165. """
  166. Tests the return of a grains.append call when the value is already
  167. present in the grains list.
  168. """
  169. msg = "The val {0} was already in the list " "salttesting-grain-key".format(
  170. self.GRAIN_VAL
  171. )
  172. # First, make sure the test grain is present
  173. ret = self.run_function("grains.append", [self.GRAIN_KEY, self.GRAIN_VAL])
  174. self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL])
  175. # Now try to append again
  176. ret = self.run_function("grains.append", [self.GRAIN_KEY, self.GRAIN_VAL])
  177. self.assertTrue(self.wait_for_grain(self.GRAIN_KEY, [self.GRAIN_VAL]))
  178. if not ret or isinstance(ret, dict):
  179. # Sleep for a bit, sometimes the second "append" runs too quickly
  180. time.sleep(5)
  181. ret = self.run_function("grains.append", [self.GRAIN_KEY, self.GRAIN_VAL])
  182. assert msg == ret
  183. @pytest.mark.flaky(max_runs=4)
  184. @pytest.mark.slow_test(seconds=120) # Test takes >60 and <=120 seconds
  185. def test_grains_append_val_is_list(self):
  186. """
  187. Tests the return of a grains.append call when val is passed in as a list.
  188. """
  189. # Start off with an empty list, don't know if the flaky decorator runs the setUp function or not...
  190. self.run_function("grains.setval", [self.GRAIN_KEY, []])
  191. second_grain = self.GRAIN_VAL + "-2"
  192. ret = self.run_function(
  193. "grains.append", [self.GRAIN_KEY, [self.GRAIN_VAL, second_grain]]
  194. )
  195. self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL, second_grain])
  196. @pytest.mark.slow_test(seconds=120) # Test takes >60 and <=120 seconds
  197. def test_grains_append_call_twice(self):
  198. """
  199. Tests the return of a grains.append call when the value is already present
  200. but also ensure the grain is not listed twice.
  201. """
  202. # First, add the test grain.
  203. append_1 = self.run_function("grains.append", [self.GRAIN_KEY, self.GRAIN_VAL])
  204. # Call the function again, which results in a string message, as tested in
  205. # test_grains_append_val_already_present above.
  206. append_2 = self.run_function("grains.append", [self.GRAIN_KEY, self.GRAIN_VAL])
  207. # Now make sure the grain doesn't show up twice.
  208. grains = self.run_function("grains.items")
  209. count = 0
  210. for grain in grains:
  211. if grain == self.GRAIN_KEY:
  212. count += 1
  213. # We should only have hit the grain key once.
  214. self.assertEqual(
  215. count,
  216. 1,
  217. msg="Count did not match({}!=1) while looking for key '{}'.\nFirst append return:\n{}\nSecond append return:\n{}".format(
  218. count,
  219. self.GRAIN_KEY,
  220. pprint.pformat(append_1),
  221. pprint.pformat(append_2),
  222. ),
  223. )
  224. def wait_for_grain(self, key, val, timeout=60, sleep=0.3):
  225. start = time.time()
  226. while time.time() - start <= timeout:
  227. ret = self.run_function("grains.get", [key])
  228. if ret == val:
  229. return True
  230. time.sleep(sleep)
  231. return False
  232. def test_grains_remove_add(self):
  233. second_grain = self.GRAIN_VAL + "-2"
  234. ret = self.run_function("grains.get", [self.GRAIN_KEY])
  235. self.assertEqual(ret, [])
  236. for i in range(10):
  237. ret = self.run_function("grains.setval", [self.GRAIN_KEY, []])
  238. self.assertEqual(ret[self.GRAIN_KEY], [])
  239. self.wait_for_grain(self.GRAIN_KEY, [])
  240. ret = self.run_function("grains.append", [self.GRAIN_KEY, self.GRAIN_VAL])
  241. self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL])
  242. self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL])
  243. ret = self.run_function("grains.setval", [self.GRAIN_KEY, []])
  244. self.wait_for_grain(self.GRAIN_KEY, [])
  245. self.assertTrue(self.wait_for_grain(self.GRAIN_KEY, []))
  246. ret = self.run_function(
  247. "grains.append", [self.GRAIN_KEY, [self.GRAIN_VAL, second_grain]]
  248. )
  249. self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL, second_grain])
  250. self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL, second_grain])