77
88import pyhelm3
99import pytest
10+ from aiohttp .client_exceptions import ClientResponseError
11+ from lightkube import AsyncClient
12+ from lightkube .resources .core_v1 import Pod
1013
1114from .fixtures import ESSData , User
1215from .lib .helpers import deploy_with_values_patch , get_deployment_marker
@@ -35,7 +38,9 @@ async def test_synapse_can_access_client_api(
3538@pytest .mark .skipif (value_file_has ("synapse.enabled" , False ), reason = "Synapse not deployed" )
3639@pytest .mark .parametrize ("users" , [(User (name = "sliding-sync-user" ),)], indirect = True )
3740@pytest .mark .asyncio_cooperative
38- async def test_simplified_sliding_sync_syncs (ssl_context , users , generated_data : ESSData ):
41+ async def test_simplified_sliding_sync_syncs (ingress_ready , ssl_context , users , generated_data : ESSData ):
42+ await ingress_ready ("synapse" )
43+
3944 access_token = users [0 ].access_token
4045
4146 sync_result = await aiohttp_post_json (
@@ -48,6 +53,73 @@ async def test_simplified_sliding_sync_syncs(ssl_context, users, generated_data:
4853 assert "pos" in sync_result
4954
5055
56+ @pytest .mark .skipif (value_file_has ("synapse.enabled" , False ), reason = "Synapse not deployed" )
57+ @pytest .mark .asyncio_cooperative
58+ async def test_routes_to_synapse_workers_correctly (
59+ ingress_ready , kube_client : AsyncClient , ssl_context , generated_data : ESSData
60+ ):
61+ await ingress_ready ("synapse" )
62+
63+ main_backend = "main/main"
64+ if value_file_has ("synapse.workers.sliding-sync.enabled" , True ):
65+ sliding_sync_backend = "sliding-sync/sliding-sync"
66+ else :
67+ sliding_sync_backend = main_backend
68+
69+ if value_file_has ("synapse.workers.synchrotron.enabled" , True ):
70+ synchrotron_backend = "synchrotron/synchrotron"
71+ else :
72+ synchrotron_backend = main_backend
73+
74+ if value_file_has ("synapse.workers.initial-synchrotron.enabled" , True ):
75+ initial_synchrotron_backend = "initial-synchrotron/initial-sync"
76+ else :
77+ initial_synchrotron_backend = synchrotron_backend
78+
79+ # We don't care about any of these succeeding, only that the requests are made and HAProxy dispatches correctly
80+ # So no auth required and parameters can be made up
81+ paths_to_backends = {
82+ # initial-synchrotron
83+ "/_matrix/client/v3/initialSync" : initial_synchrotron_backend ,
84+ "/_matrix/client/v3/rooms/aroomid/initialSync" : initial_synchrotron_backend ,
85+ "/_matrix/client/v3/sync?full_state=true" : initial_synchrotron_backend ,
86+ "/_matrix/client/v3/sync" : initial_synchrotron_backend ,
87+ "/_matrix/client/v3/events" : initial_synchrotron_backend ,
88+ # synchrotron
89+ "/_matrix/client/v3/sync?since=recently" : synchrotron_backend ,
90+ "/_matrix/client/v3/events?from=recently" : synchrotron_backend ,
91+ # sliding-sync
92+ "/_matrix/client/unstable/org.matrix.simplified_msc3575/sync" : sliding_sync_backend ,
93+ # Would be client-reader but not configured in tests
94+ "/_matrix/client/versions" : main_backend ,
95+ }
96+
97+ for path in paths_to_backends :
98+ try :
99+ await aiohttp_get_json (f"https://synapse.{ generated_data .server_name } { path } " , ssl_context )
100+ except ClientResponseError as e :
101+ # We can't use pytest.raises as no exception (200) is valid
102+ assert e .status in [401 , 405 ], f"{ path } had an unexpected status. { e = } " # noqa P1017
103+
104+ http_log_lines = []
105+ async for haproxy_pod in kube_client .list (
106+ Pod , namespace = generated_data .ess_namespace , labels = {"app.kubernetes.io/name" : "haproxy" }
107+ ):
108+ assert haproxy_pod .metadata
109+ assert haproxy_pod .metadata .name
110+ assert haproxy_pod .metadata .namespace
111+ async for log_line in kube_client .log (haproxy_pod .metadata .name , namespace = haproxy_pod .metadata .namespace ):
112+ if "HTTP/1.1" in log_line :
113+ http_log_lines .append (log_line )
114+
115+ for path , backend in paths_to_backends .items ():
116+ matching_lines = [line for line in http_log_lines if f"GET { path } HTTP/1.1" in line ]
117+
118+ assert len (matching_lines ) > 0 , f"Requests for { path } did not appear in the HAProxy logs"
119+ for matching_line in matching_lines :
120+ assert f"synapse-http-in synapse-{ backend } " in matching_line , f"{ path } was routed unexpectedly"
121+
122+
51123@pytest .mark .skipif (value_file_has ("synapse.enabled" , False ), reason = "Synapse not deployed" )
52124@pytest .mark .parametrize ("users" , [(User (name = "media-upload-unauth" ),)], indirect = True )
53125@pytest .mark .asyncio_cooperative
0 commit comments