|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available. |
| 4 | +Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved. |
| 5 | +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at https://opensource.org/licenses/MIT |
| 7 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 8 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 9 | +specific language governing permissions and limitations under the License. |
| 10 | +""" |
| 11 | +from typing import List |
| 12 | + |
| 13 | +import mock |
| 14 | + |
| 15 | +from apps.backend.api.constants import POLLING_INTERVAL |
| 16 | +from apps.backend.components.collections.agent_new.components import ( |
| 17 | + StopPluginsComponent, |
| 18 | +) |
| 19 | +from apps.mock_data import common_unit |
| 20 | +from apps.node_man import models |
| 21 | +from apps.node_man.tests.utils import NodeApi |
| 22 | +from pipeline.component_framework.test import ( |
| 23 | + ComponentTestCase, |
| 24 | + ExecuteAssertion, |
| 25 | + ScheduleAssertion, |
| 26 | +) |
| 27 | + |
| 28 | +from . import utils |
| 29 | + |
| 30 | + |
| 31 | +class StopPluginsTestCase(utils.AgentServiceBaseTestCase): |
| 32 | + CASE_NAME = "测试子订阅停止插件成功" |
| 33 | + NODEMAN_API_MOCK_PATHS = [ |
| 34 | + "common.api.NodeApi", |
| 35 | + "apps.backend.components.collections.subsubscription.NodeApi", |
| 36 | + "apps.backend.components.collections.agent_new.stop_plugins.NodeApi", |
| 37 | + ] |
| 38 | + SUBSCRIPTION_ID = common_unit.subscription.DEFAULT_SUBSCRIPTION_ID |
| 39 | + EXECUTE_RESULT = True |
| 40 | + IS_FINISHED = True |
| 41 | + SCHEDULE_RESULT = True |
| 42 | + EXTRA_OUTPUT = {} |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def init_plugins(): |
| 46 | + models.GsePluginDesc.objects.create(**common_unit.plugin.GSE_PLUGIN_DESC_MODEL_DATA) |
| 47 | + models.Packages.objects.create(**common_unit.plugin.PACKAGES_MODEL_DATA) |
| 48 | + models.ProcessStatus.objects.create(**common_unit.plugin.PROC_STATUS_MODEL_DATA) |
| 49 | + |
| 50 | + def start_patch(self): |
| 51 | + class CustomNodeApi(NodeApi): |
| 52 | + @staticmethod |
| 53 | + def create_subscription(params): |
| 54 | + return {"subscription_id": self.SUBSCRIPTION_ID} |
| 55 | + |
| 56 | + @staticmethod |
| 57 | + def get_subscription_task_status(params): |
| 58 | + task_results = NodeApi.get_subscription_task_status(params) |
| 59 | + for task_result in task_results: |
| 60 | + task_result["instance_info"]["host"]["bk_host_id"] = self.obj_factory.bk_host_ids[0] |
| 61 | + return task_results |
| 62 | + |
| 63 | + for nodeman_api_mock_path in self.NODEMAN_API_MOCK_PATHS: |
| 64 | + mock.patch(nodeman_api_mock_path, CustomNodeApi).start() |
| 65 | + |
| 66 | + def setUp(self) -> None: |
| 67 | + self.init_plugins() |
| 68 | + super().setUp() |
| 69 | + self.start_patch() |
| 70 | + |
| 71 | + def fetch_succeeded_sub_inst_ids(self) -> List[int]: |
| 72 | + return self.common_inputs["subscription_instance_ids"] |
| 73 | + |
| 74 | + def component_cls(self): |
| 75 | + return StopPluginsComponent |
| 76 | + |
| 77 | + def cases(self): |
| 78 | + return [ |
| 79 | + ComponentTestCase( |
| 80 | + name=self.CASE_NAME, |
| 81 | + inputs=self.common_inputs, |
| 82 | + parent_data={}, |
| 83 | + execute_assertion=ExecuteAssertion( |
| 84 | + success=self.EXECUTE_RESULT, |
| 85 | + outputs={ |
| 86 | + "subscription_ids": [self.SUBSCRIPTION_ID], |
| 87 | + "all_subscription_ids": [self.SUBSCRIPTION_ID], |
| 88 | + "succeeded_subscription_instance_ids": self.fetch_succeeded_sub_inst_ids(), |
| 89 | + }, |
| 90 | + ), |
| 91 | + schedule_assertion=ScheduleAssertion( |
| 92 | + success=self.SCHEDULE_RESULT, |
| 93 | + schedule_finished=self.IS_FINISHED, |
| 94 | + outputs={ |
| 95 | + "subscription_ids": [self.SUBSCRIPTION_ID], |
| 96 | + "all_subscription_ids": [self.SUBSCRIPTION_ID], |
| 97 | + "succeeded_subscription_instance_ids": self.fetch_succeeded_sub_inst_ids(), |
| 98 | + **self.EXTRA_OUTPUT, |
| 99 | + }, |
| 100 | + ), |
| 101 | + execute_call_assertion=None, |
| 102 | + ) |
| 103 | + ] |
| 104 | + |
| 105 | + @classmethod |
| 106 | + def tearDownClass(cls): |
| 107 | + mock.patch.stopall() |
| 108 | + super().tearDownClass() |
| 109 | + |
| 110 | + |
| 111 | +class StopPluginsNoSubTestCase(StopPluginsTestCase): |
| 112 | + CASE_NAME = "测试无需创建子订阅" |
| 113 | + |
| 114 | + @staticmethod |
| 115 | + def init_plugins(): |
| 116 | + pass |
| 117 | + |
| 118 | + def cases(self): |
| 119 | + return [ |
| 120 | + ComponentTestCase( |
| 121 | + name=self.CASE_NAME, |
| 122 | + inputs=self.common_inputs, |
| 123 | + parent_data={}, |
| 124 | + execute_assertion=ExecuteAssertion( |
| 125 | + success=self.EXECUTE_RESULT, |
| 126 | + outputs={ |
| 127 | + "subscription_ids": [], |
| 128 | + "all_subscription_ids": [], |
| 129 | + "succeeded_subscription_instance_ids": self.fetch_succeeded_sub_inst_ids(), |
| 130 | + }, |
| 131 | + ), |
| 132 | + schedule_assertion=[], |
| 133 | + ) |
| 134 | + ] |
| 135 | + |
| 136 | + |
| 137 | +class StopPluginsFailedTestCase(StopPluginsTestCase): |
| 138 | + CASE_NAME = "测试子订阅停止插件失败" |
| 139 | + SUBSCRIPTION_ID = common_unit.subscription.FAILED_SUBSCRIPTION_ID |
| 140 | + SCHEDULE_RESULT = False |
| 141 | + EXTRA_OUTPUT = {"succeeded_subscription_instance_ids": []} |
| 142 | + |
| 143 | + |
| 144 | +class StopPluginsCreateSubTaskFailedTestCase(StopPluginsTestCase): |
| 145 | + CASE_NAME = "测试子订阅任务创建失败" |
| 146 | + SUBSCRIPTION_ID = common_unit.subscription.CREATE_TASK_FAILED_SUBSCRIPTION_ID |
| 147 | + SCHEDULE_RESULT = False |
| 148 | + EXTRA_OUTPUT = {"succeeded_subscription_instance_ids": [], "subscription_ids": []} |
| 149 | + |
| 150 | + |
| 151 | +class StopPluginsRunningTestCase(StopPluginsTestCase): |
| 152 | + CASE_NAME = "测试子订阅停止插件运行中" |
| 153 | + SUBSCRIPTION_ID = common_unit.subscription.RUNNING_SUBSCRIPTION_ID |
| 154 | + IS_FINISHED = False |
| 155 | + EXTRA_OUTPUT = {"polling_time": POLLING_INTERVAL} |
0 commit comments