Files
galaxy/test/unit/tool_util/test_resolution_cache.py
mvdbeek 679eab2150 Fix flaky test_targets_to_mulled_name_uses_persistent_cache
The shared resolution_cache fixture expired entries after 1 s, so on a
slow runner the seeded entry could expire before it was read and the
lookup fell through to a live Quay request, returning whatever build
Quay currently serves. Only test_resolution_cache_expires needs the
short expiry, so it now builds its own cache and the shared fixture
does not expire. The tag cache seeded in test_targets_to_mulled_name
had the same 1 s race; it now uses the expiry mulled_tags_for reads
with.

Quay lookups now fail the test, so a cache miss shows up as a failure
instead of a network request.
2026-10-27 03:16:54 +01:00

103 lines
4.1 KiB
Python

import time
import pytest
from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options
from galaxy.tool_util.deps.conda_util import CondaTarget
from galaxy.tool_util.deps.container_resolvers import ResolutionCache
from galaxy.tool_util.deps.container_resolvers.mulled import targets_to_mulled_name
from galaxy.tool_util.deps.mulled.util import (
_namespace_has_repo_name,
mulled_tags_for,
NAMESPACE_HAS_REPO_NAME_KEY,
QUAY_VERSIONS_CACHE_EXPIRY,
TAG_CACHE_KEY,
)
def _resolution_cache(tmpdir, expire=None):
resolution_cache = ResolutionCache()
cache_opts = {
"cache.type": "file",
"cache.data_dir": str(tmpdir / "data"),
"cache.lock_dir": str(tmpdir / "lock"),
}
if expire is not None:
cache_opts["cache.expire"] = expire
cm = CacheManager(**parse_cache_config_options(cache_opts)).get_cache("mulled_resolution")
resolution_cache.mulled_resolution_cache = cm
return resolution_cache
@pytest.fixture()
def resolution_cache(tmpdir):
return _resolution_cache(tmpdir)
@pytest.fixture(autouse=True)
def no_quay_requests(monkeypatch):
def fail(*args, **kwargs):
pytest.fail("resolution cache miss triggered a Quay request")
monkeypatch.setattr("galaxy.tool_util.deps.mulled.util.quay_repositories", fail)
monkeypatch.setattr("galaxy.tool_util.deps.mulled.util.quay_versions", fail)
def test_resolution_cache_namespace_missing(monkeypatch):
monkeypatch.setattr("galaxy.tool_util.deps.mulled.util.quay_repositories", lambda *args, **kwargs: ["othertool"])
resolution_cache = ResolutionCache()
resolution_cache.mulled_resolution_cache = {"a": "b"}
assert not _namespace_has_repo_name("bioconda", "mytool3000", resolution_cache=resolution_cache)
def test_resolution_cache_namepace_has_repo_name(resolution_cache):
resolution_cache.mulled_resolution_cache[NAMESPACE_HAS_REPO_NAME_KEY] = ["mytool3000"]
assert _namespace_has_repo_name("bioconda", "mytool3000", resolution_cache=resolution_cache)
def test_resolution_cache_expires(tmpdir):
resolution_cache = _resolution_cache(tmpdir, expire="1")
resolution_cache.mulled_resolution_cache[NAMESPACE_HAS_REPO_NAME_KEY] = ["mytool3000"]
assert NAMESPACE_HAS_REPO_NAME_KEY in resolution_cache.mulled_resolution_cache
time.sleep(1.2)
assert NAMESPACE_HAS_REPO_NAME_KEY not in resolution_cache.mulled_resolution_cache
def test_targets_to_mulled_name(resolution_cache):
resolution_cache.mulled_resolution_cache[NAMESPACE_HAS_REPO_NAME_KEY] = ["mytool3000"]
cache = resolution_cache.mulled_resolution_cache._get_cache(
"mulled_tag_cache", {"expire": QUAY_VERSIONS_CACHE_EXPIRY}
)
cache[TAG_CACHE_KEY] = {"bioconda": {"mytool3000": ["1.0", "1.1"]}}
tags = mulled_tags_for(namespace="bioconda", image="mytool3000", resolution_cache=resolution_cache)
assert tags == ["1.1", "1.0"]
def test_targets_to_mulled_name_uses_persistent_cache(resolution_cache):
cache_key = "ns[biocontainers]__single__bwa__@__0.7.17"
resolution_cache.mulled_resolution_cache[cache_key] = "bwa:0.7.17--h7132678_9"
name = targets_to_mulled_name(
[CondaTarget("bwa", "0.7.17")],
hash_func="v2",
namespace="biocontainers",
resolution_cache=resolution_cache,
)
assert name == "bwa:0.7.17--h7132678_9"
def test_mulled_tags_are_written_back_to_persistent_cache(resolution_cache, monkeypatch):
resolution_cache.mulled_resolution_cache[NAMESPACE_HAS_REPO_NAME_KEY] = ["mytool3000"]
monkeypatch.setattr("galaxy.tool_util.deps.mulled.util.quay_versions", lambda *args, **kwargs: ["1.0"])
assert mulled_tags_for("biocontainers", "mytool3000", resolution_cache=resolution_cache) == ["1.0"]
fresh_request_cache = ResolutionCache()
fresh_request_cache.mulled_resolution_cache = resolution_cache.mulled_resolution_cache
monkeypatch.setattr(
"galaxy.tool_util.deps.mulled.util.quay_versions",
lambda *args, **kwargs: pytest.fail("cached tags should prevent a second Quay request"),
)
assert mulled_tags_for("biocontainers", "mytool3000", resolution_cache=fresh_request_cache) == ["1.0"]