|
| 1 | +import contextlib |
| 2 | +import io |
| 3 | +import pytest |
| 4 | +import subprocess |
| 5 | +from unittest.mock import MagicMock |
| 6 | +from unittest.mock import Mock |
| 7 | +from unittest.mock import patch |
| 8 | + |
| 9 | +from labgrid.remote.exporter import USBJLinkExport |
| 10 | +from labgrid.resource.remote import NetworkJLinkDevice |
| 11 | +from labgrid.resource.udev import JLinkDevice |
| 12 | +from labgrid.driver.jlinkdriver import JLinkDriver |
| 13 | + |
| 14 | +FAKE_SERIAL = 123456789 |
| 15 | +MATCH = {"ID_SERIAL_SHORT": f"000{FAKE_SERIAL}"} |
| 16 | + |
| 17 | + |
| 18 | +class Popen_mock(): |
| 19 | + """Mock of Popen object to mimmic JLinkRemoteServer output""" |
| 20 | + |
| 21 | + def __init__(self, args, **kwargs): |
| 22 | + assert "JLinkRemoteServer" in args[0] |
| 23 | + assert args[1] == "-Port" |
| 24 | + # Since args[2] is dynamic do not check it |
| 25 | + assert args[3] == "-select" |
| 26 | + assert args[4] == f"USB={FAKE_SERIAL}" |
| 27 | + self.wait_called = False |
| 28 | + |
| 29 | + stdout = io.StringIO( |
| 30 | + "SEGGER J-Link Remote Server V7.84a\n" |
| 31 | + "Compiled Dec 22 2022 16:13:52\n" |
| 32 | + "\n" |
| 33 | + "'q' to quit '?' for help\n" |
| 34 | + "\n" |
| 35 | + f"Connected to J-Link with S/N {FAKE_SERIAL}\n" |
| 36 | + "\n" |
| 37 | + "Waiting for client connections...\n" |
| 38 | + ) |
| 39 | + |
| 40 | + def communicate(self, input=None, timeout=None): |
| 41 | + # Only timeout on the first call to exercise the error handling code. |
| 42 | + if not self.wait_called: |
| 43 | + self.wait_called = True |
| 44 | + raise subprocess.TimeoutExpired("JLinkRemoteServer", timeout) |
| 45 | + |
| 46 | + def kill(self): |
| 47 | + pass |
| 48 | + |
| 49 | + def poll(self): |
| 50 | + return 0 |
| 51 | + |
| 52 | + def terminate(self): |
| 53 | + pass |
| 54 | + |
| 55 | + |
| 56 | +def test_jlink_resource(target): |
| 57 | + r = JLinkDevice(target, name=None, match=MATCH) |
| 58 | + |
| 59 | + |
| 60 | +@patch('subprocess.Popen', Popen_mock) |
| 61 | +def test_jlink_export_start(target): |
| 62 | + config = {'avail': True, 'cls': "JLinkDevice", 'params': {'match': MATCH}, } |
| 63 | + e = USBJLinkExport(config) |
| 64 | + e.local.avail = True |
| 65 | + e.local.serial = FAKE_SERIAL |
| 66 | + |
| 67 | + e.start() |
| 68 | + # Exercise the __del__ method which also exercises stop() |
| 69 | + del e |
| 70 | + |
| 71 | + |
| 72 | +@patch('subprocess.Popen', Popen_mock) |
| 73 | +def test_jlink_driver(target): |
| 74 | + pytest.importorskip("pylink") |
| 75 | + device = JLinkDevice(target, name=None, match=MATCH) |
| 76 | + device.avail = True |
| 77 | + device.serial = FAKE_SERIAL |
| 78 | + driver = JLinkDriver(target, name=None) |
| 79 | + |
| 80 | + with patch('pylink.JLink') as JLinkMock: |
| 81 | + instance = JLinkMock.return_value |
| 82 | + target.activate(driver) |
| 83 | + instance.open.assert_called_once_with(serial_no=FAKE_SERIAL) |
| 84 | + intf = driver.get_interface() |
| 85 | + assert(isinstance(intf, Mock)) |
| 86 | + target.deactivate(driver) |
| 87 | + instance.close.assert_called_once_with() |
| 88 | + |
| 89 | + |
| 90 | +@patch('subprocess.Popen', Popen_mock) |
| 91 | +def test_jlink_driver_network_device(target): |
| 92 | + pytest.importorskip("pylink") |
| 93 | + device = NetworkJLinkDevice(target, None, host='127.0.1.1', port=12345, busnum=0, devnum=1, path='0:1', vendor_id=0x0, model_id=0x0,) |
| 94 | + device.avail = True |
| 95 | + driver = JLinkDriver(target, name=None) |
| 96 | + assert (isinstance(driver, JLinkDriver)) |
| 97 | + |
| 98 | + with patch('pylink.JLink') as JLinkMock: |
| 99 | + instance = JLinkMock.return_value |
| 100 | + # Call on_activate directly since activating the driver via the target does not work during testing |
| 101 | + driver.on_activate() |
| 102 | + instance.open.assert_called_once_with(ip_addr='127.0.0.1:12345') |
| 103 | + driver.on_deactivate() |
| 104 | + instance.close.assert_called_once_with() |
0 commit comments