1import ./make-test-python.nix ({ pkgs, ... }:
2 let
3 homeserverUrl = "http://homeserver:8448";
4 in
5 {
6 name = "matrix-appservice-irc";
7 meta = {
8 maintainers = pkgs.matrix-appservice-irc.meta.maintainers;
9 };
10
11 nodes = {
12 homeserver = { pkgs, ... }: {
13 # We'll switch to this once the config is copied into place
14 specialisation.running.configuration = {
15 services.matrix-synapse = {
16 enable = true;
17 database_type = "sqlite3";
18 app_service_config_files = [ "/registration.yml" ];
19
20 enable_registration = true;
21
22 listeners = [
23 # The default but tls=false
24 {
25 "bind_address" = "";
26 "port" = 8448;
27 "resources" = [
28 { "compress" = true; "names" = [ "client" "webclient" ]; }
29 { "compress" = false; "names" = [ "federation" ]; }
30 ];
31 "tls" = false;
32 "type" = "http";
33 "x_forwarded" = false;
34 }
35 ];
36 };
37
38 networking.firewall.allowedTCPPorts = [ 8448 ];
39 };
40 };
41
42 ircd = { pkgs, ... }: {
43 services.ngircd = {
44 enable = true;
45 config = ''
46 [Global]
47 Name = ircd.ircd
48 Info = Server Info Text
49 AdminInfo1 = _
50
51 [Channel]
52 Name = #test
53 Topic = a cool place
54
55 [Options]
56 PAM = no
57 '';
58 };
59 networking.firewall.allowedTCPPorts = [ 6667 ];
60 };
61
62 appservice = { pkgs, ... }: {
63 services.matrix-appservice-irc = {
64 enable = true;
65 registrationUrl = "http://appservice:8009";
66
67 settings = {
68 homeserver.url = homeserverUrl;
69 homeserver.domain = "homeserver";
70
71 ircService.servers."ircd" = {
72 name = "IRCd";
73 port = 6667;
74 dynamicChannels = {
75 enabled = true;
76 aliasTemplate = "#irc_$CHANNEL";
77 };
78 };
79 };
80 };
81
82 networking.firewall.allowedTCPPorts = [ 8009 ];
83 };
84
85 client = { pkgs, ... }: {
86 environment.systemPackages = [
87 (pkgs.writers.writePython3Bin "do_test"
88 { libraries = [ pkgs.python3Packages.matrix-client ]; } ''
89 import socket
90 from matrix_client.client import MatrixClient
91 from time import sleep
92
93 matrix = MatrixClient("${homeserverUrl}")
94 matrix.register_with_password(username="alice", password="foobar")
95
96 irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
97 irc.connect(("ircd", 6667))
98 irc.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
99 irc.send(b"USER bob bob bob :bob\n")
100 irc.send(b"NICK bob\n")
101
102 m_room = matrix.join_room("#irc_#test:homeserver")
103 irc.send(b"JOIN #test\n")
104
105 # plenty of time for the joins to happen
106 sleep(10)
107
108 m_room.send_text("hi from matrix")
109 irc.send(b"PRIVMSG #test :hi from irc \r\n")
110
111 print("Waiting for irc message...")
112 while True:
113 buf = irc.recv(10000)
114 if b"hi from matrix" in buf:
115 break
116
117 print("Waiting for matrix message...")
118
119
120 def callback(room, e):
121 if "hi from irc" in e['content']['body']:
122 exit(0)
123
124
125 m_room.add_listener(callback, "m.room.message")
126 matrix.listen_forever()
127 ''
128 )
129 ];
130 };
131 };
132
133 testScript = ''
134 start_all()
135
136 ircd.wait_for_unit("ngircd.service")
137 ircd.wait_for_open_port(6667)
138
139 with subtest("start the appservice"):
140 appservice.wait_for_unit("matrix-appservice-irc.service")
141 appservice.wait_for_open_port(8009)
142
143 with subtest("copy the registration file"):
144 appservice.copy_from_vm("/var/lib/matrix-appservice-irc/registration.yml")
145 homeserver.copy_from_host(
146 pathlib.Path(os.environ.get("out", os.getcwd())) / "registration.yml", "/"
147 )
148 homeserver.succeed("chmod 444 /registration.yml")
149
150 with subtest("start the homeserver"):
151 homeserver.succeed(
152 "/run/current-system/specialisation/running/bin/switch-to-configuration test >&2"
153 )
154
155 homeserver.wait_for_unit("matrix-synapse.service")
156 homeserver.wait_for_open_port(8448)
157
158 with subtest("ensure messages can be exchanged"):
159 client.succeed("do_test")
160 '';
161
162 })