1
0

processes.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # -*- coding: utf-8 -*-
  2. """
  3. :copyright: Copyright 2017 by the SaltStack Team, see AUTHORS for more details.
  4. :license: Apache 2.0, see LICENSE for more details.
  5. tests.support.processes
  6. ~~~~~~~~~~~~~~~~~~~~~~~
  7. Process handling utilities
  8. """
  9. from __future__ import absolute_import
  10. import logging
  11. from saltfactories.utils.processes.helpers import ( # pylint: disable=unused-import
  12. collect_child_processes,
  13. terminate_process,
  14. terminate_process_list,
  15. )
  16. from tests.support.cli_scripts import ScriptPathMixin
  17. try:
  18. from pytestsalt.fixtures.daemons import Salt as PytestSalt
  19. from pytestsalt.fixtures.daemons import SaltCall as PytestSaltCall
  20. from pytestsalt.fixtures.daemons import SaltKey as PytestSaltKey
  21. from pytestsalt.fixtures.daemons import SaltMaster as PytestSaltMaster
  22. from pytestsalt.fixtures.daemons import SaltMinion as PytestSaltMinion
  23. from pytestsalt.fixtures.daemons import SaltProxy as PytestSaltProxy
  24. from pytestsalt.fixtures.daemons import SaltRun as PytestSaltRun
  25. from pytestsalt.fixtures.daemons import SaltSyndic as PytestSaltSyndic
  26. except ImportError:
  27. # If this happens, we are running under pytest which uninstalls pytest-salt due to impatabilites
  28. # These imports won't actually work but these classes are only used when running under runtests,
  29. # so, we're just making sure we also don't hit NameError's
  30. from saltfactories.utils.processes.salts import SaltCallCLI as PytestSaltCall
  31. from saltfactories.utils.processes.salts import SaltCLI as PytestSalt
  32. from saltfactories.utils.processes.salts import SaltKeyCLI as PytestSaltKey
  33. from saltfactories.utils.processes.salts import SaltMaster as PytestSaltMaster
  34. from saltfactories.utils.processes.salts import SaltMinion as PytestSaltMinion
  35. from saltfactories.utils.processes.salts import SaltProxyMinion as PytestSaltProxy
  36. from saltfactories.utils.processes.salts import SaltRunCLI as PytestSaltRun
  37. from saltfactories.utils.processes.salts import SaltSyndic as PytestSaltSyndic
  38. log = logging.getLogger(__name__)
  39. class GetSaltRunFixtureMixin(ScriptPathMixin):
  40. """
  41. Override this classes `get_salt_run_fixture` because we're still not running under pytest
  42. """
  43. def get_salt_run_fixture(self):
  44. pass
  45. class Salt(ScriptPathMixin, PytestSalt):
  46. """
  47. Class which runs salt-call commands
  48. """
  49. def __init__(self, *args, **kwargs):
  50. super(Salt, self).__init__(None, *args, **kwargs)
  51. class SaltCall(ScriptPathMixin, PytestSaltCall):
  52. """
  53. Class which runs salt-call commands
  54. """
  55. def __init__(self, *args, **kwargs):
  56. super(SaltCall, self).__init__(None, *args, **kwargs)
  57. class SaltKey(ScriptPathMixin, PytestSaltKey):
  58. """
  59. Class which runs salt-key commands
  60. """
  61. def __init__(self, *args, **kwargs):
  62. super(SaltKey, self).__init__(None, *args, **kwargs)
  63. class SaltRun(ScriptPathMixin, PytestSaltRun):
  64. """
  65. Class which runs salt-run commands
  66. """
  67. def __init__(self, *args, **kwargs):
  68. super(SaltRun, self).__init__(None, *args, **kwargs)
  69. class SaltProxy(GetSaltRunFixtureMixin, PytestSaltProxy):
  70. """
  71. Class which runs the salt-proxy daemon
  72. """
  73. class SaltMinion(GetSaltRunFixtureMixin, PytestSaltMinion):
  74. """
  75. Class which runs the salt-minion daemon
  76. """
  77. class SaltMaster(GetSaltRunFixtureMixin, PytestSaltMaster):
  78. """
  79. Class which runs the salt-master daemon
  80. """
  81. class SaltSyndic(GetSaltRunFixtureMixin, PytestSaltSyndic):
  82. """
  83. Class which runs the salt-syndic daemon
  84. """
  85. def start_daemon(
  86. daemon_name=None,
  87. daemon_id=None,
  88. daemon_log_prefix=None,
  89. daemon_cli_script_name=None,
  90. daemon_config=None,
  91. daemon_config_dir=None,
  92. daemon_class=None,
  93. bin_dir_path=None,
  94. fail_hard=False,
  95. start_timeout=10,
  96. slow_stop=False,
  97. environ=None,
  98. cwd=None,
  99. event_listener_config_dir=None,
  100. ):
  101. """
  102. Returns a running salt daemon
  103. """
  104. # Old config name
  105. daemon_config["pytest_port"] = daemon_config["runtests_conn_check_port"]
  106. # New config name
  107. daemon_config["pytest_engine_port"] = daemon_config["runtests_conn_check_port"]
  108. request = None
  109. if fail_hard:
  110. fail_method = RuntimeError
  111. else:
  112. fail_method = RuntimeWarning
  113. log.info("[%s] Starting pytest %s(%s)", daemon_name, daemon_log_prefix, daemon_id)
  114. attempts = 0
  115. process = None
  116. while attempts <= 3: # pylint: disable=too-many-nested-blocks
  117. attempts += 1
  118. try:
  119. process = daemon_class(
  120. request=request,
  121. config=daemon_config,
  122. config_dir=daemon_config_dir,
  123. bin_dir_path=bin_dir_path,
  124. log_prefix=daemon_log_prefix,
  125. cli_script_name=daemon_cli_script_name,
  126. slow_stop=slow_stop,
  127. environ=environ,
  128. cwd=cwd,
  129. event_listener_config_dir=event_listener_config_dir,
  130. )
  131. except TypeError:
  132. process = daemon_class(
  133. request=request,
  134. config=daemon_config,
  135. config_dir=daemon_config_dir,
  136. bin_dir_path=bin_dir_path,
  137. log_prefix=daemon_log_prefix,
  138. cli_script_name=daemon_cli_script_name,
  139. slow_stop=slow_stop,
  140. environ=environ,
  141. cwd=cwd,
  142. )
  143. process.start()
  144. if process.is_alive():
  145. try:
  146. connectable = process.wait_until_running(timeout=start_timeout)
  147. if connectable is False:
  148. connectable = process.wait_until_running(timeout=start_timeout / 2)
  149. if connectable is False:
  150. process.terminate()
  151. if attempts >= 3:
  152. fail_method(
  153. "The pytest {0}({1}) has failed to confirm running status "
  154. "after {2} attempts".format(
  155. daemon_name, daemon_id, attempts
  156. )
  157. )
  158. continue
  159. except Exception as exc: # pylint: disable=broad-except
  160. log.exception("[%s] %s", daemon_log_prefix, exc, exc_info=True)
  161. terminate_process(process.pid, kill_children=True, slow_stop=slow_stop)
  162. if attempts >= 3:
  163. raise fail_method(str(exc))
  164. continue
  165. log.info(
  166. "[%s] The pytest %s(%s) is running and accepting commands "
  167. "after %d attempts",
  168. daemon_log_prefix,
  169. daemon_name,
  170. daemon_id,
  171. attempts,
  172. )
  173. break
  174. else:
  175. terminate_process(process.pid, kill_children=True, slow_stop=slow_stop)
  176. continue
  177. else:
  178. if process is not None:
  179. terminate_process(process.pid, kill_children=True, slow_stop=slow_stop)
  180. raise fail_method(
  181. "The pytest {0}({1}) has failed to start after {2} attempts".format(
  182. daemon_name, daemon_id, attempts - 1
  183. )
  184. )
  185. return process