Skip to content

Commit

Permalink
refactor(backend): 测试框架整理重构 TencentBlueKing#9201
Browse files Browse the repository at this point in the history
  • Loading branch information
iSecloud committed Jan 24, 2025
1 parent 691e94f commit abb69a0
Show file tree
Hide file tree
Showing 37 changed files with 892 additions and 1,546 deletions.
5 changes: 4 additions & 1 deletion dbm-ui/backend/db_meta/models/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
"""
import json
import logging
import os
from collections import defaultdict
from typing import Dict, List

from django.conf import settings
from django.db import models
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -186,7 +188,8 @@ def init_spec(cls):
for spec in Spec.objects.all():
spec_namespace__name[spec.spec_cluster_type][spec.spec_machine_type].append(spec.spec_name)

with open("backend/db_meta/init/spec_init.json", "r") as f:
spec_init_path = os.path.join(settings.BASE_DIR, "backend/db_meta/init/spec_init.json")
with open(spec_init_path, "r") as f:
system_spec_init_map = json.loads(f.read())

to_init_specs: List[Spec] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from django.utils.translation import ugettext as _

from backend import env
from backend.components import DBPrivManagerApi
from backend.components.mysql_priv_manager.client import DBPrivManagerApi
from backend.configuration.constants import DBType
from backend.db_meta.enums import ClusterType
from backend.db_services.dbpermission.constants import AUTHORIZE_DATA_EXPIRE_TIME, AccountType
Expand Down
5 changes: 2 additions & 3 deletions dbm-ui/backend/tests/configuration/views/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
import pytest

from backend.configuration.models import Profile
from backend.tests.constants import TEST_ADMIN_USERNAME
from backend.ticket.constants import TicketType


@pytest.fixture
def profile():
Profile.objects.create(username=TEST_ADMIN_USERNAME, label="biz", values=["biz-1", "biz-2"])
Profile.objects.create(username="admin", label="biz", values=["biz-1", "biz-2"])
Profile.objects.create(
username=TEST_ADMIN_USERNAME,
username="admin",
label="ticket_type",
values=[TicketType.MYSQL_SINGLE_APPLY, TicketType.MYSQL_HA_APPLY],
)
205 changes: 59 additions & 146 deletions dbm-ui/backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,18 @@
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
import ipaddress
import copy
import os

import mock
import pytest
from django.contrib.auth import get_user_model
from django.utils.crypto import get_random_string

from backend.db_meta import models
from backend.db_meta.enums import AccessLayer, ClusterEntryRole, ClusterEntryType, ClusterType, MachineType
from backend.db_meta.models import AppCache, BKCity, Cluster, ClusterEntry, DBModule, LogicalCity, Machine
from backend.flow.models import FlowTree
from backend.tests.constants import TEST_ADMIN_USERNAME
from backend.db_meta.enums import ClusterEntryRole, ClusterEntryType, ClusterType
from backend.db_meta.models import AppCache, BKCity, Cluster, ClusterEntry, DBModule, LogicalCity, Machine, Spec
from backend.tests.mock_data import constant
from backend.tests.mock_data.components import cc
from backend.ticket.models import Ticket
from backend.tests.mock_data.constant import INIT_MACHINE_DATA, INIT_SPEC_DATA


def mock_bk_user(username):
Expand All @@ -45,60 +41,23 @@ def bk_user():

@pytest.fixture
def bk_admin():
return mock_bk_user(TEST_ADMIN_USERNAME)
return mock_bk_user("admin")


@pytest.fixture
def create_city():
LogicalCity.objects.get_or_create(id=1, defaults={"name": "南京"})
LogicalCity.objects.get_or_create(id=2, defaults={"name": "上海"})

BKCity.objects.create(logical_city_id=1, bk_idc_city_id=21, bk_idc_city_name="南京")
BKCity.objects.create(logical_city_id=1, bk_idc_city_id=1955, bk_idc_city_name="仪征")
BKCity.objects.create(logical_city_id=2, bk_idc_city_id=28, bk_idc_city_name="上海")


@pytest.fixture
def machine_fixture(create_city):
bk_city = BKCity.objects.first()
machine = Machine.objects.create(
ip=cc.NORMAL_IP2,
bk_biz_id=constant.BK_BIZ_ID,
machine_type=MachineType.BACKEND.value,
bk_city=bk_city,
bk_cloud_id=1,
bk_host_id=2,
)
yield machine


@pytest.fixture
def init_proxy_machine(create_city):
bk_city = models.BKCity.objects.first()
machine = models.Machine.objects.create(
ip=cc.NORMAL_IP,
bk_biz_id=constant.BK_BIZ_ID,
machine_type=MachineType.BACKEND.value,
bk_city=bk_city,
access_layer=AccessLayer.PROXY,
)
return machine


@pytest.fixture
def init_storage_machine(create_city):
bk_city = models.BKCity.objects.first()
machine = models.Machine.objects.create(
ip=cc.NORMAL_IP,
bk_biz_id=constant.BK_BIZ_ID,
machine_type=MachineType.BACKEND.value,
bk_city=bk_city,
access_layer=AccessLayer.STORAGE,
bk_host_id=int(ipaddress.IPv4Address(cc.NORMAL_IP)),
)
return machine
@pytest.fixture(scope="session", autouse=True)
def create_city(django_db_setup, django_db_blocker):
with django_db_blocker.unblock():
LogicalCity.objects.get_or_create(id=1, defaults={"name": "南京"})
LogicalCity.objects.get_or_create(id=2, defaults={"name": "上海"})
BKCity.objects.create(logical_city_id=1, bk_idc_city_id=21, bk_idc_city_name="南京")
BKCity.objects.create(logical_city_id=1, bk_idc_city_id=1955, bk_idc_city_name="仪征")
BKCity.objects.create(logical_city_id=2, bk_idc_city_id=28, bk_idc_city_name="上海")
yield
BKCity.objects.all().delete()
LogicalCity.objects.all().delete()


# TODO: 初始化各个集群的模块信息
@pytest.fixture
def init_db_module():
DBModule.objects.create(
Expand All @@ -109,7 +68,8 @@ def init_db_module():
)


@pytest.fixture
# TODO: 初始化mysql集群信息
@pytest.fixture()
def init_cluster():
cluster = Cluster.objects.create(
bk_biz_id=constant.BK_BIZ_ID,
Expand All @@ -127,93 +87,46 @@ def init_cluster():
yield cluster


@pytest.fixture
def init_cluster_entry(init_cluster):
pass


@pytest.fixture
def init_app():
app = AppCache.objects.create(
bk_biz_id=constant.BK_BIZ_ID,
db_app_abbr="DBA",
bk_biz_name="dba",
)
yield app


@pytest.fixture
def init_storage_instance():
bk_city = models.BKCity.objects.first()
machine = models.Machine.objects.create(
ip=cc.NORMAL_IP,
bk_biz_id=constant.BK_BIZ_ID,
machine_type=MachineType.BACKEND.value,
bk_city=bk_city,
access_layer=AccessLayer.PROXY,
)
cluster = Cluster.objects.first()
storage_instance = models.StorageInstance.objects.create(
version=constant.INSTANCE_VERSION,
port=constant.INSTANCE_PORT,
machine=machine,
bk_biz_id=constant.BK_BIZ_ID,
name=constant.INSTANCE_NAME,
cluster_type=ClusterType.TenDBHA.value,
)
storage_instance.cluster.add(cluster)
yield storage_instance


@pytest.fixture
def init_proxy_instance():
bk_city = models.BKCity.objects.first()
machine = models.Machine.objects.create(
ip=cc.NORMAL_IP,
bk_biz_id=constant.BK_BIZ_ID,
machine_type=MachineType.BACKEND.value,
bk_city=bk_city,
access_layer=AccessLayer.PROXY,
)
cluster = Cluster.objects.first()
proxy_instance = models.ProxyInstance.objects.create(
version=constant.INSTANCE_VERSION,
port=constant.INSTANCE_PORT,
machine=machine,
bk_biz_id=constant.BK_BIZ_ID,
name=constant.INSTANCE_NAME,
cluster_type=ClusterType.TenDBHA.value,
)
proxy_instance.cluster.add(cluster)
yield proxy_instance


@pytest.fixture
def init_flow_tree():
task = FlowTree.objects.create(
bk_biz_id=constant.BK_BIZ_ID,
uid=constant.TASK_UID,
ticket_type=constant.TICKET_TYPE,
root_id=constant.TASK_ROOT_ID,
tree={},
status=constant.TASK_STATUS,
)
yield task


@pytest.fixture
def init_ticket():
ticket = Ticket.objects.create(
bk_biz_id=constant.BK_BIZ_ID,
ticket_type=constant.TICKET_TYPE,
group=constant.DB_TYPE,
status=constant.TICKET_STATUS,
remark="",
details={},
send_msg_config={},
is_reviewed=False,
)
yield ticket
# 全局初始化AppCache信息 -- 自动创建
@pytest.fixture(scope="session", autouse=True)
def init_app(django_db_setup, django_db_blocker):
with django_db_blocker.unblock():
app = AppCache.objects.create(
bk_biz_id=constant.BK_BIZ_ID,
db_app_abbr="DBA",
bk_biz_name="dba",
)
yield app
app.delete()


# 全局初始化规格信息 -- 自动创建
@pytest.fixture(scope="session", autouse=True)
def init_spec(django_db_setup, django_db_blocker):
n = 10
specs = [
Spec(**copy.deepcopy(INIT_SPEC_DATA), spec_id=spec_id, spec_name=f"test_spec_{spec_id}")
for spec_id in range(1, n + 1)
]
with django_db_blocker.unblock():
Spec.objects.all().delete()
Spec.objects.bulk_create(specs)
yield
Spec.objects.all().delete()


# 全局初始化机器信息 -- 自动创建
@pytest.fixture(scope="session", autouse=True)
def init_machine(django_db_setup, django_db_blocker):
n = 10
machines = [
Machine(**copy.deepcopy(INIT_MACHINE_DATA), bk_host_id=host_id, ip=f"1.1.1.{host_id}")
for host_id in range(1, n + 1)
]
with django_db_blocker.unblock():
Machine.objects.bulk_create(machines)
yield
Machine.objects.all().delete()


mark_global_skip = pytest.mark.skipif(os.environ.get("GLOBAL_SKIP") == "true", reason="disable in landun WIP")
12 changes: 0 additions & 12 deletions dbm-ui/backend/tests/constants.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TestHandler:
@patch("backend.flow.utils.cc_manage.CCApi", CCApiMock())
@patch("backend.dbm_init.services.CCApi", CCApiMock())
@patch("backend.flow.utils.cc_manage.ResourceQueryHelper", ResourceQueryHelperMock())
def test_create_success(self, init_db_module, create_city):
def test_create_success(self, init_db_module):
cluster_name = "test"
clusters = [
{
Expand Down
2 changes: 1 addition & 1 deletion dbm-ui/backend/tests/db_meta/api/dbha/test_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@


@pytest.fixture
def dbha_fixture(create_city):
def dbha_fixture():
# TODO 提取常量,把 conftest 里的 yaml 用常量维护起来
city1 = models.BKCity.objects.get(bk_idc_city_name="南京")
city2 = models.BKCity.objects.get(bk_idc_city_name="上海")
Expand Down
2 changes: 1 addition & 1 deletion dbm-ui/backend/tests/db_meta/api/machine/test_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class TestCreateMachine:
@patch("backend.db_meta.api.machine.apis.CCApi", cc.CCApiMock())
def test_create_success(self, create_city):
def test_create_success(self):
"""创建成功"""
api.machine.create(
machines=[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_create_proxy_err_with_invalid_port(self):
]
)

def test_create_proxy_err_with_invalid_access_layer(self, create_city):
def test_create_proxy_err_with_invalid_access_layer(self):
"""接入层类型错误"""
bk_city = models.BKCity.objects.first()
models.Machine.objects.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


@pytest.fixture
def init_storage_machine(create_city):
def init_storage_machine():
bk_city = models.BKCity.objects.first()
machine = models.Machine.objects.create(
ip=cc.NORMAL_IP,
Expand Down Expand Up @@ -87,7 +87,7 @@ def test_create_storage_err_with_invalid_port(self):
]
)

def test_create_storage_err_with_invalid_access_layer(self, create_city):
def test_create_storage_err_with_invalid_access_layer(self):
"""接入层类型错误"""
bk_city = models.BKCity.objects.first()
models.Machine.objects.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


@pytest.fixture
def init_normal_storage_instance(create_city):
def init_normal_storage_instance():
bk_city = models.BKCity.objects.first()
machine1 = models.Machine.objects.create(
ip=cc.NORMAL_IP,
Expand Down Expand Up @@ -58,7 +58,7 @@ def init_normal_storage_instance(create_city):


@pytest.fixture
def init_remote_storage_instance(create_city):
def init_remote_storage_instance():
bk_city = models.BKCity.objects.first()
machine = models.Machine.objects.create(
ip=cc.NORMAL_IP,
Expand Down
Loading

0 comments on commit abb69a0

Please sign in to comment.