test_masterapi.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 Testing libs
  8. from tests.support.runtests import RUNTIME_VARS
  9. from tests.support.case import ShellCase
  10. # Import 3rd-party libs
  11. # Import Salt libs
  12. import salt.utils.files
  13. import salt.utils.stringutils
  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 = stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IWUSR
  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(RUNTIME_VARS.TMP, 'rootdir', 'autosign_file')
  25. shutil.copyfile(
  26. os.path.join(RUNTIME_VARS.FILES, 'autosign_grains', 'autosign_file'),
  27. self.autosign_file_path
  28. )
  29. os.chmod(self.autosign_file_path, self.autosign_file_permissions)
  30. self.run_key('-d minion -y')
  31. self.run_call('test.ping -l quiet') # get minon to try to authenticate itself again
  32. if 'minion' in self.run_key('-l acc'):
  33. self.tearDown()
  34. self.skipTest('Could not deauthorize minion')
  35. if 'minion' not in self.run_key('-l un'):
  36. self.tearDown()
  37. self.skipTest('minion did not try to reauthenticate itself')
  38. self.autosign_grains_dir = os.path.join(self.master_opts['autosign_grains_dir'])
  39. if not os.path.isdir(self.autosign_grains_dir):
  40. os.makedirs(self.autosign_grains_dir)
  41. def tearDown(self):
  42. shutil.copyfile(
  43. os.path.join(RUNTIME_VARS.FILES, 'autosign_file'),
  44. self.autosign_file_path
  45. )
  46. os.chmod(self.autosign_file_path, self.autosign_file_permissions)
  47. self.run_call('test.ping -l quiet') # get minon to authenticate itself again
  48. try:
  49. if os.path.isdir(self.autosign_grains_dir):
  50. shutil.rmtree(self.autosign_grains_dir)
  51. except AttributeError:
  52. pass
  53. def test_autosign_grains_accept(self):
  54. grain_file_path = os.path.join(self.autosign_grains_dir, 'test_grain')
  55. with salt.utils.files.fopen(grain_file_path, 'w') as f:
  56. f.write(salt.utils.stringutils.to_str('#invalid_value\ncheese'))
  57. os.chmod(grain_file_path, self.autosign_file_permissions)
  58. self.run_call('test.ping -l quiet') # get minon to try to authenticate itself again
  59. self.assertIn('minion', self.run_key('-l acc'))
  60. def test_autosign_grains_fail(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('#cheese\ninvalid_value'))
  64. os.chmod(grain_file_path, self.autosign_file_permissions)
  65. self.run_call('test.ping -l quiet') # get minon to try to authenticate itself again
  66. self.assertNotIn('minion', self.run_key('-l acc'))
  67. self.assertIn('minion', self.run_key('-l un'))