test_grains.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """
  2. :codeauthor: Daniel Mizyrycki (mzdaniel@glidelink.net)
  3. tests.integration.cli.grains
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. Test salt-ssh grains id work for localhost. (gh #16129)
  6. $ salt-ssh localhost grains.get id
  7. localhost:
  8. localhost
  9. """
  10. import logging
  11. import os
  12. import pytest
  13. import salt.utils.files
  14. from tests.support.case import ShellCase, SSHCase
  15. from tests.support.helpers import flaky, requires_system_grains, slowTest
  16. log = logging.getLogger(__name__)
  17. @pytest.mark.windows_whitelisted
  18. @pytest.mark.usefixtures("salt_sub_minion")
  19. class GrainsTargetingTest(ShellCase):
  20. """
  21. Integration tests for targeting with grains.
  22. """
  23. @slowTest
  24. @requires_system_grains
  25. def test_grains_targeting_os_running(self, grains):
  26. """
  27. Tests running "salt -G 'os:<system-os>' test.ping and minions both return True
  28. """
  29. test_ret = ["sub_minion:", " True", "minion:", " True"]
  30. ret = self.run_salt('-G "os:{}" test.ping'.format(grains["os"]))
  31. self.assertEqual(sorted(ret), sorted(test_ret))
  32. @slowTest
  33. def test_grains_targeting_minion_id_running(self):
  34. """
  35. Tests return of each running test minion targeting with minion id grain
  36. """
  37. minion = self.run_salt('-G "id:minion" test.ping')
  38. self.assertEqual(sorted(minion), sorted(["minion:", " True"]))
  39. sub_minion = self.run_salt('-G "id:sub_minion" test.ping')
  40. self.assertEqual(sorted(sub_minion), sorted(["sub_minion:", " True"]))
  41. @flaky
  42. @slowTest
  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. # ping disconnected minion and ensure it times out and returns with correct message
  54. try:
  55. ret = ""
  56. for item in self.run_salt(
  57. '-t 1 -G "id:disconnected" test.ping', timeout=40
  58. ):
  59. if item != "disconnected:":
  60. ret = item.strip()
  61. break
  62. assert ret == test_ret
  63. finally:
  64. os.unlink(key_file)
  65. @pytest.mark.windows_whitelisted
  66. class SSHGrainsTest(SSHCase):
  67. """
  68. Test salt-ssh grains functionality
  69. Depend on proper environment set by SSHCase class
  70. """
  71. @slowTest
  72. def test_grains_id(self):
  73. """
  74. Test salt-ssh grains id work for localhost.
  75. """
  76. cmd = self.run_function("grains.get", ["id"])
  77. self.assertEqual(cmd, "localhost")