Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

6.0 KB
Raw Blame
import os
import subprocess
from dataclasses import replace
from pathlib import Path
from unittest.mock import MagicMock

import pytest

from tests.pytest_dify import (
    DEFAULT_LOG_FORMAT,
    DockerComposeStack,
    build_middleware_stack,
    build_vdb_stack,
    ensure_backend_test_environment,
    ensure_compose_env_files,
    parse_services,
)


def test_ensure_backend_test_environment_uses_example_env_and_stable_logging(
    tmp_path: Path,
    monkeypatch,
):
    repo_root = tmp_path
    integration_tests_dir = repo_root / "api" / "tests" / "e2e_tests" / "middleware"
    integration_tests_dir.mkdir(parents=True)
    env_example = integration_tests_dir / ".env.example"
    env_example.write_text("LOG_LEVEL=INFO\n")
    storage_root = repo_root / "storage"

    monkeypatch.setenv("LOG_FORMAT", "json")
    monkeypatch.delenv("LOG_OUTPUT_FORMAT", raising=False)
    monkeypatch.delenv("DIFY_TEST_ENV_FILE", raising=False)
    monkeypatch.delenv("DIFY_VDB_TEST_ENV_FILE", raising=False)
    monkeypatch.delenv("STORAGE_TYPE", raising=False)
    monkeypatch.delenv("OPENDAL_SCHEME", raising=False)
    monkeypatch.setenv("OPENDAL_FS_ROOT", str(storage_root))

    ensure_backend_test_environment(repo_root)

    assert os.environ["DIFY_TEST_ENV_FILE"] == str(env_example)
    assert "DIFY_VDB_TEST_ENV_FILE" not in os.environ
    assert os.environ["LOG_OUTPUT_FORMAT"] == "text"
    assert os.environ["LOG_FORMAT"] == DEFAULT_LOG_FORMAT
    assert os.environ["STORAGE_TYPE"] == "opendal"
    assert os.environ["OPENDAL_SCHEME"] == "fs"
    assert storage_root.is_dir()


def test_ensure_compose_env_files_copies_missing_env_files(tmp_path: Path):
    docker_dir = tmp_path / "docker"
    envs_dir = docker_dir / "envs"
    envs_dir.mkdir(parents=True)
    (docker_dir / ".env.example").write_text("APP_WEB_URL=http://localhost\n")
    (envs_dir / "middleware.env.example").write_text("DB_PASSWORD=difyai123456\n")

    ensure_compose_env_files(tmp_path)

    assert (docker_dir / ".env").read_text() == "APP_WEB_URL=http://localhost\n"
    assert (docker_dir / "middleware.env").read_text() == "DB_PASSWORD=difyai123456\n"


def test_parse_services_discards_empty_items():
    assert parse_services(" db_postgres, redis,, sandbox ") == ["db_postgres", "redis", "sandbox"]


def test_stack_up_uses_waiting_compose_command(monkeypatch, tmp_path: Path):
    calls: list[list[str]] = []

    def fake_run(args, **kwargs):
        calls.append(args)
        return subprocess.CompletedProcess(args=args, returncode=0)

    monkeypatch.setattr(subprocess, "run", fake_run)
    monkeypatch.setattr("time.sleep", lambda _: None)

    stack = DockerComposeStack(
        name="middleware",
        project_name="dify-pytest-middleware",
        repo_root=tmp_path,
        compose_files=(tmp_path / "docker-compose.yaml",),
        env_file=tmp_path / "middleware.env",
        services=("db_postgres", "redis"),
    )

    stack.up()

    assert calls == [
        [
            "docker",
            "compose",
            "--project-name",
            "dify-pytest-middleware",
            "--env-file",
            str(tmp_path / "middleware.env"),
            "-f",
            str(tmp_path / "docker-compose.yaml"),
            "up",
            "-d",
            "--wait",
            "--wait-timeout",
            "180",
            "db_postgres",
            "redis",
        ]
    ]


def test_builders_use_expected_compose_files(tmp_path: Path):
    middleware = build_middleware_stack(tmp_path, ["db_postgres"])
    vdb = build_vdb_stack(tmp_path, ["weaviate", "qdrant"])

    assert middleware.compose_files == (
        tmp_path / "docker" / "docker-compose.middleware.yaml",
        tmp_path / "docker" / "docker-compose.pytest.middleware.yaml",
    )
    assert middleware.env_file == tmp_path / "docker" / "middleware.env"
    assert middleware.warmup_urls == ("http://127.0.0.1:8194/health",)
    assert vdb.compose_files == (
        tmp_path / "docker" / "docker-compose.yaml",
        tmp_path / "docker" / "docker-compose.pytest.ports.yaml",
    )
    assert vdb.env_file == tmp_path / "docker" / ".env"
    assert vdb.profiles == ("weaviate", "qdrant")


@pytest.mark.parametrize(
    ("timeout", "expected_suffix"), [(None, ["down"]), (0, ["down", "--timeout", "0"]), (2, ["down", "--timeout", "2"])]
)
def test_stack_down_preserves_default_or_uses_explicit_timeout(
    monkeypatch: pytest.MonkeyPatch, tmp_path: Path, timeout: int | None, expected_suffix: list[str]
) -> None:
    calls = []
    monkeypatch.setattr(subprocess, "run", lambda args, **kwargs: calls.append((args, kwargs)))
    stack = replace(
        build_middleware_stack(tmp_path, ["db_postgres"]),
        shutdown_timeout_seconds=timeout,
    )

    stack.down()

    assert calls == [(stack._compose_command() + expected_suffix, {"cwd": tmp_path, "check": True})]
    assert build_vdb_stack(tmp_path, ["weaviate"]).shutdown_timeout_seconds is None


def test_stack_down_propagates_failure(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
    failure = subprocess.CalledProcessError(1, ["docker", "compose", "down"])

    def fail_run(*args: object, **kwargs: object) -> None:
        raise failure

    monkeypatch.setattr(subprocess, "run", fail_run)
    stack = build_middleware_stack(tmp_path, ["redis"])

    with pytest.raises(subprocess.CalledProcessError) as exc:
        stack.down()

    assert exc.value is failure


def test_healthy_middleware_still_checks_http_readiness_without_fixed_sleep(
    monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
    monkeypatch.setattr(subprocess, "run", lambda args, **_kwargs: subprocess.CompletedProcess(args=args, returncode=0))
    sleep = MagicMock()
    monkeypatch.setattr("time.sleep", sleep)
    urlopen = MagicMock()
    urlopen.return_value.__enter__.return_value.status = 200
    monkeypatch.setattr("urllib.request.urlopen", urlopen)

    build_middleware_stack(tmp_path, ["redis", "sandbox"]).up()

    urlopen.assert_called_once_with("http://127.0.0.1:8194/health", timeout=5)
    sleep.assert_not_called()