1
0

test_grains.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Test the grains module
  4. '''
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import logging
  8. import os
  9. import time
  10. import pprint
  11. # Import Salt libs
  12. from salt.ext.six.moves import range
  13. # Import Salt Testing libs
  14. from tests.support.case import ModuleCase
  15. from tests.support.helpers import flaky
  16. from tests.support.unit import skipIf
  17. import pytest
  18. log = logging.getLogger(__name__)
  19. @pytest.mark.windows_whitelisted
  20. class TestModulesGrains(ModuleCase):
  21. '''
  22. Test the grains module
  23. '''
  24. def test_items(self):
  25. '''
  26. grains.items
  27. '''
  28. opts = self.minion_opts
  29. self.assertEqual(
  30. self.run_function('grains.items')['test_grain'],
  31. opts['grains']['test_grain']
  32. )
  33. def test_item(self):
  34. '''
  35. grains.item
  36. '''
  37. opts = self.minion_opts
  38. self.assertEqual(
  39. self.run_function('grains.item', ['test_grain'])['test_grain'],
  40. opts['grains']['test_grain']
  41. )
  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(os.environ.get('TRAVIS_PYTHON_VERSION', None) is not None,
  82. 'Travis environment can\'t keep up with salt refresh')
  83. def test_set_val(self):
  84. '''
  85. test grains.set_val
  86. '''
  87. self.assertEqual(
  88. self.run_function(
  89. 'grains.setval',
  90. ['setgrain', 'grainval']),
  91. {'setgrain': 'grainval'})
  92. time.sleep(5)
  93. ret = self.run_function('grains.item', ['setgrain'])
  94. if not ret:
  95. # Sleep longer, sometimes test systems get bogged down
  96. time.sleep(20)
  97. ret = self.run_function('grains.item', ['setgrain'])
  98. self.assertTrue(ret)
  99. def test_get(self):
  100. '''
  101. test grains.get
  102. '''
  103. self.assertEqual(
  104. self.run_function(
  105. 'grains.get',
  106. ['level1:level2']),
  107. 'foo')
  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. 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. @pytest.mark.windows_whitelisted
  139. class GrainsAppendTestCase(ModuleCase):
  140. '''
  141. Tests written specifically for the grains.append function.
  142. '''
  143. GRAIN_KEY = 'salttesting-grain-key'
  144. GRAIN_VAL = 'my-grain-val'
  145. def setUp(self):
  146. # Start off with an empty list
  147. self.run_function('grains.setval', [self.GRAIN_KEY, []])
  148. if not self.wait_for_grain(self.GRAIN_KEY, []):
  149. raise Exception('Failed to set grain')
  150. self.addCleanup(self.cleanup_grain)
  151. def cleanup_grain(self):
  152. self.run_function('grains.setval', [self.GRAIN_KEY, []])
  153. if not self.wait_for_grain(self.GRAIN_KEY, []):
  154. raise Exception('Failed to set grain')
  155. def test_grains_append(self):
  156. '''
  157. Tests the return of a simple grains.append call.
  158. '''
  159. ret = self.run_function('grains.append', [self.GRAIN_KEY, self.GRAIN_VAL])
  160. self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL])
  161. def test_grains_append_val_already_present(self):
  162. '''
  163. Tests the return of a grains.append call when the value is already
  164. present in the grains list.
  165. '''
  166. msg = 'The val {0} was already in the list ' \
  167. 'salttesting-grain-key'.format(self.GRAIN_VAL)
  168. # First, make sure the test grain is present
  169. ret = self.run_function('grains.append', [self.GRAIN_KEY, self.GRAIN_VAL])
  170. self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL])
  171. # Now try to append again
  172. ret = self.run_function('grains.append', [self.GRAIN_KEY, self.GRAIN_VAL])
  173. self.assertTrue(self.wait_for_grain(self.GRAIN_KEY, [self.GRAIN_VAL]))
  174. if not ret or isinstance(ret, dict):
  175. # Sleep for a bit, sometimes the second "append" runs too quickly
  176. time.sleep(5)
  177. ret = self.run_function('grains.append', [self.GRAIN_KEY, self.GRAIN_VAL])
  178. assert msg == ret
  179. @flaky
  180. def test_grains_append_val_is_list(self):
  181. '''
  182. Tests the return of a grains.append call when val is passed in as a list.
  183. '''
  184. # Start off with an empty list, don't know if the flaky decorator runs the setUp function or not...
  185. self.run_function('grains.setval', [self.GRAIN_KEY, []])
  186. second_grain = self.GRAIN_VAL + '-2'
  187. ret = self.run_function('grains.append', [self.GRAIN_KEY, [self.GRAIN_VAL, second_grain]])
  188. self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL, second_grain])
  189. def test_grains_append_call_twice(self):
  190. '''
  191. Tests the return of a grains.append call when the value is already present
  192. but also ensure the grain is not listed twice.
  193. '''
  194. # First, add the test grain.
  195. append_1 = self.run_function('grains.append', [self.GRAIN_KEY, self.GRAIN_VAL])
  196. # Call the function again, which results in a string message, as tested in
  197. # test_grains_append_val_already_present above.
  198. append_2 = self.run_function('grains.append', [self.GRAIN_KEY, self.GRAIN_VAL])
  199. # Now make sure the grain doesn't show up twice.
  200. grains = self.run_function('grains.items')
  201. count = 0
  202. for grain in grains:
  203. if grain == self.GRAIN_KEY:
  204. count += 1
  205. # We should only have hit the grain key once.
  206. self.assertEqual(
  207. count,
  208. 1,
  209. msg='Count did not match({}!=1) while looking for key \'{}\'.\nFirst append return:\n{}\nSecond append return:\n{}'.format(
  210. count,
  211. self.GRAIN_KEY,
  212. pprint.pformat(append_1),
  213. pprint.pformat(append_2)
  214. )
  215. )
  216. def wait_for_grain(self, key, val, timeout=60, sleep=.3):
  217. start = time.time()
  218. while time.time() - start <= timeout:
  219. ret = self.run_function('grains.get', [key])
  220. if ret == val:
  221. return True
  222. time.sleep(sleep)
  223. return False
  224. def test_grains_remove_add(self):
  225. second_grain = self.GRAIN_VAL + '-2'
  226. ret = self.run_function('grains.get', [self.GRAIN_KEY])
  227. self.assertEqual(ret, [])
  228. for i in range(10):
  229. ret = self.run_function('grains.setval', [self.GRAIN_KEY, []])
  230. self.assertEqual(ret[self.GRAIN_KEY], [])
  231. self.wait_for_grain(self.GRAIN_KEY, [])
  232. ret = self.run_function('grains.append', [self.GRAIN_KEY, self.GRAIN_VAL])
  233. self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL])
  234. self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL])
  235. ret = self.run_function('grains.setval', [self.GRAIN_KEY, []])
  236. self.wait_for_grain(self.GRAIN_KEY, [])
  237. self.assertTrue(self.wait_for_grain(self.GRAIN_KEY, []))
  238. ret = self.run_function('grains.append', [self.GRAIN_KEY, [self.GRAIN_VAL, second_grain]])
  239. self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL, second_grain])
  240. self.assertEqual(ret[self.GRAIN_KEY], [self.GRAIN_VAL, second_grain])