test_masterapi.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # -tests/integration/daemons/test_masterapi.py:71*- coding: utf-8 -*-
  2. from __future__ import absolute_import, print_function, unicode_literals
  3. import os
  4. import shutil
  5. import stat
  6. import salt.utils.files
  7. import salt.utils.stringutils
  8. from tests.support.case import ShellCase
  9. from tests.support.helpers import slowTest
  10. from tests.support.runtests import RUNTIME_VARS
  11. # Import 3rd-party libs
  12. class AutosignGrainsTest(ShellCase):
  13. """
  14. Test autosigning minions based on grain values.
  15. """
  16. def setUp(self):
  17. # all read, only owner write
  18. self.autosign_file_permissions = (
  19. stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IWUSR
  20. )
  21. if RUNTIME_VARS.PYTEST_SESSION:
  22. self.autosign_file_path = os.path.join(RUNTIME_VARS.TMP, "autosign_file")
  23. else:
  24. self.autosign_file_path = os.path.join(
  25. RUNTIME_VARS.TMP, "rootdir", "autosign_file"
  26. )
  27. shutil.copyfile(
  28. os.path.join(RUNTIME_VARS.FILES, "autosign_grains", "autosign_file"),
  29. self.autosign_file_path,
  30. )
  31. os.chmod(self.autosign_file_path, self.autosign_file_permissions)
  32. self.run_key("-d minion -y")
  33. self.run_call(
  34. "test.ping -l quiet"
  35. ) # get minion to try to authenticate itself again
  36. if "minion" in self.run_key("-l acc"):
  37. self.tearDown()
  38. self.skipTest("Could not deauthorize minion")
  39. if "minion" not in self.run_key("-l un"):
  40. self.tearDown()
  41. self.skipTest("minion did not try to reauthenticate itself")
  42. self.autosign_grains_dir = os.path.join(self.master_opts["autosign_grains_dir"])
  43. if not os.path.isdir(self.autosign_grains_dir):
  44. os.makedirs(self.autosign_grains_dir)
  45. def tearDown(self):
  46. shutil.copyfile(
  47. os.path.join(RUNTIME_VARS.FILES, "autosign_file"), self.autosign_file_path
  48. )
  49. os.chmod(self.autosign_file_path, self.autosign_file_permissions)
  50. self.run_call("test.ping -l quiet") # get minion to authenticate itself again
  51. try:
  52. if os.path.isdir(self.autosign_grains_dir):
  53. shutil.rmtree(self.autosign_grains_dir)
  54. except AttributeError:
  55. pass
  56. @slowTest
  57. def test_autosign_grains_accept(self):
  58. grain_file_path = os.path.join(self.autosign_grains_dir, "test_grain")
  59. with salt.utils.files.fopen(grain_file_path, "w") as f:
  60. f.write(salt.utils.stringutils.to_str("#invalid_value\ncheese"))
  61. os.chmod(grain_file_path, self.autosign_file_permissions)
  62. self.run_call(
  63. "test.ping -l quiet"
  64. ) # get minion to try to authenticate itself again
  65. self.assertIn("minion", self.run_key("-l acc"))
  66. @slowTest
  67. def test_autosign_grains_fail(self):
  68. grain_file_path = os.path.join(self.autosign_grains_dir, "test_grain")
  69. with salt.utils.files.fopen(grain_file_path, "w") as f:
  70. f.write(salt.utils.stringutils.to_str("#cheese\ninvalid_value"))
  71. os.chmod(grain_file_path, self.autosign_file_permissions)
  72. self.run_call(
  73. "test.ping -l quiet"
  74. ) # get minion to try to authenticate itself again
  75. self.assertNotIn("minion", self.run_key("-l acc"))
  76. self.assertIn("minion", self.run_key("-l un"))