blob: 14c09f05efec13a6469758301eab8405b6616918 [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
4#include <chrono>
Patrick Venture1fa9aab2018-06-11 10:46:49 -07005#include <memory>
6#include <sdbusplus/test/sdbus_mock.hpp>
7#include <string>
8#include <vector>
9
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070010#include <gmock/gmock.h>
11#include <gtest/gtest.h>
Patrick Venture1fa9aab2018-06-11 10:46:49 -070012
13using ::testing::IsNull;
14using ::testing::Return;
15using ::testing::StrEq;
16
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070017TEST(HostSensorTest, BoringConstructorTest)
18{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070019 // WARN: The host sensor is not presently meant to be created this way,
20 // TODO: Can I move the constructor into private?
21}
22
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070023TEST(HostSensorTest, CreateHostTempSensorTest)
24{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070025 // The normal case for this sensor is to be a temperature sensor, where
26 // the value is treated as a margin sensor.
27
28 sdbusplus::SdBusMock sdbus_mock;
29 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
30 std::string name = "fleeting0";
31 int64_t timeout = 1;
Patrick Venturee2ec0f62018-09-04 12:30:27 -070032 const char* objPath = "/asdf/asdf0";
Patrick Venture1fa9aab2018-06-11 10:46:49 -070033 bool defer = false;
34 std::string interface = "xyz.openbmc_project.Sensor.Value";
35
James Feist0709e2f2020-07-08 10:59:45 -070036 std::vector<std::string> properties = {};
37 double d;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070038
Patrick Venture563a3562018-10-30 09:31:26 -070039 // The createTemp updates all the properties, however, only Scale is set
Patrick Venture1fa9aab2018-06-11 10:46:49 -070040 // to non-default.
James Feist0709e2f2020-07-08 10:59:45 -070041 SetupDbusObject(&sdbus_mock, defer, objPath, interface, properties, &d);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070042
43 // This is called during object destruction.
44 EXPECT_CALL(sdbus_mock,
45 sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
46 .WillOnce(Return(0));
47
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070048 std::unique_ptr<Sensor> s =
Patrick Venture563a3562018-10-30 09:31:26 -070049 HostSensor::createTemp(name, timeout, bus_mock, objPath, defer);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070050}
51
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070052TEST(HostSensorTest, VerifyWriteThenReadMatches)
53{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070054 // Verify that when value is updated, the information matches
55 // what we expect when read back.
56
57 sdbusplus::SdBusMock sdbus_mock;
58 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
59 std::string name = "fleeting0";
60 int64_t timeout = 1;
Patrick Venturee2ec0f62018-09-04 12:30:27 -070061 const char* objPath = "/asdf/asdf0";
Patrick Venture1fa9aab2018-06-11 10:46:49 -070062 bool defer = false;
63 std::string interface = "xyz.openbmc_project.Sensor.Value";
64
James Feist0709e2f2020-07-08 10:59:45 -070065 std::vector<std::string> properties = {};
66 double d;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070067
James Feist0709e2f2020-07-08 10:59:45 -070068 SetupDbusObject(&sdbus_mock, defer, objPath, interface, properties, &d);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070069
70 EXPECT_CALL(sdbus_mock,
71 sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
72 .WillOnce(Return(0));
73
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070074 std::unique_ptr<Sensor> s =
Patrick Venture563a3562018-10-30 09:31:26 -070075 HostSensor::createTemp(name, timeout, bus_mock, objPath, defer);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070076
77 // Value is updated from dbus calls only (normally).
Patrick Venturee2ec0f62018-09-04 12:30:27 -070078 HostSensor* hs = static_cast<HostSensor*>(s.get());
James Feist0709e2f2020-07-08 10:59:45 -070079 double new_value = 2;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070080
81 ReadReturn r = hs->read();
82 EXPECT_EQ(r.value, 0);
83
84 EXPECT_CALL(sdbus_mock,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070085 sd_bus_emit_properties_changed_strv(
86 IsNull(), StrEq(objPath), StrEq(interface), NotNull()))
Patrick Venturee2ec0f62018-09-04 12:30:27 -070087 .WillOnce(Invoke([=](sd_bus* bus, const char* path,
88 const char* interface, char** names) {
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070089 EXPECT_STREQ("Value", names[0]);
90 return 0;
91 }));
Patrick Venture1fa9aab2018-06-11 10:46:49 -070092
93 std::chrono::high_resolution_clock::time_point t1 =
94 std::chrono::high_resolution_clock::now();
95
96 hs->value(new_value);
97 r = hs->read();
James Feist0709e2f2020-07-08 10:59:45 -070098 EXPECT_EQ(r.value, new_value);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070099
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700100 auto duration =
101 std::chrono::duration_cast<std::chrono::seconds>(t1 - r.updated)
102 .count();
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700103
104 // Verify it was updated within the last second.
105 EXPECT_TRUE(duration < 1);
106}