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