blob: d04f265520696cdb21cc25de8c629bb25507953e [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>
Alexander Hansen35ae0bb2025-10-28 14:32:38 +01009#include <xyz/openbmc_project/Sensor/Value/common.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070010
Patrick Venture1fa9aab2018-06-11 10:46:49 -070011#include <chrono>
Ed Tanousf8b6e552025-06-27 13:27:50 -070012#include <cstdint>
Patrick Venture1fa9aab2018-06-11 10:46:49 -070013#include <memory>
Patrick Venture1fa9aab2018-06-11 10:46:49 -070014#include <string>
15#include <vector>
16
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070017#include <gmock/gmock.h>
18#include <gtest/gtest.h>
Patrick Venture1fa9aab2018-06-11 10:46:49 -070019
Patrick Venturea0764872020-08-08 07:48:43 -070020namespace pid_control
21{
22namespace
23{
24
Alexander Hansen35ae0bb2025-10-28 14:32:38 +010025using SensorValue = sdbusplus::common::xyz::openbmc_project::sensor::Value;
26
Patrick Venture1fa9aab2018-06-11 10:46:49 -070027using ::testing::IsNull;
28using ::testing::Return;
29using ::testing::StrEq;
30
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070031TEST(HostSensorTest, BoringConstructorTest)
32{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070033 // WARN: The host sensor is not presently meant to be created this way,
34 // TODO: Can I move the constructor into private?
35}
36
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070037TEST(HostSensorTest, CreateHostTempSensorTest)
38{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070039 // The normal case for this sensor is to be a temperature sensor, where
40 // the value is treated as a margin sensor.
41
42 sdbusplus::SdBusMock sdbus_mock;
43 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
44 std::string name = "fleeting0";
45 int64_t timeout = 1;
Patrick Venturee2ec0f62018-09-04 12:30:27 -070046 const char* objPath = "/asdf/asdf0";
Patrick Venture1fa9aab2018-06-11 10:46:49 -070047 bool defer = false;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070048
James Feist0709e2f2020-07-08 10:59:45 -070049 std::vector<std::string> properties = {};
50 double d;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070051
Patrick Venture563a3562018-10-30 09:31:26 -070052 // The createTemp updates all the properties, however, only Scale is set
Patrick Venture1fa9aab2018-06-11 10:46:49 -070053 // to non-default.
Alexander Hansen35ae0bb2025-10-28 14:32:38 +010054 SetupDbusObject(&sdbus_mock, defer, objPath, SensorValue::interface,
55 properties, &d);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070056
57 // This is called during object destruction.
58 EXPECT_CALL(sdbus_mock,
59 sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
60 .WillOnce(Return(0));
61
Patrick Williamsbd63bca2024-08-16 15:21:10 -040062 std::unique_ptr<Sensor> s =
63 HostSensor::createTemp(name, timeout, bus_mock, objPath, defer);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070064}
65
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070066TEST(HostSensorTest, VerifyWriteThenReadMatches)
67{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070068 // Verify that when value is updated, the information matches
69 // what we expect when read back.
70
71 sdbusplus::SdBusMock sdbus_mock;
72 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
73 std::string name = "fleeting0";
74 int64_t timeout = 1;
Patrick Venturee2ec0f62018-09-04 12:30:27 -070075 const char* objPath = "/asdf/asdf0";
Patrick Venture1fa9aab2018-06-11 10:46:49 -070076 bool defer = false;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070077
James Feist0709e2f2020-07-08 10:59:45 -070078 std::vector<std::string> properties = {};
79 double d;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070080
Alexander Hansen35ae0bb2025-10-28 14:32:38 +010081 SetupDbusObject(&sdbus_mock, defer, objPath, SensorValue::interface,
82 properties, &d);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070083
84 EXPECT_CALL(sdbus_mock,
85 sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
86 .WillOnce(Return(0));
87
Patrick Williamsbd63bca2024-08-16 15:21:10 -040088 std::unique_ptr<Sensor> s =
89 HostSensor::createTemp(name, timeout, bus_mock, objPath, defer);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070090
91 // Value is updated from dbus calls only (normally).
Patrick Venturee2ec0f62018-09-04 12:30:27 -070092 HostSensor* hs = static_cast<HostSensor*>(s.get());
James Feist0709e2f2020-07-08 10:59:45 -070093 double new_value = 2;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070094
95 ReadReturn r = hs->read();
96 EXPECT_EQ(r.value, 0);
97
Alexander Hansen35ae0bb2025-10-28 14:32:38 +010098 EXPECT_CALL(sdbus_mock, sd_bus_emit_properties_changed_strv(
99 IsNull(), StrEq(objPath),
100 StrEq(SensorValue::interface), NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800101 .WillOnce(Invoke(
102 [=]([[maybe_unused]] sd_bus* bus, [[maybe_unused]] const char* path,
103 [[maybe_unused]] const char* interface, const char** names) {
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400104 EXPECT_STREQ("Value", names[0]);
105 return 0;
106 }));
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700107
108 std::chrono::high_resolution_clock::time_point t1 =
109 std::chrono::high_resolution_clock::now();
110
111 hs->value(new_value);
112 r = hs->read();
James Feist0709e2f2020-07-08 10:59:45 -0700113 EXPECT_EQ(r.value, new_value);
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700114
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700115 auto duration =
116 std::chrono::duration_cast<std::chrono::seconds>(t1 - r.updated)
117 .count();
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700118
119 // Verify it was updated within the last second.
120 EXPECT_TRUE(duration < 1);
121}
Patrick Venturea0764872020-08-08 07:48:43 -0700122
123} // namespace
124} // namespace pid_control