Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify env cluster mock related testing code #1407

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apiserver/paasng/tests/api/apigw/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
We undertake not to change the open source license (MIT license) applicable
to the current version of the project delivered to anyone in the future.
"""

import json
from unittest import mock

import pytest
from elasticsearch_dsl.response import Hit, Response
from elasticsearch_dsl.search import Search

pytestmark = pytest.mark.django_db
pytestmark = pytest.mark.django_db(databases=["default", "workloads"])


class TestLegacyStdoutLogAPIView:
Expand Down
8 changes: 0 additions & 8 deletions apiserver/paasng/tests/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

import pytest

from tests.utils.mocks.engine import mock_cluster_service


def _mock_initialize_vcs_with_template():
with mock.patch(
Expand Down Expand Up @@ -51,9 +49,3 @@ def _mock_bkpaas_auth_middlewares():
"apigw_manager.apigw.authentication.ApiGatewayJWTUserMiddleware", new=FakeMiddleware
):
yield


@pytest.fixture(autouse=True)
def _setup_cluster():
with mock_cluster_service():
yield
2 changes: 1 addition & 1 deletion apiserver/paasng/tests/api/test_cnative_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ def test_list_all_entrances(self, api_client, bk_app):
class TestChecklistInfoViewSet:
@pytest.fixture()
def _set_default_cluster(self, settings, bk_app):
# Create the second cluster
cluster_name = get_random_string(6)
G(Cluster, name=cluster_name, region=bk_app.region)
G(Cluster, name=CLUSTER_NAME_FOR_TESTING, region=bk_app.region)
settings.MGRLEGACY_CLOUD_NATIVE_TARGET_CLUSTER = cluster_name

@pytest.fixture(autouse=True)
Expand Down
3 changes: 2 additions & 1 deletion apiserver/paasng/tests/api/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
We undertake not to change the open source license (MIT license) applicable
to the current version of the project delivered to anyone in the future.
"""

import json
from unittest import mock

Expand All @@ -28,7 +29,7 @@
from paasng.accessories.log.shim.setup_bklog import build_custom_collector_config_name
from paasng.infras.bkmonitorv3.models import BKMonitorSpace

pytestmark = pytest.mark.django_db
pytestmark = pytest.mark.django_db(databases=["default", "workloads"])


class TestModuleStructuredLogAPIView:
Expand Down
6 changes: 2 additions & 4 deletions apiserver/paasng/tests/api/test_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
We undertake not to change the open source license (MIT license) applicable
to the current version of the project delivered to anyone in the future.
"""

import json
import logging
import uuid
Expand Down Expand Up @@ -175,7 +176,7 @@ def set_subdomain_exposed_url_type(region_config):


@pytest.mark.django_db(databases=["default", "workloads"])
@pytest.mark.usefixtures("_with_wl_apps", "_setup_cluster")
@pytest.mark.usefixtures("_with_wl_apps")
class TestSetEntrance:
def test_set_builtin_entrance(self, api_client, bk_app, bk_module, bk_prod_env):
market_config, _ = MarketConfig.objects.get_or_create_by_app(bk_app)
Expand All @@ -194,7 +195,6 @@ def test_set_builtin_entrance(self, api_client, bk_app, bk_module, bk_prod_env):
market_config.refresh_from_db()
assert market_config.source_url_type == ProductSourceUrlType.ENGINE_PROD_ENV

@pytest.mark.usefixtures("_setup_cluster")
def test_set_builtin_custom(self, api_client, bk_app, bk_module, bk_prod_env):
# setup data
# source type: custom
Expand All @@ -221,7 +221,6 @@ def test_set_builtin_custom(self, api_client, bk_app, bk_module, bk_prod_env):
assert market_config.source_url_type == ProductSourceUrlType.CUSTOM_DOMAIN
assert market_config.custom_domain_url == "http://foo-custom.example.com/subpath/"

@pytest.mark.usefixtures("_setup_cluster")
def test_set_failed(self, api_client, bk_app, bk_module, bk_prod_env):
# 切换不存在的独立域名
with override_region_configs(bk_app.region, set_subdomain_exposed_url_type):
Expand All @@ -241,7 +240,6 @@ def test_set_failed(self, api_client, bk_app, bk_module, bk_prod_env):
"fields_detail": {"url": ["http://foo-404.example.com/subpath/ 并非 default 模块的访问入口"]},
}

@pytest.mark.usefixtures("_setup_cluster")
def test_set_third_party_url(self, api_client, bk_app, bk_module, bk_prod_env):
bk_app.type = ApplicationType.ENGINELESS_APP
bk_app.save()
Expand Down
6 changes: 5 additions & 1 deletion apiserver/paasng/tests/api/test_market_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
We undertake not to change the open source license (MIT license) applicable
to the current version of the project delivered to anyone in the future.
"""

from unittest.mock import PropertyMock, patch

import pytest
Expand All @@ -32,7 +33,10 @@
from tests.conftest import mark_skip_if_console_not_configured
from tests.utils.helpers import generate_random_string

pytestmark = [mark_skip_if_console_not_configured(), pytest.mark.django_db]
pytestmark = [
mark_skip_if_console_not_configured(),
pytest.mark.django_db(databases=["default", "workloads"]),
]


@pytest.mark.usefixtures("_init_tmpls")
Expand Down
39 changes: 22 additions & 17 deletions apiserver/paasng/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@
from blue_krill.monitoring.probe.mysql import transfer_django_db_settings
from django.conf import settings
from django.core.management import call_command
from django.db import transaction
from django.test.utils import override_settings
from django.utils.crypto import get_random_string
from django_dynamic_fixture import G
from filelock import FileLock
from rest_framework.test import APIClient
from sqlalchemy.orm import scoped_session, sessionmaker

from paas_wl.infras.cluster.models import Cluster
from paas_wl.workloads.networking.entrance.addrs import Address, AddressType
from paasng.accessories.publish.sync_market.handlers import (
before_finishing_application_creation,
Expand All @@ -64,6 +65,7 @@
from tests.paasng.platform.engine.setup_utils import create_fake_deployment
from tests.utils import mock
from tests.utils.auth import create_user
from tests.utils.cluster import CLUSTER_NAME_FOR_TESTING, build_default_cluster
from tests.utils.helpers import (
_mock_wl_services_in_creation,
configure_regions,
Expand All @@ -74,25 +76,16 @@
initialize_module,
)

logger = logging.getLogger(__file__)
# Install auto-used fixture
from tests.utils.mocks.cluster import _cluster_service_allow_nonexisting_wl_apps # noqa: F401

logger = logging.getLogger(__name__)

# The default region for testing
DEFAULT_REGION = settings.DEFAULT_REGION_NAME
svn_lock_fn = Path(__file__).parent / ".svn"
# A random cluster name for running unit tests
cluster_name_fn = Path(__file__).parent / ".random"
with FileLock(str(cluster_name_fn.absolute()) + ".lock"):
if cluster_name_fn.is_file():
CLUSTER_NAME_FOR_TESTING = cluster_name_fn.read_text().strip()
else:
CLUSTER_NAME_FOR_TESTING = get_random_string(6)
cluster_name_fn.write_text(CLUSTER_NAME_FOR_TESTING)


@atexit.register
def clear_filelock():
cluster_name_fn.unlink(missing_ok=True)
svn_lock_fn.unlink(missing_ok=True)
svn_lock_fn = Path(__file__).parent / ".svn"
atexit.register(lambda: svn_lock_fn.unlink(missing_ok=True))


def pytest_addoption(parser):
Expand Down Expand Up @@ -123,7 +116,7 @@ def _configure_default_region():

@pytest.fixture(autouse=True, scope="session")
def _drop_legacy_db(request, django_db_keepdb: bool):
"""在单元测试结束后, 自动摧毁测试数据库, 除非用户显示要求保留"""
"""在单元测试结束后, 自动摧毁测试数据库, 除非用户显式要求保留"""
if django_db_keepdb:
return

Expand Down Expand Up @@ -151,6 +144,17 @@ def _configure_remote_service():
yield


@pytest.fixture(scope="session")
def django_db_setup(django_db_setup, django_db_blocker): # noqa: PT004
"""Create the default cluster for testing."""

with django_db_blocker.unblock(), transaction.atomic():
Cluster.objects.all().delete()
cluster, apiserver = build_default_cluster()
cluster.save()
apiserver.save()


def pytest_sessionstart(session):
"""Called before running all tests:

Expand Down Expand Up @@ -902,6 +906,7 @@ def _with_wl_apps(request):
bk_app = request.getfixturevalue("bk_cnative_app")
else:
bk_app = request.getfixturevalue("bk_app")

create_pending_wl_apps(bk_app, cluster_name=CLUSTER_NAME_FOR_TESTING)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from paas_wl.workloads.networking.ingress.models import AppDomain, AppDomainSharedCert, AppSubpath, Domain
from paasng.platform.modules.constants import ExposedURLType
from tests.utils.helpers import override_region_configs
from tests.utils.mocks.engine import replace_cluster_service
from tests.utils.mocks.cluster import cluster_ingress_config

pytestmark = pytest.mark.django_db(databases=["default", "workloads"])

Expand All @@ -38,8 +38,8 @@ def test_save_addresses(bk_prod_env, bk_prod_wl_app, settings):
def set_exposed_url_type(region_config):
region_config["entrance_config"]["exposed_url_type"] = ExposedURLType.SUBDOMAIN

with replace_cluster_service(
replaced_ingress_config={
with cluster_ingress_config(
replaced_config={
"sub_path_domains": [{"name": "sub.example.com"}, {"name": "sub.example.cn"}],
"app_root_domains": [{"name": "bkapps.example.com"}, {"name": "bkapps.example2.com"}],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
We undertake not to change the open source license (MIT license) applicable
to the current version of the project delivered to anyone in the future.
"""

import logging
from typing import Dict
from unittest import mock
Expand All @@ -36,7 +37,6 @@
)
from paas_wl.infras.resources.utils.basic import get_client_by_app
from tests.paas_wl.bk_app.cnative.specs.utils import create_condition, create_res, with_conds
from tests.utils.mocks.engine import replace_cluster_service

pytestmark = pytest.mark.django_db(databases=["default", "workloads"])
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -102,16 +102,14 @@ def test_deploy_and_get_status_v1alpha2(self, bk_app, bk_stag_env, bk_stag_wl_ap
},
}

with replace_cluster_service():
ret = deploy(bk_stag_env, manifest)
ret = deploy(bk_stag_env, manifest)

assert ret["spec"]["processes"][0]["name"] == "web"
assert get_mres_from_cluster(bk_stag_env) is not None

# 修改进程配置信息,再次部署到集群
manifest["spec"]["processes"].append({"name": "worker", "replicas": 1})
with replace_cluster_service():
ret = deploy(bk_stag_env, manifest)
ret = deploy(bk_stag_env, manifest)
assert ret["spec"]["processes"][1]["name"] == "worker"

@pytest.mark.usefixtures("_with_stag_ns")
Expand Down Expand Up @@ -140,7 +138,7 @@ def create_or_update_side_effect(*args, **kwargs):
}.get
with mock.patch(
"paas_wl.infras.resources.base.crd.BkApp.create_or_update", side_effect=create_or_update_side_effect
) as mocked_create_or_update, replace_cluster_service():
) as mocked_create_or_update:
client = get_client_by_app(bk_stag_wl_app)
create_or_update_bkapp_with_retries(client, bk_stag_env, manifest)

Expand Down
26 changes: 4 additions & 22 deletions apiserver/paasng/tests/paas_wl/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
We undertake not to change the open source license (MIT license) applicable
to the current version of the project delivered to anyone in the future.
"""

import atexit
import copy
import logging
Expand All @@ -42,7 +43,6 @@
from tests.paas_wl.utils.basic import random_resource_name
from tests.paas_wl.utils.build import create_build_proc
from tests.paas_wl.utils.wl_app import create_wl_release
from tests.utils.mocks.engine import build_default_cluster

logger = logging.getLogger(__name__)

Expand All @@ -53,16 +53,13 @@

@pytest.fixture(scope="session")
def django_db_setup(django_db_setup, django_db_blocker): # noqa: PT004
"""Create default cluster for testing"""
"""Some initialization jobs before running tests."""
with django_db_blocker.unblock():
with transaction.atomic():
# Clear cached configuration pool before creating default cluster in case
# there are some stale configurations in the pool.
# Clear cached configuration pool in case there are some stale configurations
# in the pool.
get_global_configuration_pool.cache_clear()

cluster = create_default_cluster()
setup_default_client(cluster)

# The initialization in `processes.models` will not create default package plans in the TEST database,
# it only creates the default plans in the non-test database(without the "test_" prefix).
# So it's still required to initialize the default package plans here.
Expand Down Expand Up @@ -212,15 +209,6 @@ def resource_name() -> str:
return random_resource_name()


def create_default_cluster():
"""Destroy all existing clusters and create a default one"""
Cluster.objects.all().delete()
cluster, apiserver = build_default_cluster()
cluster.save()
apiserver.save()
return cluster


@pytest.fixture()
def patch_ingress_config():
"""Patch ingress_config of the default cluster, usage:
Expand Down Expand Up @@ -248,12 +236,6 @@ def _patch_func(**kwargs):
cluster.save(update_fields=["ingress_config"])


def setup_default_client(cluster: Cluster):
"""setup the default config for those client created by `kubernetes.client.ApiClient`"""
# TODO: 待重构, 让 BaseKresource 不再接受 ModuleType 类型的 client
get_client_by_cluster_name(cluster.name)


def get_cluster_with_hook(hook_func: Callable) -> Callable:
"""Modify the original get_cluster function with extra hooks"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
We undertake not to change the open source license (MIT license) applicable
to the current version of the project delivered to anyone in the future.
"""

import pytest

from paas_wl.infras.resources.kube_res.exceptions import AppEntityNotFound
from paas_wl.workloads.networking.ingress.managers.subpath import SubPathAppIngressMgr, assign_subpaths
from paas_wl.workloads.networking.ingress.models import AppSubpath
from tests.utils.mocks.engine import replace_cluster_service
from tests.utils.mocks.cluster import cluster_ingress_config

pytestmark = pytest.mark.django_db(databases=["default", "workloads"])

Expand All @@ -32,15 +33,15 @@ def test_list_desired_domains_configured(self, bk_stag_wl_app):
AppSubpath.objects.create_obj(bk_stag_wl_app, "/bar/")

ingress_mgr = SubPathAppIngressMgr(bk_stag_wl_app)
with replace_cluster_service({"sub_path_domains": [{"name": "main.example.com"}]}):
with cluster_ingress_config({"sub_path_domains": [{"name": "main.example.com"}]}):
domains = ingress_mgr.list_desired_domains()
assert len(domains) == 1
assert domains[0].host == "main.example.com"
assert domains[0].path_prefix_list == ["/foo/", "/bar/"]

def test_list_desired_domains_not_configured(self, bk_stag_wl_app):
ingress_mgr = SubPathAppIngressMgr(bk_stag_wl_app)
with replace_cluster_service({"sub_path_domains": []}):
with cluster_ingress_config({"sub_path_domains": []}):
domains = ingress_mgr.list_desired_domains()
assert len(domains) == 0

Expand All @@ -49,7 +50,7 @@ def test_list_desired_domains_not_configured(self, bk_stag_wl_app):
class TestAssignSubpaths:
@pytest.fixture(autouse=True)
def _configure(self):
with replace_cluster_service({"sub_path_domains": [{"name": "main.example.com"}]}):
with cluster_ingress_config({"sub_path_domains": [{"name": "main.example.com"}]}):
yield

def test_brand_new_paths(self, bk_stag_wl_app):
Expand Down
Loading