Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
test_container_plugin.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#include <gtest/gtest.h>
6
8
9namespace kcenon {
10namespace monitoring {
11namespace plugins {
12namespace {
13
14// Test fixture for container plugin tests
15class ContainerPluginTest : public ::testing::Test {
16 protected:
17 void SetUp() override {
19 ASSERT_NE(plugin_, nullptr);
20 }
21
22 std::unique_ptr<container_plugin> plugin_;
23};
24
25// Test basic creation
26TEST_F(ContainerPluginTest, CreatesSuccessfully) {
27 EXPECT_NE(plugin_, nullptr);
28 EXPECT_EQ(plugin_->get_name(), "container_plugin");
29}
30
31// Test creation with custom configuration
32TEST_F(ContainerPluginTest, CreatesWithCustomConfig) {
33 container_plugin_config config;
34 config.enable_docker = true;
35 config.enable_kubernetes = false;
36 config.enable_cgroup = true;
37 config.docker_socket = "/var/run/docker.sock";
38
39 auto custom_plugin = container_plugin::create(config);
40 ASSERT_NE(custom_plugin, nullptr);
41
42 auto retrieved_config = custom_plugin->get_config();
43 EXPECT_EQ(retrieved_config.enable_docker, true);
44 EXPECT_EQ(retrieved_config.enable_kubernetes, false);
45 EXPECT_EQ(retrieved_config.enable_cgroup, true);
46 EXPECT_EQ(retrieved_config.docker_socket, "/var/run/docker.sock");
47}
48
49// Test initialization from map
50TEST_F(ContainerPluginTest, InitializesFromConfigMap) {
51 std::unordered_map<std::string, std::string> config = {{"enable_docker", "true"},
52 {"enable_kubernetes", "false"},
53 {"collect_network", "true"},
54 {"collect_blkio", "false"}};
55
56 EXPECT_TRUE(plugin_->initialize(config));
57}
58
59// Test metric types returned
60TEST_F(ContainerPluginTest, ReturnsMetricTypes) {
61 auto metric_types = plugin_->get_metric_types();
62
63 // Should return container metric types
64 EXPECT_FALSE(metric_types.empty());
65
66 auto contains = [&metric_types](const std::string& type) {
67 return std::find(metric_types.begin(), metric_types.end(), type) != metric_types.end();
68 };
69
70 EXPECT_TRUE(contains("container_cpu_usage_percent"));
71 EXPECT_TRUE(contains("container_memory_usage_bytes"));
72}
73
74// Test health check
75TEST_F(ContainerPluginTest, HealthCheck) {
76 // Plugin should be healthy after creation
77 EXPECT_TRUE(plugin_->is_healthy());
78}
79
80// Test statistics tracking
81TEST_F(ContainerPluginTest, TracksStatistics) {
82 auto stats = plugin_->get_statistics();
83
84 // Should have expected statistics keys
85 EXPECT_TRUE(stats.find("total_collections") != stats.end());
86 EXPECT_TRUE(stats.find("collection_errors") != stats.end());
87 EXPECT_TRUE(stats.find("containers_found") != stats.end());
88}
89
90// Test collect metrics (graceful degradation outside containers)
91TEST_F(ContainerPluginTest, CollectMetrics) {
92 auto metrics = plugin_->collect();
93
94 // May return empty outside container environment
95 // Just verify it doesn't crash
96 auto stats = plugin_->get_statistics();
97 EXPECT_GE(stats["total_collections"], 0.0);
98}
99
100// Test container_runtime enum values
101TEST(ContainerRuntimeTest, EnumValues) {
102 EXPECT_EQ(static_cast<int>(container_runtime::auto_detect), 0);
103 EXPECT_EQ(static_cast<int>(container_runtime::docker), 1);
104 EXPECT_EQ(static_cast<int>(container_runtime::containerd), 2);
105 EXPECT_EQ(static_cast<int>(container_runtime::podman), 3);
106 EXPECT_EQ(static_cast<int>(container_runtime::cri_o), 4);
107}
108
109// Test static detection methods
110TEST(ContainerPluginStaticTest, IsRunningInContainerDetection) {
111 // Should not crash - returns true/false based on environment
112 bool in_container = container_plugin::is_running_in_container();
113 (void)in_container; // Use variable to avoid warning
114}
115
116TEST(ContainerPluginStaticTest, IsKubernetesEnvironmentDetection) {
117 // Should not crash - returns true/false based on environment
119 (void)in_k8s; // Use variable to avoid warning
120}
121
122TEST(ContainerPluginStaticTest, DetectRuntimeDetection) {
123 // Should not crash - returns runtime type based on environment
125 (void)runtime; // Use variable to avoid warning
126}
127
128// Test availability checks
129TEST_F(ContainerPluginTest, AvailabilityChecks) {
130 // These may return true or false depending on environment
131 // Just verify they don't crash
132 bool docker_available = plugin_->is_docker_available();
133 bool k8s_available = plugin_->is_kubernetes_available();
134 bool cgroup_available = plugin_->is_cgroup_available();
135
136 (void)docker_available;
137 (void)k8s_available;
138 (void)cgroup_available;
139}
140
141// Test container_plugin_config default values
142TEST(ContainerPluginConfigTest, DefaultValues) {
143 container_plugin_config config;
144
145 EXPECT_EQ(config.runtime, container_runtime::auto_detect);
146 EXPECT_TRUE(config.enable_docker);
147 EXPECT_FALSE(config.enable_kubernetes);
148 EXPECT_TRUE(config.enable_cgroup);
149 EXPECT_EQ(config.docker_socket, "/var/run/docker.sock");
150 EXPECT_TRUE(config.kubeconfig_path.empty());
151 EXPECT_TRUE(config.namespace_filter.empty());
152 EXPECT_TRUE(config.collect_network_metrics);
153 EXPECT_TRUE(config.collect_blkio_metrics);
154 EXPECT_TRUE(config.collect_pid_metrics);
155}
156
157} // namespace
158} // namespace plugins
159} // namespace monitoring
160} // namespace kcenon
static std::unique_ptr< container_plugin > create(const container_plugin_config &config={})
static container_runtime detect_runtime()
Container monitoring plugin for Docker and Kubernetes metrics.
@ auto_detect
Automatically detect the container runtime.
TEST(AdapterFunctionalityTest, WorksWithoutLogger)
Test Scenario 1: Adapter with NULL logger.
TEST_F(AdaptiveMonitoringTest, AdaptiveConfigDefaults)
std::unique_ptr< container_plugin > plugin_