test_grains.py 9.3 KB

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