blob: 8bb9cc7dd1e1f58874f96a53ac6ad554571ff481 [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
14using ::testing::IsNull;
15using ::testing::Return;
16using ::testing::StrEq;
17
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070018TEST(HostSensorTest, BoringConstructorTest)
19{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070020 // WARN: The host sensor is not presently meant to be created this way,
21 // TODO: Can I move the constructor into private?
22}
23
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070024TEST(HostSensorTest, CreateHostTempSensorTest)
25{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070026 // The normal case for this sensor is to be a temperature sensor, where
27 // the value is treated as a margin sensor.
28
29 sdbusplus::SdBusMock sdbus_mock;
30 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
31 std::string name = "fleeting0";
32 int64_t timeout = 1;
Patrick Venturee2ec0f62018-09-04 12:30:27 -070033 const char* objPath = "/asdf/asdf0";
Patrick Venture1fa9aab2018-06-11 10:46:49 -070034 bool defer = false;
35 std::string interface = "xyz.openbmc_project.Sensor.Value";
36
James Feist0709e2f2020-07-08 10:59:45 -070037 std::vector<std::string> properties = {};
38 double d;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070039
Patrick Venture563a3562018-10-30 09:31:26 -070040 // The createTemp updates all the properties, however, only Scale is set
Patrick Venture1fa9aab2018-06-11 10:46:49 -070041 // to non-default.
James Feist0709e2f2020-07-08 10:59:45 -070042 SetupDbusObject(&sdbus_mock, defer, objPath, interface, properties, &d);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070043
44 // This is called during object destruction.
45 EXPECT_CALL(sdbus_mock,
46 sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
47 .WillOnce(Return(0));
48
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070049 std::unique_ptr<Sensor> s =
Patrick Venture563a3562018-10-30 09:31:26 -070050 HostSensor::createTemp(name, timeout, bus_mock, objPath, defer);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070051}
52
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070053TEST(HostSensorTest, VerifyWriteThenReadMatches)
54{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070055 // Verify that when value is updated, the information matches
56 // what we expect when read back.
57
58 sdbusplus::SdBusMock sdbus_mock;
59 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
60 std::string name = "fleeting0";
61 int64_t timeout = 1;
Patrick Venturee2ec0f62018-09-04 12:30:27 -070062 const char* objPath = "/asdf/asdf0";
Patrick Venture1fa9aab2018-06-11 10:46:49 -070063 bool defer = false;
64 std::string interface = "xyz.openbmc_project.Sensor.Value";
65
James Feist0709e2f2020-07-08 10:59:45 -070066 std::vector<std::string> properties = {};
67 double d;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070068
James Feist0709e2f2020-07-08 10:59:45 -070069 SetupDbusObject(&sdbus_mock, defer, objPath, interface, properties, &d);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070070
71 EXPECT_CALL(sdbus_mock,
72 sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
73 .WillOnce(Return(0));
74
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070075 std::unique_ptr<Sensor> s =
Patrick Venture563a3562018-10-30 09:31:26 -070076 HostSensor::createTemp(name, timeout, bus_mock, objPath, defer);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070077
78 // Value is updated from dbus calls only (normally).
Patrick Venturee2ec0f62018-09-04 12:30:27 -070079 HostSensor* hs = static_cast<HostSensor*>(s.get());
James Feist0709e2f2020-07-08 10:59:45 -070080 double new_value = 2;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070081
82 ReadReturn r = hs->read();
83 EXPECT_EQ(r.value, 0);
84
85 EXPECT_CALL(sdbus_mock,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070086 sd_bus_emit_properties_changed_strv(
87 IsNull(), StrEq(objPath), StrEq(interface), NotNull()))
Patrick Venturee2ec0f62018-09-04 12:30:27 -070088 .WillOnce(Invoke([=](sd_bus* bus, const char* path,
89 const char* interface, char** names) {
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070090 EXPECT_STREQ("Value", names[0]);
91 return 0;
92 }));
Patrick Venture1fa9aab2018-06-11 10:46:49 -070093
94 std::chrono::high_resolution_clock::time_point t1 =
95 std::chrono::high_resolution_clock::now();
96
97 hs->value(new_value);
98 r = hs->read();
James Feist0709e2f2020-07-08 10:59:45 -070099 EXPECT_EQ(r.value, new_value);
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700100
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700101 auto duration =
102 std::chrono::duration_cast<std::chrono::seconds>(t1 - r.updated)
103 .count();
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700104
105 // Verify it was updated within the last second.
106 EXPECT_TRUE(duration < 1);
107}