Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
test_vm_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 vm_collector tests
13class VMCollectorTest : public ::testing::Test {
14 protected:
15 void SetUp() override {
16 collector_ = std::make_unique<vm_collector>();
17 std::unordered_map<std::string, std::string> config;
18 collector_->initialize(config);
19 }
20
21 std::unique_ptr<vm_collector> collector_;
22};
23
24// Test basic initialization
25TEST_F(VMCollectorTest, InitializesSuccessfully) {
26 EXPECT_NE(collector_, nullptr);
27 EXPECT_EQ(collector_->name(), "vm_collector");
28}
29
30// Test metric types returned
31TEST_F(VMCollectorTest, ReturnsCorrectMetricTypes) {
32 auto types = collector_->get_metric_types();
33 EXPECT_FALSE(types.empty());
34
35 // Check for expected metric types
36 std::vector<std::string> expected_types = {
37 "system.vm.is_virtualized",
38 "system.vm.steal_time"
39 };
40
41 for (const auto& expected : expected_types) {
42 bool found = std::find(types.begin(), types.end(), expected) != types.end();
43 EXPECT_TRUE(found) << "Expected metric type not found: " << expected;
44 }
45}
46
47// Test configuration options
48TEST_F(VMCollectorTest, ConfigurationOptions) {
49 auto collector = std::make_unique<vm_collector>();
50
51 std::unordered_map<std::string, std::string> config = {
52 {"enabled", "true"}
53 };
54
55 EXPECT_TRUE(collector->initialize(config));
56}
57
58// Test disable collector
59TEST_F(VMCollectorTest, CanBeDisabled) {
60 auto collector = std::make_unique<vm_collector>();
61
62 std::unordered_map<std::string, std::string> config = {
63 {"enabled", "false"}
64 };
65
66 collector->initialize(config);
67
68 auto metrics = collector->collect();
69 // Disabled collector should return empty metrics
70 EXPECT_TRUE(metrics.empty());
71}
72
73// Test statistics
74TEST_F(VMCollectorTest, TracksStatistics) {
75 auto stats = collector_->get_statistics();
76 EXPECT_TRUE(stats.find("collection_count") != stats.end());
77 EXPECT_TRUE(stats.find("collection_errors") != stats.end());
78}
79
80// Test collect returns metrics
81TEST_F(VMCollectorTest, CollectReturnsMetrics) {
82 auto metrics = collector_->collect();
83 // Should return at least 'system.vm.is_virtualized'
84 EXPECT_FALSE(metrics.empty());
85
86 bool found_is_virt = false;
87 for (const auto& m : metrics) {
88 if (m.name == "system.vm.is_virtualized") found_is_virt = true;
89 }
90 EXPECT_TRUE(found_is_virt);
91}
92
93// Test vm_type_to_string
94TEST(VMTypeTest, ToStringWorks) {
95 EXPECT_EQ(vm_type_to_string(vm_type::kvm), "KVM");
96 EXPECT_EQ(vm_type_to_string(vm_type::vmware), "VMWARE");
97 EXPECT_EQ(vm_type_to_string(vm_type::none), "NONE");
98}
99
100} // namespace
101} // namespace monitoring
102} // namespace kcenon
@ none
Bare metal (or undetected)
std::string vm_type_to_string(vm_type type)
Convert vm_type to string representation.
TEST(AdapterFunctionalityTest, WorksWithoutLogger)
Test Scenario 1: Adapter with NULL logger.
TEST_F(AdaptiveMonitoringTest, AdaptiveConfigDefaults)
std::unique_ptr< battery_collector > collector_
Virtualization metrics collector.