| 1 |
import logging |
| 2 |
from signal import SIGTERM |
| 3 |
import sys |
| 4 |
import threading |
| 5 |
|
| 6 |
import pytest |
| 7 |
import pexpect |
| 8 |
|
| 9 |
from labgrid import Target |
| 10 |
from labgrid.driver import SerialDriver |
| 11 |
from labgrid.resource import RawSerialPort, NetworkSerialPort |
| 12 |
from labgrid.driver.fake import FakeConsoleDriver |
| 13 |
|
| 14 |
psutil = pytest.importorskip("psutil") |
| 15 |
|
| 16 |
@pytest.fixture(scope="session") |
| 17 |
def curses_init(): |
| 18 |
""" curses only reads the terminfo DB once on the first import, so make |
| 19 |
sure we prime it correctly.""" |
| 20 |
try: |
| 21 |
import curses |
| 22 |
curses.setupterm("linux") |
| 23 |
except ModuleNotFoundError: |
| 24 |
logging.warning("curses module not found, not setting up a default terminal – tests may fail") |
| 25 |
|
| 26 |
|
| 27 |
class Prefixer: |
| 28 |
def __init__(self, wrapped, prefix): |
| 29 |
self.__wrapped = wrapped |
| 30 |
self.__prefix = prefix.encode()+b": " |
| 31 |
self.__continuation = False |
| 32 |
|
| 33 |
def write(self, data): |
| 34 |
if data[-1:] == b'\n': |
| 35 |
continuation = False |
| 36 |
data = data[:-1] |
| 37 |
else: |
| 38 |
continuation = True |
| 39 |
data = data.replace(b'\n', b'\n'+self.__prefix) |
| 40 |
if not self.__continuation: |
| 41 |
data = self.__prefix+data |
| 42 |
if not continuation: |
| 43 |
data += b'\n' |
| 44 |
self.__continuation = continuation |
| 45 |
self.__wrapped.write(data) |
| 46 |
|
| 47 |
def __getattr__(self, name): |
| 48 |
return getattr(self.__wrapped, name) |
| 49 |
|
| 50 |
|
| 51 |
class LabgridComponent: |
| 52 |
def __init__(self, cwd): |
| 53 |
self.cwd = str(cwd) |
| 54 |
self.spawn = None |
| 55 |
self.reader = None |
| 56 |
|
| 57 |
def stop(self): |
| 58 |
logging.info("stopping %s pid=%s", self.__class__.__name__, self.spawn.pid) |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
self.spawn.kill(SIGTERM) |
| 63 |
if not self.spawn.closed: |
| 64 |
self.spawn.expect(pexpect.EOF) |
| 65 |
self.spawn.wait() |
| 66 |
assert not self.spawn.isalive() |
| 67 |
|
| 68 |
self.spawn = None |
| 69 |
self.stop_reader() |
| 70 |
|
| 71 |
@staticmethod |
| 72 |
def keep_reading(spawn): |
| 73 |
"The output from background processes must be read to avoid blocking them." |
| 74 |
while spawn.isalive(): |
| 75 |
try: |
| 76 |
data = spawn.read_nonblocking(size=1024, timeout=0.1) |
| 77 |
if not data: |
| 78 |
return |
| 79 |
except pexpect.TIMEOUT: |
| 80 |
continue |
| 81 |
except pexpect.EOF: |
| 82 |
return |
| 83 |
except OSError: |
| 84 |
return |
| 85 |
|
| 86 |
def start_reader(self): |
| 87 |
self.reader = threading.Thread( |
| 88 |
target=LabgridComponent.keep_reading, |
| 89 |
name=f'{self.__class__.__name__}-reader-{self.pid}', |
| 90 |
args=(self.spawn,), daemon=True) |
| 91 |
self.reader.start() |
| 92 |
|
| 93 |
def stop_reader(self): |
| 94 |
self.reader.join() |
| 95 |
|
| 96 |
self.reader = None |
| 97 |
|
| 98 |
def isalive(self): |
| 99 |
return self.spawn.isalive() |
| 100 |
|
| 101 |
@property |
| 102 |
def exitstatus(self): |
| 103 |
return self.spawn.exitstatus |
| 104 |
|
| 105 |
@property |
| 106 |
def pid(self): |
| 107 |
return self.spawn.pid |
| 108 |
|
| 109 |
def suspend_tree(self): |
| 110 |
main = psutil.Process(self.pid) |
| 111 |
main.suspend() |
| 112 |
for child in main.children(recursive=True): |
| 113 |
child.suspend() |
| 114 |
|
| 115 |
def resume_tree(self): |
| 116 |
main = psutil.Process(self.pid) |
| 117 |
main.resume() |
| 118 |
for child in main.children(recursive=True): |
| 119 |
child.resume() |
| 120 |
|
| 121 |
|
| 122 |
class Exporter(LabgridComponent): |
| 123 |
def __init__(self, config, cwd): |
| 124 |
super().__init__(cwd) |
| 125 |
self.config = config |
| 126 |
|
| 127 |
def start(self): |
| 128 |
assert self.spawn is None |
| 129 |
assert self.reader is None |
| 130 |
|
| 131 |
self.spawn = pexpect.spawn( |
| 132 |
f'python -m labgrid.remote.exporter --name testhost {self.config}', |
| 133 |
logfile=Prefixer(sys.stdout.buffer, 'exporter'), |
| 134 |
cwd=self.cwd) |
| 135 |
try: |
| 136 |
self.spawn.expect('exporter name: testhost') |
| 137 |
self.spawn.expect('connected to coordinator') |
| 138 |
except Exception as e: |
| 139 |
raise Exception(f"exporter startup failed with {self.spawn.before}") from e |
| 140 |
|
| 141 |
self.start_reader() |
| 142 |
|
| 143 |
|
| 144 |
class Coordinator(LabgridComponent): |
| 145 |
def start(self): |
| 146 |
assert self.spawn is None |
| 147 |
assert self.reader is None |
| 148 |
|
| 149 |
self.spawn = pexpect.spawn( |
| 150 |
'python -m labgrid.remote.coordinator', |
| 151 |
logfile=Prefixer(sys.stdout.buffer, 'coordinator'), |
| 152 |
cwd=self.cwd) |
| 153 |
try: |
| 154 |
self.spawn.expect('Coordinator ready') |
| 155 |
except Exception as e: |
| 156 |
raise Exception(f"coordinator startup failed with {self.spawn.before}") from e |
| 157 |
|
| 158 |
self.start_reader() |
| 159 |
|
| 160 |
|
| 161 |
@pytest.fixture(scope='function') |
| 162 |
def target(): |
| 163 |
return Target('Test') |
| 164 |
|
| 165 |
@pytest.fixture(scope='function') |
| 166 |
def target_with_fakeconsole(): |
| 167 |
t = Target('dummy') |
| 168 |
cp = FakeConsoleDriver(t, "console") |
| 169 |
return t |
| 170 |
|
| 171 |
@pytest.fixture(scope='function') |
| 172 |
def serial_port(target): |
| 173 |
return RawSerialPort(target, 'serial', '/dev/test') |
| 174 |
|
| 175 |
@pytest.fixture(scope='function') |
| 176 |
def serial_rfc2711_port(target): |
| 177 |
return NetworkSerialPort(target, 'rfc2711', host='localhost', port=8888) |
| 178 |
|
| 179 |
@pytest.fixture(scope='function') |
| 180 |
def serial_raw_port(target): |
| 181 |
return NetworkSerialPort(target, 'serialraw', host='localhost', port=8888, protocol="raw") |
| 182 |
|
| 183 |
|
| 184 |
@pytest.fixture(scope='function') |
| 185 |
def serial_driver(target, serial_port, mocker): |
| 186 |
m = mocker.patch('serial.Serial') |
| 187 |
s = SerialDriver(target, 'serial') |
| 188 |
target.activate(s) |
| 189 |
return s |
| 190 |
|
| 191 |
@pytest.fixture(scope='function') |
| 192 |
def serial_driver_no_name(target, serial_port, mocker): |
| 193 |
m = mocker.patch('serial.Serial') |
| 194 |
s = SerialDriver(target, None) |
| 195 |
target.activate(s) |
| 196 |
return s |
| 197 |
|
| 198 |
@pytest.fixture(scope='function') |
| 199 |
def coordinator(tmpdir): |
| 200 |
coordinator = Coordinator(tmpdir) |
| 201 |
coordinator.start() |
| 202 |
|
| 203 |
yield coordinator |
| 204 |
|
| 205 |
coordinator.stop() |
| 206 |
|
| 207 |
@pytest.fixture(scope='function') |
| 208 |
def exporter(tmpdir, coordinator): |
| 209 |
config = "exports.yaml" |
| 210 |
p = tmpdir.join(config) |
| 211 |
p.write( |
| 212 |
""" |
| 213 |
Testport: |
| 214 |
NetworkSerialPort: |
| 215 |
host: 'localhost' |
| 216 |
port: 4000 |
| 217 |
Broken: |
| 218 |
RawSerialPort: |
| 219 |
port: 'none' |
| 220 |
Many: |
| 221 |
NetworkSerialPort: |
| 222 |
host: 'localhost' |
| 223 |
port: 4000 |
| 224 |
NetworkService: |
| 225 |
address: "192.168.0.1" |
| 226 |
username: "root" |
| 227 |
""" |
| 228 |
) |
| 229 |
|
| 230 |
exporter = Exporter(config, tmpdir) |
| 231 |
exporter.start() |
| 232 |
|
| 233 |
yield exporter |
| 234 |
|
| 235 |
exporter.stop() |
| 236 |
|
| 237 |
def pytest_addoption(parser): |
| 238 |
parser.addoption("--sigrok-usb", action="store_true", |
| 239 |
help="Run sigrok usb tests with fx2lafw device (0925:3881)") |
| 240 |
parser.addoption("--local-sshmanager", action="store_true", |
| 241 |
help="Run SSHManager tests against localhost") |
| 242 |
parser.addoption("--ssh-username", default=None, |
| 243 |
help="SSH username to use for SSHDriver testing") |
| 244 |
|
| 245 |
def pytest_configure(config): |
| 246 |
|
| 247 |
config.addinivalue_line("markers", |
| 248 |
"sigrokusb: enable fx2lafw USB tests (0925:3881)") |
| 249 |
config.addinivalue_line("markers", |
| 250 |
"localsshmanager: test SSHManager against Localhost") |
| 251 |
config.addinivalue_line("markers", |
| 252 |
"sshusername: test SSHDriver against Localhost") |
| 253 |
config.addinivalue_line("markers", |
| 254 |
"coordinator: test against local coordinator") |
| 255 |
|
| 256 |
def pytest_runtest_setup(item): |
| 257 |
envmarker = item.get_closest_marker("sigrokusb") |
| 258 |
if envmarker is not None: |
| 259 |
if item.config.getoption("--sigrok-usb") is False: |
| 260 |
pytest.skip("sigrok usb tests not enabled (enable with --sigrok-usb)") |
| 261 |
envmarker = item.get_closest_marker("localsshmanager") |
| 262 |
if envmarker is not None: |
| 263 |
if item.config.getoption("--local-sshmanager") is False: |
| 264 |
pytest.skip("SSHManager tests against localhost not enabled (enable with --local-sshmanager)") |
| 265 |
envmarker = item.get_closest_marker("sshusername") |
| 266 |
if envmarker is not None: |
| 267 |
if item.config.getoption("--ssh-username") is None: |
| 268 |
pytest.skip("SSHDriver tests against localhost not enabled (enable with --ssh-username <username>)") |