blob: 47a840a74a82224fc37d8521232f63106dc7325d [file] [log] [blame]
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -08001#include "health_metric.hpp"
2
3#include <sdbusplus/test/sdbus_mock.hpp>
4#include <xyz/openbmc_project/Metric/Value/server.hpp>
5
6#include <gmock/gmock.h>
7#include <gtest/gtest.h>
8
9namespace ConfigIntf = phosphor::health::metric::config;
10using PathIntf =
11 sdbusplus::server::xyz::openbmc_project::metric::Value::namespace_path;
12using namespace phosphor::health::metric;
13using namespace phosphor::health::utils;
14
15using ::testing::_;
16using ::testing::InSequence;
17using ::testing::Invoke;
18using ::testing::IsNull;
19using ::testing::NotNull;
20using ::testing::Pair;
21using ::testing::StrEq;
22
23class HealthMetricTest : public ::testing::Test
24{
25 public:
26 sdbusplus::SdBusMock sdbusMock;
Patrick Williamscfd889f2024-02-10 02:39:17 -060027 sdbusplus::bus_t bus = sdbusplus::get_mocked_new(&sdbusMock);
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -080028 static constexpr auto busName = "xyz.openbmc_project.test.HealthMon";
29 const std::set<std::string> properties = {"Value", "MaxValue", "MinValue",
30 "Unit"};
31 const std::string objPath = std::string(PathIntf::value) + "/bmc/" +
32 PathIntf::kernel_cpu;
33 ConfigIntf::HealthMetric config;
34
35 void SetUp() override
36 {
37 config.name = "CPU_Kernel";
38 config.subType = SubType::cpuKernel;
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -080039 config.windowSize = 1;
40 config.thresholds = {
41 {{ThresholdIntf::Type::Critical, ThresholdIntf::Bound::Upper},
42 {.value = 90.0, .log = true, .target = ""}},
43 {{ThresholdIntf::Type::Warning, ThresholdIntf::Bound::Upper},
44 {.value = 80.0, .log = false, .target = ""}}};
45 config.path = "";
46 }
47};
48
49TEST_F(HealthMetricTest, TestMetricUnmockedObjectAddRemove)
50{
Patrick Williamscfd889f2024-02-10 02:39:17 -060051 sdbusplus::bus_t unmockedBus = sdbusplus::bus::new_bus();
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -080052 unmockedBus.request_name(busName);
53 auto metric = std::make_unique<HealthMetric>(unmockedBus, Type::cpu, config,
54 paths_t());
55}
56
57TEST_F(HealthMetricTest, TestMetricThresholdChange)
58{
59 sdbusplus::server::manager_t objManager(bus, objPath.c_str());
60 bus.request_name(busName);
61 const auto thresholdProperties = std::set<std::string>{"Value", "Asserted"};
62
63 EXPECT_CALL(sdbusMock, sd_bus_emit_properties_changed_strv(
64 IsNull(), StrEq(objPath),
65 StrEq(ValueIntf::interface), NotNull()))
66 .WillRepeatedly(Invoke(
67 [&]([[maybe_unused]] sd_bus* bus, [[maybe_unused]] const char* path,
68 [[maybe_unused]] const char* interface, const char** names) {
69 EXPECT_THAT(properties, testing::Contains(names[0]));
70 return 0;
71 }));
72 EXPECT_CALL(sdbusMock, sd_bus_emit_properties_changed_strv(
73 IsNull(), StrEq(objPath),
74 StrEq(ThresholdIntf::interface), NotNull()))
75 .WillRepeatedly(Invoke(
76 [&]([[maybe_unused]] sd_bus* bus, [[maybe_unused]] const char* path,
77 [[maybe_unused]] const char* interface, const char** names) {
78 EXPECT_THAT(thresholdProperties, testing::Contains(names[0]));
79 return 0;
80 }));
81 EXPECT_CALL(sdbusMock,
82 sd_bus_message_new_signal(_, _, StrEq(objPath),
83 StrEq(ThresholdIntf::interface),
84 StrEq("AssertionChanged")))
85 .Times(4);
86
87 auto metric = std::make_unique<HealthMetric>(bus, Type::cpu, config,
88 paths_t());
89 // Exceed the critical threshold
Jagpal Singh Gill6a3884a2024-02-24 18:08:23 -080090 metric->update(MValue(1351, 1500));
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -080091 // Go below critical threshold but above warning threshold
Jagpal Singh Gill6a3884a2024-02-24 18:08:23 -080092 metric->update(MValue(1399, 1500));
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -080093 // Go below warning threshold
Jagpal Singh Gill6a3884a2024-02-24 18:08:23 -080094 metric->update(MValue(1199, 1500));
Jagpal Singh Gill23f091e2023-12-10 15:23:19 -080095}