base.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """
  2. tests.unit.utils.scheduler.base
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. """
  5. import copy
  6. import logging
  7. import os
  8. import salt.utils.platform
  9. import salt.utils.schedule
  10. from salt.modules.test import ping
  11. from salt.utils.process import SubprocessList
  12. from saltfactories.utils.processes import terminate_process
  13. from tests.support.mixins import SaltReturnAssertsMixin
  14. from tests.support.mock import MagicMock, patch
  15. from tests.support.runtests import RUNTIME_VARS
  16. from tests.support.unit import TestCase
  17. log = logging.getLogger(__name__)
  18. class SchedulerTestsBase(TestCase, SaltReturnAssertsMixin):
  19. """
  20. Validate the pkg module
  21. """
  22. @classmethod
  23. def setUpClass(cls):
  24. root_dir = os.path.join(RUNTIME_VARS.TMP, "schedule-unit-tests")
  25. sock_dir = os.path.join(root_dir, "test-socks")
  26. default_config = salt.config.minion_config(None)
  27. default_config["conf_dir"] = root_dir
  28. default_config["root_dir"] = root_dir
  29. default_config["sock_dir"] = sock_dir
  30. default_config["pki_dir"] = os.path.join(root_dir, "pki")
  31. default_config["cachedir"] = os.path.join(root_dir, "cache")
  32. cls.default_config = default_config
  33. cls.subprocess_list = SubprocessList()
  34. @classmethod
  35. def tearDownClass(cls):
  36. del cls.default_config
  37. del cls.subprocess_list
  38. def setUp(self):
  39. with patch("salt.utils.schedule.clean_proc_dir", MagicMock(return_value=None)):
  40. functions = {"test.ping": ping}
  41. self.schedule = salt.utils.schedule.Schedule(
  42. copy.deepcopy(self.default_config),
  43. functions,
  44. returners={},
  45. new_instance=True,
  46. )
  47. self.schedule._subprocess_list = self.subprocess_list
  48. def tearDown(self):
  49. subprocess_list = self.subprocess_list
  50. processes = subprocess_list.processes
  51. self.schedule.reset()
  52. del self.schedule
  53. for proc in processes:
  54. if proc.is_alive():
  55. terminate_process(proc.pid, kill_children=True, slow_stop=True)
  56. subprocess_list.cleanup()
  57. processes = subprocess_list.processes
  58. if processes:
  59. for proc in processes:
  60. if proc.is_alive():
  61. terminate_process(proc.pid, kill_children=True, slow_stop=False)
  62. subprocess_list.cleanup()
  63. processes = subprocess_list.processes
  64. if processes:
  65. log.warning("Processes left running: %s", processes)