blob: 710dcb83f9c8fac30790ba972832975caa7f3bef [file] [log] [blame]
Maxc35bb472017-12-15 11:56:05 +01001#!/usr/bin/env python3
2
Max566f2a72017-12-21 14:38:39 +01003# just a smoke test for osmopy
Maxc35bb472017-12-15 11:56:05 +01004
Maxcd5a6e42018-01-14 18:27:31 +01005import asyncio, random, sys, os
6
7# we have to use this ugly hack to workaroundbrokenrelative imports in py3:
8# from ..osmopy.osmo_ipa import Ctrl
9# does not work as expected
Max901f5eb2018-01-15 14:24:15 +010010sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
Max566f2a72017-12-21 14:38:39 +010011from osmopy.osmo_ipa import Ctrl
12from osmopy import __version__
Maxc35bb472017-12-15 11:56:05 +010013
Max566f2a72017-12-21 14:38:39 +010014class CtrlProtocol(asyncio.Protocol):
15 def connection_made(self, transport):
16 peername = transport.get_extra_info('peername')
17 print('Connection from {}'.format(peername))
18 self.transport = transport
19
20 def data_received(self, data):
21 (i, v, k) = Ctrl().parse(data)
22 if not k:
23 print('Ctrl GET received: %s' % v)
24 else:
25 print('Ctrl SET received: %s :: %s' % (v, k))
26
27 message = Ctrl().reply(i, v, k)
28 self.transport.write(message)
29
30 self.transport.close()
31 # quit the loop gracefully
32 print('Closing the loop...')
33 loop.stop()
34
35
36if __name__ == '__main__':
37 loop = asyncio.get_event_loop()
38 test_host = '127.0.0.5'
39 test_port = str(random.randint(1025, 60000))
40
Maxc41b7512018-01-15 14:08:54 +010041 print('Testing v%s on %s:%s' % (__version__, test_host, test_port))
42
Max566f2a72017-12-21 14:38:39 +010043 # Each client connection will create a new protocol instance
44 server = loop.run_until_complete(loop.create_server(CtrlProtocol, test_host, test_port))
45
46 print('Serving on {}...'.format(server.sockets[0].getsockname()))
47
48 # Async client running in the subprocess plugged to the same event loop
Oliver Smith92014a42023-09-13 15:55:37 +020049 loop.run_until_complete(asyncio.gather(asyncio.create_subprocess_exec('./scripts/osmo_ctrl.py', '-g', 'mnc', '-d', test_host, '-p', test_port)))
Max566f2a72017-12-21 14:38:39 +010050
51 loop.run_forever()
52
53 # Cleanup after loop is finished
54 server.close()
55 loop.run_until_complete(server.wait_closed())
56 loop.close()
57
58 print('[Python3] Smoke test PASSED for v%s' % __version__)