Bundled commit across unrelated threads: perf collector/meter/logs/export
updates, SSO bootstrap and traefik/nginx/gitea infra, dev backend scripts
(gitea/github/woodpecker), diag test updates, and misc config.
Also fix test pollution: five tests were doing raw sys.modules.pop("conf")
to simulate ImportError on `import conf`, but raw dict mutation isn't
tracked by monkeypatch and leaked the eviction into subsequent tests.
Downstream tests that did `from conf import secret` at module level held
references to the pre-eviction conf, while re-imports inside tests got a
fresh conf, so patches to conf.cfg._data didn't land on the secret()
closure's cfg — causing tests/conf/test_conf.py::TestSecret and
tests/conf/test_connect.py::TestDuckdb::test_custom_db_name to fail.
Fix: use monkeypatch.delitem(sys.modules, "conf", raising=False) so the
eviction is reverted on teardown. Applied to test_diag_ci.py, test_init.py,
test_tracer.py, and test_resource.py (two sites).
165 lines
5.1 KiB
Python
165 lines
5.1 KiB
Python
"""Tests for perf._resource — OTel Resource builder and metadata helpers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import subprocess
|
|
|
|
|
|
class TestBuildResource:
|
|
"""build_resource() returns an OTel Resource with stack attributes."""
|
|
|
|
def test_returns_resource_with_service_name(self):
|
|
from perf._resource import build_resource
|
|
|
|
resource = build_resource()
|
|
attrs = resource.attributes
|
|
assert "service.name" in attrs
|
|
assert attrs["service.name"] # non-empty
|
|
|
|
def test_includes_service_version(self):
|
|
from perf._resource import build_resource
|
|
|
|
resource = build_resource()
|
|
assert "service.version" in resource.attributes
|
|
|
|
def test_optional_vcs_revision(self):
|
|
from perf._resource import build_resource
|
|
|
|
resource = build_resource()
|
|
# vcs.revision is set when git rev-parse succeeds; not strict
|
|
if "vcs.revision" in resource.attributes:
|
|
assert isinstance(resource.attributes["vcs.revision"], str)
|
|
|
|
def test_optional_deployment_environment(self):
|
|
from perf._resource import build_resource
|
|
|
|
resource = build_resource()
|
|
# deployment.environment is set when conf.context() succeeds
|
|
if "deployment.environment" in resource.attributes:
|
|
assert isinstance(resource.attributes["deployment.environment"], str)
|
|
|
|
|
|
class TestServiceName:
|
|
"""_service_name() resolves from env, then conf, then default."""
|
|
|
|
def test_env_var_takes_precedence(self, monkeypatch):
|
|
monkeypatch.setenv("OTEL_SERVICE_NAME", "my-service")
|
|
from perf._resource import _service_name
|
|
|
|
assert _service_name() == "my-service"
|
|
|
|
def test_falls_back_to_conf(self, monkeypatch):
|
|
monkeypatch.delenv("OTEL_SERVICE_NAME", raising=False)
|
|
from perf._resource import _service_name
|
|
|
|
# conf.cfg.telemetry.service_name should resolve from stack.toml
|
|
name = _service_name()
|
|
assert isinstance(name, str)
|
|
assert name # non-empty
|
|
|
|
def test_default_when_conf_unavailable(self, monkeypatch):
|
|
monkeypatch.delenv("OTEL_SERVICE_NAME", raising=False)
|
|
# Force the conf import path to raise
|
|
import sys
|
|
|
|
import perf._resource as mod
|
|
|
|
real_import = (
|
|
__builtins__["__import__"]
|
|
if isinstance(__builtins__, dict)
|
|
else __builtins__.__import__
|
|
)
|
|
|
|
def fake_import(name, *args, **kwargs):
|
|
if name == "conf":
|
|
raise ImportError("conf unavailable")
|
|
return real_import(name, *args, **kwargs)
|
|
|
|
monkeypatch.setattr("builtins.__import__", fake_import)
|
|
# Drop any cached conf module so the import is re-attempted
|
|
monkeypatch.delitem(sys.modules, "conf", raising=False)
|
|
assert mod._service_name() == "stack"
|
|
|
|
|
|
class TestVersion:
|
|
"""_version() reads installed package version, defaults on failure."""
|
|
|
|
def test_returns_string(self):
|
|
from perf._resource import _version
|
|
|
|
v = _version()
|
|
assert isinstance(v, str)
|
|
assert v # non-empty
|
|
|
|
def test_default_on_metadata_error(self, monkeypatch):
|
|
import perf._resource as mod
|
|
|
|
def boom(_pkg):
|
|
raise Exception("no metadata")
|
|
|
|
monkeypatch.setattr("importlib.metadata.version", boom)
|
|
assert mod._version() == "0.0.0"
|
|
|
|
|
|
class TestGitSha:
|
|
"""_git_sha() returns short SHA from git rev-parse, None on failure."""
|
|
|
|
def test_success_returns_sha(self, monkeypatch):
|
|
from perf._resource import _git_sha
|
|
|
|
class FakeResult:
|
|
returncode = 0
|
|
stdout = "abcd1234\n"
|
|
|
|
monkeypatch.setattr(subprocess, "run", lambda *a, **kw: FakeResult())
|
|
assert _git_sha() == "abcd1234"
|
|
|
|
def test_failure_returns_none(self, monkeypatch):
|
|
from perf._resource import _git_sha
|
|
|
|
def raises(*a, **kw):
|
|
raise FileNotFoundError("git not found")
|
|
|
|
monkeypatch.setattr(subprocess, "run", raises)
|
|
assert _git_sha() is None
|
|
|
|
def test_nonzero_returncode_returns_none(self, monkeypatch):
|
|
from perf._resource import _git_sha
|
|
|
|
class FakeResult:
|
|
returncode = 1
|
|
stdout = ""
|
|
|
|
monkeypatch.setattr(subprocess, "run", lambda *a, **kw: FakeResult())
|
|
assert _git_sha() is None
|
|
|
|
|
|
class TestContextName:
|
|
"""_context_name() returns conf.context().db_backend or None."""
|
|
|
|
def test_returns_string_or_none(self):
|
|
from perf._resource import _context_name
|
|
|
|
result = _context_name()
|
|
assert result is None or isinstance(result, str)
|
|
|
|
def test_returns_none_on_exception(self, monkeypatch):
|
|
import sys
|
|
|
|
import perf._resource as mod
|
|
|
|
real_import = (
|
|
__builtins__["__import__"]
|
|
if isinstance(__builtins__, dict)
|
|
else __builtins__.__import__
|
|
)
|
|
|
|
def fake_import(name, *args, **kwargs):
|
|
if name == "conf":
|
|
raise ImportError("conf unavailable")
|
|
return real_import(name, *args, **kwargs)
|
|
|
|
monkeypatch.setattr("builtins.__import__", fake_import)
|
|
monkeypatch.delitem(sys.modules, "conf", raising=False)
|
|
assert mod._context_name() is None
|