blob: 5e958c00a73102cc1e3db3ff03020fde4d77a680 [file] [log] [blame]
James Zheng6df8bb52024-11-27 23:38:47 +00001#include "failsafeloggers/builder.hpp"
2#include "failsafeloggers/failsafe_logger.hpp"
3#include "failsafeloggers/failsafe_logger_utility.hpp"
Patrick Venture1fa9aab2018-06-11 10:46:49 -07004#include "sensors/host.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07005#include "test/helpers.hpp"
Patrick Venture1fa9aab2018-06-11 10:46:49 -07006
Patrick Venturea83a3ec2020-08-04 09:52:05 -07007#include <sdbusplus/test/sdbus_mock.hpp>
8
Patrick Venture1fa9aab2018-06-11 10:46:49 -07009#include <chrono>
Patrick Venture1fa9aab2018-06-11 10:46:49 -070010#include <memory>
Patrick Venture1fa9aab2018-06-11 10:46:49 -070011#include <string>
12#include <vector>
13
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070014#include <gmock/gmock.h>
15#include <gtest/gtest.h>
Patrick Venture1fa9aab2018-06-11 10:46:49 -070016
Patrick Venturea0764872020-08-08 07:48:43 -070017namespace pid_control
18{
19namespace
20{
21
Patrick Venture1fa9aab2018-06-11 10:46:49 -070022using ::testing::IsNull;
23using ::testing::Return;
24using ::testing::StrEq;
25
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070026TEST(HostSensorTest, BoringConstructorTest)
27{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070028 // WARN: The host sensor is not presently meant to be created this way,
29 // TODO: Can I move the constructor into private?
30}
31
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070032TEST(HostSensorTest, CreateHostTempSensorTest)
33{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070034 // The normal case for this sensor is to be a temperature sensor, where
35 // the value is treated as a margin sensor.
36
37 sdbusplus::SdBusMock sdbus_mock;
38 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
39 std::string name = "fleeting0";
40 int64_t timeout = 1;
Patrick Venturee2ec0f62018-09-04 12:30:27 -070041 const char* objPath = "/asdf/asdf0";
Patrick Venture1fa9aab2018-06-11 10:46:49 -070042 bool defer = false;
43 std::string interface = "xyz.openbmc_project.Sensor.Value";
44
James Feist0709e2f2020-07-08 10:59:45 -070045 std::vector<std::string> properties = {};
46 double d;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070047
Patrick Venture563a3562018-10-30 09:31:26 -070048 // The createTemp updates all the properties, however, only Scale is set
Patrick Venture1fa9aab2018-06-11 10:46:49 -070049 // to non-default.
James Feist0709e2f2020-07-08 10:59:45 -070050 SetupDbusObject(&sdbus_mock, defer, objPath, interface, properties, &d);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070051
52 // This is called during object destruction.
53 EXPECT_CALL(sdbus_mock,
54 sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
55 .WillOnce(Return(0));
56
Patrick Williamsbd63bca2024-08-16 15:21:10 -040057 std::unique_ptr<Sensor> s =
58 HostSensor::createTemp(name, timeout, bus_mock, objPath, defer);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070059}
60
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070061TEST(HostSensorTest, VerifyWriteThenReadMatches)
62{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070063 // Verify that when value is updated, the information matches
64 // what we expect when read back.
65
66 sdbusplus::SdBusMock sdbus_mock;
67 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
68 std::string name = "fleeting0";
69 int64_t timeout = 1;
Patrick Venturee2ec0f62018-09-04 12:30:27 -070070 const char* objPath = "/asdf/asdf0";
Patrick Venture1fa9aab2018-06-11 10:46:49 -070071 bool defer = false;
72 std::string interface = "xyz.openbmc_project.Sensor.Value";
73
James Feist0709e2f2020-07-08 10:59:45 -070074 std::vector<std::string> properties = {};
75 double d;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070076
James Feist0709e2f2020-07-08 10:59:45 -070077 SetupDbusObject(&sdbus_mock, defer, objPath, interface, properties, &d);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070078
79 EXPECT_CALL(sdbus_mock,
80 sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
81 .WillOnce(Return(0));
82
Patrick Williamsbd63bca2024-08-16 15:21:10 -040083 std::unique_ptr<Sensor> s =
84 HostSensor::createTemp(name, timeout, bus_mock, objPath, defer);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070085
86 // Value is updated from dbus calls only (normally).
Patrick Venturee2ec0f62018-09-04 12:30:27 -070087 HostSensor* hs = static_cast<HostSensor*>(s.get());
James Feist0709e2f2020-07-08 10:59:45 -070088 double new_value = 2;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070089
90 ReadReturn r = hs->read();
91 EXPECT_EQ(r.value, 0);
92
93 EXPECT_CALL(sdbus_mock,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070094 sd_bus_emit_properties_changed_strv(
95 IsNull(), StrEq(objPath), StrEq(interface), NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +080096 .WillOnce(Invoke(
97 [=]([[maybe_unused]] sd_bus* bus, [[maybe_unused]] const char* path,
98 [[maybe_unused]] const char* interface, const char** names) {
Patrick Williamsbd63bca2024-08-16 15:21:10 -040099 EXPECT_STREQ("Value", names[0]);
100 return 0;
101 }));
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700102
103 std::chrono::high_resolution_clock::time_point t1 =
104 std::chrono::high_resolution_clock::now();
105
106 hs->value(new_value);
107 r = hs->read();
James Feist0709e2f2020-07-08 10:59:45 -0700108 EXPECT_EQ(r.value, new_value);
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700109
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700110 auto duration =
111 std::chrono::duration_cast<std::chrono::seconds>(t1 - r.updated)
112 .count();
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700113
114 // Verify it was updated within the last second.
115 EXPECT_TRUE(duration < 1);
116}
Patrick Venturea0764872020-08-08 07:48:43 -0700117
118} // namespace
119} // namespace pid_control