123f091e5SJagpal Singh Gill #include "health_metric.hpp"
223f091e5SJagpal Singh Gill
323f091e5SJagpal Singh Gill #include <sdbusplus/test/sdbus_mock.hpp>
423f091e5SJagpal Singh Gill #include <xyz/openbmc_project/Metric/Value/server.hpp>
523f091e5SJagpal Singh Gill
623f091e5SJagpal Singh Gill #include <gmock/gmock.h>
723f091e5SJagpal Singh Gill #include <gtest/gtest.h>
823f091e5SJagpal Singh Gill
923f091e5SJagpal Singh Gill namespace ConfigIntf = phosphor::health::metric::config;
1023f091e5SJagpal Singh Gill using PathIntf =
1123f091e5SJagpal Singh Gill sdbusplus::server::xyz::openbmc_project::metric::Value::namespace_path;
1223f091e5SJagpal Singh Gill using namespace phosphor::health::metric;
1323f091e5SJagpal Singh Gill using namespace phosphor::health::utils;
1423f091e5SJagpal Singh Gill
1523f091e5SJagpal Singh Gill using ::testing::_;
1623f091e5SJagpal Singh Gill using ::testing::InSequence;
1723f091e5SJagpal Singh Gill using ::testing::Invoke;
1823f091e5SJagpal Singh Gill using ::testing::IsNull;
1923f091e5SJagpal Singh Gill using ::testing::NotNull;
2023f091e5SJagpal Singh Gill using ::testing::Pair;
2123f091e5SJagpal Singh Gill using ::testing::StrEq;
2223f091e5SJagpal Singh Gill
2323f091e5SJagpal Singh Gill class HealthMetricTest : public ::testing::Test
2423f091e5SJagpal Singh Gill {
2523f091e5SJagpal Singh Gill public:
2623f091e5SJagpal Singh Gill sdbusplus::SdBusMock sdbusMock;
27cfd889feSPatrick Williams sdbusplus::bus_t bus = sdbusplus::get_mocked_new(&sdbusMock);
2823f091e5SJagpal Singh Gill static constexpr auto busName = "xyz.openbmc_project.test.HealthMon";
2923f091e5SJagpal Singh Gill const std::set<std::string> properties = {"Value", "MaxValue", "MinValue",
3023f091e5SJagpal Singh Gill "Unit"};
31*ce8b5ae4SPatrick Williams const std::string objPath =
32*ce8b5ae4SPatrick Williams std::string(PathIntf::value) + "/bmc/" + PathIntf::kernel_cpu;
3323f091e5SJagpal Singh Gill ConfigIntf::HealthMetric config;
3423f091e5SJagpal Singh Gill
SetUp()3523f091e5SJagpal Singh Gill void SetUp() override
3623f091e5SJagpal Singh Gill {
3723f091e5SJagpal Singh Gill config.name = "CPU_Kernel";
3823f091e5SJagpal Singh Gill config.subType = SubType::cpuKernel;
3923f091e5SJagpal Singh Gill config.windowSize = 1;
4023f091e5SJagpal Singh Gill config.thresholds = {
4123f091e5SJagpal Singh Gill {{ThresholdIntf::Type::Critical, ThresholdIntf::Bound::Upper},
4223f091e5SJagpal Singh Gill {.value = 90.0, .log = true, .target = ""}},
4323f091e5SJagpal Singh Gill {{ThresholdIntf::Type::Warning, ThresholdIntf::Bound::Upper},
4423f091e5SJagpal Singh Gill {.value = 80.0, .log = false, .target = ""}}};
4523f091e5SJagpal Singh Gill config.path = "";
4623f091e5SJagpal Singh Gill }
4723f091e5SJagpal Singh Gill };
4823f091e5SJagpal Singh Gill
TEST_F(HealthMetricTest,TestMetricUnmockedObjectAddRemove)4923f091e5SJagpal Singh Gill TEST_F(HealthMetricTest, TestMetricUnmockedObjectAddRemove)
5023f091e5SJagpal Singh Gill {
51cfd889feSPatrick Williams sdbusplus::bus_t unmockedBus = sdbusplus::bus::new_bus();
5223f091e5SJagpal Singh Gill unmockedBus.request_name(busName);
5323f091e5SJagpal Singh Gill auto metric = std::make_unique<HealthMetric>(unmockedBus, Type::cpu, config,
5423f091e5SJagpal Singh Gill paths_t());
5523f091e5SJagpal Singh Gill }
5623f091e5SJagpal Singh Gill
TEST_F(HealthMetricTest,TestMetricThresholdChange)5723f091e5SJagpal Singh Gill TEST_F(HealthMetricTest, TestMetricThresholdChange)
5823f091e5SJagpal Singh Gill {
5923f091e5SJagpal Singh Gill sdbusplus::server::manager_t objManager(bus, objPath.c_str());
6023f091e5SJagpal Singh Gill bus.request_name(busName);
6123f091e5SJagpal Singh Gill const auto thresholdProperties = std::set<std::string>{"Value", "Asserted"};
6223f091e5SJagpal Singh Gill
6323f091e5SJagpal Singh Gill EXPECT_CALL(sdbusMock, sd_bus_emit_properties_changed_strv(
6423f091e5SJagpal Singh Gill IsNull(), StrEq(objPath),
6523f091e5SJagpal Singh Gill StrEq(ValueIntf::interface), NotNull()))
6623f091e5SJagpal Singh Gill .WillRepeatedly(Invoke(
6723f091e5SJagpal Singh Gill [&]([[maybe_unused]] sd_bus* bus, [[maybe_unused]] const char* path,
6823f091e5SJagpal Singh Gill [[maybe_unused]] const char* interface, const char** names) {
6923f091e5SJagpal Singh Gill EXPECT_THAT(properties, testing::Contains(names[0]));
7023f091e5SJagpal Singh Gill return 0;
7123f091e5SJagpal Singh Gill }));
7223f091e5SJagpal Singh Gill EXPECT_CALL(sdbusMock, sd_bus_emit_properties_changed_strv(
7323f091e5SJagpal Singh Gill IsNull(), StrEq(objPath),
7423f091e5SJagpal Singh Gill StrEq(ThresholdIntf::interface), NotNull()))
7523f091e5SJagpal Singh Gill .WillRepeatedly(Invoke(
7623f091e5SJagpal Singh Gill [&]([[maybe_unused]] sd_bus* bus, [[maybe_unused]] const char* path,
7723f091e5SJagpal Singh Gill [[maybe_unused]] const char* interface, const char** names) {
7823f091e5SJagpal Singh Gill EXPECT_THAT(thresholdProperties, testing::Contains(names[0]));
7923f091e5SJagpal Singh Gill return 0;
8023f091e5SJagpal Singh Gill }));
8123f091e5SJagpal Singh Gill EXPECT_CALL(sdbusMock,
8223f091e5SJagpal Singh Gill sd_bus_message_new_signal(_, _, StrEq(objPath),
8323f091e5SJagpal Singh Gill StrEq(ThresholdIntf::interface),
8423f091e5SJagpal Singh Gill StrEq("AssertionChanged")))
8523f091e5SJagpal Singh Gill .Times(4);
8623f091e5SJagpal Singh Gill
87*ce8b5ae4SPatrick Williams auto metric =
88*ce8b5ae4SPatrick Williams std::make_unique<HealthMetric>(bus, Type::cpu, config, paths_t());
8923f091e5SJagpal Singh Gill // Exceed the critical threshold
906a3884a4SJagpal Singh Gill metric->update(MValue(1351, 1500));
9123f091e5SJagpal Singh Gill // Go below critical threshold but above warning threshold
926a3884a4SJagpal Singh Gill metric->update(MValue(1399, 1500));
9323f091e5SJagpal Singh Gill // Go below warning threshold
946a3884a4SJagpal Singh Gill metric->update(MValue(1199, 1500));
9523f091e5SJagpal Singh Gill }
96