test_masterapi.py 3.5 KB

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