5#include <gtest/gtest.h>
13class SmartCollectorTest :
public ::testing::Test {
15 void SetUp()
override {
16 collector_ = std::make_unique<smart_collector>();
17 std::unordered_map<std::string, std::string> config;
25TEST_F(SmartCollectorTest, InitializesSuccessfully) {
27 EXPECT_EQ(
collector_->name(),
"smart_collector");
31TEST_F(SmartCollectorTest, ReturnsCorrectMetricTypes) {
32 auto metric_types =
collector_->get_metric_types();
35 EXPECT_FALSE(metric_types.empty());
38 auto contains = [&metric_types](
const std::string& type) {
39 return std::find(metric_types.begin(), metric_types.end(), type) != metric_types.end();
42 EXPECT_TRUE(contains(
"smart_health_ok"));
43 EXPECT_TRUE(contains(
"smart_temperature_celsius"));
44 EXPECT_TRUE(contains(
"smart_reallocated_sectors"));
45 EXPECT_TRUE(contains(
"smart_power_on_hours"));
46 EXPECT_TRUE(contains(
"smart_power_cycle_count"));
47 EXPECT_TRUE(contains(
"smart_pending_sectors"));
48 EXPECT_TRUE(contains(
"smart_uncorrectable_errors"));
52TEST_F(SmartCollectorTest, ConfigurationOptions) {
53 auto custom_collector = std::make_unique<smart_collector>();
55 std::unordered_map<std::string, std::string> config = {
56 {
"enabled",
"true"}, {
"collect_temperature",
"true"}, {
"collect_error_rates",
"true"}};
58 EXPECT_TRUE(custom_collector->initialize(config));
59 EXPECT_TRUE(custom_collector->is_healthy());
63TEST_F(SmartCollectorTest, CanBeDisabled) {
64 auto custom_collector = std::make_unique<smart_collector>();
66 std::unordered_map<std::string, std::string> config = {{
"enabled",
"false"}};
68 custom_collector->initialize(config);
71 auto metrics = custom_collector->collect();
72 EXPECT_TRUE(metrics.empty());
76TEST_F(SmartCollectorTest, TracksStatistics) {
80 EXPECT_TRUE(stats.find(
"collection_count") != stats.end());
81 EXPECT_TRUE(stats.find(
"collection_errors") != stats.end());
82 EXPECT_TRUE(stats.find(
"disks_found") != stats.end());
85 EXPECT_EQ(stats[
"collection_count"], 0.0);
86 EXPECT_EQ(stats[
"collection_errors"], 0.0);
90TEST_F(SmartCollectorTest, CollectReturnsMetrics) {
96 EXPECT_GE(stats[
"collection_count"], 0.0);
100TEST_F(SmartCollectorTest, GetLastMetrics) {
102 auto last_metrics =
collector_->get_last_metrics();
110TEST_F(SmartCollectorTest, SmartAvailabilityCheck) {
112 bool available =
collector_->is_smart_available();
117TEST(SmartDiskMetricsTest, DefaultInitialization) {
118 smart_disk_metrics metrics;
120 EXPECT_TRUE(metrics.device_path.empty());
121 EXPECT_TRUE(metrics.model_name.empty());
122 EXPECT_TRUE(metrics.serial_number.empty());
123 EXPECT_FALSE(metrics.smart_supported);
124 EXPECT_FALSE(metrics.smart_enabled);
125 EXPECT_TRUE(metrics.health_ok);
126 EXPECT_EQ(metrics.temperature_celsius, 0.0);
127 EXPECT_EQ(metrics.reallocated_sectors, 0);
128 EXPECT_EQ(metrics.power_on_hours, 0);
129 EXPECT_EQ(metrics.power_cycle_count, 0);
130 EXPECT_EQ(metrics.pending_sectors, 0);
131 EXPECT_EQ(metrics.uncorrectable_errors, 0);
135TEST(DiskInfoTest, DefaultInitialization) {
138 EXPECT_TRUE(
info.device_path.empty());
139 EXPECT_TRUE(
info.device_type.empty());
140 EXPECT_FALSE(
info.smart_available);
144TEST(SmartInfoCollectorTest, BasicFunctionality) {
145 smart_info_collector collector;
148 bool available = collector.is_smartctl_available();
152 auto disks = collector.enumerate_disks();
153 EXPECT_TRUE(disks.empty());
156 auto disks = collector.enumerate_disks();
163TEST(SmartInfoCollectorTest, EnumerateDisks) {
164 smart_info_collector collector;
166 auto disks = collector.enumerate_disks();
174TEST(SmartInfoCollectorTest, CollectMetricsNonExistentDisk) {
175 smart_info_collector collector;
178 fake_disk.device_path =
"/dev/nonexistent_disk_xyz";
179 fake_disk.device_type =
"auto";
180 fake_disk.smart_available =
false;
183 auto metrics = collector.collect_smart_metrics(fake_disk);
185 EXPECT_EQ(metrics.device_path,
"/dev/nonexistent_disk_xyz");
187 EXPECT_FALSE(metrics.smart_supported);
191TEST_F(SmartCollectorTest, MultipleCollectionsAreStable) {
192 for (
int i = 0; i < 5; ++i) {
199 EXPECT_GE(stats[
"collection_count"], 5.0);
200 EXPECT_EQ(stats[
"collection_errors"], 0.0);
@ info
Informational, no action required.
S.M.A.R.T. disk health monitoring collector.
TEST(AdapterFunctionalityTest, WorksWithoutLogger)
Test Scenario 1: Adapter with NULL logger.
TEST_F(AdaptiveMonitoringTest, AdaptiveConfigDefaults)
std::unique_ptr< battery_collector > collector_