From adaed7fa7377ac56b78bcd9464f83d09a5744142 Mon Sep 17 00:00:00 2001 From: uy_sun Date: Wed, 20 Nov 2024 16:34:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(docker=5Ftest):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E6=B5=8B=E8=AF=95=E8=8E=B7=E5=8F=96=E5=88=B0?= =?UTF-8?q?=E7=9A=84=E6=8F=92=E4=BB=B6=E5=85=83=E6=95=B0=E6=8D=AE=E4=B8=BB?= =?UTF-8?q?=E9=A1=B5=E4=B8=BA=E7=A9=BA=E6=97=B6=E6=8A=A5=E9=94=99=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 ++ src/providers/docker_test/__init__.py | 4 +- .../docker_test/test_docker_plugin_test.py | 64 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5855300..ab85c99d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/ ## [Unreleased] +### Fixed + +- 修复插件测试获取到的插件元数据主页为空时报错的问题 + ## [4.0.6] - 2024-11-20 ### Fixed diff --git a/src/providers/docker_test/__init__.py b/src/providers/docker_test/__init__.py index 62cf0015..6c95dd56 100644 --- a/src/providers/docker_test/__init__.py +++ b/src/providers/docker_test/__init__.py @@ -1,11 +1,11 @@ import json from typing import Any -import docker from pydantic import BaseModel, Field, field_validator, model_validator from pydantic_core import PydanticCustomError from pyjson5 import Json5DecoderException +import docker from src.providers.constants import DOCKER_IMAGES, REGISTRY_PLUGINS_URL from src.providers.utils import load_json @@ -15,7 +15,7 @@ class Metadata(BaseModel): name: str desc: str - homepage: str + homepage: str | None = None type: str | None = None supported_adapters: list[str] | None = None diff --git a/tests/utils/docker_test/test_docker_plugin_test.py b/tests/utils/docker_test/test_docker_plugin_test.py index 97f3915d..5d66afd9 100644 --- a/tests/utils/docker_test/test_docker_plugin_test.py +++ b/tests/utils/docker_test/test_docker_plugin_test.py @@ -52,3 +52,67 @@ async def test_docker_plugin_test(mocked_api: MockRouter, mocker: MockerFixture) detach=False, remove=True, ) + + +async def test_docker_plugin_test_metadata_some_fields_empty( + mocked_api: MockRouter, mocker: MockerFixture +): + """测试 metadata 的部分字段为空""" + from src.providers.docker_test import DockerPluginTest, DockerTestResult, Metadata + + mocked_run = mocker.Mock() + mocked_run.return_value = json.dumps( + { + "metadata": { + "name": "name", + "desc": "desc", + "homepage": None, + "type": None, + "supported_adapters": None, + }, + "outputs": ["test"], + "load": True, + "run": True, + "version": "0.0.1", + "config": "", + } + ).encode() + mocked_client = mocker.Mock() + mocked_client.containers.run = mocked_run + mocked_docker = mocker.patch("docker.DockerClient") + mocked_docker.return_value = mocked_client + + test = DockerPluginTest("project_link", "module_name") + result = await test.run("3.12") + + assert result == snapshot( + DockerTestResult( + config="", + load=True, + metadata=Metadata( + desc="desc", + homepage=None, + name="name", + supported_adapters=None, + type=None, + ), + outputs=["test"], + run=True, + test_env="python==3.12", + version="0.0.1", + ) + ) + + assert not mocked_api["store_plugins"].called + mocked_run.assert_called_once_with( + "ghcr.io/nonebot/nonetest:3.12-latest", + environment=snapshot( + { + "PLUGIN_INFO": "project_link:module_name", + "PLUGIN_CONFIG": "", + "PLUGINS_URL": "https://raw.githubusercontent.com/nonebot/registry/results/plugins.json", + } + ), + detach=False, + remove=True, + )