| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 1 | #include "interfaces.hpp" |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 2 | #include "sensors/host.hpp" |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 3 | #include "sensors/sensor.hpp" |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 4 | #include "test/helpers.hpp" |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 5 | |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 6 | #include <systemd/sd-bus.h> |
| 7 | |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 8 | #include <sdbusplus/test/sdbus_mock.hpp> |
| Alexander Hansen | 35ae0bb | 2025-10-28 14:32:38 +0100 | [diff] [blame^] | 9 | #include <xyz/openbmc_project/Sensor/Value/common.hpp> |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 10 | |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 11 | #include <chrono> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 12 | #include <cstdint> |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 13 | #include <memory> |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 17 | #include <gmock/gmock.h> |
| 18 | #include <gtest/gtest.h> |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 19 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 20 | namespace pid_control |
| 21 | { |
| 22 | namespace |
| 23 | { |
| 24 | |
| Alexander Hansen | 35ae0bb | 2025-10-28 14:32:38 +0100 | [diff] [blame^] | 25 | using SensorValue = sdbusplus::common::xyz::openbmc_project::sensor::Value; |
| 26 | |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 27 | using ::testing::IsNull; |
| 28 | using ::testing::Return; |
| 29 | using ::testing::StrEq; |
| 30 | |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 31 | TEST(HostSensorTest, BoringConstructorTest) |
| 32 | { |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 33 | // WARN: The host sensor is not presently meant to be created this way, |
| 34 | // TODO: Can I move the constructor into private? |
| 35 | } |
| 36 | |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 37 | TEST(HostSensorTest, CreateHostTempSensorTest) |
| 38 | { |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 39 | // The normal case for this sensor is to be a temperature sensor, where |
| 40 | // the value is treated as a margin sensor. |
| 41 | |
| 42 | sdbusplus::SdBusMock sdbus_mock; |
| 43 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 44 | std::string name = "fleeting0"; |
| 45 | int64_t timeout = 1; |
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 46 | const char* objPath = "/asdf/asdf0"; |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 47 | bool defer = false; |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 48 | |
| James Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 49 | std::vector<std::string> properties = {}; |
| 50 | double d; |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 51 | |
| Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 52 | // The createTemp updates all the properties, however, only Scale is set |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 53 | // to non-default. |
| Alexander Hansen | 35ae0bb | 2025-10-28 14:32:38 +0100 | [diff] [blame^] | 54 | SetupDbusObject(&sdbus_mock, defer, objPath, SensorValue::interface, |
| 55 | properties, &d); |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 56 | |
| 57 | // This is called during object destruction. |
| 58 | EXPECT_CALL(sdbus_mock, |
| 59 | sd_bus_emit_object_removed(IsNull(), StrEq(objPath))) |
| 60 | .WillOnce(Return(0)); |
| 61 | |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 62 | std::unique_ptr<Sensor> s = |
| 63 | HostSensor::createTemp(name, timeout, bus_mock, objPath, defer); |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 66 | TEST(HostSensorTest, VerifyWriteThenReadMatches) |
| 67 | { |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 68 | // Verify that when value is updated, the information matches |
| 69 | // what we expect when read back. |
| 70 | |
| 71 | sdbusplus::SdBusMock sdbus_mock; |
| 72 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 73 | std::string name = "fleeting0"; |
| 74 | int64_t timeout = 1; |
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 75 | const char* objPath = "/asdf/asdf0"; |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 76 | bool defer = false; |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 77 | |
| James Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 78 | std::vector<std::string> properties = {}; |
| 79 | double d; |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 80 | |
| Alexander Hansen | 35ae0bb | 2025-10-28 14:32:38 +0100 | [diff] [blame^] | 81 | SetupDbusObject(&sdbus_mock, defer, objPath, SensorValue::interface, |
| 82 | properties, &d); |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 83 | |
| 84 | EXPECT_CALL(sdbus_mock, |
| 85 | sd_bus_emit_object_removed(IsNull(), StrEq(objPath))) |
| 86 | .WillOnce(Return(0)); |
| 87 | |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 88 | std::unique_ptr<Sensor> s = |
| 89 | HostSensor::createTemp(name, timeout, bus_mock, objPath, defer); |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 90 | |
| 91 | // Value is updated from dbus calls only (normally). |
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 92 | HostSensor* hs = static_cast<HostSensor*>(s.get()); |
| James Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 93 | double new_value = 2; |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 94 | |
| 95 | ReadReturn r = hs->read(); |
| 96 | EXPECT_EQ(r.value, 0); |
| 97 | |
| Alexander Hansen | 35ae0bb | 2025-10-28 14:32:38 +0100 | [diff] [blame^] | 98 | EXPECT_CALL(sdbus_mock, sd_bus_emit_properties_changed_strv( |
| 99 | IsNull(), StrEq(objPath), |
| 100 | StrEq(SensorValue::interface), NotNull())) |
| Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 101 | .WillOnce(Invoke( |
| 102 | [=]([[maybe_unused]] sd_bus* bus, [[maybe_unused]] const char* path, |
| 103 | [[maybe_unused]] const char* interface, const char** names) { |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 104 | EXPECT_STREQ("Value", names[0]); |
| 105 | return 0; |
| 106 | })); |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 107 | |
| 108 | std::chrono::high_resolution_clock::time_point t1 = |
| 109 | std::chrono::high_resolution_clock::now(); |
| 110 | |
| 111 | hs->value(new_value); |
| 112 | r = hs->read(); |
| James Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 113 | EXPECT_EQ(r.value, new_value); |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 114 | |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 115 | auto duration = |
| 116 | std::chrono::duration_cast<std::chrono::seconds>(t1 - r.updated) |
| 117 | .count(); |
| Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 118 | |
| 119 | // Verify it was updated within the last second. |
| 120 | EXPECT_TRUE(duration < 1); |
| 121 | } |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 122 | |
| 123 | } // namespace |
| 124 | } // namespace pid_control |