test_extend.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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, print_function, unicode_literals
  9. import os
  10. import shutil
  11. from datetime import date
  12. # Import salt libs
  13. import salt.utils.extend
  14. import salt.utils.files
  15. from tests.support.mock import MagicMock, patch
  16. from tests.support.runtests import RUNTIME_VARS
  17. # Import Salt Testing libs
  18. from tests.support.unit import TestCase, skipIf
  19. class ExtendTestCase(TestCase):
  20. def setUp(self):
  21. self.starting_dir = os.getcwd()
  22. os.chdir(RUNTIME_VARS.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(
  30. not os.path.exists(os.path.join(RUNTIME_VARS.CODE_DIR, "templates")),
  31. "Test template directory 'templates/' missing.",
  32. )
  33. def test_run(self):
  34. with patch("sys.exit", MagicMock):
  35. out = salt.utils.extend.run(
  36. "test", "test", "this description", RUNTIME_VARS.CODE_DIR, False
  37. )
  38. self.out = out
  39. year = date.today().strftime("%Y")
  40. self.assertTrue(os.path.exists(out))
  41. self.assertFalse(os.path.exists(os.path.join(out, "template.yml")))
  42. self.assertTrue(os.path.exists(os.path.join(out, "directory")))
  43. self.assertTrue(os.path.exists(os.path.join(out, "directory", "test.py")))
  44. with salt.utils.files.fopen(
  45. os.path.join(out, "directory", "test.py"), "r"
  46. ) as test_f:
  47. self.assertEqual(test_f.read(), year)