|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +Copyright 2026 The Dapr Authors |
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +""" |
| 15 | + |
| 16 | +import subprocess |
| 17 | +from typing import Callable, Iterator |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +from tests.ollama_utils import DEFAULT_MODEL, model_available, ollama_ready |
| 22 | +from tests.process_utils import get_kwargs_for_process_group, terminate_process_group |
| 23 | +from tests.wait_utils import wait_until |
| 24 | + |
| 25 | +REDIS_CONTAINER = 'dapr_redis' |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(scope='session') |
| 29 | +def flush_redis() -> None: |
| 30 | + """Flush the ``dapr_redis`` container once per session.""" |
| 31 | + subprocess.run( |
| 32 | + args=('docker', 'exec', REDIS_CONTAINER, 'redis-cli', 'FLUSHDB'), |
| 33 | + check=True, |
| 34 | + capture_output=True, |
| 35 | + timeout=10, |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture(scope='session') |
| 40 | +def redis_set_config() -> Callable[[str, str, int], None]: |
| 41 | + """Dapr encodes values in the config store as ``value||version``""" |
| 42 | + |
| 43 | + def _set(key: str, value: str, version: int = 1) -> None: |
| 44 | + subprocess.run( |
| 45 | + args=( |
| 46 | + 'docker', |
| 47 | + 'exec', |
| 48 | + REDIS_CONTAINER, |
| 49 | + 'redis-cli', |
| 50 | + 'SET', |
| 51 | + key, |
| 52 | + f'{value}||{version}', |
| 53 | + ), |
| 54 | + check=True, |
| 55 | + capture_output=True, |
| 56 | + timeout=10, |
| 57 | + ) |
| 58 | + |
| 59 | + return _set |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture(scope='session') |
| 63 | +def ollama() -> Iterator[None]: |
| 64 | + """Ensure an Ollama server with the default model is running for the session.""" |
| 65 | + started: subprocess.Popen[str] | None = None |
| 66 | + try: |
| 67 | + if not ollama_ready(): |
| 68 | + try: |
| 69 | + started = subprocess.Popen( |
| 70 | + ['ollama', 'serve'], |
| 71 | + stdout=subprocess.DEVNULL, |
| 72 | + stderr=subprocess.DEVNULL, |
| 73 | + text=True, |
| 74 | + **get_kwargs_for_process_group(), |
| 75 | + ) |
| 76 | + except FileNotFoundError as exc: |
| 77 | + pytest.fail(f'ollama CLI is not installed: {exc}') |
| 78 | + wait_until(ollama_ready, timeout=30.0, interval=0.5) |
| 79 | + |
| 80 | + if not model_available(DEFAULT_MODEL): |
| 81 | + subprocess.run(['ollama', 'pull', DEFAULT_MODEL], check=True, capture_output=True) |
| 82 | + |
| 83 | + yield |
| 84 | + finally: |
| 85 | + if started and started.poll() is None: |
| 86 | + terminate_process_group(started) |
| 87 | + try: |
| 88 | + started.wait(timeout=10) |
| 89 | + except subprocess.TimeoutExpired: |
| 90 | + terminate_process_group(started, force=True) |
| 91 | + started.wait() |
0 commit comments