|
| 1 | +import re |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from plain.test import Client |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def real_tracing(): |
| 10 | + """Install a real SDK TracerProvider so requests get valid, sampled spans. |
| 11 | +
|
| 12 | + Without an SDK provider, OpenTelemetry hands out no-op spans whose context |
| 13 | + is invalid (all-zero trace id). The toolbar item needs a real trace id to |
| 14 | + build a link, so the export-link tests opt into this. connect itself only |
| 15 | + installs a provider when an export token is set at startup, which the test |
| 16 | + settings don't do. |
| 17 | + """ |
| 18 | + from opentelemetry import trace |
| 19 | + from opentelemetry.sdk.trace import TracerProvider |
| 20 | + from opentelemetry.sdk.trace.sampling import ALWAYS_ON |
| 21 | + |
| 22 | + if isinstance(trace.get_tracer_provider(), trace.ProxyTracerProvider): |
| 23 | + trace.set_tracer_provider(TracerProvider(sampler=ALWAYS_ON)) |
| 24 | + |
| 25 | + |
| 26 | +def test_no_trace_button_when_export_is_not_configured(db, settings): |
| 27 | + # The toolbar renders in DEBUG, but with no CONNECT_EXPORT_TOKEN the |
| 28 | + # connect item disables itself and contributes nothing. |
| 29 | + settings.DEBUG = True |
| 30 | + response = Client().get("/") |
| 31 | + assert response.status_code == 200 |
| 32 | + assert b"plainframework.com/t/" not in response.content |
| 33 | + |
| 34 | + |
| 35 | +def test_trace_button_links_to_the_dashboard(db, settings, real_tracing): |
| 36 | + # The test suite exports PLAIN_CONNECT_EXPORT_ENABLED=false, so re-enable it. |
| 37 | + settings.DEBUG = True |
| 38 | + settings.CONNECT_EXPORT_ENABLED = True |
| 39 | + settings.CONNECT_EXPORT_TOKEN = "test-token" |
| 40 | + response = Client().get("/") |
| 41 | + assert response.status_code == 200 |
| 42 | + match = re.search(rb"https://plainframework\.com/t/[0-9a-f]{32}", response.content) |
| 43 | + assert match, f"no trace link in toolbar: {response.content!r}" |
| 44 | + |
| 45 | + |
| 46 | +def test_trace_link_uses_the_configured_dashboard_url(db, settings, real_tracing): |
| 47 | + settings.DEBUG = True |
| 48 | + settings.CONNECT_EXPORT_ENABLED = True |
| 49 | + settings.CONNECT_EXPORT_TOKEN = "test-token" |
| 50 | + settings.CONNECT_DASHBOARD_URL = "https://cloud.example.com" |
| 51 | + response = Client().get("/") |
| 52 | + assert response.status_code == 200 |
| 53 | + assert re.search(rb"https://cloud\.example\.com/t/[0-9a-f]{32}", response.content) |
0 commit comments