-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
73 lines (54 loc) · 1.56 KB
/
Copy pathconftest.py
File metadata and controls
73 lines (54 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# SPDX-License-Identifier: Apache-2.0
import os
import signal
import time
from typing import Any
import pytest
from rotel import Config
from tests.utils_server import MockServer
PID_FILE = "/tmp/rotel-agent.pid"
@pytest.fixture(scope="function", autouse=True)
def restore_environment() -> Any:
orig_environ = dict(os.environ)
yield
os.environ.clear()
os.environ.update(orig_environ)
@pytest.fixture(scope="function", autouse=True)
def stop_agent() -> Any:
yield
pid_file = get_pid_file()
try:
with open(pid_file) as file:
line = file.readline()
pid = int(line)
os.kill(pid, signal.SIGTERM)
# wait up to 2.5secs for this exit
time.sleep(0.5)
for i in range(4):
try:
# is this portable?
os.kill(pid, 0)
except OSError:
break
else:
time.sleep(0.5)
except ProcessLookupError:
pass # In multi-worker configs, the process may have already terminated
except FileNotFoundError:
pass
@pytest.fixture(scope="function", autouse=True)
def cleanup_agent() -> Any:
yield
# keep the log file for debugging?
rm_file(get_pid_file())
@pytest.fixture(scope="function", autouse=True)
def reset_tracker() -> Any:
yield
MockServer.reset_count()
def rm_file(file_path):
try:
os.remove(file_path)
except FileNotFoundError:
pass
def get_pid_file() -> Any:
return Config.DEFAULT_OPTIONS['pid_file']