Debian Codegraph @

test/pytest/mockpeer.py

raw
1 import asyncio
2 import os
3 import sys
4
5 from cockpit._vendor import systemd_ctypes
6 from cockpit.router import Router
7 from cockpit.transports import StdioTransport
8
9
10 class MockPeer(Router):
11 def do_send_init(self):
12 init_type = os.environ.get('INIT_TYPE', None)
13 if init_type == 'wrong-command':
14 self.write_control(command='xnit', version=1)
15 elif init_type == 'wrong-version':
16 self.write_control(command='init', version=2)
17 elif init_type == 'channel-control':
18 self.write_control(command='init', channel='x')
19 elif init_type == 'data':
20 self.write_channel_data('x', b'123')
21 elif init_type == 'break-protocol':
22 print('i like printf debugging', flush=True)
23 elif init_type == 'exit':
24 sys.exit()
25 elif init_type == 'exit-not-found':
26 # shell error code for "command not found"
27 sys.exit(127)
28 elif init_type != 'silence':
29 self.write_control(command='init', version=1)
30
31 def channel_control_received(self, channel, command, message):
32 if command == 'open':
33 self.write_control(command='ready', channel=channel)
34
35 def channel_data_received(self, channel, data):
36 pass
37
38
39 async def run():
40 protocol = MockPeer([])
41 StdioTransport(asyncio.get_running_loop(), protocol)
42 await protocol.communicate()
43
44
45 if __name__ == '__main__':
46 systemd_ctypes.run_async(run())