diff --git a/src/dashboard/apigateway/apigateway/biz/validators.py b/src/dashboard/apigateway/apigateway/biz/validators.py index e1437bb04..67386a9d1 100644 --- a/src/dashboard/apigateway/apigateway/biz/validators.py +++ b/src/dashboard/apigateway/apigateway/biz/validators.py @@ -191,9 +191,14 @@ def _validate_stage_backends(self): 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) + 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(stage=self.stage, backend_id__in=backend_ids) else: backend_configs = BackendConfig.objects.filter(stage=self.stage) @@ -202,9 +207,10 @@ def _validate_stage_backends(self): if not core_constants.HOST_WITHOUT_SCHEME_PATTERN.match(host["host"]): raise ReleaseValidationError( _( - "网关环境【{stage_name}】中的配置【后端服务地址】不合法。请在网关 `后端服务` 中进行配置。" + "网关环境【{stage_name}】中的配置【后端服务 {backend_name} 地址】不合法。请在网关 `后端服务` 中进行配置。" ).format( stage_name=self.stage.name, + backend_name=backend_config.backend.name, ) ) diff --git a/src/dashboard/apigateway/apigateway/tests/biz/test_validators.py b/src/dashboard/apigateway/apigateway/tests/biz/test_validators.py index 84bcdbe42..c3970f3ba 100644 --- a/src/dashboard/apigateway/apigateway/tests/biz/test_validators.py +++ b/src/dashboard/apigateway/apigateway/tests/biz/test_validators.py @@ -335,12 +335,7 @@ def test_validate_stage_plugins( with pytest.raises(ReleaseValidationError): publish_validator._validate_stage_plugins() - def test_validate_stage_backends( - self, - fake_stage, - fake_gateway - ): - + def test_validate_stage_backends(self, fake_stage, fake_gateway): backend = G( Backend, gateway=fake_gateway, @@ -349,13 +344,35 @@ def test_validate_stage_backends( description="test", ) - backend_config = G( + stage2 = G( + Stage, + gateway=fake_gateway, + name="test_stage2", + description="test", + ) + + G( + BackendConfig, + gateway=fake_gateway, + backend=backend, + stage=fake_stage, + config={ + "timeout": 30, + "loadbalance": "roundrobin", + "hosts": [{"scheme": "http", "host": "ee.com", "weight": 100}], + }, + ) + + 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}]}, + backend=backend, + stage=stage2, + config={ + "timeout": 30, + "loadbalance": "roundrobin", + "hosts": [{"scheme": "", "host": "", "weight": 100}], + }, ) resource_version = G( @@ -364,22 +381,23 @@ def test_validate_stage_backends( 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 - }) - } - }]), - ) + _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()