-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconftest.py
115 lines (87 loc) · 2.94 KB
/
conftest.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from __future__ import annotations
import os
import socket
import string
import subprocess
import sys
import time as ttime
import pytest
from srx_caproto_iocs.base import OphydDeviceWithCaprotoIOC
from srx_caproto_iocs.example.ophyd import OphydChannelTypes
CAPROTO_PV_PREFIX = "BASE:{{Dev:Save1}}:"
OPHYD_PV_PREFIX = CAPROTO_PV_PREFIX.replace("{{", "{").replace("}}", "}")
def get_epics_env():
first_three = ".".join(socket.gethostbyname(socket.gethostname()).split(".")[:3])
broadcast = f"{first_three}.255"
print(f"{broadcast = }")
# from pprint import pformat
# import netifaces
# interfaces = netifaces.interfaces()
# print(f"{interfaces = }")
# for interface in interfaces:
# addrs = netifaces.ifaddresses(interface)
# try:
# print(f"{interface = }: {pformat(addrs[netifaces.AF_INET])}")
# except Exception as e:
# print(f"{interface = }: exception:\n {e}")
return {
"EPICS_CAS_BEACON_ADDR_LIST": os.getenv("EPICS_CA_ADDR_LIST", broadcast),
"EPICS_CAS_AUTO_BEACON_ADDR_LIST": "no",
}
def start_ioc_subprocess(ioc_name="srx_caproto_iocs.base", pv_prefix=CAPROTO_PV_PREFIX):
env = get_epics_env()
command = f"{sys.executable} -m {ioc_name} --prefix={pv_prefix} --list-pvs"
print(
f"\nStarting caproto IOC in via a fixture using the following command:\n\n {command}\n"
)
os.environ.update(env)
return subprocess.Popen(
command.split(),
start_new_session=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=False,
env=os.environ,
)
@pytest.fixture(scope="session")
def base_caproto_ioc(wait=5):
p = start_ioc_subprocess(
ioc_name="srx_caproto_iocs.base", pv_prefix=CAPROTO_PV_PREFIX
)
print(f"Wait for {wait} seconds...")
ttime.sleep(wait)
yield p
p.terminate()
std_out, std_err = p.communicate()
std_out = std_out.decode()
sep = "=" * 80
print(f"STDOUT:\n{sep}\n{std_out}")
print(f"STDERR:\n{sep}\n{std_err}")
@pytest.fixture()
def base_ophyd_device():
dev = OphydDeviceWithCaprotoIOC(
OPHYD_PV_PREFIX, name="ophyd_device_with_caproto_ioc"
)
yield dev
dev.ioc_stage.put("unstaged")
@pytest.fixture(scope="session")
def caproto_ioc_channel_types(wait=5):
p = start_ioc_subprocess(
ioc_name="srx_caproto_iocs.example.caproto_ioc", pv_prefix=CAPROTO_PV_PREFIX
)
print(f"Wait for {wait} seconds...")
ttime.sleep(wait)
yield p
p.terminate()
std_out, std_err = p.communicate()
std_out = std_out.decode()
sep = "=" * 80
print(f"STDOUT:\n{sep}\n{std_out}")
print(f"STDERR:\n{sep}\n{std_err}")
@pytest.fixture()
def ophyd_channel_types():
dev = OphydChannelTypes(OPHYD_PV_PREFIX, name="ophyd_channel_type")
letters = iter(string.ascii_letters)
for cpt in sorted(dev.component_names):
getattr(dev, cpt).put(next(letters))
return dev