5#include <gtest/gtest.h>
13class PowerCollectorTest :
public ::testing::Test {
15 void SetUp()
override {
16 collector_ = std::make_unique<power_collector>();
17 std::unordered_map<std::string, std::string> config;
25TEST_F(PowerCollectorTest, InitializesSuccessfully) {
30TEST_F(PowerCollectorTest, ReturnsCorrectMetricTypes) {
31 auto metric_types =
collector_->get_metric_types();
34 EXPECT_FALSE(metric_types.empty());
37 auto contains = [&metric_types](
const std::string& type) {
38 return std::find(metric_types.begin(), metric_types.end(), type) != metric_types.end();
41 EXPECT_TRUE(contains(
"power_consumption_watts"));
42 EXPECT_TRUE(contains(
"energy_consumed_joules"));
43 EXPECT_TRUE(contains(
"power_limit_watts"));
44 EXPECT_TRUE(contains(
"battery_percent"));
45 EXPECT_TRUE(contains(
"battery_is_charging"));
49TEST_F(PowerCollectorTest, ConfigurationOptions) {
50 auto custom_collector = std::make_unique<power_collector>();
52 std::unordered_map<std::string, std::string> config = {
53 {
"enabled",
"true"}, {
"collect_battery",
"true"}, {
"collect_rapl",
"true"}};
55 EXPECT_TRUE(custom_collector->initialize(config));
59TEST_F(PowerCollectorTest, CanBeDisabled) {
60 auto custom_collector = std::make_unique<power_collector>();
62 std::unordered_map<std::string, std::string> config = {{
"enabled",
"false"}};
64 custom_collector->initialize(config);
67 auto metrics = custom_collector->collect();
68 EXPECT_TRUE(metrics.empty());
72TEST_F(PowerCollectorTest, TracksStatistics) {
76 EXPECT_TRUE(stats.find(
"collection_count") != stats.end());
77 EXPECT_TRUE(stats.find(
"collection_errors") != stats.end());
78 EXPECT_TRUE(stats.find(
"sources_found") != stats.end());
81 EXPECT_EQ(stats[
"collection_count"], 0.0);
82 EXPECT_EQ(stats[
"collection_errors"], 0.0);
86TEST_F(PowerCollectorTest, CollectReturnsMetrics) {
92 EXPECT_GE(stats[
"collection_count"], 1.0);
96TEST_F(PowerCollectorTest, GetLastReadings) {
98 auto last_readings =
collector_->get_last_readings();
106TEST_F(PowerCollectorTest, PowerAvailabilityCheck) {
108 bool available =
collector_->is_power_available();
113TEST(PowerReadingTest, DefaultInitialization) {
114 power_reading reading;
116 EXPECT_TRUE(reading.source.id.empty());
117 EXPECT_TRUE(reading.source.name.empty());
119 EXPECT_EQ(reading.power_watts, 0.0);
120 EXPECT_EQ(reading.energy_joules, 0.0);
121 EXPECT_EQ(reading.power_limit_watts, 0.0);
122 EXPECT_EQ(reading.voltage_volts, 0.0);
123 EXPECT_EQ(reading.battery_percent, 0.0);
124 EXPECT_FALSE(reading.power_available);
125 EXPECT_FALSE(reading.battery_available);
126 EXPECT_FALSE(reading.is_charging);
127 EXPECT_FALSE(reading.is_discharging);
131TEST(PowerSourceInfoTest, DefaultInitialization) {
132 power_source_info
info;
134 EXPECT_TRUE(
info.id.empty());
135 EXPECT_TRUE(
info.name.empty());
136 EXPECT_TRUE(
info.path.empty());
141TEST(PowerSourceTypeTest, TypeToStringConversion) {
156TEST(PowerInfoCollectorTest, BasicFunctionality) {
157 power_info_collector collector;
160 bool available = collector.is_power_available();
164 auto sources = collector.enumerate_sources();
169 auto sources = collector.enumerate_sources();
176TEST(PowerInfoCollectorTest, EnumerateSources) {
177 power_info_collector collector;
179 auto sources = collector.enumerate_sources();
187TEST_F(PowerCollectorTest, MultipleCollectionsAreStable) {
188 for (
int i = 0; i < 5; ++i) {
195 EXPECT_GE(stats[
"collection_count"], 5.0);
196 EXPECT_EQ(stats[
"collection_errors"], 0.0);
200TEST_F(PowerCollectorTest, MetricsHaveCorrectTags) {
203 for (
const auto& m : metrics) {
205 if (!m.name.empty()) {
206 EXPECT_TRUE(m.tags.find(
"source_id") != m.tags.end());
207 EXPECT_TRUE(m.tags.find(
"source_name") != m.tags.end());
208 EXPECT_TRUE(m.tags.find(
"source_type") != m.tags.end());
214TEST(PowerInfoCollectorTest, ReadAllPower) {
215 power_info_collector collector;
217 auto readings = collector.read_all_power();
221 for (
const auto& reading : readings) {
223 EXPECT_FALSE(reading.source.id.empty());
228TEST_F(PowerCollectorTest, BatteryConfigurationDisabled) {
229 auto custom_collector = std::make_unique<power_collector>();
231 std::unordered_map<std::string, std::string> config = {
232 {
"enabled",
"true"}, {
"collect_battery",
"false"}};
234 EXPECT_TRUE(custom_collector->initialize(config));
237 auto metrics = custom_collector->collect();
242TEST_F(PowerCollectorTest, RaplConfigurationDisabled) {
243 auto custom_collector = std::make_unique<power_collector>();
245 std::unordered_map<std::string, std::string> config = {
246 {
"enabled",
"true"}, {
"collect_rapl",
"false"}};
248 EXPECT_TRUE(custom_collector->initialize(config));
251 auto metrics = custom_collector->collect();
std::string power_source_type_to_string(power_source_type type)
Convert power_source_type to string representation.
@ info
Informational, no action required.
@ battery
Battery power source.
@ platform
Platform/system power domain.
@ wireless
Wireless charging.
@ other
Other power source type.
@ unknown
Unknown power source type.
@ memory
Memory/DRAM power domain (RAPL)
@ cpu
CPU power domain (RAPL)
@ ac
AC adapter / mains power.
@ package
Processor package power domain (RAPL)
Power consumption monitoring collector.
TEST(AdapterFunctionalityTest, WorksWithoutLogger)
Test Scenario 1: Adapter with NULL logger.
TEST_F(AdaptiveMonitoringTest, AdaptiveConfigDefaults)
std::unique_ptr< battery_collector > collector_