1
0

test_grains.py 2.9 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. from __future__ import absolute_import, print_function, unicode_literals
  12. import logging
  13. import os
  14. import pytest
  15. import salt.utils.files
  16. from tests.support.case import ShellCase, SSHCase
  17. from tests.support.helpers import flaky, requires_system_grains, slowTest
  18. log = logging.getLogger(__name__)
  19. @pytest.mark.windows_whitelisted
  20. @pytest.mark.usefixtures("salt_sub_minion")
  21. class GrainsTargetingTest(ShellCase):
  22. """
  23. Integration tests for targeting with grains.
  24. """
  25. @slowTest
  26. @requires_system_grains
  27. def test_grains_targeting_os_running(self, grains):
  28. """
  29. Tests running "salt -G 'os:<system-os>' test.ping and minions both return True
  30. """
  31. test_ret = ["sub_minion:", " True", "minion:", " True"]
  32. ret = self.run_salt('-G "os:{0}" test.ping'.format(grains["os"]))
  33. self.assertEqual(sorted(ret), sorted(test_ret))
  34. @slowTest
  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. @flaky
  44. @slowTest
  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. # ping disconnected minion and ensure it times out and returns with correct message
  56. try:
  57. ret = ""
  58. for item in self.run_salt(
  59. '-t 1 -G "id:disconnected" test.ping', timeout=40
  60. ):
  61. if item != "disconnected:":
  62. ret = item.strip()
  63. break
  64. assert ret == test_ret
  65. finally:
  66. os.unlink(key_file)
  67. @pytest.mark.windows_whitelisted
  68. class SSHGrainsTest(SSHCase):
  69. """
  70. Test salt-ssh grains functionality
  71. Depend on proper environment set by SSHCase class
  72. """
  73. @slowTest
  74. def test_grains_id(self):
  75. """
  76. Test salt-ssh grains id work for localhost.
  77. """
  78. cmd = self.run_function("grains.get", ["id"])
  79. self.assertEqual(cmd, "localhost")