[Bugfix] Isolate weekly CPU tests from inherited attention env and hf_api mocks (#7565)

Signed-off-by: wangyu <410167048@qq.com>
Signed-off-by: [Your Name] <your.email@example.com>
Signed-off-by: [Your Name] [Your Email]
This commit is contained in:
wangyu authored and GitHub committed 2026-09-21 15:55:00 +00:00
1 parent 2c2cffd2aa
commit 279b1b9687
22 files changed
+236 -221

No files matched your search

+1 -40
View File
@@ -3,49 +3,10 @@
"""Shared, importable test helper utilities.
Submodules (``assertions``, ``clean``, ``client``, ``media``, ``runtime``, …) are imported
Submodules (``assertions``, ``clean``, ``client``, ``media``, ``mock``, ``runtime``, …) are imported
explicitly by callers. Unit tests for these helpers live under ``tests/``.
Avoid star-importing everything here: that ran before refactor only inside the
old monolithic ``conftest``; a greedy ``__init__`` changes import order and can
affect in-process Omni (``OmniRunner`` / offline e2e) vs subprocess-based
``OmniServer`` tests.
"""
from __future__ import annotations
def skip_if_gated_repo_inaccessible(
repo_id: str,
*,
revision: str | None = None,
filename: str = "config.json",
) -> None:
"""Skip the test if a gated HuggingFace repo is not accessible.
Tries to download ``filename`` via ``hf_hub_download``, which performs an
actual file-access check (unlike ``HfApi().model_info()``
that only checks metadata). If the token has metadata access but not
file-download access, ``hf_hub_download`` will raise ``GatedRepoError``
and we skip cleanly.
"""
try:
from huggingface_hub.errors import GatedRepoError, RepositoryNotFoundError
from vllm_omni.transformers_utils.repo_utils import hf_api
except Exception:
return
try:
hf_api().hf_hub_download(repo_id=repo_id, filename=filename, revision=revision)
except GatedRepoError as exc:
import pytest
pytest.skip(
f"Skipping: gated HF repo {repo_id!r} inaccessible to the current "
f"HF_TOKEN ({exc}). See docs/contributing/ci/hf_credentials.md."
)
except RepositoryNotFoundError as exc:
import pytest
pytest.skip(f"Skipping: HF repo {repo_id!r} not found ({exc}).")
except Exception:
return
+51
View File
@@ -0,0 +1,51 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Shared pytest mocks for unit tests.
Add reusable ``monkeypatch`` helpers here instead of a new module per target.
Callers import explicitly: ``from tests.helpers.mock import ...``.
"""
from __future__ import annotations
from collections.abc import Callable
from os import PathLike
from pathlib import Path
from typing import Any
def patch_hf_snapshot_download(
monkeypatch: Any,
fake: Callable[..., Any],
*,
hf_home: str | PathLike[str] | None = None,
) -> None:
"""Replace ``snapshot_download`` on the cached ``hf_api()`` singleton.
Production code calls ``hf_api().snapshot_download``. Patching
``huggingface_hub.HfApi.snapshot_download`` misses that instance, so
weekly CPU jobs talk to the real Hub / ``HF_HOME`` cache.
``fake`` is installed on the instance, so it is called without ``self``.
Pass ``hf_home`` (typically pytest ``tmp_path``) to isolate the Hub cache.
``repo_utils`` imports ``huggingface_hub`` before tests can ``setenv``, so
``constants.HF_HUB_CACHE`` is already frozen from the process environment
(weekly: ``/fsx/hf_cache``). Downloads without ``cache_dir`` use that
constant, not ``HF_HOME``. Patch both the env and the imported constants
so a missed instance mock cannot read the builder cache.
"""
from huggingface_hub import constants as hub_constants
from vllm_omni.transformers_utils import repo_utils
monkeypatch.setattr(repo_utils.hf_api(), "snapshot_download", fake)
if hf_home is not None:
hf_home_str = str(hf_home)
hub_cache = str(Path(hf_home_str) / "hub")
monkeypatch.setenv("HF_HOME", hf_home_str)
monkeypatch.setenv("HF_HUB_CACHE", hub_cache)
monkeypatch.setattr(hub_constants, "HF_HOME", hf_home_str)
monkeypatch.setattr(hub_constants, "default_cache_path", hub_cache)
monkeypatch.setattr(hub_constants, "HF_HUB_CACHE", hub_cache)
monkeypatch.setattr(hub_constants, "HUGGINGFACE_HUB_CACHE", hub_cache)
+93
View File
@@ -0,0 +1,93 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
import os
from pathlib import Path
import pytest
from tests.helpers.mock import patch_hf_snapshot_download
pytestmark = [pytest.mark.core_model, pytest.mark.cpu]
def test_patch_hf_snapshot_download_binds_instance_without_self(monkeypatch):
from vllm_omni.transformers_utils.repo_utils import hf_api
calls: list[tuple[tuple[object, ...], dict[str, object]]] = []
def fake(*args, **kwargs):
calls.append((args, kwargs))
return "ok"
patch_hf_snapshot_download(monkeypatch, fake)
assert hf_api().snapshot_download("org/model", revision="abc") == "ok"
assert calls == [(("org/model",), {"revision": "abc"})]
def test_patch_hf_snapshot_download_sets_hf_home(monkeypatch, tmp_path):
hf_home = tmp_path / "hf-home"
patch_hf_snapshot_download(monkeypatch, lambda *args, **kwargs: "ok", hf_home=hf_home)
assert os.environ["HF_HOME"] == str(hf_home)
from huggingface_hub import constants
assert constants.HF_HOME == str(hf_home)
assert constants.HF_HUB_CACHE == str(hf_home / "hub")
assert constants.HUGGINGFACE_HUB_CACHE == str(hf_home / "hub")
assert constants.default_cache_path == str(hf_home / "hub")
def test_patch_hf_snapshot_download_leaves_hf_home_when_omitted(monkeypatch):
from huggingface_hub import constants
monkeypatch.setenv("HF_HOME", "/original-hf-home")
original_cache = constants.HF_HUB_CACHE
original_default = constants.default_cache_path
patch_hf_snapshot_download(monkeypatch, lambda *args, **kwargs: "ok")
assert os.environ["HF_HOME"] == "/original-hf-home"
assert constants.HF_HUB_CACHE == original_cache
assert constants.default_cache_path == original_default
def test_patch_hf_snapshot_download_overrides_preimported_hub_cache(monkeypatch, tmp_path):
"""Hub is imported before the helper; ``HF_HOME`` env alone does not move the cache.
``repo_utils`` / ``huggingface_hub`` compute ``constants.HF_HUB_CACHE`` at
import time. Weekly CPU already imported them with ``HF_HOME=/fsx/hf_cache``.
A download that bypasses the instance mock uses that constant, not the env.
"""
from huggingface_hub import constants, snapshot_download
from huggingface_hub.errors import LocalEntryNotFoundError
from vllm_omni.transformers_utils.repo_utils import hf_api
hf_api()
poisoned_home = tmp_path / "fsx-hf-cache"
poisoned_cache = poisoned_home / "hub"
repo_dir = poisoned_cache / "models--org--model"
(repo_dir / "refs").mkdir(parents=True)
(repo_dir / "refs" / "main").write_text("deadbeef", encoding="utf-8")
snapshot = repo_dir / "snapshots" / "deadbeef"
snapshot.mkdir(parents=True)
(snapshot / "config.json").write_text("{}", encoding="utf-8")
monkeypatch.setenv("HF_HOME", str(poisoned_home))
monkeypatch.setattr(constants, "HF_HOME", str(poisoned_home))
monkeypatch.setattr(constants, "HF_HUB_CACHE", str(poisoned_cache))
monkeypatch.setattr(constants, "HUGGINGFACE_HUB_CACHE", str(poisoned_cache))
found = snapshot_download("org/model", local_files_only=True)
assert Path(found).resolve() == snapshot.resolve()
isolated = tmp_path / "isolated-hf-home"
patch_hf_snapshot_download(monkeypatch, lambda *args, **kwargs: "mocked", hf_home=isolated)
assert os.environ["HF_HOME"] == str(isolated)
assert os.environ["HF_HUB_CACHE"] == str(isolated / "hub")
assert constants.HF_HOME == str(isolated)
assert constants.HF_HUB_CACHE == str(isolated / "hub")
assert constants.HUGGINGFACE_HUB_CACHE == str(isolated / "hub")
assert constants.default_cache_path == str(isolated / "hub")
with pytest.raises(LocalEntryNotFoundError):
snapshot_download("org/model", local_files_only=True)