blob: 855908167fab074b87711fd7e8106566ce39e286 [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()))
Patrick Venturee2ec0f62018-09-04 12:30:27 -070093 .WillOnce(Invoke([=](sd_bus* bus, const char* path,
94 const char* interface, char** names) {
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070095 EXPECT_STREQ("Value", names[0]);
96 return 0;
97 }));
Patrick Venture1fa9aab2018-06-11 10:46:49 -070098
99 std::chrono::high_resolution_clock::time_point t1 =
100 std::chrono::high_resolution_clock::now();
101
102 hs->value(new_value);
103 r = hs->read();
James Feist0709e2f2020-07-08 10:59:45 -0700104 EXPECT_EQ(r.value, new_value);
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700105
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700106 auto duration =
107 std::chrono::duration_cast<std::chrono::seconds>(t1 - r.updated)
108 .count();
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700109
110 // Verify it was updated within the last second.
111 EXPECT_TRUE(duration < 1);
112}
Patrick Venturea0764872020-08-08 07:48:43 -0700113
114} // namespace
115} // namespace pid_control