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