From cacb47d53281f411eccb70ecb5c5149663e3b7b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E9=A3=8F?= Date: Fri, 27 Dec 2024 13:55:39 +0800 Subject: [PATCH] add ut --- core/monitor/SelfMonitorServer.h | 3 + core/pipeline/Pipeline.h | 1 + core/plugin/input/InputInternalMetrics.h | 3 + core/unittest/input/CMakeLists.txt | 4 + .../input/InputInternalMetricsUnittest.cpp | 184 ++++++++++++++++++ 5 files changed, 195 insertions(+) create mode 100644 core/unittest/input/InputInternalMetricsUnittest.cpp diff --git a/core/monitor/SelfMonitorServer.h b/core/monitor/SelfMonitorServer.h index cea3448b0c..fc92f6ac17 100644 --- a/core/monitor/SelfMonitorServer.h +++ b/core/monitor/SelfMonitorServer.h @@ -56,6 +56,9 @@ class SelfMonitorServer { PipelineContext* mAlarmPipelineCtx; std::mutex mAlarmPipelineMux; +#ifdef APSARA_UNIT_TEST_MAIN + friend class InputInternalMetricsUnittest; +#endif }; } // namespace logtail \ No newline at end of file diff --git a/core/pipeline/Pipeline.h b/core/pipeline/Pipeline.h index 29666c68c1..b03457e71a 100644 --- a/core/pipeline/Pipeline.h +++ b/core/pipeline/Pipeline.h @@ -121,6 +121,7 @@ class Pipeline { friend class PipelineUnittest; friend class InputContainerStdioUnittest; friend class InputFileUnittest; + friend class InputInternalMetricsUnittest; friend class InputPrometheusUnittest; friend class ProcessorTagNativeUnittest; friend class FlusherSLSUnittest; diff --git a/core/plugin/input/InputInternalMetrics.h b/core/plugin/input/InputInternalMetrics.h index 694edf85af..6a11d5788e 100644 --- a/core/plugin/input/InputInternalMetrics.h +++ b/core/plugin/input/InputInternalMetrics.h @@ -32,6 +32,9 @@ class InputInternalMetrics : public Input { bool SupportAck() const override { return true; } private: SelfMonitorMetricRules mSelfMonitorMetricRules; +#ifdef APSARA_UNIT_TEST_MAIN + friend class InputInternalMetricsUnittest; +#endif }; } // namespace logtail \ No newline at end of file diff --git a/core/unittest/input/CMakeLists.txt b/core/unittest/input/CMakeLists.txt index 49129244cf..4544e875bf 100644 --- a/core/unittest/input/CMakeLists.txt +++ b/core/unittest/input/CMakeLists.txt @@ -36,6 +36,9 @@ target_link_libraries(input_ebpf_network_security_unittest unittest_base) add_executable(input_ebpf_network_observer_unittest InputNetworkObserverUnittest.cpp) target_link_libraries(input_ebpf_network_observer_unittest unittest_base) +add_executable(input_internal_metrics_unittest InputInternalMetricsUnittest.cpp) +target_link_libraries(input_internal_metrics_unittest ${UT_BASE_TARGET}) + include(GoogleTest) gtest_discover_tests(input_file_unittest) gtest_discover_tests(input_container_stdio_unittest) @@ -44,3 +47,4 @@ gtest_discover_tests(input_ebpf_file_security_unittest) gtest_discover_tests(input_ebpf_process_security_unittest) gtest_discover_tests(input_ebpf_network_security_unittest) gtest_discover_tests(input_ebpf_network_observer_unittest) +gtest_discover_tests(input_internal_metrics_unittest) diff --git a/core/unittest/input/InputInternalMetricsUnittest.cpp b/core/unittest/input/InputInternalMetricsUnittest.cpp new file mode 100644 index 0000000000..e9e01455b5 --- /dev/null +++ b/core/unittest/input/InputInternalMetricsUnittest.cpp @@ -0,0 +1,184 @@ +// Copyright 2023 iLogtail Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +#include + +#include +#include +#include + +#include "app_config/AppConfig.h" +#include "common/JsonUtil.h" +#include "monitor/Monitor.h" +#include "pipeline/Pipeline.h" +#include "pipeline/PipelineContext.h" +#include "pipeline/plugin/PluginRegistry.h" +#include "plugin/input/InputInternalMetrics.h" +#include "unittest/Unittest.h" + +DECLARE_FLAG_INT32(default_plugin_log_queue_size); + +using namespace std; + +namespace logtail { + +class InputInternalMetricsUnittest : public testing::Test { +public: + void OnInit(); + void OnPipelineUpdate(); + +protected: + static void SetUpTestCase() { + LoongCollectorMonitor::GetInstance()->Init(); + PluginRegistry::GetInstance()->LoadPlugins(); + } + + static void TearDownTestCase() { + PluginRegistry::GetInstance()->UnloadPlugins(); + LoongCollectorMonitor::GetInstance()->Stop(); + } + + void SetUp() override { + p.mName = "test_config"; + ctx.SetConfigName("test_config"); + p.mPluginID.store(0); + ctx.SetPipeline(p); + } + +private: + Pipeline p; + PipelineContext ctx; +}; + +void InputInternalMetricsUnittest::OnInit() { + unique_ptr input; + Json::Value configJson, optionalGoPipeline; + string configStr, errorMsg; + + configStr = R"( + { + "Type": "input_internal_metrics", + "Agent": { + "Enable": true, + "Interval": 1 + }, + "Runner": { + "Enable": false, + "Interval": 2 + }, + "Pipeline": { + "Enable": true, + "Interval": 3 + }, + "PluginSource": { + "Enable": true, + "Interval": 4 + }, + "Plugin": { + "Enable": true, + "Interval": 5 + }, + "Component": { + "Enable": false, + "Interval": 6 + } + } + )"; + APSARA_TEST_TRUE(ParseJsonTable(configStr, configJson, errorMsg)); + input.reset(new InputInternalMetrics()); + input->SetContext(ctx); + input->SetMetricsRecordRef(InputInternalMetrics::sName, "1"); + APSARA_TEST_TRUE(input->Init(configJson, optionalGoPipeline)); + APSARA_TEST_TRUE(input->Start()); + APSARA_TEST_EQUAL(input->mSelfMonitorMetricRules.mAgentMetricsRule.mEnable, true); + APSARA_TEST_EQUAL(input->mSelfMonitorMetricRules.mAgentMetricsRule.mInterval, 1); + APSARA_TEST_EQUAL(input->mSelfMonitorMetricRules.mComponentMetricsRule.mEnable, false); + APSARA_TEST_EQUAL(input->mSelfMonitorMetricRules.mComponentMetricsRule.mInterval, 6); + APSARA_TEST_EQUAL(input->mSelfMonitorMetricRules.mPipelineMetricsRule.mEnable, true); + APSARA_TEST_EQUAL(input->mSelfMonitorMetricRules.mPipelineMetricsRule.mInterval, 3); + APSARA_TEST_EQUAL(input->mSelfMonitorMetricRules.mPluginMetricsRule.mEnable, true); + APSARA_TEST_EQUAL(input->mSelfMonitorMetricRules.mPluginMetricsRule.mInterval, 5); + APSARA_TEST_EQUAL(input->mSelfMonitorMetricRules.mPluginSourceMetricsRule.mEnable, true); + APSARA_TEST_EQUAL(input->mSelfMonitorMetricRules.mPluginSourceMetricsRule.mInterval, 4); + APSARA_TEST_EQUAL(input->mSelfMonitorMetricRules.mRunnerMetricsRule.mEnable, false); + APSARA_TEST_EQUAL(input->mSelfMonitorMetricRules.mRunnerMetricsRule.mInterval, 2); + APSARA_TEST_TRUE(input->Stop(true)); +} + +void InputInternalMetricsUnittest::OnPipelineUpdate() { + Json::Value configJson, optionalGoPipeline; + InputInternalMetrics input; + input.SetContext(ctx); + string configStr, errorMsg; + + configStr = R"( + { + "Type": "input_internal_metrics", + "Agent": { + "Enable": false, + "Interval": 7 + }, + "Runner": { + "Enable": true, + "Interval": 8 + }, + "Pipeline": { + "Enable": false, + "Interval": 9 + }, + "PluginSource": { + "Enable": false, + "Interval": 10 + }, + "Plugin": { + "Enable": false, + "Interval": 11 + }, + "Component": { + "Enable": true, + "Interval": 12 + } + } + )"; + APSARA_TEST_TRUE(ParseJsonTable(configStr, configJson, errorMsg)); + input.SetContext(ctx); + input.SetMetricsRecordRef(InputInternalMetrics::sName, "1"); + APSARA_TEST_TRUE(input.Init(configJson, optionalGoPipeline)); + + APSARA_TEST_TRUE(input.Start()); + APSARA_TEST_NOT_EQUAL(nullptr, SelfMonitorServer::GetInstance()->mSelfMonitorMetricRules); + APSARA_TEST_EQUAL(SelfMonitorServer::GetInstance()->mSelfMonitorMetricRules->mAgentMetricsRule.mEnable, false); + APSARA_TEST_EQUAL(SelfMonitorServer::GetInstance()->mSelfMonitorMetricRules->mAgentMetricsRule.mInterval, 7); + APSARA_TEST_EQUAL(SelfMonitorServer::GetInstance()->mSelfMonitorMetricRules->mComponentMetricsRule.mEnable, true); + APSARA_TEST_EQUAL(SelfMonitorServer::GetInstance()->mSelfMonitorMetricRules->mComponentMetricsRule.mInterval, 12); + APSARA_TEST_EQUAL(SelfMonitorServer::GetInstance()->mSelfMonitorMetricRules->mPipelineMetricsRule.mEnable, false); + APSARA_TEST_EQUAL(SelfMonitorServer::GetInstance()->mSelfMonitorMetricRules->mPipelineMetricsRule.mInterval, 9); + APSARA_TEST_EQUAL(SelfMonitorServer::GetInstance()->mSelfMonitorMetricRules->mPluginMetricsRule.mEnable, false); + APSARA_TEST_EQUAL(SelfMonitorServer::GetInstance()->mSelfMonitorMetricRules->mPluginMetricsRule.mInterval, 11); + APSARA_TEST_EQUAL(SelfMonitorServer::GetInstance()->mSelfMonitorMetricRules->mPluginSourceMetricsRule.mEnable, false); + APSARA_TEST_EQUAL(SelfMonitorServer::GetInstance()->mSelfMonitorMetricRules->mPluginSourceMetricsRule.mInterval, 10); + APSARA_TEST_EQUAL(SelfMonitorServer::GetInstance()->mSelfMonitorMetricRules->mRunnerMetricsRule.mEnable, true); + APSARA_TEST_EQUAL(SelfMonitorServer::GetInstance()->mSelfMonitorMetricRules->mRunnerMetricsRule.mInterval, 8); + + APSARA_TEST_TRUE(input.Stop(true)); + APSARA_TEST_EQUAL(nullptr, SelfMonitorServer::GetInstance()->mSelfMonitorMetricRules); + APSARA_TEST_EQUAL(nullptr, SelfMonitorServer::GetInstance()->mMetricPipelineCtx); +} + +UNIT_TEST_CASE(InputInternalMetricsUnittest, OnInit) +UNIT_TEST_CASE(InputInternalMetricsUnittest, OnPipelineUpdate) + +} // namespace logtail + +UNIT_TEST_MAIN