-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42953a6
commit bf025d4
Showing
4 changed files
with
283 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available. | ||
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
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 typing | ||
|
||
import mock | ||
from django.test import TestCase | ||
|
||
from apps.backend.subscription.tools import covert_biz_set_scope_to_scope | ||
from apps.backend.tests.subscription import utils | ||
from apps.node_man.handlers.cmdb import CmdbHandler | ||
from apps.node_man.models import Subscription | ||
|
||
|
||
class TestSubscrptionByScope(TestCase): | ||
SCOPE_TYPE = Subscription.ScopeType.BIZ_SET | ||
maxDiff = None | ||
|
||
def setUp(self): | ||
mock.patch.stopall() | ||
self.query_limit = mock.patch("apps.node_man.constants.QUERY_CMDB_LIMIT", 10) | ||
self.handlers_client = mock.patch("apps.node_man.handlers.cmdb.client_v2", utils.BizSetCmdbClient) | ||
self.handlers_client.start() | ||
self.query_limit.start() | ||
|
||
def topo__host_scope_with_other_biz(self, bk_biz_ids: typing.List[int]): | ||
scope = { | ||
"node_type": Subscription.NodeType.TOPO, | ||
"object_type": Subscription.ObjectType.HOST, | ||
"scope_type": self.SCOPE_TYPE, | ||
"scope_id": utils.SCOPE_ID, | ||
} | ||
scope["nodes"] = [ | ||
{"bk_obj_id": "biz", "bk_inst_id": bk_biz_id, "bk_biz_id": bk_biz_id} for bk_biz_id in bk_biz_ids | ||
] | ||
return scope | ||
|
||
def topo__host_scope_with_biz_set(self): | ||
scope = { | ||
"node_type": Subscription.NodeType.TOPO, | ||
"object_type": Subscription.ObjectType.HOST, | ||
"scope_type": self.SCOPE_TYPE, | ||
"scope_id": utils.SCOPE_ID, | ||
} | ||
scope["nodes"] = [{"bk_obj_id": "biz_set", "bk_inst_id": utils.SCOPE_ID}] | ||
return scope | ||
|
||
def instance__host_scope(self, bk_biz_ids: typing.List[int]): | ||
scope = { | ||
"node_type": Subscription.NodeType.INSTANCE, | ||
"object_type": Subscription.ObjectType.HOST, | ||
"scope_type": self.SCOPE_TYPE, | ||
"scope_id": utils.SCOPE_ID, | ||
} | ||
scope["nodes"] = [ | ||
{ | ||
"bk_host_id": bk_biz_id, | ||
"bk_biz_id": bk_biz_id, | ||
} | ||
for bk_biz_id in bk_biz_ids | ||
] | ||
return scope | ||
|
||
def topo__host_scope_with_set(self, bk_biz_ids: typing.List[int]): | ||
scope = { | ||
"node_type": Subscription.NodeType.TOPO, | ||
"object_type": Subscription.ObjectType.HOST, | ||
"scope_type": self.SCOPE_TYPE, | ||
"scope_id": utils.SCOPE_ID, | ||
} | ||
scope["nodes"] = [ | ||
{ | ||
"bk_obj_id": "set", | ||
"bk_inst_id": bk_biz_id, | ||
"bk_biz_id": bk_biz_id, | ||
} | ||
for bk_biz_id in bk_biz_ids | ||
] | ||
return scope | ||
|
||
def set_template__service_scope(self, bk_biz_ids: typing.List[int]): | ||
scope = { | ||
"node_type": Subscription.NodeType.SET_TEMPLATE, | ||
"object_type": Subscription.ObjectType.SERVICE, | ||
"scope_type": self.SCOPE_TYPE, | ||
"scope_id": utils.SCOPE_ID, | ||
} | ||
scope["nodes"] = [ | ||
{ | ||
"bk_obj_id": Subscription.NodeType.SET_TEMPLATE, | ||
"bk_inst_id": bk_biz_id, | ||
"bk_biz_id": bk_biz_id, | ||
} | ||
for bk_biz_id in bk_biz_ids | ||
] | ||
return scope | ||
|
||
def test_covert_biz_set_scope_to_scope(self): | ||
total_biz_ids = list(set(utils.BIZ_SET_COVERATE_BIZ_IDS + utils.BIZ_SET_NOT_COVERATE_BIZ_IDS)) | ||
# biz_set_scope 过滤后只包括业务集内的 node | ||
biz_set_scope = self.topo__host_scope_with_other_biz(total_biz_ids) | ||
covert_biz_set_scope_to_scope(biz_set_scope=biz_set_scope, biz_set_id=utils.SCOPE_ID) | ||
self.assertEqual(biz_set_scope, self.topo__host_scope_with_other_biz(utils.BIZ_SET_COVERATE_BIZ_IDS)) | ||
|
||
# 业务集转换为多业务 | ||
biz_set_node_scope = self.topo__host_scope_with_biz_set() | ||
covert_biz_set_scope_to_scope(biz_set_scope=biz_set_node_scope, biz_set_id=utils.SCOPE_ID) | ||
self.assertEqual(biz_set_node_scope, self.topo__host_scope_with_other_biz(utils.BIZ_SET_COVERATE_BIZ_IDS)) | ||
|
||
# 主机范围过滤 | ||
instance__host_scope = self.instance__host_scope(bk_biz_ids=total_biz_ids) | ||
covert_biz_set_scope_to_scope(biz_set_scope=instance__host_scope, biz_set_id=utils.SCOPE_ID) | ||
self.assertEqual(instance__host_scope, self.instance__host_scope(utils.BIZ_SET_COVERATE_BIZ_IDS)) | ||
|
||
# 集群范围过滤 | ||
topo__host_with_set_scope = self.topo__host_scope_with_set(bk_biz_ids=total_biz_ids) | ||
covert_biz_set_scope_to_scope(biz_set_scope=topo__host_with_set_scope, biz_set_id=utils.SCOPE_ID) | ||
self.assertEqual(topo__host_with_set_scope, self.topo__host_scope_with_set(utils.BIZ_SET_COVERATE_BIZ_IDS)) | ||
|
||
# 集群模版过滤 | ||
set_template__service_scope = self.set_template__service_scope(bk_biz_ids=total_biz_ids) | ||
covert_biz_set_scope_to_scope(biz_set_scope=set_template__service_scope, biz_set_id=utils.SCOPE_ID) | ||
self.assertEqual(set_template__service_scope, self.set_template__service_scope(utils.BIZ_SET_COVERATE_BIZ_IDS)) | ||
|
||
def test_list_business_in_business_set(self): | ||
# 测试分批查询业务集场景下参数拼接 | ||
params_list = [ | ||
{ | ||
"fields": ["bk_biz_id"], | ||
"bk_biz_set_id": utils.SCOPE_ID, | ||
"page": {"start": 0, "limit": 10, "enable_count": False, "sort": "bk_biz_id"}, | ||
}, | ||
{ | ||
"fields": ["bk_biz_id"], | ||
"bk_biz_set_id": utils.SCOPE_ID, | ||
"page": {"start": 10, "limit": 10, "enable_count": False, "sort": "bk_biz_id"}, | ||
}, | ||
{ | ||
"fields": ["bk_biz_id"], | ||
"bk_biz_set_id": utils.SCOPE_ID, | ||
"page": {"start": 20, "limit": 10, "enable_count": False, "sort": "bk_biz_id"}, | ||
}, | ||
{ | ||
"fields": ["bk_biz_id"], | ||
"bk_biz_set_id": utils.SCOPE_ID, | ||
"page": {"start": 30, "limit": 10, "enable_count": False, "sort": "bk_biz_id"}, | ||
}, | ||
{ | ||
"fields": ["bk_biz_id"], | ||
"bk_biz_set_id": utils.SCOPE_ID, | ||
"page": {"start": 40, "limit": 9, "enable_count": False, "sort": "bk_biz_id"}, | ||
}, | ||
] | ||
|
||
self.assertEqual( | ||
params_list, | ||
CmdbHandler.splice_business_set_query( | ||
biz_count=len(utils.BIZ_SET_COVERATE_BIZ_IDS), biz_set_id=utils.SCOPE_ID | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters