blob: e9800cf3e12ebbd327b385ba70dc51a4e03a4691 [file] [log] [blame]
Patrick Venture1fa9aab2018-06-11 10:46:49 -07001#include "sensors/host.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07002#include "test/helpers.hpp"
Patrick Venture1fa9aab2018-06-11 10:46:49 -07003
Patrick Venturea83a3ec2020-08-04 09:52:05 -07004#include <sdbusplus/test/sdbus_mock.hpp>
5
Patrick Venture1fa9aab2018-06-11 10:46:49 -07006#include <chrono>
Patrick Venture1fa9aab2018-06-11 10:46:49 -07007#include <memory>
Patrick Venture1fa9aab2018-06-11 10:46:49 -07008#include <string>
9#include <vector>
10
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070011#include <gmock/gmock.h>
12#include <gtest/gtest.h>
Patrick Venture1fa9aab2018-06-11 10:46:49 -070013
Patrick Venturea0764872020-08-08 07:48:43 -070014namespace pid_control
15{
16namespace
17{
18
Patrick Venture1fa9aab2018-06-11 10:46:49 -070019using ::testing::IsNull;
20using ::testing::Return;
21using ::testing::StrEq;
22
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070023TEST(HostSensorTest, BoringConstructorTest)
24{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070025 // 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 Ventureda4a5dd2018-08-31 09:42:48 -070029TEST(HostSensorTest, CreateHostTempSensorTest)
30{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070031 // 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 Venturee2ec0f62018-09-04 12:30:27 -070038 const char* objPath = "/asdf/asdf0";
Patrick Venture1fa9aab2018-06-11 10:46:49 -070039 bool defer = false;
40 std::string interface = "xyz.openbmc_project.Sensor.Value";
41
James Feist0709e2f2020-07-08 10:59:45 -070042 std::vector<std::string> properties = {};
43 double d;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070044
Patrick Venture563a3562018-10-30 09:31:26 -070045 // The createTemp updates all the properties, however, only Scale is set
Patrick Venture1fa9aab2018-06-11 10:46:49 -070046 // to non-default.
James Feist0709e2f2020-07-08 10:59:45 -070047 SetupDbusObject(&sdbus_mock, defer, objPath, interface, properties, &d);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070048
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 Ventureda4a5dd2018-08-31 09:42:48 -070054 std::unique_ptr<Sensor> s =
Patrick Venture563a3562018-10-30 09:31:26 -070055 HostSensor::createTemp(name, timeout, bus_mock, objPath, defer);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070056}
57
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070058TEST(HostSensorTest, VerifyWriteThenReadMatches)
59{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070060 // 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 Venturee2ec0f62018-09-04 12:30:27 -070067 const char* objPath = "/asdf/asdf0";
Patrick Venture1fa9aab2018-06-11 10:46:49 -070068 bool defer = false;
69 std::string interface = "xyz.openbmc_project.Sensor.Value";
70
James Feist0709e2f2020-07-08 10:59:45 -070071 std::vector<std::string> properties = {};
72 double d;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070073
James Feist0709e2f2020-07-08 10:59:45 -070074 SetupDbusObject(&sdbus_mock, defer, objPath, interface, properties, &d);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070075
76 EXPECT_CALL(sdbus_mock,
77 sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
78 .WillOnce(Return(0));
79
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070080 std::unique_ptr<Sensor> s =
Patrick Venture563a3562018-10-30 09:31:26 -070081 HostSensor::createTemp(name, timeout, bus_mock, objPath, defer);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070082
83 // Value is updated from dbus calls only (normally).
Patrick Venturee2ec0f62018-09-04 12:30:27 -070084 HostSensor* hs = static_cast<HostSensor*>(s.get());
James Feist0709e2f2020-07-08 10:59:45 -070085 double new_value = 2;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070086
87 ReadReturn r = hs->read();
88 EXPECT_EQ(r.value, 0);
89
90 EXPECT_CALL(sdbus_mock,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070091 sd_bus_emit_properties_changed_strv(
92 IsNull(), StrEq(objPath), StrEq(interface), NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +080093 .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 Venture1fa9aab2018-06-11 10:46:49 -070099
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 Feist0709e2f2020-07-08 10:59:45 -0700105 EXPECT_EQ(r.value, new_value);
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700106
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700107 auto duration =
108 std::chrono::duration_cast<std::chrono::seconds>(t1 - r.updated)
109 .count();
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700110
111 // Verify it was updated within the last second.
112 EXPECT_TRUE(duration < 1);
113}
Patrick Venturea0764872020-08-08 07:48:43 -0700114
115} // namespace
116} // namespace pid_control