Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
test_temperature_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
12// Test fixture for temperature collector tests
13class TemperatureCollectorTest : public ::testing::Test {
14 protected:
15 void SetUp() override {
16 collector_ = std::make_unique<temperature_collector>();
17 std::unordered_map<std::string, std::string> config;
18 collector_->initialize(config);
19 }
20
21 std::unique_ptr<temperature_collector> collector_;
22};
23
24// Test basic initialization
25TEST_F(TemperatureCollectorTest, InitializesSuccessfully) {
26 EXPECT_EQ(collector_->name(), "temperature");
27}
28
29// Test metric types returned
30TEST_F(TemperatureCollectorTest, ReturnsCorrectMetricTypes) {
31 auto metric_types = collector_->get_metric_types();
32
33 // Should include all expected temperature metrics
34 EXPECT_FALSE(metric_types.empty());
35
36 // Check for expected metric types
37 auto contains = [&metric_types](const std::string& type) {
38 return std::find(metric_types.begin(), metric_types.end(), type) != metric_types.end();
39 };
40
41 EXPECT_TRUE(contains("temperature_celsius"));
42 EXPECT_TRUE(contains("temperature_critical_threshold"));
43 EXPECT_TRUE(contains("temperature_warning_threshold"));
44 EXPECT_TRUE(contains("temperature_is_critical"));
45 EXPECT_TRUE(contains("temperature_is_warning"));
46}
47
48// Test configuration options
49TEST_F(TemperatureCollectorTest, ConfigurationOptions) {
50 auto custom_collector = std::make_unique<temperature_collector>();
51
52 std::unordered_map<std::string, std::string> config = {
53 {"enabled", "true"}, {"collect_thresholds", "true"}, {"collect_warnings", "true"}};
54
55 EXPECT_TRUE(custom_collector->initialize(config));
56}
57
58// Test disable collector
59TEST_F(TemperatureCollectorTest, CanBeDisabled) {
60 auto custom_collector = std::make_unique<temperature_collector>();
61
62 std::unordered_map<std::string, std::string> config = {{"enabled", "false"}};
63
64 custom_collector->initialize(config);
65
66 // When disabled, collect should return empty
67 auto metrics = custom_collector->collect();
68 EXPECT_TRUE(metrics.empty());
69}
70
71// Test statistics
72TEST_F(TemperatureCollectorTest, TracksStatistics) {
73 auto stats = collector_->get_statistics();
74
75 // Should have expected statistics keys
76 EXPECT_TRUE(stats.find("collection_count") != stats.end());
77 EXPECT_TRUE(stats.find("collection_errors") != stats.end());
78 EXPECT_TRUE(stats.find("sensors_found") != stats.end());
79
80 // Initial values should be 0
81 EXPECT_EQ(stats["collection_count"], 0.0);
82 EXPECT_EQ(stats["collection_errors"], 0.0);
83}
84
85// Test collect returns metrics (graceful degradation when sensors unavailable)
86TEST_F(TemperatureCollectorTest, CollectReturnsMetrics) {
87 auto metrics = collector_->collect();
88
89 // This may return empty metrics if thermal sensors are not available
90 // This is expected behavior - graceful degradation
91 auto stats = collector_->get_statistics();
92 EXPECT_GE(stats["collection_count"], 1.0);
93}
94
95// Test get_last_readings
96TEST_F(TemperatureCollectorTest, GetLastReadings) {
97 collector_->collect();
98 auto last_readings = collector_->get_last_readings();
99
100 // Should return vector (may be empty if thermal not available)
101 // No assertion - just verify it doesn't crash
102 (void)last_readings;
103}
104
105// Test is_thermal_available
106TEST_F(TemperatureCollectorTest, ThermalAvailabilityCheck) {
107 // Should not crash - returns true/false based on sensor availability
108 bool available = collector_->is_thermal_available();
109 (void)available; // Use the variable to avoid warning
110}
111
112// Test temperature_reading structure
113TEST(TemperatureReadingTest, DefaultInitialization) {
114 temperature_reading reading;
115
116 EXPECT_TRUE(reading.sensor.id.empty());
117 EXPECT_TRUE(reading.sensor.name.empty());
118 EXPECT_EQ(reading.sensor.type, sensor_type::unknown);
119 EXPECT_EQ(reading.temperature_celsius, 0.0);
120 EXPECT_EQ(reading.critical_threshold_celsius, 0.0);
121 EXPECT_EQ(reading.warning_threshold_celsius, 0.0);
122 EXPECT_FALSE(reading.thresholds_available);
123 EXPECT_FALSE(reading.is_critical);
124 EXPECT_FALSE(reading.is_warning);
125}
126
127// Test temperature_sensor_info structure
128TEST(TemperatureSensorInfoTest, DefaultInitialization) {
129 temperature_sensor_info info;
130
131 EXPECT_TRUE(info.id.empty());
132 EXPECT_TRUE(info.name.empty());
133 EXPECT_TRUE(info.zone_path.empty());
134 EXPECT_EQ(info.type, sensor_type::unknown);
135}
136
137// Test sensor_type_to_string conversion
138TEST(SensorTypeTest, TypeToStringConversion) {
139 EXPECT_EQ(sensor_type_to_string(sensor_type::cpu), "cpu");
140 EXPECT_EQ(sensor_type_to_string(sensor_type::gpu), "gpu");
141 EXPECT_EQ(sensor_type_to_string(sensor_type::motherboard), "motherboard");
142 EXPECT_EQ(sensor_type_to_string(sensor_type::storage), "storage");
143 EXPECT_EQ(sensor_type_to_string(sensor_type::ambient), "ambient");
144 EXPECT_EQ(sensor_type_to_string(sensor_type::other), "other");
145 EXPECT_EQ(sensor_type_to_string(sensor_type::unknown), "unknown");
146}
147
148// Test temperature_info_collector basic functionality
149TEST(TemperatureInfoCollectorTest, BasicFunctionality) {
150 temperature_info_collector collector;
151
152 // Test thermal availability check (should not crash)
153 bool available = collector.is_thermal_available();
154
155 // If thermal is not available, enumerate_sensors should still work
156 if (!available) {
157 auto sensors = collector.enumerate_sensors();
158 EXPECT_TRUE(sensors.empty()); // No sensors without thermal
159 } else {
160 // If thermal is available, we might get some sensors
161 auto sensors = collector.enumerate_sensors();
162 // Just verify it doesn't crash - actual sensor count depends on system
163 (void)sensors;
164 }
165}
166
167// Test sensor enumeration (graceful degradation)
168TEST(TemperatureInfoCollectorTest, EnumerateSensors) {
169 temperature_info_collector collector;
170
171 auto sensors = collector.enumerate_sensors();
172
173 // Should return a vector (may be empty if thermal not available)
174 // No assertion on size - just verify it doesn't crash
175 (void)sensors;
176}
177
178// Test multiple collections don't cause issues
179TEST_F(TemperatureCollectorTest, MultipleCollectionsAreStable) {
180 for (int i = 0; i < 5; ++i) {
181 auto metrics = collector_->collect();
182 // Should not crash on repeated calls
183 (void)metrics;
184 }
185
186 auto stats = collector_->get_statistics();
187 EXPECT_GE(stats["collection_count"], 5.0);
188 EXPECT_EQ(stats["collection_errors"], 0.0);
189}
190
191// Test that metrics have correct tags when collected
192TEST_F(TemperatureCollectorTest, MetricsHaveCorrectTags) {
193 auto metrics = collector_->collect();
194
195 for (const auto& m : metrics) {
196 // If we have any metrics, check they have the expected tags
197 if (!m.name.empty()) {
198 EXPECT_TRUE(m.tags.find("sensor_id") != m.tags.end());
199 EXPECT_TRUE(m.tags.find("sensor_name") != m.tags.end());
200 EXPECT_TRUE(m.tags.find("sensor_type") != m.tags.end());
201 }
202 }
203}
204
205// Test read_all_temperatures
206TEST(TemperatureInfoCollectorTest, ReadAllTemperatures) {
207 temperature_info_collector collector;
208
209 auto readings = collector.read_all_temperatures();
210
211 // Should return a vector (may be empty if thermal not available)
212 // No assertion on size - just verify it doesn't crash
213 for (const auto& reading : readings) {
214 // If we got readings, they should have valid sensor info
215 EXPECT_FALSE(reading.sensor.id.empty());
216 }
217}
218
219} // namespace
220} // namespace monitoring
221} // namespace kcenon
std::string sensor_type_to_string(sensor_type type)
Convert sensor_type to string representation.
@ info
Informational, no action required.
@ gpu
GPU temperature sensor.
@ motherboard
Motherboard/chipset sensor.
@ unknown
Unknown sensor type.
@ cpu
CPU temperature sensor.
@ storage
Storage device sensor.
@ ambient
Ambient/case temperature.
Hardware temperature monitoring collector.
TEST(AdapterFunctionalityTest, WorksWithoutLogger)
Test Scenario 1: Adapter with NULL logger.
TEST_F(AdaptiveMonitoringTest, AdaptiveConfigDefaults)
std::unique_ptr< battery_collector > collector_