Skip to content

Commit

Permalink
add ut (#1997)
Browse files Browse the repository at this point in the history
  • Loading branch information
Takuka0311 authored Dec 30, 2024
1 parent 0544cfc commit 6b2fde7
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/monitor/SelfMonitorServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class SelfMonitorServer {

PipelineContext* mAlarmPipelineCtx;
std::mutex mAlarmPipelineMux;
#ifdef APSARA_UNIT_TEST_MAIN
friend class InputInternalMetricsUnittest;
#endif
};

} // namespace logtail
1 change: 1 addition & 0 deletions core/pipeline/Pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions core/plugin/input/InputInternalMetrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions core/unittest/input/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
184 changes: 184 additions & 0 deletions core/unittest/input/InputInternalMetricsUnittest.cpp
Original file line number Diff line number Diff line change
@@ -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 <json/json.h>

#include <filesystem>
#include <memory>
#include <string>

#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<InputInternalMetrics> 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

0 comments on commit 6b2fde7

Please sign in to comment.