Skip to content

Commit

Permalink
fix(biz/validators): 修复缺少stage查询条件问题 (#1104)
Browse files Browse the repository at this point in the history
  • Loading branch information
F-cq authored Nov 5, 2024
1 parent f6669ce commit 4dfdffb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 31 deletions.
14 changes: 10 additions & 4 deletions src/dashboard/apigateway/apigateway/biz/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
)
)

Expand Down
72 changes: 45 additions & 27 deletions src/dashboard/apigateway/apigateway/tests/biz/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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()
Expand Down

0 comments on commit 4dfdffb

Please sign in to comment.