From f4e0667c2bcf11ea220719e138b2a466047f5aa1 Mon Sep 17 00:00:00 2001 From: xcwang <1366993017@qq.com> Date: Wed, 22 Nov 2023 13:22:39 +0800 Subject: [PATCH] =?UTF-8?q?bugfix:=20=20=E6=8F=92=E4=BB=B6=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E5=8C=B9=E9=85=8D=E9=80=BB=E8=BE=91=E4=B8=8D=E5=AE=8C?= =?UTF-8?q?=E6=95=B4=20(closed=20#1971)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../node_man/tests/test_views/test_plugins.py | 57 +++++++++++++++++++ apps/node_man/urls.py | 2 +- apps/node_man/views/plugin.py | 8 ++- 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 apps/node_man/tests/test_views/test_plugins.py diff --git a/apps/node_man/tests/test_views/test_plugins.py b/apps/node_man/tests/test_views/test_plugins.py new file mode 100644 index 000000000..d56e08d5b --- /dev/null +++ b/apps/node_man/tests/test_views/test_plugins.py @@ -0,0 +1,57 @@ +# -*- 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. +""" + + +from apps.backend.tests.components.collections.plugin.utils import ( + GSE_PLUGIN_DESC_INFO, + PKG_INFO, + PKG_PROJECT_NAME, +) +from apps.node_man import models +from apps.utils.unittest.testcase import CustomAPITestCase + + +class PluginViewTestCase(CustomAPITestCase): + PLUGIN_NAME = "bk_collector" + + def setUp(self) -> None: + # 初始化数据 + gse_plugin_dict = dict(GSE_PLUGIN_DESC_INFO, **{"name": self.PLUGIN_NAME, "config_file": "config.yaml"}) + models.GsePluginDesc.objects.create(**gse_plugin_dict) + package_dict = dict(PKG_INFO, **{"project": self.PLUGIN_NAME}) + models.Packages.objects.create(**package_dict) + + def test_common_plugin_list(self): + response = self.client.get(f"/api/plugin/{self.PLUGIN_NAME}/package/", {"os": "LINUX"}) + self.assertTrue(response["result"]) + self.assertEqual(response["data"][0]["project"], self.PLUGIN_NAME) + + +class PluginViewCommonTestCase(PluginViewTestCase): + PLUGIN_NAME = "bkmonitorbeat" + + +class PluginPackageDashTestCase(PluginViewTestCase): + PLUGIN_NAME = "bk-collector" + + +class PluginNotFoundViewTestCase(PluginViewTestCase): + PLUGIN_NAME = "bk_collector_not-found" + + def setUp(self) -> None: + super().setUp() + models.GsePluginDesc.objects.filter(name=self.PLUGIN_NAME).update(name=PKG_PROJECT_NAME) + models.Packages.objects.filter(project=self.PLUGIN_NAME).update(project=PKG_PROJECT_NAME) + + def test_common_plugin_list(self): + response = self.client.get(f"/api/plugin/{self.PLUGIN_NAME}/package/", {"os": "LINUX"}) + self.assertFalse(response["result"]) + self.assertRegex(response["message"], r"bk_collector_not-found in.*") diff --git a/apps/node_man/urls.py b/apps/node_man/urls.py index 42144e222..faa90fc48 100644 --- a/apps/node_man/urls.py +++ b/apps/node_man/urls.py @@ -62,7 +62,7 @@ router.register(r"policy", policy.PolicyViewSet, basename="policy") router.register(r"plugin/(?P\w+)/process", GsePluginViewSet) router.register(r"plugin", plugin.PluginViewSet, basename="plugin") -router.register(r"plugin/(?P\w+)/package", plugin.PackagesViews, basename="package") +router.register(r"plugin/(?P[\w-]+)/package", plugin.PackagesViews, basename="package") router.register(r"plugin/process", plugin.ProcessStatusViewSet, basename="process_status") router.register(r"v2/plugin", PluginV2ViewSet, basename="plugin_v2") router.register(r"healthz", HealthzViewSet, basename="healthz") diff --git a/apps/node_man/views/plugin.py b/apps/node_man/views/plugin.py index 4bc017689..7489d08f0 100644 --- a/apps/node_man/views/plugin.py +++ b/apps/node_man/views/plugin.py @@ -13,6 +13,7 @@ from rest_framework.decorators import action from rest_framework.response import Response +from apps.exceptions import ValidationError from apps.generic import ModelViewSet from apps.node_man.handlers.plugin import PluginHandler from apps.node_man.models import GsePluginDesc, Host, Packages, ProcessStatus @@ -221,9 +222,14 @@ def list(self, request, *args, **kwargs): } ] """ + project = kwargs["process"] os_type = request.query_params.get("os", "") - return Response(PluginHandler.get_packages(project, os_type)) + try: + packages = PluginHandler.get_packages(project, os_type) + except Exception as e: + raise ValidationError(f"{project} in os -> [{os_type}] not found, error: {e}") + return Response(packages) class ProcessStatusViewSet(ModelViewSet):