1
0

test_grains.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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
  18. from tests.support.unit import skipIf
  19. log = logging.getLogger(__name__)
  20. @pytest.mark.windows_whitelisted
  21. class GrainsTargetingTest(ShellCase):
  22. """
  23. Integration tests for targeting with grains.
  24. """
  25. @skipIf(True, "SLOWTEST skip")
  26. def test_grains_targeting_os_running(self):
  27. """
  28. Tests running "salt -G 'os:<system-os>' test.ping and minions both return True
  29. """
  30. test_ret = ["sub_minion:", " True", "minion:", " True"]
  31. os_grain = ""
  32. for item in self.run_salt("minion grains.get os"):
  33. if item != "minion:":
  34. os_grain = item.strip()
  35. ret = self.run_salt('-G "os:{0}" test.ping'.format(os_grain))
  36. self.assertEqual(sorted(ret), sorted(test_ret))
  37. @skipIf(True, "SLOWTEST skip")
  38. def test_grains_targeting_minion_id_running(self):
  39. """
  40. Tests return of each running test minion targeting with minion id grain
  41. """
  42. minion = self.run_salt('-G "id:minion" test.ping')
  43. self.assertEqual(sorted(minion), sorted(["minion:", " True"]))
  44. sub_minion = self.run_salt('-G "id:sub_minion" test.ping')
  45. self.assertEqual(sorted(sub_minion), sorted(["sub_minion:", " True"]))
  46. @flaky
  47. @skipIf(True, "SLOWTEST skip")
  48. def test_grains_targeting_disconnected(self):
  49. """
  50. Tests return of minion using grains targeting on a disconnected minion.
  51. """
  52. test_ret = "Minion did not return. [No response]"
  53. # Create a minion key, but do not start the "fake" minion. This mimics a
  54. # disconnected minion.
  55. key_file = os.path.join(self.master_opts["pki_dir"], "minions", "disconnected")
  56. with salt.utils.files.fopen(key_file, "a"):
  57. pass
  58. # ping disconnected minion and ensure it times out and returns with correct message
  59. try:
  60. ret = ""
  61. for item in self.run_salt(
  62. '-t 1 -G "id:disconnected" test.ping', timeout=40
  63. ):
  64. if item != "disconnected:":
  65. ret = item.strip()
  66. break
  67. assert ret == test_ret
  68. finally:
  69. os.unlink(key_file)
  70. @pytest.mark.windows_whitelisted
  71. class SSHGrainsTest(SSHCase):
  72. """
  73. Test salt-ssh grains functionality
  74. Depend on proper environment set by SSHCase class
  75. """
  76. @skipIf(True, "SLOWTEST skip")
  77. def test_grains_id(self):
  78. """
  79. Test salt-ssh grains id work for localhost.
  80. """
  81. cmd = self.run_function("grains.get", ["id"])
  82. self.assertEqual(cmd, "localhost")