blob: 61d1ed6a5f49c50f3f693bceb3156517d89a762f [file] [log] [blame]
Ed Tanousf8b6e552025-06-27 13:27:50 -07001#include "interfaces.hpp"
Patrick Venture1fa9aab2018-06-11 10:46:49 -07002#include "sensors/host.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -07003#include "sensors/sensor.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07004#include "test/helpers.hpp"
Patrick Venture1fa9aab2018-06-11 10:46:49 -07005
Ed Tanousf8b6e552025-06-27 13:27:50 -07006#include <systemd/sd-bus.h>
7
Patrick Venturea83a3ec2020-08-04 09:52:05 -07008#include <sdbusplus/test/sdbus_mock.hpp>
9
Patrick Venture1fa9aab2018-06-11 10:46:49 -070010#include <chrono>
Ed Tanousf8b6e552025-06-27 13:27:50 -070011#include <cstdint>
Patrick Venture1fa9aab2018-06-11 10:46:49 -070012#include <memory>
Patrick Venture1fa9aab2018-06-11 10:46:49 -070013#include <string>
14#include <vector>
15
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070016#include <gmock/gmock.h>
17#include <gtest/gtest.h>
Patrick Venture1fa9aab2018-06-11 10:46:49 -070018
Patrick Venturea0764872020-08-08 07:48:43 -070019namespace pid_control
20{
21namespace
22{
23
Patrick Venture1fa9aab2018-06-11 10:46:49 -070024using ::testing::IsNull;
25using ::testing::Return;
26using ::testing::StrEq;
27
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070028TEST(HostSensorTest, BoringConstructorTest)
29{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070030 // WARN: The host sensor is not presently meant to be created this way,
31 // TODO: Can I move the constructor into private?
32}
33
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070034TEST(HostSensorTest, CreateHostTempSensorTest)
35{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070036 // The normal case for this sensor is to be a temperature sensor, where
37 // the value is treated as a margin sensor.
38
39 sdbusplus::SdBusMock sdbus_mock;
40 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
41 std::string name = "fleeting0";
42 int64_t timeout = 1;
Patrick Venturee2ec0f62018-09-04 12:30:27 -070043 const char* objPath = "/asdf/asdf0";
Patrick Venture1fa9aab2018-06-11 10:46:49 -070044 bool defer = false;
45 std::string interface = "xyz.openbmc_project.Sensor.Value";
46
James Feist0709e2f2020-07-08 10:59:45 -070047 std::vector<std::string> properties = {};
48 double d;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070049
Patrick Venture563a3562018-10-30 09:31:26 -070050 // The createTemp updates all the properties, however, only Scale is set
Patrick Venture1fa9aab2018-06-11 10:46:49 -070051 // to non-default.
James Feist0709e2f2020-07-08 10:59:45 -070052 SetupDbusObject(&sdbus_mock, defer, objPath, interface, properties, &d);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070053
54 // This is called during object destruction.
55 EXPECT_CALL(sdbus_mock,
56 sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
57 .WillOnce(Return(0));
58
Patrick Williamsbd63bca2024-08-16 15:21:10 -040059 std::unique_ptr<Sensor> s =
60 HostSensor::createTemp(name, timeout, bus_mock, objPath, defer);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070061}
62
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070063TEST(HostSensorTest, VerifyWriteThenReadMatches)
64{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070065 // Verify that when value is updated, the information matches
66 // what we expect when read back.
67
68 sdbusplus::SdBusMock sdbus_mock;
69 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
70 std::string name = "fleeting0";
71 int64_t timeout = 1;
Patrick Venturee2ec0f62018-09-04 12:30:27 -070072 const char* objPath = "/asdf/asdf0";
Patrick Venture1fa9aab2018-06-11 10:46:49 -070073 bool defer = false;
74 std::string interface = "xyz.openbmc_project.Sensor.Value";
75
James Feist0709e2f2020-07-08 10:59:45 -070076 std::vector<std::string> properties = {};
77 double d;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070078
James Feist0709e2f2020-07-08 10:59:45 -070079 SetupDbusObject(&sdbus_mock, defer, objPath, interface, properties, &d);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070080
81 EXPECT_CALL(sdbus_mock,
82 sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
83 .WillOnce(Return(0));
84
Patrick Williamsbd63bca2024-08-16 15:21:10 -040085 std::unique_ptr<Sensor> s =
86 HostSensor::createTemp(name, timeout, bus_mock, objPath, defer);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070087
88 // Value is updated from dbus calls only (normally).
Patrick Venturee2ec0f62018-09-04 12:30:27 -070089 HostSensor* hs = static_cast<HostSensor*>(s.get());
James Feist0709e2f2020-07-08 10:59:45 -070090 double new_value = 2;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070091
92 ReadReturn r = hs->read();
93 EXPECT_EQ(r.value, 0);
94
95 EXPECT_CALL(sdbus_mock,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070096 sd_bus_emit_properties_changed_strv(
97 IsNull(), StrEq(objPath), StrEq(interface), NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +080098 .WillOnce(Invoke(
99 [=]([[maybe_unused]] sd_bus* bus, [[maybe_unused]] const char* path,
100 [[maybe_unused]] const char* interface, const char** names) {
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400101 EXPECT_STREQ("Value", names[0]);
102 return 0;
103 }));
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700104
105 std::chrono::high_resolution_clock::time_point t1 =
106 std::chrono::high_resolution_clock::now();
107
108 hs->value(new_value);
109 r = hs->read();
James Feist0709e2f2020-07-08 10:59:45 -0700110 EXPECT_EQ(r.value, new_value);
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700111
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700112 auto duration =
113 std::chrono::duration_cast<std::chrono::seconds>(t1 - r.updated)
114 .count();
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700115
116 // Verify it was updated within the last second.
117 EXPECT_TRUE(duration < 1);
118}
Patrick Venturea0764872020-08-08 07:48:43 -0700119
120} // namespace
121} // namespace pid_control