conftest.py 3.84 KB
Newer Older
1
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
3
4
# SPDX-License-Identifier: Apache-2.0

import os
5
from io import BytesIO
6
7
8
9
10

import pytest
from pytest_httpserver import HTTPServer

from dynamo.common.utils.paths import WORKSPACE_DIR
11
from tests.serve.lora_utils import MinioLoraConfig, MinioService
12
from tests.utils.port_utils import allocate_port, deallocate_port
13
14

# Shared constants for multimodal testing
15
IMAGE_SERVER_PORT = allocate_port(8765)
16
17
18
19
20
21
MULTIMODAL_IMG_PATH = os.path.join(
    WORKSPACE_DIR, "lib/llm/tests/data/media/llm-optimize-deploy-graphic.png"
)
MULTIMODAL_IMG_URL = f"http://localhost:{IMAGE_SERVER_PORT}/llm-graphic.png"


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Git LFS pointer files start with "version "; serve a real PNG when the asset is not pulled.
def get_multimodal_test_image_bytes() -> bytes:
    """Return valid PNG bytes for /llm-graphic.png (file or minimal fallback)."""
    if os.path.isfile(MULTIMODAL_IMG_PATH):
        with open(MULTIMODAL_IMG_PATH, "rb") as f:
            data = f.read()
        if not data.startswith(b"version "):
            # GitHub path
            return data

    # Local path where we cannot retrieve the above .png file

    # Lazy import so conftest loads in environments that don't have Pillow (e.g. pre-commit).
    from PIL import Image

    buf = BytesIO()
    # TODO: differerent models / tests may expect different colors. Need to reconcicle
    # code to support all cases locally if needed.
    Image.new("RGB", (2, 2), color="green").save(buf, format="PNG")
    return buf.getvalue()


44
45
@pytest.fixture(scope="session")
def httpserver_listen_address():
46
47
    yield ("127.0.0.1", IMAGE_SERVER_PORT)
    deallocate_port(IMAGE_SERVER_PORT)
48
49
50
51
52
53
54
55
56
57
58
59
60


@pytest.fixture(scope="function")
def image_server(httpserver: HTTPServer):
    """
    Provide an HTTP server that serves test images for multimodal inference.

    This function-scoped fixture configures pytest-httpserver to serve
    the LLM optimization diagram image. It's designed for testing multimodal
    inference capabilities where models need to fetch images via HTTP.

    Currently serves:
        - /llm-graphic.png - LLM diagram image for multimodal tests
61
          (or a minimal PNG if the file is a Git LFS pointer / not pulled)
62
63
64

    Usage:
        def test_multimodal(image_server):
65
            # Use MULTIMODAL_IMG_URL from this module
66
67
            # ... use url in your test payload
    """
68
    image_data = get_multimodal_test_image_bytes()
69
70
71
72
73
74
75
76

    # Configure server endpoint
    httpserver.expect_request("/llm-graphic.png").respond_with_data(
        image_data,
        content_type="image/png",
    )

    return httpserver
77
78
79
80
81
82
83
84


@pytest.fixture(scope="function")
def minio_lora_service():
    """
    Provide a MinIO service with a pre-uploaded LoRA adapter for testing.

    This fixture:
85
    1. Connects to existing MinIO or starts a Docker container
86
87
88
89
    2. Creates the required S3 bucket
    3. Downloads the LoRA adapter from Hugging Face Hub
    4. Uploads it to MinIO
    5. Yields the MinioLoraConfig with connection details
90
    6. Cleans up after the test (only stops container if we started it)
91
92
93
94
95
96
97
98
99
100
101

    Usage:
        def test_lora(minio_lora_service):
            config = minio_lora_service
            # Use config.get_env_vars() for environment setup
            # Use config.get_s3_uri() to get the S3 URI for loading LoRA
    """
    config = MinioLoraConfig()
    service = MinioService(config)

    try:
102
        # Start or connect to MinIO
103
104
        service.start()

105
        # Create bucket and upload LoRA
106
107
108
109
        service.create_bucket()
        local_path = service.download_lora()
        service.upload_lora(local_path)

110
111
        # Clean up downloaded files (keep MinIO data intact)
        service.cleanup_download()
112
113
114
115

        yield config

    finally:
116
        # Stop MinIO only if we started it, clean up temp dirs
117
118
        service.stop()
        service.cleanup_temp()