diff options
author | piegames <git@piegames.de> | 2021-10-26 20:33:13 +0200 |
---|---|---|
committer | piegames <git@piegames.de> | 2021-10-26 22:13:23 +0200 |
commit | 03857732e941eeb040792ba49a0f30025115c567 (patch) | |
tree | 0d6b0b474fbbc8926764e60805ea2bf284968ca1 /nixos/tests/matrix-appservice-irc.nix | |
parent | 4bf7da5720c6de80251f2dbef13eac79a0fe4959 (diff) | |
download | nixpkgs-03857732e941eeb040792ba49a0f30025115c567.tar nixpkgs-03857732e941eeb040792ba49a0f30025115c567.tar.gz nixpkgs-03857732e941eeb040792ba49a0f30025115c567.tar.bz2 nixpkgs-03857732e941eeb040792ba49a0f30025115c567.tar.lz nixpkgs-03857732e941eeb040792ba49a0f30025115c567.tar.xz nixpkgs-03857732e941eeb040792ba49a0f30025115c567.tar.zst nixpkgs-03857732e941eeb040792ba49a0f30025115c567.zip |
nixos/tests/matrix-appservice-irc: Refactor test
Co-authored-by: Andreas Rammhold <andreas@rammhold.de>
Diffstat (limited to 'nixos/tests/matrix-appservice-irc.nix')
-rw-r--r-- | nixos/tests/matrix-appservice-irc.nix | 151 |
1 files changed, 95 insertions, 56 deletions
diff --git a/nixos/tests/matrix-appservice-irc.nix b/nixos/tests/matrix-appservice-irc.nix index cf1fae16312..e1da410af06 100644 --- a/nixos/tests/matrix-appservice-irc.nix +++ b/nixos/tests/matrix-appservice-irc.nix @@ -85,61 +85,100 @@ import ./make-test-python.nix ({ pkgs, ... }: client = { pkgs, ... }: { environment.systemPackages = [ (pkgs.writers.writePython3Bin "do_test" - { libraries = [ pkgs.python3Packages.matrix-nio ]; } '' - import socket - from nio import AsyncClient, RoomMessageText - from time import sleep - import asyncio - - - async def run(): - matrix = AsyncClient("${homeserverUrl}") - await matrix.register("alice", "foobar") - - irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - irc.connect(("ircd", 6667)) - irc.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) - irc.send(b"USER bob bob bob :bob\n") - irc.send(b"NICK bob\n") - - room_id = (await matrix.join("#irc_#test:homeserver")).room_id - irc.send(b"JOIN #test\n") - - # plenty of time for the joins to happen - sleep(10) - - # Exchange messages - await matrix.room_send( - room_id=room_id, - message_type="m.room.message", - content={ - "msgtype": "m.text", - "body": "hi from matrix" - } - ) - irc.send(b"PRIVMSG #test :hi from irc \r\n") - - print("Waiting for irc message...") - while True: - buf = irc.recv(10000) - if b"hi from matrix" in buf: - print("Got IRC message") - break - - print("Waiting for matrix message...") - - async def callback(room, e): - if "hi from irc" in e.body: - print("Got Matrix message") - await matrix.close() - exit(0) # Actual exit point - - matrix.add_event_callback(callback, RoomMessageText) - await matrix.sync_forever() - exit(1) # Unreachable - - asyncio.run(run()) - '' + { + libraries = [ pkgs.python3Packages.matrix-nio ]; + flakeIgnore = [ + # We don't live in the dark ages anymore. + # Languages like Python that are whitespace heavy will overrun + # 79 characters.. + "E501" + ]; + } '' + import sys + import socket + import functools + from time import sleep + import asyncio + + from nio import AsyncClient, RoomMessageText, JoinResponse + + + async def matrix_room_message_text_callback(matrix: AsyncClient, msg: str, _r, e): + print("Received matrix text message: ", e) + if msg in e.body: + print("Received hi from IRC") + await matrix.close() + exit(0) # Actual exit point + + + class IRC: + def __init__(self): + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect(("ircd", 6667)) + sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + sock.send(b"USER bob bob bob :bob\n") + sock.send(b"NICK bob\n") + self.sock = sock + + def join(self, room: str): + self.sock.send(f"JOIN {room}\n".encode()) + + def privmsg(self, room: str, msg: str): + self.sock.send(f"PRIVMSG {room} :{msg}\n".encode()) + + def expect_msg(self, body: str): + buffer = "" + while True: + buf = self.sock.recv(1024).decode() + buffer += buf + if body in buffer: + return + + + async def run(homeserver: str): + irc = IRC() + + matrix = AsyncClient(homeserver) + response = await matrix.register("alice", "foobar") + print("Matrix register response: ", response) + + response = await matrix.join("#irc_#test:homeserver") + print("Matrix join room response:", response) + assert isinstance(response, JoinResponse) + room_id = response.room_id + + irc.join("#test") + # FIXME: what are we waiting on here? Matrix? IRC? Both? + # 10s seem bad for busy hydra machines. + sleep(10) + + # Exchange messages + print("Sending text message to matrix room") + response = await matrix.room_send( + room_id=room_id, + message_type="m.room.message", + content={"msgtype": "m.text", "body": "hi from matrix"}, + ) + print("Matrix room send response: ", response) + irc.privmsg("#test", "hi from irc") + + print("Waiting for the matrix message to appear on the IRC side...") + irc.expect_msg("hi from matrix") + + callback = functools.partial( + matrix_room_message_text_callback, matrix, "hi from irc" + ) + matrix.add_event_callback(callback, RoomMessageText) + + print("Waiting for matrix message...") + await matrix.sync_forever() + + exit(1) # Unreachable + + + if __name__ == "__main__": + asyncio.run(run(sys.argv[1])) + '' ) ]; }; @@ -173,6 +212,6 @@ import ./make-test-python.nix ({ pkgs, ... }: homeserver.wait_for_open_port(8448) with subtest("ensure messages can be exchanged"): - client.succeed("do_test >&2") + client.succeed("do_test ${homeserverUrl} >&2") ''; }) |