xref: /openbmc/phosphor-pid-control/test/sensor_host_unittest.cpp (revision 6df8bb5086b29c43217596b194dda7fbc4e3ec4a)
1*6df8bb50SJames Zheng #include "failsafeloggers/builder.hpp"
2*6df8bb50SJames Zheng #include "failsafeloggers/failsafe_logger.hpp"
3*6df8bb50SJames Zheng #include "failsafeloggers/failsafe_logger_utility.hpp"
41fa9aabdSPatrick Venture #include "sensors/host.hpp"
5da4a5dd1SPatrick Venture #include "test/helpers.hpp"
61fa9aabdSPatrick Venture 
7a83a3eccSPatrick Venture #include <sdbusplus/test/sdbus_mock.hpp>
8a83a3eccSPatrick Venture 
91fa9aabdSPatrick Venture #include <chrono>
101fa9aabdSPatrick Venture #include <memory>
111fa9aabdSPatrick Venture #include <string>
121fa9aabdSPatrick Venture #include <vector>
131fa9aabdSPatrick Venture 
14da4a5dd1SPatrick Venture #include <gmock/gmock.h>
15da4a5dd1SPatrick Venture #include <gtest/gtest.h>
161fa9aabdSPatrick Venture 
17a076487aSPatrick Venture namespace pid_control
18a076487aSPatrick Venture {
19a076487aSPatrick Venture namespace
20a076487aSPatrick Venture {
21a076487aSPatrick Venture 
221fa9aabdSPatrick Venture using ::testing::IsNull;
231fa9aabdSPatrick Venture using ::testing::Return;
241fa9aabdSPatrick Venture using ::testing::StrEq;
251fa9aabdSPatrick Venture 
TEST(HostSensorTest,BoringConstructorTest)26da4a5dd1SPatrick Venture TEST(HostSensorTest, BoringConstructorTest)
27da4a5dd1SPatrick Venture {
281fa9aabdSPatrick Venture     // WARN: The host sensor is not presently meant to be created this way,
291fa9aabdSPatrick Venture     // TODO: Can I move the constructor into private?
301fa9aabdSPatrick Venture }
311fa9aabdSPatrick Venture 
TEST(HostSensorTest,CreateHostTempSensorTest)32da4a5dd1SPatrick Venture TEST(HostSensorTest, CreateHostTempSensorTest)
33da4a5dd1SPatrick Venture {
341fa9aabdSPatrick Venture     // The normal case for this sensor is to be a temperature sensor, where
351fa9aabdSPatrick Venture     // the value is treated as a margin sensor.
361fa9aabdSPatrick Venture 
371fa9aabdSPatrick Venture     sdbusplus::SdBusMock sdbus_mock;
381fa9aabdSPatrick Venture     auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
391fa9aabdSPatrick Venture     std::string name = "fleeting0";
401fa9aabdSPatrick Venture     int64_t timeout = 1;
411fa9aabdSPatrick Venture     const char* objPath = "/asdf/asdf0";
421fa9aabdSPatrick Venture     bool defer = false;
431fa9aabdSPatrick Venture     std::string interface = "xyz.openbmc_project.Sensor.Value";
441fa9aabdSPatrick Venture 
450709e2f1SJames Feist     std::vector<std::string> properties = {};
460709e2f1SJames Feist     double d;
471fa9aabdSPatrick Venture 
48563a356fSPatrick Venture     // The createTemp updates all the properties, however, only Scale is set
491fa9aabdSPatrick Venture     // to non-default.
500709e2f1SJames Feist     SetupDbusObject(&sdbus_mock, defer, objPath, interface, properties, &d);
511fa9aabdSPatrick Venture 
521fa9aabdSPatrick Venture     // This is called during object destruction.
531fa9aabdSPatrick Venture     EXPECT_CALL(sdbus_mock,
541fa9aabdSPatrick Venture                 sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
551fa9aabdSPatrick Venture         .WillOnce(Return(0));
561fa9aabdSPatrick Venture 
57bd63bcacSPatrick Williams     std::unique_ptr<Sensor> s =
58bd63bcacSPatrick Williams         HostSensor::createTemp(name, timeout, bus_mock, objPath, defer);
591fa9aabdSPatrick Venture }
601fa9aabdSPatrick Venture 
TEST(HostSensorTest,VerifyWriteThenReadMatches)61da4a5dd1SPatrick Venture TEST(HostSensorTest, VerifyWriteThenReadMatches)
62da4a5dd1SPatrick Venture {
631fa9aabdSPatrick Venture     // Verify that when value is updated, the information matches
641fa9aabdSPatrick Venture     // what we expect when read back.
651fa9aabdSPatrick Venture 
661fa9aabdSPatrick Venture     sdbusplus::SdBusMock sdbus_mock;
671fa9aabdSPatrick Venture     auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
681fa9aabdSPatrick Venture     std::string name = "fleeting0";
691fa9aabdSPatrick Venture     int64_t timeout = 1;
701fa9aabdSPatrick Venture     const char* objPath = "/asdf/asdf0";
711fa9aabdSPatrick Venture     bool defer = false;
721fa9aabdSPatrick Venture     std::string interface = "xyz.openbmc_project.Sensor.Value";
731fa9aabdSPatrick Venture 
740709e2f1SJames Feist     std::vector<std::string> properties = {};
750709e2f1SJames Feist     double d;
761fa9aabdSPatrick Venture 
770709e2f1SJames Feist     SetupDbusObject(&sdbus_mock, defer, objPath, interface, properties, &d);
781fa9aabdSPatrick Venture 
791fa9aabdSPatrick Venture     EXPECT_CALL(sdbus_mock,
801fa9aabdSPatrick Venture                 sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
811fa9aabdSPatrick Venture         .WillOnce(Return(0));
821fa9aabdSPatrick Venture 
83bd63bcacSPatrick Williams     std::unique_ptr<Sensor> s =
84bd63bcacSPatrick Williams         HostSensor::createTemp(name, timeout, bus_mock, objPath, defer);
851fa9aabdSPatrick Venture 
861fa9aabdSPatrick Venture     // Value is updated from dbus calls only (normally).
871fa9aabdSPatrick Venture     HostSensor* hs = static_cast<HostSensor*>(s.get());
880709e2f1SJames Feist     double new_value = 2;
891fa9aabdSPatrick Venture 
901fa9aabdSPatrick Venture     ReadReturn r = hs->read();
911fa9aabdSPatrick Venture     EXPECT_EQ(r.value, 0);
921fa9aabdSPatrick Venture 
931fa9aabdSPatrick Venture     EXPECT_CALL(sdbus_mock,
941fa9aabdSPatrick Venture                 sd_bus_emit_properties_changed_strv(
95da4a5dd1SPatrick Venture                     IsNull(), StrEq(objPath), StrEq(interface), NotNull()))
96a1ae4fa1SHarvey.Wu         .WillOnce(Invoke(
97a1ae4fa1SHarvey.Wu             [=]([[maybe_unused]] sd_bus* bus, [[maybe_unused]] const char* path,
98a1ae4fa1SHarvey.Wu                 [[maybe_unused]] const char* interface, const char** names) {
991fa9aabdSPatrick Venture                 EXPECT_STREQ("Value", names[0]);
1001fa9aabdSPatrick Venture                 return 0;
101da4a5dd1SPatrick Venture             }));
1021fa9aabdSPatrick Venture 
1031fa9aabdSPatrick Venture     std::chrono::high_resolution_clock::time_point t1 =
1041fa9aabdSPatrick Venture         std::chrono::high_resolution_clock::now();
1051fa9aabdSPatrick Venture 
1061fa9aabdSPatrick Venture     hs->value(new_value);
1071fa9aabdSPatrick Venture     r = hs->read();
1080709e2f1SJames Feist     EXPECT_EQ(r.value, new_value);
1091fa9aabdSPatrick Venture 
110da4a5dd1SPatrick Venture     auto duration =
111da4a5dd1SPatrick Venture         std::chrono::duration_cast<std::chrono::seconds>(t1 - r.updated)
112da4a5dd1SPatrick Venture             .count();
1131fa9aabdSPatrick Venture 
1141fa9aabdSPatrick Venture     // Verify it was updated within the last second.
1151fa9aabdSPatrick Venture     EXPECT_TRUE(duration < 1);
1161fa9aabdSPatrick Venture }
117a076487aSPatrick Venture 
118a076487aSPatrick Venture } // namespace
119a076487aSPatrick Venture } // namespace pid_control
120