-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathconftest.py
More file actions
205 lines (152 loc) · 7.25 KB
/
Copy pathconftest.py
File metadata and controls
205 lines (152 loc) · 7.25 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
# Copyright (c) 2024-2026, Arm Limited and Contributors. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
"""Root conftest for cross-repo integration tests.
Infrastructure:
- Session-scoped Docker Compose (NATS + Zenoh + etcd + registry)
- Dev mode: no TLS, no JWT (DEVICE_CONNECT_ALLOW_INSECURE=true)
Fixtures:
- device_spawner: DeviceFactory using device_connect_edge package
- event_capture: EventCollector for messaging event capture
- event_injector: EventInjector for simulating device events
- mock_orchestrator: Rule-based orchestrator (no LLM)
- messaging_client: Connected Edge MessagingClient for direct RPC calls
Backend parameterization:
All fixtures are parameterized over NATS and Zenoh via messaging_backend.
Use --backend=nats or --backend=zenoh to run a single backend.
"""
import logging
import os
import sys
from pathlib import Path
import pytest
import pytest_asyncio
# Ensure drivers/ and fixtures/ are importable
ITEST_ROOT = Path(__file__).parent
if str(ITEST_ROOT) not in sys.path:
sys.path.insert(0, str(ITEST_ROOT))
from fixtures.infrastructure import ( # noqa: E402
DockerComposeManager,
clear_device_registry,
wait_for_all_services,
)
logger = logging.getLogger(__name__)
BACKEND_URLS = {
"nats": os.getenv("NATS_URL", "nats://localhost:4222"),
"zenoh": os.getenv("ZENOH_CONNECT", "tcp/localhost:7447"),
}
# ── Pytest hooks ──────────────────────────────────────────────────
def pytest_addoption(parser):
parser.addoption(
"--backend", action="store", default=None,
help="Run only this messaging backend (nats or zenoh)",
)
def pytest_configure(config):
config.addinivalue_line("markers", "integration: requires Docker infrastructure")
config.addinivalue_line("markers", "llm: requires real LLM API key")
config.addinivalue_line("markers", "slow: takes > 30 seconds")
config.addinivalue_line("markers", "conformance: messaging backend conformance test")
def pytest_collection_modifyitems(config, items):
for item in items:
if "tests" in str(item.fspath):
item.add_marker(pytest.mark.integration)
# ── Session-scoped infrastructure ──────────────────────────────────
@pytest_asyncio.fixture(scope="session", loop_scope="session")
async def infrastructure():
"""Start Docker Compose infrastructure for the test session."""
manager = DockerComposeManager()
try:
await manager.start()
await wait_for_all_services()
logger.info("Infrastructure ready")
yield manager
finally:
if manager._started_by_us:
keep = os.getenv("ITEST_KEEP_INFRA", "").lower() in ("1", "true", "yes")
if not keep:
await manager.stop()
else:
logger.info("Keeping infrastructure running (ITEST_KEEP_INFRA=1)")
# ── Backend parameterization ──────────────────────────────────────
@pytest.fixture(params=["nats", "zenoh"])
def messaging_backend(request):
"""Parameterized messaging backend — tests run once per backend."""
selected = request.config.getoption("--backend")
if selected and request.param != selected:
pytest.skip(f"Skipping {request.param} (--backend={selected})")
return request.param
@pytest.fixture
def messaging_url(messaging_backend):
"""Messaging broker URL for the current backend."""
return BACKEND_URLS[messaging_backend]
@pytest.fixture(autouse=True)
def _set_backend_env(messaging_backend):
"""Set env vars so SDK/agent-tools auto-detect the correct backend."""
os.environ["MESSAGING_BACKEND"] = messaging_backend
if messaging_backend == "zenoh":
os.environ["DEVICE_CONNECT_DISCOVERY_MODE"] = "d2d"
os.environ["ZENOH_CONNECT"] = BACKEND_URLS["zenoh"]
yield
os.environ.pop("MESSAGING_BACKEND", None)
os.environ.pop("DEVICE_CONNECT_DISCOVERY_MODE", None)
os.environ.pop("ZENOH_CONNECT", None)
# Keep nats_url as alias for backward compatibility
@pytest.fixture
def nats_url(messaging_url) -> str:
return messaging_url
# ── Messaging client (for direct RPC calls in tests) ─────────────
@pytest_asyncio.fixture
async def messaging_client(infrastructure, messaging_backend, messaging_url):
"""Connected Edge MessagingClient for direct RPC calls in tests."""
from device_connect_edge.messaging import create_client
client = create_client(messaging_backend)
await client.connect(servers=[messaging_url])
try:
yield client
finally:
await client.close()
# ── Device spawner (uses device_connect_edge) ────────────────────────────
@pytest_asyncio.fixture
async def device_spawner(infrastructure, messaging_url):
"""Factory for spawning simulated devices via device_connect_edge."""
from fixtures.devices import DeviceFactory
factory = DeviceFactory(messaging_url=messaging_url)
try:
yield factory
finally:
await factory.cleanup()
# ── Event capture ──────────────────────────────────────────────────
@pytest_asyncio.fixture
async def event_capture(infrastructure, messaging_backend, messaging_url):
"""Messaging event capture utility."""
from fixtures.events import EventCollector
collector = EventCollector(backend=messaging_backend, url=messaging_url)
async with collector:
yield collector
# ── Event injector ─────────────────────────────────────────────────
@pytest_asyncio.fixture
async def event_injector(infrastructure, messaging_backend, messaging_url):
"""Messaging event injection utility."""
from fixtures.inject import EventInjector
injector = EventInjector(backend=messaging_backend, url=messaging_url)
async with injector:
yield injector
# ── Mock orchestrator (no LLM) ────────────────────────────────────
@pytest_asyncio.fixture
async def mock_orchestrator(infrastructure, messaging_backend, messaging_url):
"""Rule-based orchestrator for fast tests (no LLM)."""
from fixtures.orchestrator import MockOrchestrator
orchestrator = MockOrchestrator(backend=messaging_backend, url=messaging_url)
async with orchestrator:
yield orchestrator
# ── Registry cleanup ──────────────────────────────────────────────
@pytest_asyncio.fixture
async def clear_registry(infrastructure):
"""Clear all devices from registry before and after a registry-isolated test."""
count = await clear_device_registry()
logger.info(f"Registry cleared: {count} devices removed")
try:
yield count
finally:
count = await clear_device_registry()
logger.info(f"Registry cleared after test: {count} devices removed")