test_extend.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding: utf-8 -*-
  2. '''
  3. tests.unit.utils.extend_test
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. Test the salt extend script, leave templates/test alone to keep this working!
  6. '''
  7. # Import python libs
  8. from __future__ import absolute_import, unicode_literals, print_function
  9. import os
  10. import shutil
  11. from datetime import date
  12. # Import Salt Testing libs
  13. from tests.support.unit import TestCase, skipIf
  14. from tests.support.mock import MagicMock, patch
  15. # Import salt libs
  16. import tests.integration as integration
  17. import salt.utils.extend
  18. import salt.utils.files
  19. class ExtendTestCase(TestCase):
  20. def setUp(self):
  21. self.starting_dir = os.getcwd()
  22. os.chdir(integration.CODE_DIR)
  23. self.out = None
  24. def tearDown(self):
  25. if self.out is not None:
  26. if os.path.exists(self.out):
  27. shutil.rmtree(self.out, True)
  28. os.chdir(self.starting_dir)
  29. @skipIf(not os.path.exists(os.path.join(integration.CODE_DIR, 'templates')),
  30. "Test template directory 'templates/' missing.")
  31. def test_run(self):
  32. with patch('sys.exit', MagicMock):
  33. out = salt.utils.extend.run('test', 'test', 'this description', integration.CODE_DIR, False)
  34. self.out = out
  35. year = date.today().strftime('%Y')
  36. self.assertTrue(os.path.exists(out))
  37. self.assertFalse(os.path.exists(os.path.join(out, 'template.yml')))
  38. self.assertTrue(os.path.exists(os.path.join(out, 'directory')))
  39. self.assertTrue(os.path.exists(os.path.join(out, 'directory', 'test.py')))
  40. with salt.utils.files.fopen(os.path.join(out, 'directory', 'test.py'), 'r') as test_f:
  41. self.assertEqual(test_f.read(), year)