|
| 1 | +"""Shared fixtures for live e2e tests against a real OpenStack cloud. |
| 2 | +
|
| 3 | +Tests in this directory are marked ``@pytest.mark.live`` and excluded |
| 4 | +from the default ``pytest`` run (see ``pyproject.toml``). To execute:: |
| 5 | +
|
| 6 | + pytest -m live tests/devstack/ |
| 7 | +
|
| 8 | +A working orca profile named ``devstack`` (or whatever |
| 9 | +``ORCA_LIVE_PROFILE`` points to) must exist in ``~/.orca/config.yaml``. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import os |
| 15 | +import re |
| 16 | +import uuid |
| 17 | +from collections.abc import Callable |
| 18 | + |
| 19 | +import pytest |
| 20 | +from click.testing import CliRunner |
| 21 | + |
| 22 | +from orca_cli.core.client import OrcaClient |
| 23 | +from orca_cli.core.config import load_config |
| 24 | +from orca_cli.main import cli |
| 25 | + |
| 26 | +# ── Profile resolution & client ───────────────────────────────────────── |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture(scope="session") |
| 30 | +def live_profile_name() -> str: |
| 31 | + return os.environ.get("ORCA_LIVE_PROFILE", "devstack") |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture(scope="session") |
| 35 | +def live_config(live_profile_name: str) -> dict: |
| 36 | + try: |
| 37 | + return load_config(live_profile_name) |
| 38 | + except Exception as exc: |
| 39 | + pytest.skip(f"orca profile {live_profile_name!r} not loadable: {exc}") |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture(scope="session") |
| 43 | +def live_client(live_config: dict) -> OrcaClient: |
| 44 | + """Authenticated OrcaClient against the live cloud.""" |
| 45 | + client = OrcaClient(live_config) |
| 46 | + client.authenticate() |
| 47 | + return client |
| 48 | + |
| 49 | + |
| 50 | +# ── CLI runner ─────────────────────────────────────────────────────────── |
| 51 | + |
| 52 | + |
| 53 | +@pytest.fixture |
| 54 | +def live_invoke(live_profile_name: str): |
| 55 | + """Invoke the orca CLI in-process against the live profile. |
| 56 | +
|
| 57 | + Returns a ``click.testing.Result`` — check ``result.exit_code`` and |
| 58 | + ``result.output``. |
| 59 | + """ |
| 60 | + runner = CliRunner() |
| 61 | + |
| 62 | + def _invoke(*args: str, input: str | None = None): |
| 63 | + return runner.invoke( |
| 64 | + cli, |
| 65 | + ["-P", live_profile_name, *args], |
| 66 | + catch_exceptions=False, |
| 67 | + input=input, |
| 68 | + ) |
| 69 | + |
| 70 | + return _invoke |
| 71 | + |
| 72 | + |
| 73 | +# ── Cleanup tracker ────────────────────────────────────────────────────── |
| 74 | + |
| 75 | + |
| 76 | +@pytest.fixture |
| 77 | +def cleanup(): |
| 78 | + """LIFO cleanup callback registry that runs even on test failure. |
| 79 | +
|
| 80 | + Use it like:: |
| 81 | +
|
| 82 | + def test_foo(live_client, cleanup): |
| 83 | + res = create_something(live_client) |
| 84 | + cleanup(lambda: delete_something(live_client, res["id"])) |
| 85 | + ... |
| 86 | +
|
| 87 | + Callbacks run in reverse-registration order during teardown, and |
| 88 | + failures are logged (not re-raised) so a botched cleanup doesn't |
| 89 | + mask the original test result. |
| 90 | + """ |
| 91 | + callbacks: list[Callable[[], None]] = [] |
| 92 | + |
| 93 | + yield callbacks.append |
| 94 | + |
| 95 | + for cb in reversed(callbacks): |
| 96 | + try: |
| 97 | + cb() |
| 98 | + except Exception as exc: # noqa: BLE001 - cleanup must be lenient |
| 99 | + print(f"[live-cleanup] {cb!r} failed: {exc}") |
| 100 | + |
| 101 | + |
| 102 | +# ── Naming helper ──────────────────────────────────────────────────────── |
| 103 | + |
| 104 | + |
| 105 | +@pytest.fixture |
| 106 | +def live_name() -> Callable[[str], str]: |
| 107 | + """Return a unique, traceable resource name (prefix + short uuid). |
| 108 | +
|
| 109 | + Lets cleanup-by-prefix work if a test crashes hard (Ctrl+C between |
| 110 | + creation and registration). |
| 111 | + """ |
| 112 | + def _name(prefix: str) -> str: |
| 113 | + return f"orca-live-{prefix}-{uuid.uuid4().hex[:8]}" |
| 114 | + return _name |
| 115 | + |
| 116 | + |
| 117 | +# ── Output parsing helpers ─────────────────────────────────────────────── |
| 118 | + |
| 119 | + |
| 120 | +_ID_RE = re.compile( |
| 121 | + # Canonical UUID (Nova/Cinder/Neutron/Glance) and bare 32-hex (Keystone). |
| 122 | + r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|[0-9a-f]{32}", |
| 123 | + re.IGNORECASE, |
| 124 | +) |
| 125 | + |
| 126 | + |
| 127 | +def extract_uuid(text: str) -> str: |
| 128 | + """Pull the first resource ID out of a CLI message. |
| 129 | +
|
| 130 | + orca's create commands print messages like ``Volume 'foo' (uuid) created.`` |
| 131 | + rather than emitting the ID via ``-f value``, so live tests parse the ID |
| 132 | + out of the human-readable output. Both canonical UUIDs (Nova/Cinder/...) |
| 133 | + and bare 32-hex IDs (Keystone) are recognised. |
| 134 | + """ |
| 135 | + match = _ID_RE.search(text) |
| 136 | + if not match: |
| 137 | + raise AssertionError(f"no resource ID found in output: {text!r}") |
| 138 | + return match.group(0) |
0 commit comments