blob: 6851732080c103d343dbdd05a7e96afee259c913 [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
36 // Scale is the only property we change in the code. Also,
37 // emit_object_added isn't called twice.
38 // Temperature is the default for type, and everything, so those aren't
39 // updated.
40 std::vector<std::string> properties = {"Scale"};
41 int i;
42
Patrick Venture563a3562018-10-30 09:31:26 -070043 // The createTemp updates all the properties, however, only Scale is set
Patrick Venture1fa9aab2018-06-11 10:46:49 -070044 // to non-default.
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070045 SetupDbusObject(&sdbus_mock, defer, objPath, interface, properties, &i);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070046
47 // This is called during object destruction.
48 EXPECT_CALL(sdbus_mock,
49 sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
50 .WillOnce(Return(0));
51
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070052 std::unique_ptr<Sensor> s =
Patrick Venture563a3562018-10-30 09:31:26 -070053 HostSensor::createTemp(name, timeout, bus_mock, objPath, defer);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070054}
55
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070056TEST(HostSensorTest, VerifyWriteThenReadMatches)
57{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070058 // Verify that when value is updated, the information matches
59 // what we expect when read back.
60
61 sdbusplus::SdBusMock sdbus_mock;
62 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
63 std::string name = "fleeting0";
64 int64_t timeout = 1;
Patrick Venturee2ec0f62018-09-04 12:30:27 -070065 const char* objPath = "/asdf/asdf0";
Patrick Venture1fa9aab2018-06-11 10:46:49 -070066 bool defer = false;
67 std::string interface = "xyz.openbmc_project.Sensor.Value";
68
69 // Scale is the only property we change in the code. Also,
70 // emit_object_added isn't called twice.
71 // Temperature is the default for type, and everything, so those aren't
72 // updated.
73 std::vector<std::string> properties = {"Scale"};
74 int i;
75
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070076 SetupDbusObject(&sdbus_mock, defer, objPath, interface, properties, &i);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070077
78 EXPECT_CALL(sdbus_mock,
79 sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
80 .WillOnce(Return(0));
81
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070082 std::unique_ptr<Sensor> s =
Patrick Venture563a3562018-10-30 09:31:26 -070083 HostSensor::createTemp(name, timeout, bus_mock, objPath, defer);
Patrick Venture1fa9aab2018-06-11 10:46:49 -070084
85 // Value is updated from dbus calls only (normally).
Patrick Venturee2ec0f62018-09-04 12:30:27 -070086 HostSensor* hs = static_cast<HostSensor*>(s.get());
Patrick Venture1fa9aab2018-06-11 10:46:49 -070087 int64_t new_value = 2;
88
89 ReadReturn r = hs->read();
90 EXPECT_EQ(r.value, 0);
91
92 EXPECT_CALL(sdbus_mock,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070093 sd_bus_emit_properties_changed_strv(
94 IsNull(), StrEq(objPath), StrEq(interface), NotNull()))
Patrick Venturee2ec0f62018-09-04 12:30:27 -070095 .WillOnce(Invoke([=](sd_bus* bus, const char* path,
96 const char* interface, char** names) {
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070097 EXPECT_STREQ("Value", names[0]);
98 return 0;
99 }));
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700100
101 std::chrono::high_resolution_clock::time_point t1 =
102 std::chrono::high_resolution_clock::now();
103
104 hs->value(new_value);
105 r = hs->read();
106 EXPECT_EQ(r.value, new_value * 0.001);
107
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700108 auto duration =
109 std::chrono::duration_cast<std::chrono::seconds>(t1 - r.updated)
110 .count();
Patrick Venture1fa9aab2018-06-11 10:46:49 -0700111
112 // Verify it was updated within the last second.
113 EXPECT_TRUE(duration < 1);
114}