1
0

processes.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. # Import python libs
  10. from __future__ import absolute_import
  11. import logging
  12. # Import pytest-salt libs
  13. from pytestsalt.utils import collect_child_processes, terminate_process, terminate_process_list # pylint: disable=unused-import
  14. from pytestsalt.fixtures.daemons import Salt as PytestSalt
  15. from pytestsalt.fixtures.daemons import SaltKey as PytestSaltKey
  16. from pytestsalt.fixtures.daemons import SaltRun as PytestSaltRun
  17. from pytestsalt.fixtures.daemons import SaltCall as PytestSaltCall
  18. from pytestsalt.fixtures.daemons import SaltMaster as PytestSaltMaster
  19. from pytestsalt.fixtures.daemons import SaltMinion as PytestSaltMinion
  20. from pytestsalt.fixtures.daemons import SaltSyndic as PytestSaltSyndic
  21. from pytestsalt.fixtures.daemons import SaltProxy as PytestSaltProxy
  22. # Import tests support libs
  23. from tests.support.cli_scripts import ScriptPathMixin
  24. log = logging.getLogger(__name__)
  25. class GetSaltRunFixtureMixin(ScriptPathMixin):
  26. '''
  27. Override this classes `get_salt_run_fixture` because we're still not running under pytest
  28. '''
  29. def get_salt_run_fixture(self):
  30. pass
  31. class Salt(ScriptPathMixin, PytestSalt):
  32. '''
  33. Class which runs salt-call commands
  34. '''
  35. def __init__(self, *args, **kwargs):
  36. super(Salt, self).__init__(None, *args, **kwargs)
  37. class SaltCall(ScriptPathMixin, PytestSaltCall):
  38. '''
  39. Class which runs salt-call commands
  40. '''
  41. def __init__(self, *args, **kwargs):
  42. super(SaltCall, self).__init__(None, *args, **kwargs)
  43. class SaltKey(ScriptPathMixin, PytestSaltKey):
  44. '''
  45. Class which runs salt-key commands
  46. '''
  47. def __init__(self, *args, **kwargs):
  48. super(SaltKey, self).__init__(None, *args, **kwargs)
  49. class SaltRun(ScriptPathMixin, PytestSaltRun):
  50. '''
  51. Class which runs salt-run commands
  52. '''
  53. def __init__(self, *args, **kwargs):
  54. super(SaltRun, self).__init__(None, *args, **kwargs)
  55. class SaltProxy(GetSaltRunFixtureMixin, PytestSaltProxy):
  56. '''
  57. Class which runs the salt-proxy daemon
  58. '''
  59. class SaltMinion(GetSaltRunFixtureMixin, PytestSaltMinion):
  60. '''
  61. Class which runs the salt-minion daemon
  62. '''
  63. class SaltMaster(GetSaltRunFixtureMixin, PytestSaltMaster):
  64. '''
  65. Class which runs the salt-master daemon
  66. '''
  67. class SaltSyndic(GetSaltRunFixtureMixin, PytestSaltSyndic):
  68. '''
  69. Class which runs the salt-syndic daemon
  70. '''
  71. def start_daemon(daemon_name=None,
  72. daemon_id=None,
  73. daemon_log_prefix=None,
  74. daemon_cli_script_name=None,
  75. daemon_config=None,
  76. daemon_config_dir=None,
  77. daemon_class=None,
  78. bin_dir_path=None,
  79. fail_hard=False,
  80. start_timeout=10,
  81. slow_stop=False,
  82. environ=None,
  83. cwd=None,
  84. event_listener_config_dir=None):
  85. '''
  86. Returns a running salt daemon
  87. '''
  88. # Old config name
  89. daemon_config['pytest_port'] = daemon_config['runtests_conn_check_port']
  90. # New config name
  91. daemon_config['pytest_engine_port'] = daemon_config['runtests_conn_check_port']
  92. request = None
  93. if fail_hard:
  94. fail_method = RuntimeError
  95. else:
  96. fail_method = RuntimeWarning
  97. log.info('[%s] Starting pytest %s(%s)', daemon_name, daemon_log_prefix, daemon_id)
  98. attempts = 0
  99. process = None
  100. while attempts <= 3: # pylint: disable=too-many-nested-blocks
  101. attempts += 1
  102. try:
  103. process = daemon_class(request=request,
  104. config=daemon_config,
  105. config_dir=daemon_config_dir,
  106. bin_dir_path=bin_dir_path,
  107. log_prefix=daemon_log_prefix,
  108. cli_script_name=daemon_cli_script_name,
  109. slow_stop=slow_stop,
  110. environ=environ,
  111. cwd=cwd,
  112. event_listener_config_dir=event_listener_config_dir)
  113. except TypeError:
  114. process = daemon_class(request=request,
  115. config=daemon_config,
  116. config_dir=daemon_config_dir,
  117. bin_dir_path=bin_dir_path,
  118. log_prefix=daemon_log_prefix,
  119. cli_script_name=daemon_cli_script_name,
  120. slow_stop=slow_stop,
  121. environ=environ,
  122. cwd=cwd)
  123. process.start()
  124. if process.is_alive():
  125. try:
  126. connectable = process.wait_until_running(timeout=start_timeout)
  127. if connectable is False:
  128. connectable = process.wait_until_running(timeout=start_timeout/2)
  129. if connectable is False:
  130. process.terminate()
  131. if attempts >= 3:
  132. fail_method(
  133. 'The pytest {0}({1}) has failed to confirm running status '
  134. 'after {2} attempts'.format(daemon_name, daemon_id, attempts))
  135. continue
  136. except Exception as exc: # pylint: disable=broad-except
  137. log.exception('[%s] %s', daemon_log_prefix, exc, exc_info=True)
  138. terminate_process(process.pid, kill_children=True, slow_stop=slow_stop)
  139. if attempts >= 3:
  140. raise fail_method(str(exc))
  141. continue
  142. log.info(
  143. '[%s] The pytest %s(%s) is running and accepting commands '
  144. 'after %d attempts',
  145. daemon_log_prefix,
  146. daemon_name,
  147. daemon_id,
  148. attempts
  149. )
  150. return process
  151. else:
  152. terminate_process(process.pid, kill_children=True, slow_stop=slow_stop)
  153. continue
  154. else: # pylint: disable=useless-else-on-loop
  155. # Wrong, we have a return, its not useless
  156. if process is not None:
  157. terminate_process(process.pid, kill_children=True, slow_stop=slow_stop)
  158. raise fail_method(
  159. 'The pytest {0}({1}) has failed to start after {2} attempts'.format(
  160. daemon_name,
  161. daemon_id,
  162. attempts-1
  163. )
  164. )