From 56b990fb4ecb07aae42a7a685484a74082f95982 Mon Sep 17 00:00:00 2001 From: F-cq <42882578+F-cq@users.noreply.github.com> Date: Wed, 30 Oct 2024 17:23:56 +0800 Subject: [PATCH] =?UTF-8?q?fix(biz/validators):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=8F=91=E5=B8=83=E7=8E=AF=E5=A2=83=E6=A0=A1=E9=AA=8C=E7=9A=84?= =?UTF-8?q?=E5=90=8E=E7=AB=AF=E9=85=8D=E7=BD=AE=E9=97=AE=E9=A2=98=20(#1089?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apigateway/apigateway/biz/validators.py | 10 +++- .../apigateway/tests/biz/test_validators.py | 49 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/dashboard/apigateway/apigateway/biz/validators.py b/src/dashboard/apigateway/apigateway/biz/validators.py index 85464d27b..e1437bb04 100644 --- a/src/dashboard/apigateway/apigateway/biz/validators.py +++ b/src/dashboard/apigateway/apigateway/biz/validators.py @@ -188,7 +188,15 @@ def __init__(self, gateway: Gateway, stage: Stage, resource_version: Optional[Re def _validate_stage_backends(self): """校验待发布环境的backend配置""" - backend_configs = BackendConfig.objects.filter(stage=self.stage) + resource_version = self.resource_version + + if resource_version and resource_version.data: + backend_ids = list({resource["proxy"]["backend_id"] for resource in resource_version.data + if resource["proxy"].get("backend_id", None)}) + backend_configs = BackendConfig.objects.filter(backend_id__in=backend_ids) + else: + backend_configs = BackendConfig.objects.filter(stage=self.stage) + for backend_config in backend_configs: for host in backend_config.config["hosts"]: if not core_constants.HOST_WITHOUT_SCHEME_PATTERN.match(host["host"]): diff --git a/src/dashboard/apigateway/apigateway/tests/biz/test_validators.py b/src/dashboard/apigateway/apigateway/tests/biz/test_validators.py index f0318a948..84bcdbe42 100644 --- a/src/dashboard/apigateway/apigateway/tests/biz/test_validators.py +++ b/src/dashboard/apigateway/apigateway/tests/biz/test_validators.py @@ -335,6 +335,55 @@ def test_validate_stage_plugins( with pytest.raises(ReleaseValidationError): publish_validator._validate_stage_plugins() + def test_validate_stage_backends( + self, + fake_stage, + fake_gateway + ): + + backend = G( + Backend, + gateway=fake_gateway, + type="http", + name="test_name", + description="test", + ) + + backend_config = G( + BackendConfig, + gateway=fake_gateway, + backend_id=backend.id, + stage_id=227, + config={"timeout": 30, "loadbalance": "roundrobin", + "hosts": [{"scheme": "http", "host": "ee.com", "weight": 100}]}, + ) + + resource_version = G( + ResourceVersion, + gateway=fake_gateway, + version="1", + name="11", + title="11", + _data=json.dumps([{ + "id": 1, + "name": "approval_add_workitems", + "proxy": { + "id": 28, + "type": "http", + "backend_id": backend.id, + "config": json.dumps({ + "method": "ANY", + "path": "/api/v2/", + "match_subpath": False, + "timeout": 0 + }) + } + }]), + ) + + publish_validator = PublishValidator(fake_gateway, fake_stage, resource_version) + publish_validator._validate_stage_backends() + class TestSchemeInputValidator: @pytest.mark.parametrize(