1
0

test_grains.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. from __future__ import absolute_import, print_function, unicode_literals
  12. import os
  13. import pytest
  14. import salt.utils.files
  15. from tests.support.case import ShellCase, SSHCase
  16. @pytest.mark.windows_whitelisted
  17. @pytest.mark.usefixtures("salt_sub_minion")
  18. class GrainsTargetingTest(ShellCase):
  19. """
  20. Integration tests for targeting with grains.
  21. """
  22. @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
  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. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  35. def test_grains_targeting_minion_id_running(self):
  36. """
  37. Tests return of each running test minion targeting with minion id grain
  38. """
  39. minion = self.run_salt('-G "id:minion" test.ping')
  40. self.assertEqual(sorted(minion), sorted(["minion:", " True"]))
  41. sub_minion = self.run_salt('-G "id:sub_minion" test.ping')
  42. self.assertEqual(sorted(sub_minion), sorted(["sub_minion:", " True"]))
  43. @pytest.mark.flaky(max_runs=4)
  44. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  45. def test_grains_targeting_disconnected(self):
  46. """
  47. Tests return of minion using grains targeting on a disconnected minion.
  48. """
  49. test_ret = "Minion did not return. [No response]"
  50. # Create a minion key, but do not start the "fake" minion. This mimics a
  51. # disconnected minion.
  52. key_file = os.path.join(self.master_opts["pki_dir"], "minions", "disconnected")
  53. with salt.utils.files.fopen(key_file, "a"):
  54. pass
  55. import logging
  56. log = logging.getLogger(__name__)
  57. # ping disconnected minion and ensure it times out and returns with correct message
  58. try:
  59. if salt.utils.platform.is_windows():
  60. cmd_str = '-t 1 -G "id:disconnected" test.ping'
  61. else:
  62. cmd_str = "-t 1 -G 'id:disconnected' test.ping"
  63. ret = ""
  64. for item in self.run_salt(
  65. '-t 1 -G "id:disconnected" test.ping', timeout=40
  66. ):
  67. if item != "disconnected:":
  68. ret = item.strip()
  69. assert ret == test_ret
  70. finally:
  71. os.unlink(key_file)
  72. @pytest.mark.windows_whitelisted
  73. class SSHGrainsTest(SSHCase):
  74. """
  75. Test salt-ssh grains functionality
  76. Depend on proper environment set by SSHCase class
  77. """
  78. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  79. def test_grains_id(self):
  80. """
  81. Test salt-ssh grains id work for localhost.
  82. """
  83. cmd = self.run_function("grains.get", ["id"])
  84. self.assertEqual(cmd, "localhost")