test_grains.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Daniel Mizyrycki (mzdaniel@glidelink.net)
  4. tests.integration.cli.grains
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. Test salt-ssh grains id work for localhost. (gh #16129)
  7. $ salt-ssh localhost grains.get id
  8. localhost:
  9. localhost
  10. '''
  11. # Import Python libs
  12. from __future__ import absolute_import, print_function, unicode_literals
  13. import os
  14. # Import Salt Libs
  15. import salt.utils.files
  16. # Import Salt Testing Libs
  17. from tests.support.case import ShellCase, SSHCase
  18. from tests.support.helpers import flaky
  19. class GrainsTargetingTest(ShellCase):
  20. '''
  21. Integration tests for targeting with grains.
  22. '''
  23. def test_grains_targeting_os_running(self):
  24. '''
  25. Tests running "salt -G 'os:<system-os>' test.ping and minions both return True
  26. '''
  27. test_ret = ['sub_minion:', ' True', 'minion:', ' True']
  28. os_grain = ''
  29. for item in self.run_salt('minion grains.get os'):
  30. if item != 'minion:':
  31. os_grain = item.strip()
  32. ret = self.run_salt('-G "os:{0}" test.ping'.format(os_grain))
  33. self.assertEqual(sorted(ret), sorted(test_ret))
  34. def test_grains_targeting_minion_id_running(self):
  35. '''
  36. Tests return of each running test minion targeting with minion id grain
  37. '''
  38. minion = self.run_salt('-G "id:minion" test.ping')
  39. self.assertEqual(sorted(minion), sorted(['minion:', ' True']))
  40. sub_minion = self.run_salt('-G "id:sub_minion" test.ping')
  41. self.assertEqual(sorted(sub_minion), sorted(['sub_minion:', ' True']))
  42. @flaky
  43. def test_grains_targeting_disconnected(self):
  44. '''
  45. Tests return of minion using grains targeting on a disconnected minion.
  46. '''
  47. test_ret = 'Minion did not return. [No response]'
  48. # Create a minion key, but do not start the "fake" minion. This mimics a
  49. # disconnected minion.
  50. key_file = os.path.join(self.master_opts['pki_dir'], 'minions', 'disconnected')
  51. with salt.utils.files.fopen(key_file, 'a'):
  52. pass
  53. import logging
  54. log = logging.getLogger(__name__)
  55. # ping disconnected minion and ensure it times out and returns with correct message
  56. try:
  57. if salt.utils.platform.is_windows():
  58. cmd_str = '-t 1 -G "id:disconnected" test.ping'
  59. else:
  60. cmd_str = '-t 1 -G \'id:disconnected\' test.ping'
  61. ret = ''
  62. for item in self.run_salt('-t 1 -G "id:disconnected" test.ping', timeout=40):
  63. if item != 'disconnected:':
  64. ret = item.strip()
  65. assert ret == test_ret
  66. finally:
  67. os.unlink(key_file)
  68. class SSHGrainsTest(SSHCase):
  69. '''
  70. Test salt-ssh grains functionality
  71. Depend on proper environment set by SSHCase class
  72. '''
  73. def test_grains_id(self):
  74. '''
  75. Test salt-ssh grains id work for localhost.
  76. '''
  77. cmd = self.run_function('grains.get', ['id'])
  78. self.assertEqual(cmd, 'localhost')