| 1 |
import asyncio |
| 2 |
import os |
| 3 |
import sys |
| 4 |
import time |
| 5 |
|
| 6 |
import pytest |
| 7 |
import pytest_asyncio |
| 8 |
|
| 9 |
from cockpit.channel import ChannelError |
| 10 |
from cockpit.packages import BridgeConfig |
| 11 |
from cockpit.peer import ConfiguredPeer, PeerRoutingRule |
| 12 |
from cockpit.protocol import CockpitProtocolError |
| 13 |
from cockpit.router import Router |
| 14 |
from cockpit.transports import SubprocessTransport |
| 15 |
|
| 16 |
from . import mockpeer |
| 17 |
from .mocktransport import MockTransport |
| 18 |
|
| 19 |
PEER_CONFIG = BridgeConfig({ |
| 20 |
"spawn": [sys.executable, mockpeer.__file__], |
| 21 |
"environ": ['PYTHONPATH=' + ':'.join(sys.path)], |
| 22 |
"match": {"payload": "test"}, |
| 23 |
}) |
| 24 |
|
| 25 |
|
| 26 |
class Bridge(Router): |
| 27 |
init_host = 'localhost' |
| 28 |
|
| 29 |
def __init__(self): |
| 30 |
rule = PeerRoutingRule(self, PEER_CONFIG) |
| 31 |
super().__init__([rule]) |
| 32 |
|
| 33 |
def do_send_init(self): |
| 34 |
pass |
| 35 |
|
| 36 |
|
| 37 |
@pytest.fixture |
| 38 |
def bridge(): |
| 39 |
return Bridge() |
| 40 |
|
| 41 |
|
| 42 |
# requires an event loop for queue in MockTransport |
| 43 |
@pytest_asyncio.fixture |
| 44 |
async def transport(bridge): # ruff: ignore[unused-async] |
| 45 |
return MockTransport(bridge) |
| 46 |
|
| 47 |
|
| 48 |
@pytest.fixture |
| 49 |
def rule(bridge): |
| 50 |
return bridge.routing_rules[0] |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.asyncio |
| 54 |
async def test_shutdown(transport, rule): |
| 55 |
await transport.check_open('test') |
| 56 |
await transport.check_open('xest', problem='not-supported') |
| 57 |
|
| 58 |
# Force the Peer closed |
| 59 |
rule.peer.close() |
| 60 |
await transport.assert_msg('', command='close', channel='channel.1', problem='terminated') |
| 61 |
|
| 62 |
# But it should spawn again |
| 63 |
await transport.check_open('test') |
| 64 |
await transport.check_open('xest', problem='not-supported') |
| 65 |
rule.peer.close() |
| 66 |
|
| 67 |
|
| 68 |
@pytest.mark.asyncio |
| 69 |
@pytest.mark.parametrize('init_type', ['wrong-command', 'channel-control', 'data', 'break-protocol']) |
| 70 |
async def test_init_failure(rule, init_type, monkeypatch, transport): |
| 71 |
monkeypatch.setenv('INIT_TYPE', init_type) |
| 72 |
await transport.check_open('test', problem='protocol-error') |
| 73 |
|
| 74 |
|
| 75 |
# this doesn't use await, but requires an event loop |
| 76 |
@pytest.mark.asyncio |
| 77 |
async def test_immediate_shutdown(rule): # ruff: ignore[unused-async] |
| 78 |
peer = rule.apply_rule({'payload': 'test'}) |
| 79 |
assert peer is not None |
| 80 |
peer.close() |
| 81 |
|
| 82 |
|
| 83 |
@pytest.mark.asyncio |
| 84 |
async def test_shutdown_before_init(monkeypatch, transport, rule): |
| 85 |
monkeypatch.setenv('INIT_TYPE', 'silence') |
| 86 |
channel = transport.send_open('test') |
| 87 |
assert rule.peer is not None |
| 88 |
assert rule.peer.transport is None |
| 89 |
while rule.peer.transport is None: |
| 90 |
await asyncio.sleep(0) |
| 91 |
rule.peer.close() |
| 92 |
await transport.assert_msg('', command='close', channel=channel, problem='terminated') |
| 93 |
|
| 94 |
|
| 95 |
@pytest.mark.asyncio |
| 96 |
async def test_exit_without_init(monkeypatch, transport): |
| 97 |
monkeypatch.setenv('INIT_TYPE', 'exit') |
| 98 |
await transport.check_open('test', problem='terminated') |
| 99 |
|
| 100 |
|
| 101 |
@pytest.mark.asyncio |
| 102 |
async def test_exit_not_found(monkeypatch, transport): |
| 103 |
monkeypatch.setenv('INIT_TYPE', 'exit-not-found') |
| 104 |
await transport.check_open('test', problem='no-cockpit') |
| 105 |
|
| 106 |
|
| 107 |
@pytest.mark.asyncio |
| 108 |
async def test_killed(monkeypatch, transport, rule): |
| 109 |
channel = await transport.check_open('test') |
| 110 |
os.kill(rule.peer.transport._process.pid, 9) |
| 111 |
await transport.assert_msg('', command='close', channel=channel, problem='terminated') |
| 112 |
|
| 113 |
|
| 114 |
@pytest.mark.asyncio |
| 115 |
@pytest.mark.parametrize('init_type', ['wrong-command', 'channel-control', 'data', 'break-protocol']) |
| 116 |
async def test_await_failure(init_type, monkeypatch, bridge): |
| 117 |
monkeypatch.setenv('INIT_TYPE', init_type) |
| 118 |
peer = ConfiguredPeer(bridge, PEER_CONFIG) |
| 119 |
with pytest.raises(CockpitProtocolError): |
| 120 |
await peer.start() |
| 121 |
peer.close() |
| 122 |
|
| 123 |
|
| 124 |
@pytest.mark.asyncio |
| 125 |
async def test_await_broken_connect(bridge): |
| 126 |
class BrokenConnect(ConfiguredPeer): |
| 127 |
async def do_connect_transport(self): |
| 128 |
_ = 42 / 0 |
| 129 |
|
| 130 |
peer = BrokenConnect(bridge, PEER_CONFIG) |
| 131 |
with pytest.raises(ZeroDivisionError): |
| 132 |
await peer.start() |
| 133 |
peer.close() |
| 134 |
|
| 135 |
|
| 136 |
@pytest.mark.asyncio |
| 137 |
async def test_await_broken_after_connect(bridge): |
| 138 |
class BrokenConnect(ConfiguredPeer): |
| 139 |
async def do_connect_transport(self): |
| 140 |
await super().do_connect_transport() |
| 141 |
_ = 42 / 0 |
| 142 |
|
| 143 |
peer = BrokenConnect(bridge, PEER_CONFIG) |
| 144 |
with pytest.raises(ZeroDivisionError): |
| 145 |
await peer.start() |
| 146 |
peer.close() |
| 147 |
|
| 148 |
|
| 149 |
class CancellableConnect(ConfiguredPeer): |
| 150 |
was_cancelled = False |
| 151 |
|
| 152 |
async def do_connect_transport(self): |
| 153 |
await super().do_connect_transport() |
| 154 |
try: |
| 155 |
# We should get cancelled here when the mockpeer sends "init" |
| 156 |
await asyncio.sleep(10000) |
| 157 |
except asyncio.CancelledError: |
| 158 |
self.was_cancelled = True |
| 159 |
raise |
| 160 |
|
| 161 |
|
| 162 |
@pytest.mark.asyncio |
| 163 |
async def test_await_cancellable_connect_init(bridge): |
| 164 |
peer = CancellableConnect(bridge, PEER_CONFIG) |
| 165 |
await peer.start() |
| 166 |
peer.close() |
| 167 |
while len(asyncio.all_tasks()) > 1: |
| 168 |
await asyncio.sleep(0.1) |
| 169 |
assert peer.was_cancelled |
| 170 |
|
| 171 |
|
| 172 |
@pytest.mark.asyncio |
| 173 |
async def test_await_cancellable_connect_close(monkeypatch, bridge): |
| 174 |
monkeypatch.setenv('INIT_TYPE', 'silence') # make sure we never get "init" |
| 175 |
peer = CancellableConnect(bridge, PEER_CONFIG) |
| 176 |
asyncio.get_running_loop().call_later(0.1, peer.close) # call peer.close() after .start() is running |
| 177 |
with pytest.raises(asyncio.CancelledError): |
| 178 |
await peer.start() |
| 179 |
# we already called .close() |
| 180 |
while len(asyncio.all_tasks()) > 1: |
| 181 |
await asyncio.sleep(0.1) |
| 182 |
assert peer.was_cancelled |
| 183 |
|
| 184 |
|
| 185 |
@pytest.mark.asyncio |
| 186 |
async def test_spawn_broken_pipe(bridge): |
| 187 |
class BrokenPipePeer(ConfiguredPeer): |
| 188 |
def __init__(self, *, specific_error=False): |
| 189 |
super().__init__(bridge, PEER_CONFIG) |
| 190 |
self.specific_error = specific_error |
| 191 |
|
| 192 |
async def do_connect_transport(self) -> None: |
| 193 |
transport = await self.spawn(['sh', '-c', 'read a; exit 9'], ()) |
| 194 |
assert isinstance(transport, SubprocessTransport) |
| 195 |
# Make the process exit by writing a newline (causing `read` to finish) |
| 196 |
transport.write(b'\n') |
| 197 |
# The process will exit soon — try writing to it until a write fails. |
| 198 |
while not transport.is_closing(): |
| 199 |
transport.write(b'x') |
| 200 |
time.sleep(0.1) |
| 201 |
while transport.get_returncode() is None: |
| 202 |
await asyncio.sleep(0.1) |
| 203 |
if self.specific_error: |
| 204 |
raise ChannelError('not-supported', message='kaputt') |
| 205 |
|
| 206 |
# BrokenPipe bubbles up without an error returned by do_connect_transport |
| 207 |
peer = BrokenPipePeer(specific_error=False) |
| 208 |
with pytest.raises(BrokenPipeError): |
| 209 |
await peer.start() |
| 210 |
peer.close() |
| 211 |
|
| 212 |
# BrokenPipe gets trumped by specific error returned by do_connect_transport |
| 213 |
peer = BrokenPipePeer(specific_error=True) |
| 214 |
with pytest.raises(ChannelError) as raises: |
| 215 |
await peer.start() |
| 216 |
assert raises.value.attrs == {'message': 'kaputt', 'problem': 'not-supported'} |
| 217 |
peer.close() |