8da43e610e
Address every substantive critique from the peer review: test_cache: replace tautological same-inputs key test with hardcoded hash assertion; assert setex call + TTL in mark_index_cached test; assert client is None for unavailable no-op; rename Packages.gz test to document intentional behaviour; add alpine sig/tmp negatives; add hyphenated and date-tag docker positive cases; add key hash-length assertion. test_config: replace live-constant comparisons with literal string assertions for alpine/rpm/docker; add unknown package type test; add dict-keyed repositories branch coverage (per-repo override and fallback); fix cache config to full equality check; add explicit empty index_patterns test. test_docker_auth: fix case-insensitive test to verify realm value; add field-order (scope before service) limitation test; add pipe-char collision documentation test; add missing fetch_token edge cases (no token field, HTTPStatusError, missing expires_in default 300); replace rubber-stamp delegate test with end-to-end parse→fetch test. test_storage: replace split prefix/suffix assertions with structural 3-part check + pinned sha256 assertion; fix Docker blob digests to 64-char hex; add secure=True URL test; add upload return value test; add download_object 404-on-ClientError test; remove redundant subset test. test_routes: add metrics.record_cache_hit/miss assertions; add mark_index_cached assertion after cache miss on index (docker + generic); add Content-Disposition, X-Artifact-Size header checks; add rpm/xml content-type tests; add flush test that verifies Redis keys are deleted when cache is available; add smoke coverage for upload (PUT), HEAD, DELETE, /metrics, and /config routes.
132 lines
3.9 KiB
Python
132 lines
3.9 KiB
Python
"""
|
|
Pytest configuration and shared fixtures.
|
|
|
|
Module-level setup (env vars + connection patches) runs before any test
|
|
module is imported, so the FastAPI app initialises against mocks rather
|
|
than real S3 / Redis / PostgreSQL services.
|
|
"""
|
|
|
|
import os
|
|
import tempfile
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import yaml
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Test remote configuration
|
|
# ---------------------------------------------------------------------------
|
|
|
|
TEST_REMOTES = {
|
|
"remotes": {
|
|
"alpine-test": {
|
|
"base_url": "https://dl-cdn.alpinelinux.org",
|
|
"type": "remote",
|
|
"package": "alpine",
|
|
"include_patterns": [".*/x86_64/.*\\.apk$"],
|
|
"cache": {"file_ttl": 0, "index_ttl": 3600},
|
|
},
|
|
"rpm-test": {
|
|
"base_url": "https://example.com/rpm",
|
|
"type": "remote",
|
|
"package": "rpm",
|
|
"include_patterns": [".*/x86_64/.*\\.rpm$", ".*/repodata/.*$"],
|
|
"cache": {"file_ttl": 0, "index_ttl": 3600},
|
|
},
|
|
"docker-test": {
|
|
"base_url": "https://registry.example.com",
|
|
"type": "remote",
|
|
"package": "docker",
|
|
"cache": {"file_ttl": 0, "index_ttl": 300},
|
|
},
|
|
"docker-restricted": {
|
|
"base_url": "https://registry.example.com",
|
|
"type": "remote",
|
|
"package": "docker",
|
|
"include_patterns": ["^library/nginx"],
|
|
"cache": {"file_ttl": 0, "index_ttl": 300},
|
|
},
|
|
"generic-test": {
|
|
"base_url": "https://releases.example.com",
|
|
"type": "remote",
|
|
"package": "generic",
|
|
"include_patterns": [".*\\.tar\\.gz$"],
|
|
"cache": {"file_ttl": 0, "index_ttl": 0},
|
|
},
|
|
"custom-index-test": {
|
|
"base_url": "https://example.com",
|
|
"type": "remote",
|
|
"package": "generic",
|
|
"index_patterns": ["metadata\\.json$"],
|
|
"cache": {"file_ttl": 0, "index_ttl": 600},
|
|
},
|
|
"local-test": {
|
|
"type": "local",
|
|
"package": "generic",
|
|
"cache": {"file_ttl": 0, "index_ttl": 0},
|
|
},
|
|
}
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Write temp config and set env vars BEFORE importing the package
|
|
# ---------------------------------------------------------------------------
|
|
|
|
_tmpdir = tempfile.mkdtemp()
|
|
_config_path = os.path.join(_tmpdir, "remotes.yaml")
|
|
with open(_config_path, "w") as _f:
|
|
yaml.dump(TEST_REMOTES, _f)
|
|
|
|
os.environ.update(
|
|
{
|
|
"CONFIG_PATH": _config_path,
|
|
"MINIO_ENDPOINT": "localhost:9000",
|
|
"MINIO_ACCESS_KEY": "testkey",
|
|
"MINIO_SECRET_KEY": "testsecret",
|
|
"MINIO_BUCKET": "testbucket",
|
|
"REDIS_URL": "redis://localhost:6379/0",
|
|
"DBHOST": "localhost",
|
|
"DBPORT": "5432",
|
|
"DBUSER": "test",
|
|
"DBPASS": "test",
|
|
"DBNAME": "test",
|
|
}
|
|
)
|
|
|
|
# Patch external service connections before the package is imported.
|
|
# These stay active for the whole session (process exits after tests finish).
|
|
_boto3_patch = patch("boto3.client", return_value=MagicMock())
|
|
_redis_patch = patch("redis.from_url", return_value=MagicMock())
|
|
_psycopg2_patch = patch("psycopg2.connect", return_value=MagicMock())
|
|
_boto3_patch.start()
|
|
_redis_patch.start()
|
|
_psycopg2_patch.start()
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Shared fixtures
|
|
# ---------------------------------------------------------------------------
|
|
|
|
import pytest # noqa: E402
|
|
from fastapi.testclient import TestClient # noqa: E402
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def app():
|
|
from artifactapi.main import app as fastapi_app
|
|
|
|
return fastapi_app
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def client(app):
|
|
return TestClient(app)
|
|
|
|
|
|
@pytest.fixture
|
|
def config_path():
|
|
return _config_path
|
|
|
|
|
|
@pytest.fixture
|
|
def test_remotes():
|
|
return TEST_REMOTES
|