|
| 1 | +import pytest |
| 2 | +import socket |
| 3 | + |
| 4 | +from labgrid.driver import Eth008DigitalOutputDriver |
| 5 | +from labgrid.resource import Eth008DigitalOutput |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture(scope="function") |
| 9 | +def mock_tcp_server(mocker): |
| 10 | + """Mock the ETH008 TCP server responses.""" |
| 11 | + state = 0x00 # Initial state: all relays OFF |
| 12 | + |
| 13 | + # Track sent commands for verification |
| 14 | + sent_commands = [] |
| 15 | + |
| 16 | + def mock_sendall(data): |
| 17 | + """Mock socket.sendall.""" |
| 18 | + nonlocal state, sent_commands |
| 19 | + |
| 20 | + sent_commands.append(data) |
| 21 | + |
| 22 | + if len(data) == 1 and data[0] == 0x24: |
| 23 | + return |
| 24 | + elif len(data) == 3: |
| 25 | + command = data[0] |
| 26 | + relay_index = data[1] |
| 27 | + |
| 28 | + if command == 0x20: # Digital Active (ON) |
| 29 | + state |= (1 << (relay_index - 1)) |
| 30 | + elif command == 0x21: # Digital Inactive (OFF) |
| 31 | + state &= ~(1 << (relay_index - 1)) |
| 32 | + |
| 33 | + def mock_recv(bufsize): |
| 34 | + """Mock socket.recv to return appropriate responses.""" |
| 35 | + nonlocal state, sent_commands |
| 36 | + |
| 37 | + last_sent = sent_commands[-1] if sent_commands else None |
| 38 | + |
| 39 | + if last_sent and len(last_sent) == 1 and last_sent[0] == 0x24: |
| 40 | + return bytes([state]) |
| 41 | + else: |
| 42 | + return b'\x00' |
| 43 | + |
| 44 | + mock_socket = mocker.MagicMock() |
| 45 | + mock_socket.sendall.side_effect = mock_sendall |
| 46 | + mock_socket.recv.side_effect = mock_recv |
| 47 | + mock_socket.__enter__ = mocker.MagicMock(return_value=mock_socket) |
| 48 | + mock_socket.__exit__ = mocker.MagicMock(return_value=None) |
| 49 | + |
| 50 | + def mock_socket_factory(*args, **kwargs): |
| 51 | + return mock_socket |
| 52 | + |
| 53 | + mock_socket_class = mocker.patch("socket.socket") |
| 54 | + mock_socket_class.side_effect = mock_socket_factory |
| 55 | + mock_socket_class.return_value = mock_socket |
| 56 | + |
| 57 | + return mock_socket |
| 58 | + |
| 59 | + |
| 60 | +def _make_eth008_driver(target, index, invert): |
| 61 | + """Helper function to create an ETH008 digital output driver with resource.""" |
| 62 | + eth008_res = Eth008DigitalOutput( |
| 63 | + target, |
| 64 | + name=None, |
| 65 | + host='192.168.1.100', |
| 66 | + index=str(index), |
| 67 | + invert=invert |
| 68 | + ) |
| 69 | + |
| 70 | + driver = Eth008DigitalOutputDriver(target, name=None) |
| 71 | + target.activate(driver) |
| 72 | + |
| 73 | + return driver |
| 74 | + |
| 75 | + |
| 76 | +def test_eth008_instance(target, mocker): |
| 77 | + """Test that Eth008DigitalOutputDriver can be instantiated and activated.""" |
| 78 | + mocker.patch("socket.socket") |
| 79 | + driver = _make_eth008_driver(target, 1, False) |
| 80 | + assert isinstance(driver, Eth008DigitalOutputDriver) |
| 81 | + |
| 82 | + |
| 83 | +def test_eth008_set_true(target, mock_tcp_server): |
| 84 | + """Test setting ETH008 relay to True (asserted).""" |
| 85 | + driver = _make_eth008_driver(target, 1, False) |
| 86 | + |
| 87 | + driver.set(True) |
| 88 | + |
| 89 | + # Verify the correct command was sent (0x20 = Digital Active, 0x01 = relay 1, 0x00 = permanent) |
| 90 | + mock_tcp_server.sendall.assert_any_call(bytes([0x20, 0x01, 0x00])) |
| 91 | + |
| 92 | + |
| 93 | +def test_eth008_set_false(target, mock_tcp_server): |
| 94 | + """Test setting ETH008 relay to False (de-asserted).""" |
| 95 | + driver = _make_eth008_driver(target, 2, False) |
| 96 | + |
| 97 | + driver.set(False) |
| 98 | + |
| 99 | + # Verify the correct command was sent (0x21 = Digital Inactive, 0x02 = relay 2, 0x00 = permanent) |
| 100 | + mock_tcp_server.sendall.assert_any_call(bytes([0x21, 0x02, 0x00])) |
| 101 | + |
| 102 | + |
| 103 | +def test_eth008_get_true(target, mock_tcp_server): |
| 104 | + """Test getting ETH008 relay state when True.""" |
| 105 | + driver = _make_eth008_driver(target, 1, False) |
| 106 | + |
| 107 | + # Set the relay to True first |
| 108 | + driver.set(True) |
| 109 | + |
| 110 | + # Get the state |
| 111 | + result = driver.get() |
| 112 | + |
| 113 | + assert result is True |
| 114 | + |
| 115 | + |
| 116 | +def test_eth008_get_false(target, mock_tcp_server): |
| 117 | + """Test getting ETH008 relay state when False.""" |
| 118 | + driver = _make_eth008_driver(target, 2, False) |
| 119 | + |
| 120 | + # Set the relay to False first |
| 121 | + driver.set(False) |
| 122 | + |
| 123 | + # Get the state |
| 124 | + result = driver.get() |
| 125 | + |
| 126 | + assert result is False |
| 127 | + |
| 128 | + |
| 129 | +def test_eth008_invert_true(target, mock_tcp_server): |
| 130 | + """Test ETH008 with inversion enabled.""" |
| 131 | + driver = _make_eth008_driver(target, 1, True) # Invert the logic |
| 132 | + |
| 133 | + # When invert=True, setting True should actually set the relay to False |
| 134 | + driver.set(True) |
| 135 | + |
| 136 | + # Verify that 0x21 (Digital Inactive) was sent instead of 0x20 (Digital Active) |
| 137 | + mock_tcp_server.sendall.assert_any_call(bytes([0x21, 0x01, 0x00])) |
| 138 | + |
| 139 | + |
| 140 | +def test_eth008_invert_get(target, mock_tcp_server): |
| 141 | + """Test getting ETH008 relay state with inversion enabled.""" |
| 142 | + driver = _make_eth008_driver(target, 1, True) # Invert the logic |
| 143 | + |
| 144 | + # Set the relay to True (which will actually set it to False due to inversion) |
| 145 | + driver.set(True) |
| 146 | + |
| 147 | + # When invert=True, get() should return the opposite of the actual state |
| 148 | + result = driver.get() |
| 149 | + |
| 150 | + # The actual relay state is False, but with inversion it should return True |
| 151 | + assert result is True |
| 152 | + |
| 153 | + |
| 154 | +def test_eth008_invalid_index(target, mocker): |
| 155 | + """Test that invalid relay indices are handled correctly.""" |
| 156 | + mocker.patch("socket.socket") |
| 157 | + |
| 158 | + driver = _make_eth008_driver(target, 9, False) # Invalid index (must be 1-8) |
| 159 | + |
| 160 | + # This should raise an AssertionError due to invalid index |
| 161 | + with pytest.raises(AssertionError): |
| 162 | + driver.set(True) |
| 163 | + |
| 164 | + |
| 165 | +def test_eth008_different_indices(target, mock_tcp_server): |
| 166 | + """Test that different relay indices work correctly.""" |
| 167 | + # Test first, middle, and last relays |
| 168 | + # Create separate targets for each test to avoid resource conflicts |
| 169 | + from labgrid.target import Target |
| 170 | + for index in [1, 4, 8]: |
| 171 | + test_target = Target(name=f'Test-{index}', env=None) |
| 172 | + |
| 173 | + driver = _make_eth008_driver(test_target, index, False) |
| 174 | + |
| 175 | + # Set and get should work for all indices |
| 176 | + driver.set(True) |
| 177 | + result = driver.get() |
| 178 | + |
| 179 | + assert result is True |
0 commit comments