test_masterapi.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # -*- 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.case import ShellCase
  9. from tests.support.paths import TMP, INTEGRATION_TEST_DIR
  10. # Import 3rd-party libs
  11. # Import Salt libs
  12. import salt.utils.files
  13. import salt.utils.stringutils
  14. # all read, only owner write
  15. autosign_file_permissions = stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IWUSR
  16. autosign_file_path = os.path.join(TMP, 'rootdir', 'autosign_file')
  17. class AutosignGrainsTest(ShellCase):
  18. '''
  19. Test autosigning minions based on grain values.
  20. '''
  21. def setUp(self):
  22. shutil.copyfile(
  23. os.path.join(INTEGRATION_TEST_DIR, 'files', 'autosign_grains', 'autosign_file'),
  24. autosign_file_path
  25. )
  26. os.chmod(autosign_file_path, autosign_file_permissions)
  27. self.run_key('-d minion -y')
  28. self.run_call('test.ping -l quiet') # get minon to try to authenticate itself again
  29. if 'minion' in self.run_key('-l acc'):
  30. self.tearDown()
  31. self.skipTest('Could not deauthorize minion')
  32. if 'minion' not in self.run_key('-l un'):
  33. self.tearDown()
  34. self.skipTest('minion did not try to reauthenticate itself')
  35. self.autosign_grains_dir = os.path.join(self.master_opts['autosign_grains_dir'])
  36. if not os.path.isdir(self.autosign_grains_dir):
  37. os.makedirs(self.autosign_grains_dir)
  38. def tearDown(self):
  39. shutil.copyfile(
  40. os.path.join(INTEGRATION_TEST_DIR, 'files', 'autosign_file'),
  41. autosign_file_path
  42. )
  43. os.chmod(autosign_file_path, autosign_file_permissions)
  44. self.run_call('test.ping -l quiet') # get minon to authenticate itself again
  45. if os.path.isdir(self.autosign_grains_dir):
  46. shutil.rmtree(self.autosign_grains_dir)
  47. def test_autosign_grains_accept(self):
  48. grain_file_path = os.path.join(self.autosign_grains_dir, 'test_grain')
  49. with salt.utils.files.fopen(grain_file_path, 'w') as f:
  50. f.write(salt.utils.stringutils.to_str('#invalid_value\ncheese'))
  51. os.chmod(grain_file_path, autosign_file_permissions)
  52. self.run_call('test.ping -l quiet') # get minon to try to authenticate itself again
  53. self.assertIn('minion', self.run_key('-l acc'))
  54. def test_autosign_grains_fail(self):
  55. grain_file_path = os.path.join(self.autosign_grains_dir, 'test_grain')
  56. with salt.utils.files.fopen(grain_file_path, 'w') as f:
  57. f.write(salt.utils.stringutils.to_str('#cheese\ninvalid_value'))
  58. os.chmod(grain_file_path, autosign_file_permissions)
  59. self.run_call('test.ping -l quiet') # get minon to try to authenticate itself again
  60. self.assertNotIn('minion', self.run_key('-l acc'))
  61. self.assertIn('minion', self.run_key('-l un'))