1
0

base.py 2.5 KB

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