Repository navigation
Expand file tree
/
Copy pathconftest.py
More file actions
339 lines (249 loc) Β· 10.4 KB
/
Copy pathconftest.py
File metadata and controls
339 lines (249 loc) Β· 10.4 KB
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
"""Top-level pytest fixtures shared by all example test suites.
These fixtures wrap the ``unikraft`` CLI binary and provide reusable building
blocks for end-to-end tests that deploy an example to Unikraft Cloud and
exercise it over HTTP.
Authentication is expected to come from the caller's existing Unikraft CLI
profile (set up via ``unikraft login``). The tests do not read or manage API
tokens themselves.
Required environment variables:
* ``UKC_METRO`` β Unikraft Cloud metro to deploy into, e.g. ``fra``.
* ``UKC_IMAGE_PREFIX`` β Prefix used when tagging built images, typically the
user's organisation name (e.g. ``my-org``). Required for tests that build an
image.
"""
from __future__ import annotations
import logging
import os
import time
import uuid
from collections.abc import Callable, Mapping, Sequence
from pathlib import Path
from typing import Any
import pytest
from _testlib.http_client import http_get, http_post
from _testlib.socat import SocatTunnel
from _testlib.unikraft import (
UnikraftCLI,
extract_instance_name,
extract_instance_url,
)
log = logging.getLogger(__name__)
REPO_ROOT = Path(__file__).resolve().parent
# ---------------------------------------------------------------------------
# Environment / configuration
# ---------------------------------------------------------------------------
def _require_env(name: str) -> str:
value = os.environ.get(name)
if not value:
pytest.skip(f"{name} is not set in the environment")
return value
@pytest.fixture(scope="session")
def ukc_metro() -> str:
return _require_env("UKC_METRO")
@pytest.fixture(scope="session")
def ukc_image_prefix() -> str:
"""Image repository prefix (typically the user's org name).
E.g. if set to ``my-org``, built images will be tagged
``my-org/<example>:<tag>``.
"""
return _require_env("UKC_IMAGE_PREFIX")
@pytest.fixture(scope="session")
def repo_root() -> Path:
return REPO_ROOT
@pytest.fixture(scope="session")
def test_run_id() -> str:
"""Identifier unique to this pytest session, taken from ``UKC_TEST_ID``.
Set ``UKC_TEST_ID`` to the CI run ID (e.g. ``${{ github.run_id }}``) so
that resources can be correlated and cleaned up after the run.
"""
return _require_env("UKC_TEST_ID")
# ---------------------------------------------------------------------------
# Unikraft CLI
# ---------------------------------------------------------------------------
@pytest.fixture(scope="session")
def unikraft(ukc_metro: str) -> UnikraftCLI:
"""Session-scoped Unikraft CLI wrapper."""
return UnikraftCLI(metro=ukc_metro)
# ---------------------------------------------------------------------------
# Image build fixture
# ---------------------------------------------------------------------------
BuildImage = Callable[[str, str], str]
@pytest.fixture
def build_image(
request: pytest.FixtureRequest,
unikraft: UnikraftCLI,
repo_root: Path,
ukc_image_prefix: str,
test_run_id: str,
) -> BuildImage:
"""Factory fixture that builds an example directory into an image.
Usage::
def test_foo(build_image):
image = build_image("nginx", "nginx")
``example_dir`` is resolved relative to the repository root. ``image_name``
is the short image name (without prefix/tag); the final tag is
``<UKC_IMAGE_PREFIX>/<image_name>:test-<run-id>``.
A finalizer is registered to delete the image after the test finishes.
Pytest runs finalizers in LIFO order across fixtures, so any
``run_instance`` teardowns (registered later in the test) execute
*before* this image-delete, ensuring the image is no longer in use.
"""
def _build(example_dir: str, image_name: str) -> str:
context = repo_root / example_dir
assert context.is_dir(), f"example directory not found: {context}"
tag = f"{ukc_image_prefix}/{image_name}:{test_run_id}"
# Register the image-delete finalizer *before* invoking the build
# so a partial build is still cleaned up.
request.addfinalizer(lambda: unikraft.delete_image(tag))
unikraft.build(context, tag)
return tag
return _build
# ---------------------------------------------------------------------------
# Instance lifecycle fixture
# ---------------------------------------------------------------------------
RunInstance = Callable[..., dict[str, Any]]
@pytest.fixture
def run_instance(
request: pytest.FixtureRequest,
unikraft: UnikraftCLI,
test_run_id: str,
) -> RunInstance:
"""Factory fixture that launches instances with guaranteed teardown.
Each call creates a new instance. A cleanup finalizer is registered
**before** invoking the CLI, so the instance is deleted even if the
``unikraft run`` command or subsequent JSON parsing fails part-way
through. Finalizers registered via :meth:`request.addfinalizer` are
executed by pytest unconditionally after the test completes, whether it
passed, failed, or errored.
Parameters passed through to the CLI (see
:meth:`_testlib.unikraft.UnikraftCLI.run_instance` for full details):
* ``image`` (positional) β image tag to run.
* ``publish`` β iterable of ``-p`` port mappings, e.g.
``["443:8080/tls+http"]``. Defaults to none.
* ``memory`` β memory allocation (``-m``), e.g. ``"256M"``. Defaults to
the CLI default.
* ``name`` β explicit instance name. If omitted, a unique name is
generated so parallel runs don't collide.
* ``metro`` β per-call metro override.
* ``scale_to_zero`` β ``--scale-to-zero`` spec string, e.g.
``"policy=on,cooldown-time=1000"``.
* ``template`` β ``--template`` name.
* ``env`` β ``--env`` entries (``"K=V"`` strings or a mapping).
* ``domain`` / ``volume`` / ``rom`` β single value or sequence for the
corresponding repeatable flags.
* ``vcpus`` β ``--vcpus``.
* ``command`` β instance command placed after ``--`` (string or
sequence of arguments).
* ``extra_args`` β extra positional CLI flags for escape-hatch needs.
"""
def _run(
image: str,
*,
publish: Sequence[str] = (),
memory: str | None = None,
name: str | None = None,
metro: str | None = None,
scale_to_zero: str | Mapping[str, Any] | None = None,
template: str | None = None,
env: Sequence[str] | Mapping[str, Any] = (),
domain: str | Sequence[str] | None = None,
volume: str | Sequence[str] | None = None,
rom: str | Mapping[str, Any] | Sequence[str | Mapping[str, Any]] | None = None,
vcpus: int | str | None = None,
command: str | Sequence[str] | None = None,
extra_args: Sequence[str] = (),
) -> dict[str, Any]:
instance_name = name or f"{test_run_id}"
# Identifier used by the finalizer. Seeded with the name we asked
# the CLI to use so teardown still works if `unikraft run` raises
# before returning a parsed response. Replaced below with the
# CLI-reported name/uuid once we have it.
cleanup_target = instance_name
def _cleanup() -> None:
log.info("tearing down instance: %s", cleanup_target)
unikraft.delete_instance(cleanup_target)
# Register the finalizer before invoking the CLI so the instance is
# torn down even if `unikraft run` fails mid-flight after having
# created the instance on the cloud side.
request.addfinalizer(_cleanup)
instance = unikraft.run_instance(
image,
publish=publish,
memory=memory,
name=instance_name,
metro=metro,
scale_to_zero=scale_to_zero,
template=template,
env=env,
domain=domain,
volume=volume,
rom=rom,
vcpus=vcpus,
command=command,
extra_args=extra_args,
)
# Prefer the CLI-reported identifier in case it diverges from the
# name we requested. `delete_instance` accepts either name or uuid.
for key in ("name", "uuid"):
value = instance.get(key)
if isinstance(value, str) and value:
cleanup_target = value
break
return instance
return _run
# ---------------------------------------------------------------------------
# Instance wait fixture
# ---------------------------------------------------------------------------
WaitInstance = Callable[[str, str], dict[str, Any]]
@pytest.fixture
def wait_instance(unikraft: UnikraftCLI) -> WaitInstance:
"""Wait until a named instance reaches the given state.
Usage::
def test_foo(run_instance, wait_instance):
instance = run_instance(image, ...)
wait_instance(extract_instance_name(instance), "running")
"""
def _wait(target: str, state: str) -> dict[str, Any]:
return unikraft.wait_instance(target, state)
return _wait
# ---------------------------------------------------------------------------
# HTTP helper
# ---------------------------------------------------------------------------
@pytest.fixture
def http():
"""Expose the shared HTTP GET helper as a fixture for convenience."""
return http_get
@pytest.fixture(name="http_post")
def http_post_fixture():
"""Expose the shared HTTP POST helper as a fixture for convenience."""
return http_post
# ---------------------------------------------------------------------------
# TLS tunnel (socat) helper
# ---------------------------------------------------------------------------
SocatTunnelFactory = Callable[[str, int, int], SocatTunnel]
@pytest.fixture
def socat_tunnel(request: pytest.FixtureRequest) -> SocatTunnelFactory:
"""Factory fixture that creates socat TLS tunnels with guaranteed teardown.
``local_port`` is the TCP port socat will listen on locally.
Usage::
def test_mydb(socat_tunnel, ...):
tunnel = socat_tunnel(host, 3306, 3306)
conn = my_connect("127.0.0.1", tunnel.local_port)
"""
def _create(host: str, port: int, local_port: int) -> SocatTunnel:
tunnel = SocatTunnel(host, port, local_port)
tunnel.open()
request.addfinalizer(tunnel.close)
return tunnel
return _create
# Re-export for callers that import from conftest.
__all__ = [
"build_image",
"extract_instance_url",
"http",
"http_post",
"run_instance",
"socat_tunnel",
"unikraft",
"wait_instance",
]