Skip to content

Commit

Permalink
bugfix: 插件路由匹配逻辑不完整 (closed #1971)
Browse files Browse the repository at this point in the history
  • Loading branch information
CohleRustW authored and ZhuoZhuoCrayon committed Feb 29, 2024
1 parent a449a44 commit f4e0667
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
57 changes: 57 additions & 0 deletions apps/node_man/tests/test_views/test_plugins.py
Original file line number Diff line number Diff line change
@@ -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.*")
2 changes: 1 addition & 1 deletion apps/node_man/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
router.register(r"policy", policy.PolicyViewSet, basename="policy")
router.register(r"plugin/(?P<category>\w+)/process", GsePluginViewSet)
router.register(r"plugin", plugin.PluginViewSet, basename="plugin")
router.register(r"plugin/(?P<process>\w+)/package", plugin.PackagesViews, basename="package")
router.register(r"plugin/(?P<process>[\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")
Expand Down
8 changes: 7 additions & 1 deletion apps/node_man/views/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit f4e0667

Please sign in to comment.