diff --git a/apps/backend/subscription/tools.py b/apps/backend/subscription/tools.py index a470aace5..e1cb03b86 100644 --- a/apps/backend/subscription/tools.py +++ b/apps/backend/subscription/tools.py @@ -1046,9 +1046,13 @@ def get_all_subscription_steps_context( all_step_data[step.step_id] = step_context plugin_path = get_plugin_path(plugin_name, target_host, agent_config) - # 当前step_id的数据单独拎出来,作为 shortcut - step_params = policy_step_adapter.get_matching_step_params(target_host.os_type.lower(), target_host.cpu_arch) + # 将 step.params 中 context 提取到第一层,提供给模板渲染 + step_params = policy_step_adapter.get_matching_step_params( + target_host.os_type.lower(), + target_host.cpu_arch + ) + context.update(step_params.get("context", {})) context.update(all_step_data[subscription_step.step_id]) context.update( diff --git a/apps/backend/tests/plugin/views/test_template_render.py b/apps/backend/tests/plugin/views/test_template_render.py index 51ab43696..9270d8539 100644 --- a/apps/backend/tests/plugin/views/test_template_render.py +++ b/apps/backend/tests/plugin/views/test_template_render.py @@ -21,6 +21,8 @@ ) from apps.node_man import constants, models from apps.utils.unittest.testcase import CustomAPITestCase +from apps.node_man.tests.utils import create_host +from apps.backend.subscription.tools import get_all_subscription_steps_context PLATFORMS = ["linux_x86_64", "linux_x86", "windows_x86_64"] @@ -343,6 +345,70 @@ def test_plugin_version__without_tmpl_case(self): match_config_tmpl_version="1.2", ) + def test_get_all_subscription_steps_context(self): + """测试获取渲染配置模板参数""" + # 创建主机数据 + host_list, _, _ = create_host( + 1, + bk_host_id=1, + bk_cloud_id=0, + ip="127.0.0.1", + os_type=constants.OsType.LINUX, + ) + target_host = host_list[0] + + # 创建订阅数据 + subscription = models.Subscription.objects.create( + bk_biz_id=target_host.bk_biz_id, + object_type=models.Subscription.ObjectType.HOST, + node_type=models.Subscription.NodeType.TOPO, + nodes=[{"bk_host_id": target_host.bk_host_id}], + from_system="blueking", + enable=False, + creator="admin", + ) + subscription_step = models.SubscriptionStep.objects.create( + subscription_id=subscription.id, + index=0, + step_id=common_unit.plugin.PLUGIN_NAME, + type="PLUGIN", + config={ + "plugin_name": common_unit.plugin.PLUGIN_NAME, + "plugin_version": common_unit.plugin.PACKAGE_VERSION, + "config_templates": [{ + "name": common_unit.plugin.GSE_PLUGIN_DESC_MODEL_DATA["config_file"], + "version": common_unit.plugin.PACKAGE_VERSION + }] + }, + params={"context": {"params_context": "test"}}, + ) + + # 创建插件相关数据数据 + models.GsePluginDesc.objects.create(**common_unit.plugin.GSE_PLUGIN_DESC_MODEL_DATA) + models.Packages.objects.create(**common_unit.plugin.PACKAGES_MODEL_DATA) + models.ProcControl.objects.create(**common_unit.plugin.PROC_CONTROL_MODEL_DATA) + + proc_status_model_data = deepcopy(common_unit.plugin.PROC_STATUS_MODEL_DATA) + proc_status_model_data["group_id"] = f"sub_{subscription.id}_host_{target_host.bk_host_id}" + proc_status_model_data["source_id"] = subscription.id + models.ProcessStatus.objects.create(**proc_status_model_data) + + policy_step_adapter = PolicyStepAdapter(subscription_step) + context = get_all_subscription_steps_context( + subscription_step, + {"host": {"bk_host_id": target_host.bk_host_id}}, + target_host, + common_unit.plugin.PLUGIN_NAME, + target_host.agent_config, + policy_step_adapter, + ) + + # 验证 context 中包含 control_info + self.assertTrue("control_info" in context.keys()) + + # 验证 step.params.context 处于 context 根节点下 + self.assertTrue("params_context" in context.keys()) + # 当前没有指定具体插件模板版本好的需求 有需要再打开 # def test_plugin_version__lower_tmpl_case(self): diff --git a/apps/mock_data/common_unit/plugin.py b/apps/mock_data/common_unit/plugin.py index 2be885bf6..b2704d2a3 100644 --- a/apps/mock_data/common_unit/plugin.py +++ b/apps/mock_data/common_unit/plugin.py @@ -74,3 +74,21 @@ "health_cmd": f"./{PLUGIN_NAME} -z", "debug_cmd": f"./{PLUGIN_NAME} -d", } + +PROC_STATUS_MODEL_DATA = { + "name": PLUGIN_NAME, + "status": "RUNNING", + "is_auto": "AUTO", + "version": PACKAGE_VERSION, + "proc_type": "PLUGIN", + "configs": [], + "setup_path": "/usr/local/gse/plugins/bin", + "log_path": "/var/log/gse", + "data_path": "/var/lib/gse", + "pid_path": f"/var/run/gse/{PLUGIN_NAME}.pid", + "group_id": "sub_1_host_1", + "source_type": "subscription", + "source_id": 1, + "bk_host_id": 1, + "is_latest": True, +} diff --git a/apps/node_man/tests/utils.py b/apps/node_man/tests/utils.py index b305d23fa..bab618de8 100644 --- a/apps/node_man/tests/utils.py +++ b/apps/node_man/tests/utils.py @@ -68,6 +68,7 @@ def create_host_from_a_to_b( outer_ip=None, login_ip=None, proc_type=None, + os_type=None, ): # 若传了bk_host_id,number必须为1 host_to_create = [] @@ -90,7 +91,7 @@ def create_host_from_a_to_b( login_ip or f"{random.randint(1, 250)}.{random.randint(1, 250)}." f"{random.randint(1, 250)}.1" ), "node_type": node_type or ["AGENT", "PAGENT", "PROXY"][random.randint(0, 2)], - "os_type": constants.OS_TUPLE[random.randint(0, 1)], + "os_type": os_type or constants.OS_TUPLE[random.randint(0, 1)], "ap_id": 1, "upstream_nodes": upstream_nodes if upstream_nodes else [i for i in range(100)], "extra_data": { @@ -143,6 +144,7 @@ def create_host( outer_ip=None, login_ip=None, proc_type=None, + os_type=None, ): # 若传了bk_host_id,number必须为1 host_to_create = [] @@ -164,6 +166,7 @@ def create_host( outer_ip=outer_ip, login_ip=login_ip, proc_type=proc_type, + os_type=os_type, ) else: host, process, identity = create_host_from_a_to_b( @@ -178,6 +181,7 @@ def create_host( outer_ip=outer_ip, login_ip=login_ip, proc_type=proc_type, + os_type=os_type, ) host_to_create.extend(host) process_to_create.extend(process)