chore: terminate process on windows
This commit is contained in:
parent
eb9c629bbf
commit
dc628f300a
|
@ -26,7 +26,7 @@ def application_logs():
|
||||||
for app_data in configs.testpath.STATUS_DATA.iterdir():
|
for app_data in configs.testpath.STATUS_DATA.iterdir():
|
||||||
for log in (app_data / 'logs').glob('*.log'):
|
for log in (app_data / 'logs').glob('*.log'):
|
||||||
allure.attach.file(log, name=str(log.name), attachment_type=allure.attachment_type.TEXT)
|
allure.attach.file(log, name=str(log.name), attachment_type=allure.attachment_type.TEXT)
|
||||||
log.unlink()
|
log.unlink() # FIXME: it does not work on Windows, permission error
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|
|
@ -2,14 +2,13 @@ import logging
|
||||||
import os
|
import os
|
||||||
import signal
|
import signal
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
|
||||||
import typing
|
import typing
|
||||||
|
import platform
|
||||||
|
|
||||||
import allure
|
import allure
|
||||||
import psutil
|
import psutil
|
||||||
|
|
||||||
import configs
|
import configs
|
||||||
from configs.system import IS_WIN
|
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -33,22 +32,20 @@ def find_free_port(start: int, step: int):
|
||||||
|
|
||||||
|
|
||||||
@allure.step('Kill process')
|
@allure.step('Kill process')
|
||||||
def kill_process(pid, sig: signal.Signals = signal.SIGKILL):
|
def kill_process(pid):
|
||||||
LOG.debug('Sending %s to %d process', sig.name, pid)
|
LOG.debug(f'Terminating process {pid}')
|
||||||
try:
|
|
||||||
p = psutil.Process(pid)
|
|
||||||
|
|
||||||
except ProcessLookupError as err:
|
current_platform = platform.system()
|
||||||
LOG.error('Failed to find process %d: %s', pid, err)
|
|
||||||
raise err
|
try:
|
||||||
for i in range(2):
|
if current_platform == "Windows":
|
||||||
try:
|
subprocess.call(f"taskkill /F /T /PID {str(pid)}")
|
||||||
LOG.warning('Waiting for process to exit: %d', pid)
|
elif current_platform in ["Linux", "Darwin"]:
|
||||||
p.kill()
|
os.kill(pid, signal.SIGKILL)
|
||||||
if not p.is_running():
|
else:
|
||||||
break
|
raise NotImplementedError(f"Unsupported platform: {current_platform}")
|
||||||
except RuntimeError:
|
except Exception as e:
|
||||||
raise ('Failed to kill process: %d' % pid)
|
print(f"Failed to terminate process {pid}: {e}")
|
||||||
|
|
||||||
|
|
||||||
@allure.step('System execute command')
|
@allure.step('System execute command')
|
||||||
|
|
Loading…
Reference in New Issue