James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame^] | 1 | #include "failsafeloggers/builder.hpp" |
| 2 | #include "failsafeloggers/failsafe_logger.hpp" |
| 3 | #include "failsafeloggers/failsafe_logger_utility.hpp" |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 4 | #include "sensors/host.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 5 | #include "test/helpers.hpp" |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 6 | |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 7 | #include <sdbusplus/test/sdbus_mock.hpp> |
| 8 | |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 9 | #include <chrono> |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 10 | #include <memory> |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 14 | #include <gmock/gmock.h> |
| 15 | #include <gtest/gtest.h> |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 16 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 17 | namespace pid_control |
| 18 | { |
| 19 | namespace |
| 20 | { |
| 21 | |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 22 | using ::testing::IsNull; |
| 23 | using ::testing::Return; |
| 24 | using ::testing::StrEq; |
| 25 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 26 | TEST(HostSensorTest, BoringConstructorTest) |
| 27 | { |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 28 | // 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 Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 32 | TEST(HostSensorTest, CreateHostTempSensorTest) |
| 33 | { |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 34 | // 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 Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 41 | const char* objPath = "/asdf/asdf0"; |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 42 | bool defer = false; |
| 43 | std::string interface = "xyz.openbmc_project.Sensor.Value"; |
| 44 | |
James Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 45 | std::vector<std::string> properties = {}; |
| 46 | double d; |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 47 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 48 | // The createTemp updates all the properties, however, only Scale is set |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 49 | // to non-default. |
James Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 50 | SetupDbusObject(&sdbus_mock, defer, objPath, interface, properties, &d); |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 51 | |
| 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 Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 57 | std::unique_ptr<Sensor> s = |
| 58 | HostSensor::createTemp(name, timeout, bus_mock, objPath, defer); |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 61 | TEST(HostSensorTest, VerifyWriteThenReadMatches) |
| 62 | { |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 63 | // 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 Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 70 | const char* objPath = "/asdf/asdf0"; |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 71 | bool defer = false; |
| 72 | std::string interface = "xyz.openbmc_project.Sensor.Value"; |
| 73 | |
James Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 74 | std::vector<std::string> properties = {}; |
| 75 | double d; |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 76 | |
James Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 77 | SetupDbusObject(&sdbus_mock, defer, objPath, interface, properties, &d); |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 78 | |
| 79 | EXPECT_CALL(sdbus_mock, |
| 80 | sd_bus_emit_object_removed(IsNull(), StrEq(objPath))) |
| 81 | .WillOnce(Return(0)); |
| 82 | |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 83 | std::unique_ptr<Sensor> s = |
| 84 | HostSensor::createTemp(name, timeout, bus_mock, objPath, defer); |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 85 | |
| 86 | // Value is updated from dbus calls only (normally). |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 87 | HostSensor* hs = static_cast<HostSensor*>(s.get()); |
James Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 88 | double new_value = 2; |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 89 | |
| 90 | ReadReturn r = hs->read(); |
| 91 | EXPECT_EQ(r.value, 0); |
| 92 | |
| 93 | EXPECT_CALL(sdbus_mock, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 94 | sd_bus_emit_properties_changed_strv( |
| 95 | IsNull(), StrEq(objPath), StrEq(interface), NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 96 | .WillOnce(Invoke( |
| 97 | [=]([[maybe_unused]] sd_bus* bus, [[maybe_unused]] const char* path, |
| 98 | [[maybe_unused]] const char* interface, const char** names) { |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 99 | EXPECT_STREQ("Value", names[0]); |
| 100 | return 0; |
| 101 | })); |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 102 | |
| 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 Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 108 | EXPECT_EQ(r.value, new_value); |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 109 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 110 | auto duration = |
| 111 | std::chrono::duration_cast<std::chrono::seconds>(t1 - r.updated) |
| 112 | .count(); |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 113 | |
| 114 | // Verify it was updated within the last second. |
| 115 | EXPECT_TRUE(duration < 1); |
| 116 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 117 | |
| 118 | } // namespace |
| 119 | } // namespace pid_control |