Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
test_uptime_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
8#include <thread>
9
10namespace kcenon {
11namespace monitoring {
12namespace {
13
14// Test fixture for uptime_collector tests
15class UptimeCollectorTest : public ::testing::Test {
16 protected:
17 void SetUp() override {
18 collector_ = std::make_unique<uptime_collector>();
19 std::unordered_map<std::string, std::string> config;
20 collector_->initialize(config);
21 }
22
23 std::unique_ptr<uptime_collector> collector_;
24};
25
26// Test basic initialization
27TEST_F(UptimeCollectorTest, InitializesSuccessfully) {
28 EXPECT_NE(collector_, nullptr);
29 EXPECT_EQ(collector_->name(), "uptime");
30}
31
32// Test metric types returned
33TEST_F(UptimeCollectorTest, ReturnsCorrectMetricTypes) {
34 auto types = collector_->get_metric_types();
35 EXPECT_FALSE(types.empty());
36
37 // Check for expected metric types
38 std::vector<std::string> expected = {
39 "system_uptime_seconds",
40 "system_boot_timestamp",
41 "system_idle_seconds"
42 };
43
44 for (const auto& expected_type : expected) {
45 EXPECT_NE(std::find(types.begin(), types.end(), expected_type), types.end())
46 << "Missing metric type: " << expected_type;
47 }
48}
49
50// Test configuration options
51TEST_F(UptimeCollectorTest, ConfigurationOptions) {
52 auto collector = std::make_unique<uptime_collector>();
53 std::unordered_map<std::string, std::string> config;
54 config["collect_idle_time"] = "false";
55 EXPECT_TRUE(collector->initialize(config));
56
57 auto stats = collector->get_statistics();
58 EXPECT_DOUBLE_EQ(stats["collect_idle_time"], 0.0);
59}
60
61// Test disable collector
62TEST_F(UptimeCollectorTest, CanBeDisabled) {
63 auto collector = std::make_unique<uptime_collector>();
64 std::unordered_map<std::string, std::string> config;
65 config["enabled"] = "false";
66 collector->initialize(config);
67
68 auto metrics = collector->collect();
69 EXPECT_TRUE(metrics.empty());
70
71 auto stats = collector->get_statistics();
72 EXPECT_DOUBLE_EQ(stats["enabled"], 0.0);
73}
74
75// Test statistics
76TEST_F(UptimeCollectorTest, TracksStatistics) {
77 // Collect some metrics
78 collector_->collect();
79 collector_->collect();
80
81 auto stats = collector_->get_statistics();
82 EXPECT_GE(stats["collection_count"], 2.0);
83 EXPECT_GE(stats["collection_errors"], 0.0);
84}
85
86// Test collect returns metrics (graceful degradation when unavailable)
87TEST_F(UptimeCollectorTest, CollectReturnsMetrics) {
88 // Should not throw even if platform-specific metrics fail
89 EXPECT_NO_THROW(collector_->collect());
90}
91
92// Test get_last_metrics
93TEST_F(UptimeCollectorTest, GetLastMetrics) {
94 collector_->collect();
95 auto last = collector_->get_last_metrics();
96
97 // Timestamp should be set
98 auto now = std::chrono::system_clock::now();
99 auto diff = std::chrono::duration_cast<std::chrono::seconds>(now - last.timestamp);
100 EXPECT_LT(diff.count(), 10); // Within 10 seconds
101}
102
103// Test is_uptime_monitoring_available
104TEST_F(UptimeCollectorTest, UptimeMonitoringAvailabilityCheck) {
105 // This should return true or false depending on platform
106 // Either result is valid - we just want to ensure it doesn't crash
107 EXPECT_NO_THROW(collector_->is_uptime_monitoring_available());
108}
109
110// Test uptime_metrics structure
111TEST(UptimeMetricsTest, DefaultInitialization) {
112 uptime_metrics metrics;
113 EXPECT_DOUBLE_EQ(metrics.uptime_seconds, 0.0);
114 EXPECT_EQ(metrics.boot_timestamp, 0);
115 EXPECT_DOUBLE_EQ(metrics.idle_seconds, 0.0);
116 EXPECT_FALSE(metrics.metrics_available);
117}
118
119// Test uptime_info_collector basic functionality
120TEST(UptimeInfoCollectorTest, BasicFunctionality) {
121 uptime_info_collector collector;
122
123 // Test availability check
124 EXPECT_NO_THROW(collector.is_uptime_monitoring_available());
125
126 // Test metrics collection
127 auto metrics = collector.collect_metrics();
128
129 // Timestamp should be set
130 auto now = std::chrono::system_clock::now();
131 auto diff = std::chrono::duration_cast<std::chrono::seconds>(now - metrics.timestamp);
132 EXPECT_LT(diff.count(), 10);
133}
134
135// Test multiple collections are stable
136TEST_F(UptimeCollectorTest, MultipleCollectionsAreStable) {
137 for (int i = 0; i < 10; ++i) {
138 auto metrics = collector_->collect();
139 // Should not crash or throw
140 EXPECT_NO_THROW(collector_->get_statistics());
141 }
142
143 auto stats = collector_->get_statistics();
144 EXPECT_GE(stats["collection_count"], 10.0);
145}
146
147// Test that metrics have correct tags when collected
148TEST_F(UptimeCollectorTest, MetricsHaveCorrectTags) {
149 auto metrics = collector_->collect();
150
151 for (const auto& m : metrics) {
152 // All metrics should have the collector tag
153 auto it = m.tags.find("collector");
154 if (it != m.tags.end()) {
155 EXPECT_EQ(it->second, "uptime");
156 }
157 }
158}
159
160// Test is_available reflects actual state
161TEST_F(UptimeCollectorTest, IsHealthyReflectsState) {
162 // When enabled, availability depends on platform support
163 EXPECT_NO_THROW(collector_->is_available());
164
165 // Disabled collectors should still report availability status
166 auto disabled_collector = std::make_unique<uptime_collector>();
167 std::unordered_map<std::string, std::string> config;
168 config["enabled"] = "false";
169 disabled_collector->initialize(config);
170 // Availability is independent of enabled state
171 EXPECT_NO_THROW(disabled_collector->is_available());
172}
173
174// All platforms should have uptime monitoring available
175TEST_F(UptimeCollectorTest, UptimeMonitoringAvailable) {
176 EXPECT_TRUE(collector_->is_uptime_monitoring_available());
177}
178
179// Uptime metrics should be available on all platforms
180TEST(UptimeInfoCollectorTest, ReturnsMetrics) {
181 uptime_info_collector collector;
182
183 if (collector.is_uptime_monitoring_available()) {
184 auto metrics = collector.collect_metrics();
185 EXPECT_TRUE(metrics.metrics_available);
186 }
187}
188
189// Uptime should be positive and reasonable
190TEST(UptimeInfoCollectorTest, UptimeIsPositive) {
191 uptime_info_collector collector;
192
193 if (collector.is_uptime_monitoring_available()) {
194 auto metrics = collector.collect_metrics();
195 EXPECT_GT(metrics.uptime_seconds, 0.0);
196 // Reasonable upper bound: 10 years in seconds
197 EXPECT_LT(metrics.uptime_seconds, 10.0 * 365.25 * 24.0 * 3600.0);
198 }
199}
200
201// Boot timestamp should be in the past
202TEST(UptimeInfoCollectorTest, BootTimestampInPast) {
203 uptime_info_collector collector;
204
205 if (collector.is_uptime_monitoring_available()) {
206 auto metrics = collector.collect_metrics();
207 auto now_epoch = std::chrono::duration_cast<std::chrono::seconds>(
208 std::chrono::system_clock::now().time_since_epoch()).count();
209 EXPECT_LT(metrics.boot_timestamp, now_epoch);
210 // Boot time should be after year 2000 (946684800 = 2000-01-01)
211 EXPECT_GT(metrics.boot_timestamp, 946684800);
212 }
213}
214
215// Test uptime consistency across multiple collections
216TEST(UptimeInfoCollectorTest, UptimeIncreases) {
217 uptime_info_collector collector;
218
219 if (collector.is_uptime_monitoring_available()) {
220 auto first = collector.collect_metrics();
221 std::this_thread::sleep_for(std::chrono::milliseconds(100));
222 auto second = collector.collect_metrics();
223
224 // Uptime should have increased (or stayed same within measurement precision)
225 EXPECT_GE(second.uptime_seconds, first.uptime_seconds);
226 }
227}
228
229#if defined(__linux__)
230// Linux-specific test: Idle time should be available
231TEST(UptimeInfoCollectorTest, LinuxIdleTimeAvailable) {
232 uptime_info_collector collector;
233
234 if (collector.is_uptime_monitoring_available()) {
235 auto metrics = collector.collect_metrics();
236 // On Linux, idle_seconds should be available and >= 0
237 EXPECT_GE(metrics.idle_seconds, 0.0);
238 }
239}
240#endif
241
242} // namespace
243} // namespace monitoring
244} // namespace kcenon
TEST(AdapterFunctionalityTest, WorksWithoutLogger)
Test Scenario 1: Adapter with NULL logger.
TEST_F(AdaptiveMonitoringTest, AdaptiveConfigDefaults)
std::unique_ptr< battery_collector > collector_
System uptime monitoring collector.