5#include <gtest/gtest.h>
12class PlatformMetricsCollectorTest :
public ::testing::Test {
14 void SetUp()
override {
15 collector_ = std::make_unique<platform_metrics_collector>();
22TEST_F(PlatformMetricsCollectorTest, CollectorNameIsCorrect) {
23 EXPECT_EQ(
collector_->name(),
"platform_metrics_collector");
27TEST_F(PlatformMetricsCollectorTest, PlatformIsAvailable) {
28#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
29 EXPECT_TRUE(
collector_->is_platform_available());
34 EXPECT_TRUE(
collector_->is_platform_available());
39TEST_F(PlatformMetricsCollectorTest, PlatformNameIsCorrect) {
40 std::string platform_name =
collector_->get_platform_name();
43 EXPECT_EQ(platform_name,
"linux");
44#elif defined(__APPLE__)
45 EXPECT_EQ(platform_name,
"macos");
47 EXPECT_EQ(platform_name,
"windows");
49 EXPECT_EQ(platform_name,
"unknown");
54TEST_F(PlatformMetricsCollectorTest, InitializationWithDefaultConfig) {
60TEST_F(PlatformMetricsCollectorTest, InitializationWithCustomConfig) {
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";
72TEST_F(PlatformMetricsCollectorTest, CollectReturnsMetrics) {
74 GTEST_SKIP() <<
"Platform collector not available on this platform";
80 EXPECT_FALSE(metrics.empty());
83 bool has_platform_info =
false;
84 for (
const auto& m : metrics) {
85 if (m.name ==
"platform_info") {
86 has_platform_info =
true;
88 auto it = m.tags.find(
"platform");
89 EXPECT_NE(it, m.tags.end());
93 EXPECT_TRUE(has_platform_info);
97TEST_F(PlatformMetricsCollectorTest, GetPlatformInfoReturnsValidInfo) {
100#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
101 EXPECT_TRUE(
info.available);
102 EXPECT_FALSE(
info.name.empty());
104 EXPECT_EQ(
info.name,
"unknown");
109TEST_F(PlatformMetricsCollectorTest, GetMetricTypesReturnsExpectedTypes) {
112 EXPECT_FALSE(types.empty());
115 bool has_platform_info =
false;
116 for (
const auto& type : types) {
117 if (type ==
"platform_info") {
118 has_platform_info =
true;
122 EXPECT_TRUE(has_platform_info);
126TEST_F(PlatformMetricsCollectorTest, GetStatisticsReturnsConfigValues) {
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());
137 EXPECT_EQ(stats[
"collect_uptime"], 1.0);
138 EXPECT_EQ(stats[
"collect_context_switches"], 1.0);
142TEST_F(PlatformMetricsCollectorTest, GetLastMetricsReturnsCachedData) {
144 GTEST_SKIP() <<
"Platform collector not available on this platform";
151 auto last_metrics =
collector_->get_last_metrics();
154 auto epoch = std::chrono::system_clock::time_point{};
155 EXPECT_GT(last_metrics.timestamp, epoch);
159TEST_F(PlatformMetricsCollectorTest, MultipleCollectionsAreConsistent) {
161 GTEST_SKIP() <<
"Platform collector not available on this platform";
168 EXPECT_FALSE(metrics1.empty());
169 EXPECT_FALSE(metrics2.empty());
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;
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;
191 EXPECT_EQ(platform1, platform2);
195TEST_F(PlatformMetricsCollectorTest, DisabledUptimeCollectionExcludesUptimeMetrics) {
196 platform_metrics_config config;
197 config.collect_uptime =
false;
199 auto collector = std::make_unique<platform_metrics_collector>(config);
201 if (!collector->is_available()) {
202 GTEST_SKIP() <<
"Platform collector not available on this platform";
205 auto metrics = collector->collect();
208 for (
const auto& m : metrics) {
209 EXPECT_NE(m.name,
"platform_uptime_seconds");
210 EXPECT_NE(m.name,
"platform_boot_timestamp");
215TEST_F(PlatformMetricsCollectorTest, HealthCheckReturnsCorrectly) {
221TEST_F(PlatformMetricsCollectorTest, CollectionSucceeds) {
223 GTEST_SKIP() <<
"Platform collector not available on this platform";
228 EXPECT_FALSE(metrics1.empty());
232 EXPECT_FALSE(metrics2.empty());
236TEST(PlatformInfoCollectorTest, CreateSucceeds) {
237 auto info_collector = std::make_unique<platform_info_collector>();
238 EXPECT_NE(info_collector,
nullptr);
241TEST(PlatformInfoCollectorTest, PlatformAvailableOnSupportedPlatforms) {
242 auto info_collector = std::make_unique<platform_info_collector>();
244#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
245 EXPECT_TRUE(info_collector->is_platform_available());
248 EXPECT_TRUE(info_collector->is_platform_available());
252TEST(PlatformInfoCollectorTest, GetPlatformInfoReturnsValidData) {
253 auto info_collector = std::make_unique<platform_info_collector>();
254 auto info = info_collector->get_platform_info();
256#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
257 EXPECT_TRUE(
info.available);
259 EXPECT_FALSE(
info.name.empty());
262TEST(PlatformInfoCollectorTest, GetUptimeReturnsData) {
263 auto info_collector = std::make_unique<platform_info_collector>();
264 auto uptime = info_collector->get_uptime();
266#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
267 if (uptime.available) {
268 EXPECT_GT(uptime.uptime_seconds, 0);
std::unordered_map< std::string, std::string > config_map
Type alias for configuration map.
@ info
Informational, no action required.
TEST(AdapterFunctionalityTest, WorksWithoutLogger)
Test Scenario 1: Adapter with NULL logger.
TEST_F(AdaptiveMonitoringTest, AdaptiveConfigDefaults)
std::unique_ptr< battery_collector > collector_