/
conftest.py
357 lines (290 loc) · 11.9 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.
"""Fixtures for Synapse charm integration tests."""
import json
import typing
from secrets import token_hex
import pytest
import pytest_asyncio
import requests
from juju.action import Action
from juju.application import Application
from juju.model import Model
from ops.model import ActiveStatus
from pytest import Config
from pytest_operator.plugin import OpsTest
from tests.conftest import SYNAPSE_IMAGE_PARAM, SYNAPSE_NGINX_IMAGE_PARAM
# caused by pytest fixtures, mark does not work in fixtures
# pylint: disable=too-many-arguments, unused-argument
# mypy has trouble to inferred types for variables that are initialized in subclasses.
ACTIVE_STATUS_NAME = typing.cast(str, ActiveStatus.name) # type: ignore
@pytest_asyncio.fixture(scope="module", name="server_name")
async def server_name_fixture() -> str:
"""Return a server_name."""
return "my.synapse.local"
@pytest_asyncio.fixture(scope="module", name="another_server_name")
async def another_server_name_fixture() -> str:
"""Return a server_name."""
return "another.synapse.local"
@pytest_asyncio.fixture(scope="module", name="model")
async def model_fixture(ops_test: OpsTest) -> Model:
"""Return the current testing juju model."""
assert ops_test.model
return ops_test.model
@pytest_asyncio.fixture(scope="module", name="model_name")
async def model_name_fixture(ops_test: OpsTest) -> str:
"""Return the current testing juju model name."""
assert ops_test.model_name
return ops_test.model_name
@pytest_asyncio.fixture(scope="module", name="synapse_charm")
async def synapse_charm_fixture(pytestconfig: Config):
"""Get value from parameter charm-file."""
charm = pytestconfig.getoption("--charm-file")
assert charm, "--charm-file must be set"
return charm
@pytest_asyncio.fixture(scope="module", name="synapse_image")
def synapse_image_fixture(pytestconfig: Config):
"""Get value from parameter synapse-image."""
synapse_image = pytestconfig.getoption(SYNAPSE_IMAGE_PARAM)
assert synapse_image, f"{SYNAPSE_IMAGE_PARAM} must be set"
return synapse_image
@pytest_asyncio.fixture(scope="module", name="synapse_nginx_image")
def synapse_nginx_image_fixture(pytestconfig: Config):
"""Get value from parameter synapse-nginx-image."""
synapse_nginx_image = pytestconfig.getoption(SYNAPSE_NGINX_IMAGE_PARAM)
assert synapse_nginx_image, f"{SYNAPSE_NGINX_IMAGE_PARAM} must be set"
return synapse_nginx_image
@pytest_asyncio.fixture(scope="module", name="synapse_app_name")
def synapse_app_name_fixture() -> str:
"""Get Synapse application name."""
return "synapse"
@pytest_asyncio.fixture(scope="module", name="synapse_app_refresh_name")
def synapse_app_refresh_name_fixture() -> str:
"""Get Synapse application name for refresh fixture."""
return "synapse-refresh"
@pytest_asyncio.fixture(scope="module", name="synapse_app")
async def synapse_app_fixture(
ops_test: OpsTest,
synapse_app_name: str,
synapse_app_refresh_name: str,
synapse_image: str,
synapse_nginx_image: str,
model: Model,
server_name: str,
synapse_charm: str,
postgresql_app: Application,
postgresql_app_name: str,
pytestconfig: Config,
):
"""Build and deploy the Synapse charm."""
use_existing = pytestconfig.getoption("--use-existing", default=False)
if use_existing:
return model.applications[synapse_app_name]
if synapse_app_refresh_name in model.applications:
await model.remove_application(synapse_app_refresh_name, block_until_done=True)
await model.wait_for_idle(status=ACTIVE_STATUS_NAME, idle_period=5)
resources = {
"synapse-image": synapse_image,
"synapse-nginx-image": synapse_nginx_image,
}
app = await model.deploy(
f"./{synapse_charm}",
resources=resources,
application_name=synapse_app_name,
series="jammy",
config={"server_name": server_name},
)
async with ops_test.fast_forward():
await model.wait_for_idle(raise_on_blocked=True, status=ACTIVE_STATUS_NAME)
await model.relate(f"{synapse_app_name}:database", f"{postgresql_app_name}")
await model.wait_for_idle(status=ACTIVE_STATUS_NAME)
return app
@pytest_asyncio.fixture(scope="module", name="synapse_refresh_app")
async def synapse_refresh_app_fixture(
ops_test: OpsTest,
model: Model,
server_name: str,
synapse_app_refresh_name: str,
postgresql_app: Application,
postgresql_app_name: str,
synapse_charm: str,
):
"""Remove existing Synapse and deploy synapse from Charmhub so the refresh can be tested."""
async with ops_test.fast_forward():
synapse_app = await model.deploy(
"synapse",
application_name=synapse_app_refresh_name,
trust=True,
channel="latest/edge",
series="jammy",
config={"server_name": server_name},
)
await model.wait_for_idle(
apps=[synapse_app_refresh_name, postgresql_app_name],
status=ACTIVE_STATUS_NAME,
idle_period=5,
)
await model.relate(f"{synapse_app_refresh_name}:database", f"{postgresql_app_name}")
await model.wait_for_idle(idle_period=5)
await synapse_app.set_config({"enable_mjolnir": "true"})
await model.wait_for_idle(apps=[synapse_app_refresh_name], idle_period=5, status="blocked")
app = model.applications[synapse_app_refresh_name]
await app.refresh(path=f"./{synapse_charm}")
await model.wait_for_idle(apps=[synapse_app_refresh_name], idle_period=5, status="blocked")
return app
@pytest_asyncio.fixture(scope="module", name="get_unit_ips")
async def get_unit_ips_fixture(ops_test: OpsTest):
"""Return an async function to retrieve unit ip addresses of a certain application."""
async def get_unit_ips(application_name: str):
"""Retrieve unit ip addresses of a certain application.
Args:
application_name: application name.
Returns:
a list containing unit ip addresses.
"""
_, status, _ = await ops_test.juju("status", "--format", "json")
status = json.loads(status)
units = status["applications"][application_name]["units"]
return tuple(
unit_status["address"]
for _, unit_status in sorted(units.items(), key=lambda kv: int(kv[0].split("/")[-1]))
)
return get_unit_ips
@pytest.fixture(scope="module", name="external_hostname")
def external_hostname_fixture() -> str:
"""Return the external hostname for ingress-related tests."""
return "juju.test"
@pytest.fixture(scope="module", name="nginx_integrator_app_name")
def nginx_integrator_app_name_fixture() -> str:
"""Return the name of the nginx integrator application deployed for tests."""
return "nginx-ingress-integrator"
@pytest_asyncio.fixture(scope="module", name="nginx_integrator_app")
async def nginx_integrator_app_fixture(
ops_test: OpsTest,
model: Model,
synapse_app,
nginx_integrator_app_name: str,
):
"""Deploy nginx-ingress-integrator."""
async with ops_test.fast_forward():
app = await model.deploy(
"nginx-ingress-integrator",
application_name=nginx_integrator_app_name,
trust=True,
channel="latest/edge",
)
await model.wait_for_idle(raise_on_blocked=True, status=ACTIVE_STATUS_NAME)
return app
@pytest_asyncio.fixture(scope="function", name="another_synapse_app")
async def another_synapse_app_fixture(
model: Model, synapse_app: Application, server_name: str, another_server_name: str
):
"""Change server_name."""
# First we guarantee that the first server_name is set
# Then change it.
await synapse_app.set_config({"server_name": server_name})
await model.wait_for_idle()
await synapse_app.set_config({"server_name": another_server_name})
await model.wait_for_idle()
yield synapse_app
@pytest.fixture(scope="module", name="postgresql_app_name")
def postgresql_app_name_app_name_fixture() -> str:
"""Return the name of the postgresql application deployed for tests."""
return "postgresql-k8s"
@pytest_asyncio.fixture(scope="module", name="postgresql_app")
async def postgresql_app_fixture(
ops_test: OpsTest, model: Model, postgresql_app_name: str, pytestconfig: Config
):
"""Deploy postgresql."""
use_existing = pytestconfig.getoption("--use-existing", default=False)
if use_existing:
return model.applications[postgresql_app_name]
async with ops_test.fast_forward():
await model.deploy(postgresql_app_name, channel="14/stable", trust=True)
await model.wait_for_idle(status=ACTIVE_STATUS_NAME)
@pytest.fixture(scope="module", name="grafana_app_name")
def grafana_app_name_fixture() -> str:
"""Return the name of the grafana application deployed for tests."""
return "grafana-k8s"
@pytest_asyncio.fixture(scope="module", name="grafana_app")
async def grafana_app_fixture(
ops_test: OpsTest,
model: Model,
grafana_app_name: str,
):
"""Deploy grafana."""
async with ops_test.fast_forward():
app = await model.deploy(
grafana_app_name,
application_name=grafana_app_name,
channel="stable",
series="focal",
revision=82, # last one compatible with Juju 2
trust=True,
)
await model.wait_for_idle(raise_on_blocked=True, status=ACTIVE_STATUS_NAME)
return app
@pytest.fixture(scope="module", name="prometheus_app_name")
def prometheus_app_name_fixture() -> str:
"""Return the name of the prometheus application deployed for tests."""
return "prometheus-k8s"
@pytest_asyncio.fixture(scope="module", name="prometheus_app")
async def deploy_prometheus_fixture(
ops_test: OpsTest,
model: Model,
prometheus_app_name: str,
):
"""Deploy prometheus."""
async with ops_test.fast_forward():
app = await model.deploy(
prometheus_app_name,
application_name=prometheus_app_name,
channel="stable",
series="focal",
revision=129, # last one compatible with Juju 2
trust=True,
)
await model.wait_for_idle(raise_on_blocked=True, status=ACTIVE_STATUS_NAME)
return app
@pytest.fixture(scope="module", name="user_username")
def user_username_fixture() -> typing.Generator[str, None, None]:
"""Return the a username to be created for tests."""
yield token_hex(16)
@pytest_asyncio.fixture(scope="module", name="user_password")
async def user_password_fixture(
synapse_app: Application, user_username: str
) -> typing.AsyncGenerator[str, None]:
"""Return the a username to be created for tests."""
action_register_user: Action = await synapse_app.units[0].run_action( # type: ignore
"register-user", username=user_username, admin=True
)
await action_register_user.wait()
assert action_register_user.status == "completed"
assert action_register_user.results["register-user"]
password = action_register_user.results["user-password"]
assert password
yield password
@pytest_asyncio.fixture(scope="module", name="access_token")
async def access_token_fixture(
user_username: str,
user_password: str,
synapse_app: Application,
get_unit_ips: typing.Callable[[str], typing.Awaitable[tuple[str, ...]]],
) -> typing.AsyncGenerator[str, None]:
"""Return the access token after login with the username and password."""
synapse_ip = (await get_unit_ips(synapse_app.name))[0]
# login
sess = requests.session()
res = sess.post(
f"http://{synapse_ip}:8080/_matrix/client/r0/login",
json={
"identifier": {"type": "m.id.user", "user": user_username},
"password": user_password,
"type": "m.login.password",
},
timeout=5,
)
res.raise_for_status()
access_token = res.json()["access_token"]
assert access_token
yield access_token