1
0

test_masterapi.py 3.3 KB

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