|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# TencentBlueKing is pleased to support the open source community by making |
| 3 | +# 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available. |
| 4 | +# Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. |
| 5 | +# Licensed under the MIT License (the "License"); you may not use this file except |
| 6 | +# in compliance with the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://opensource.org/licenses/MIT |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software distributed under |
| 11 | +# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 12 | +# either express or implied. See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# We undertake not to change the open source license (MIT license) applicable |
| 16 | +# to the current version of the project delivered to anyone in the future. |
| 17 | +from dataclasses import dataclass |
| 18 | +from unittest import mock |
| 19 | + |
| 20 | +import pytest |
| 21 | +from rest_framework.exceptions import ValidationError |
| 22 | + |
| 23 | +from paasng.accessories.servicehub.services import ServiceObj |
| 24 | +from paasng.plat_mgt.infras.services.serializers.services import ServiceCreateSLZ |
| 25 | + |
| 26 | + |
| 27 | +@dataclass |
| 28 | +class StubServiceObj(ServiceObj): |
| 29 | + """用于测试的简单 ServiceObj 存根""" |
| 30 | + |
| 31 | + |
| 32 | +def _make_stub_service(uuid: str, name: str) -> StubServiceObj: |
| 33 | + """创建一个用于测试的 ServiceObj 存根对象""" |
| 34 | + return StubServiceObj(uuid=uuid, name=name, logo="", is_visible=True) |
| 35 | + |
| 36 | + |
| 37 | +class TestServiceCreateSLZValidateNameDuplication: |
| 38 | + """测试 ServiceCreateSLZ.validate_name 中禁止增强服务重名的逻辑""" |
| 39 | + |
| 40 | + @pytest.fixture() |
| 41 | + def current_service(self) -> StubServiceObj: |
| 42 | + """当前正在创建/更新的服务对象(放入 serializer context)""" |
| 43 | + return _make_stub_service(uuid="aaaa-1111", name="my-svc") |
| 44 | + |
| 45 | + @pytest.fixture() |
| 46 | + def existing_services(self) -> list: |
| 47 | + """模拟 mixed_service_mgr.list() 返回的已有服务列表""" |
| 48 | + return [ |
| 49 | + _make_stub_service(uuid="bbbb-2222", name="mysql"), |
| 50 | + _make_stub_service(uuid="cccc-3333", name="redis"), |
| 51 | + ] |
| 52 | + |
| 53 | + @pytest.fixture() |
| 54 | + def _mock_list(self, existing_services): |
| 55 | + """Mock mixed_service_mgr.list() 返回预设的已有服务列表""" |
| 56 | + with mock.patch("paasng.plat_mgt.infras.services.serializers.services.mixed_service_mgr") as m: |
| 57 | + m.list.return_value = existing_services |
| 58 | + yield m |
| 59 | + |
| 60 | + def _run_validate_name(self, current_service: StubServiceObj, name: str) -> str: |
| 61 | + """构造 serializer 实例并调用 validate_name""" |
| 62 | + slz = ServiceCreateSLZ(context={"service": current_service}) |
| 63 | + return slz.validate_name(name) |
| 64 | + |
| 65 | + @pytest.mark.usefixtures("_mock_list") |
| 66 | + def test_name_no_conflict(self, current_service): |
| 67 | + """名称与已有服务均不冲突时,校验通过并返回原名称""" |
| 68 | + result = self._run_validate_name(current_service, "new-svc") |
| 69 | + assert result == "new-svc" |
| 70 | + |
| 71 | + @pytest.mark.usefixtures("_mock_list") |
| 72 | + def test_name_conflicts_with_existing_service(self, current_service): |
| 73 | + """名称与另一个已有服务重名时,应抛出 ValidationError""" |
| 74 | + with pytest.raises(ValidationError, match="禁止存在相同的增强服务"): |
| 75 | + self._run_validate_name(current_service, "mysql") |
| 76 | + |
| 77 | + @pytest.mark.usefixtures("_mock_list") |
| 78 | + def test_update_allows_same_name_as_self(self, existing_services): |
| 79 | + """更新时,允许与自身重名(UUID 相同)""" |
| 80 | + # 使用已有服务列表中的第一个服务作为当前服务(模拟更新场景) |
| 81 | + self_service = existing_services[0] |
| 82 | + result = self._run_validate_name(self_service, self_service.name) |
| 83 | + assert result == self_service.name |
0 commit comments