Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
test_platform_metrics_collector.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>
7
8namespace kcenon {
9namespace monitoring {
10namespace {
11
12class PlatformMetricsCollectorTest : public ::testing::Test {
13 protected:
14 void SetUp() override {
15 collector_ = std::make_unique<platform_metrics_collector>();
16 }
17
18 std::unique_ptr<platform_metrics_collector> collector_;
19};
20
21// Test collector name
22TEST_F(PlatformMetricsCollectorTest, CollectorNameIsCorrect) {
23 EXPECT_EQ(collector_->name(), "platform_metrics_collector");
24}
25
26// Test platform availability
27TEST_F(PlatformMetricsCollectorTest, PlatformIsAvailable) {
28#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
29 EXPECT_TRUE(collector_->is_platform_available());
30 EXPECT_TRUE(collector_->is_available());
31#else
32 // On unsupported platforms, should still return true (null_metrics_provider)
33 // but platform name will be "unknown"
34 EXPECT_TRUE(collector_->is_platform_available());
35#endif
36}
37
38// Test platform name detection
39TEST_F(PlatformMetricsCollectorTest, PlatformNameIsCorrect) {
40 std::string platform_name = collector_->get_platform_name();
41
42#if defined(__linux__)
43 EXPECT_EQ(platform_name, "linux");
44#elif defined(__APPLE__)
45 EXPECT_EQ(platform_name, "macos");
46#elif defined(_WIN32)
47 EXPECT_EQ(platform_name, "windows");
48#else
49 EXPECT_EQ(platform_name, "unknown");
50#endif
51}
52
53// Test initialization with default config
54TEST_F(PlatformMetricsCollectorTest, InitializationWithDefaultConfig) {
55 config_map config;
56 EXPECT_TRUE(collector_->initialize(config));
57}
58
59// Test initialization with custom config
60TEST_F(PlatformMetricsCollectorTest, InitializationWithCustomConfig) {
61 config_map config;
62 config["collect_uptime"] = "true";
63 config["collect_context_switches"] = "false";
64 config["collect_tcp_states"] = "true";
65 config["collect_socket_buffers"] = "false";
66 config["collect_interrupts"] = "false";
67
68 EXPECT_TRUE(collector_->initialize(config));
69}
70
71// Test metric collection
72TEST_F(PlatformMetricsCollectorTest, CollectReturnsMetrics) {
73 if (!collector_->is_available()) {
74 GTEST_SKIP() << "Platform collector not available on this platform";
75 }
76
77 auto metrics = collector_->collect();
78
79 // Should at least have platform_info metric
80 EXPECT_FALSE(metrics.empty());
81
82 // Check for platform_info metric
83 bool has_platform_info = false;
84 for (const auto& m : metrics) {
85 if (m.name == "platform_info") {
86 has_platform_info = true;
87 // Check that platform tag is present
88 auto it = m.tags.find("platform");
89 EXPECT_NE(it, m.tags.end());
90 break;
91 }
92 }
93 EXPECT_TRUE(has_platform_info);
94}
95
96// Test platform info retrieval
97TEST_F(PlatformMetricsCollectorTest, GetPlatformInfoReturnsValidInfo) {
98 auto info = collector_->get_platform_info();
99
100#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
101 EXPECT_TRUE(info.available);
102 EXPECT_FALSE(info.name.empty());
103#else
104 EXPECT_EQ(info.name, "unknown");
105#endif
106}
107
108// Test metric types
109TEST_F(PlatformMetricsCollectorTest, GetMetricTypesReturnsExpectedTypes) {
110 auto types = collector_->get_metric_types();
111
112 EXPECT_FALSE(types.empty());
113
114 // Should include platform_info
115 bool has_platform_info = false;
116 for (const auto& type : types) {
117 if (type == "platform_info") {
118 has_platform_info = true;
119 break;
120 }
121 }
122 EXPECT_TRUE(has_platform_info);
123}
124
125// Test statistics
126TEST_F(PlatformMetricsCollectorTest, GetStatisticsReturnsConfigValues) {
127 auto stats = collector_->get_statistics();
128
129 // Should have config-related statistics
130 EXPECT_NE(stats.find("collect_uptime"), stats.end());
131 EXPECT_NE(stats.find("collect_context_switches"), stats.end());
132 EXPECT_NE(stats.find("collect_tcp_states"), stats.end());
133 EXPECT_NE(stats.find("collect_socket_buffers"), stats.end());
134 EXPECT_NE(stats.find("collect_interrupts"), stats.end());
135
136 // Default config should have all enabled
137 EXPECT_EQ(stats["collect_uptime"], 1.0);
138 EXPECT_EQ(stats["collect_context_switches"], 1.0);
139}
140
141// Test last metrics caching
142TEST_F(PlatformMetricsCollectorTest, GetLastMetricsReturnsCachedData) {
143 if (!collector_->is_available()) {
144 GTEST_SKIP() << "Platform collector not available on this platform";
145 }
146
147 // First collection
148 collector_->collect();
149
150 // Get last metrics
151 auto last_metrics = collector_->get_last_metrics();
152
153 // Timestamp should be set
154 auto epoch = std::chrono::system_clock::time_point{};
155 EXPECT_GT(last_metrics.timestamp, epoch);
156}
157
158// Test multiple collections
159TEST_F(PlatformMetricsCollectorTest, MultipleCollectionsAreConsistent) {
160 if (!collector_->is_available()) {
161 GTEST_SKIP() << "Platform collector not available on this platform";
162 }
163
164 auto metrics1 = collector_->collect();
165 auto metrics2 = collector_->collect();
166
167 // Both collections should have metrics
168 EXPECT_FALSE(metrics1.empty());
169 EXPECT_FALSE(metrics2.empty());
170
171 // Platform name should be the same (it's cached)
172 std::string platform1, platform2;
173 for (const auto& m : metrics1) {
174 if (m.name == "platform_info") {
175 auto it = m.tags.find("platform");
176 if (it != m.tags.end()) {
177 platform1 = it->second;
178 }
179 break;
180 }
181 }
182 for (const auto& m : metrics2) {
183 if (m.name == "platform_info") {
184 auto it = m.tags.find("platform");
185 if (it != m.tags.end()) {
186 platform2 = it->second;
187 }
188 break;
189 }
190 }
191 EXPECT_EQ(platform1, platform2);
192}
193
194// Test collector with disabled uptime collection
195TEST_F(PlatformMetricsCollectorTest, DisabledUptimeCollectionExcludesUptimeMetrics) {
196 platform_metrics_config config;
197 config.collect_uptime = false;
198
199 auto collector = std::make_unique<platform_metrics_collector>(config);
200
201 if (!collector->is_available()) {
202 GTEST_SKIP() << "Platform collector not available on this platform";
203 }
204
205 auto metrics = collector->collect();
206
207 // Should not have uptime metrics
208 for (const auto& m : metrics) {
209 EXPECT_NE(m.name, "platform_uptime_seconds");
210 EXPECT_NE(m.name, "platform_boot_timestamp");
211 }
212}
213
214// Test health check
215TEST_F(PlatformMetricsCollectorTest, HealthCheckReturnsCorrectly) {
216 EXPECT_TRUE(collector_->is_healthy());
217}
218
219// Test collection count tracking
220// Note: collector_plugin interface doesn't expose collection count
221TEST_F(PlatformMetricsCollectorTest, CollectionSucceeds) {
222 if (!collector_->is_available()) {
223 GTEST_SKIP() << "Platform collector not available on this platform";
224 }
225
226 // First collection should succeed
227 auto metrics1 = collector_->collect();
228 EXPECT_FALSE(metrics1.empty());
229
230 // Second collection should also succeed
231 auto metrics2 = collector_->collect();
232 EXPECT_FALSE(metrics2.empty());
233}
234
235// Test info collector standalone
236TEST(PlatformInfoCollectorTest, CreateSucceeds) {
237 auto info_collector = std::make_unique<platform_info_collector>();
238 EXPECT_NE(info_collector, nullptr);
239}
240
241TEST(PlatformInfoCollectorTest, PlatformAvailableOnSupportedPlatforms) {
242 auto info_collector = std::make_unique<platform_info_collector>();
243
244#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
245 EXPECT_TRUE(info_collector->is_platform_available());
246#else
247 // Null provider is still "available" (returns valid null object)
248 EXPECT_TRUE(info_collector->is_platform_available());
249#endif
250}
251
252TEST(PlatformInfoCollectorTest, GetPlatformInfoReturnsValidData) {
253 auto info_collector = std::make_unique<platform_info_collector>();
254 auto info = info_collector->get_platform_info();
255
256#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
257 EXPECT_TRUE(info.available);
258#endif
259 EXPECT_FALSE(info.name.empty());
260}
261
262TEST(PlatformInfoCollectorTest, GetUptimeReturnsData) {
263 auto info_collector = std::make_unique<platform_info_collector>();
264 auto uptime = info_collector->get_uptime();
265
266#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
267 if (uptime.available) {
268 EXPECT_GT(uptime.uptime_seconds, 0);
269 }
270#endif
271}
272
273} // namespace
274} // namespace monitoring
275} // namespace kcenon
std::unordered_map< std::string, std::string > config_map
Type alias for configuration map.
@ info
Informational, no action required.
Unified platform-agnostic metrics collector.
TEST(AdapterFunctionalityTest, WorksWithoutLogger)
Test Scenario 1: Adapter with NULL logger.
TEST_F(AdaptiveMonitoringTest, AdaptiveConfigDefaults)
std::unique_ptr< battery_collector > collector_