blob: 5bddf5163dfd0a55deb82aff921c144146826699 [file] [log] [blame]
James Feist75eb7692019-02-25 12:50:02 -08001#include "conf.hpp"
Patrick Venture0ef1faf2018-06-13 12:50:53 -07002#include "dbus/dbuspassive.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07003#include "test/dbushelper_mock.hpp"
Patrick Venture0ef1faf2018-06-13 12:50:53 -07004
Patrick Venture0ef1faf2018-06-13 12:50:53 -07005#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -07006
7#include <functional>
Patrick Venture0ef1faf2018-06-13 12:50:53 -07008#include <string>
James Feist1f802f52019-02-08 13:51:43 -08009#include <variant>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070010
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070011#include <gmock/gmock.h>
12#include <gtest/gtest.h>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070013
Patrick Venturea0764872020-08-08 07:48:43 -070014namespace pid_control
15{
16namespace
17{
18
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070019using ::testing::_;
Patrick Venture0ef1faf2018-06-13 12:50:53 -070020using ::testing::InSequence;
21using ::testing::Invoke;
22using ::testing::IsNull;
23using ::testing::NotNull;
24using ::testing::Return;
25using ::testing::StrEq;
Patrick Venture0ef1faf2018-06-13 12:50:53 -070026
27std::string SensorIntf = "xyz.openbmc_project.Sensor.Value";
28
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070029TEST(DbusPassiveTest, FactoryFailsWithInvalidType)
30{
Patrick Venture0ef1faf2018-06-13 12:50:53 -070031 // Verify the type is checked by the factory.
32
33 sdbusplus::SdBusMock sdbus_mock;
34 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
35 std::string type = "invalid";
36 std::string id = "id";
37
38 DbusHelperMock helper;
James Feistf81f2882019-02-26 11:26:36 -080039 auto info = conf::SensorConfig();
Patrick Venture0ef1faf2018-06-13 12:50:53 -070040
James Feist98b704e2019-06-03 16:24:53 -070041 std::unique_ptr<ReadInterface> ri = DbusPassive::createDbusPassive(
42 bus_mock, type, id, &helper, &info, nullptr);
Patrick Venture0ef1faf2018-06-13 12:50:53 -070043
44 EXPECT_EQ(ri, nullptr);
45}
46
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070047TEST(DbusPassiveTest, BoringConstructorTest)
48{
Patrick Venturef8cb4642018-10-30 12:02:53 -070049 // Simply build the object, does no error checking.
Patrick Venture0ef1faf2018-06-13 12:50:53 -070050
51 sdbusplus::SdBusMock sdbus_mock;
52 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
53 std::string type = "invalid";
54 std::string id = "id";
55 std::string path = "/xyz/openbmc_project/sensors/unknown/id";
56
57 DbusHelperMock helper;
Patrick Venturef8cb4642018-10-30 12:02:53 -070058 struct SensorProperties properties;
Patrick Venture0ef1faf2018-06-13 12:50:53 -070059
James Feist98b704e2019-06-03 16:24:53 -070060 DbusPassive(bus_mock, type, id, &helper, properties, false, path, nullptr);
Patrick Venture0ef1faf2018-06-13 12:50:53 -070061 // Success
62}
63
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070064class DbusPassiveTestObj : public ::testing::Test
65{
66 protected:
67 DbusPassiveTestObj() :
68 sdbus_mock(),
69 bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), helper()
70 {
Patrick Venture563a3562018-10-30 09:31:26 -070071 EXPECT_CALL(helper, getService(_, StrEq(SensorIntf), StrEq(path)))
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070072 .WillOnce(Return("asdf"));
Patrick Venture0ef1faf2018-06-13 12:50:53 -070073
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070074 EXPECT_CALL(helper,
Patrick Venture563a3562018-10-30 09:31:26 -070075 getProperties(_, StrEq("asdf"), StrEq(path), NotNull()))
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070076 .WillOnce(Invoke(
Patrick Venturee2ec0f62018-09-04 12:30:27 -070077 [&](sdbusplus::bus::bus& bus, const std::string& service,
78 const std::string& path, struct SensorProperties* prop) {
Patrick Venture0ef1faf2018-06-13 12:50:53 -070079 prop->scale = _scale;
80 prop->value = _value;
81 prop->unit = "x";
Patrick Venture6b9f5992019-09-10 09:18:28 -070082 prop->min = 0;
83 prop->max = 0;
Patrick Venture0ef1faf2018-06-13 12:50:53 -070084 }));
Patrick Venture563a3562018-10-30 09:31:26 -070085 EXPECT_CALL(helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path)))
James Feist36b7d8e2018-10-05 15:39:01 -070086 .WillOnce(Return(false));
Patrick Venture0ef1faf2018-06-13 12:50:53 -070087
James Feistf81f2882019-02-26 11:26:36 -080088 auto info = conf::SensorConfig();
James Feist98b704e2019-06-03 16:24:53 -070089 ri = DbusPassive::createDbusPassive(bus_mock, type, id, &helper, &info,
90 nullptr);
Patrick Venturee2ec0f62018-09-04 12:30:27 -070091 passive = reinterpret_cast<DbusPassive*>(ri.get());
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070092 EXPECT_FALSE(passive == nullptr);
93 }
Patrick Venture0ef1faf2018-06-13 12:50:53 -070094
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070095 sdbusplus::SdBusMock sdbus_mock;
96 sdbusplus::bus::bus bus_mock;
97 DbusHelperMock helper;
98 std::string type = "temp";
99 std::string id = "id";
100 std::string path = "/xyz/openbmc_project/sensors/temperature/id";
101 int64_t _scale = -3;
102 int64_t _value = 10;
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700103
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700104 std::unique_ptr<ReadInterface> ri;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700105 DbusPassive* passive;
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700106};
107
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700108TEST_F(DbusPassiveTestObj, ReadReturnsExpectedValues)
109{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700110 // Verify read is returning the values.
111 ReadReturn v;
112 v.value = 0.01;
113 // TODO: updated is set when the value is created, so we can range check
114 // it.
115 ReadReturn r = passive->read();
116 EXPECT_EQ(v.value, r.value);
117}
118
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700119TEST_F(DbusPassiveTestObj, SetValueUpdatesValue)
120{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700121 // Verify setvalue does as advertised.
122
123 double value = 0.01;
124 passive->setValue(value);
125
126 // TODO: updated is set when the value is set, so we can range check it.
127 ReadReturn r = passive->read();
128 EXPECT_EQ(value, r.value);
129}
130
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700131TEST_F(DbusPassiveTestObj, GetScaleReturnsExpectedValue)
132{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700133 // Verify the scale is returned as expected.
134 EXPECT_EQ(_scale, passive->getScale());
135}
136
Patrick Venture563a3562018-10-30 09:31:26 -0700137TEST_F(DbusPassiveTestObj, getIDReturnsExpectedValue)
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700138{
Patrick Venture563a3562018-10-30 09:31:26 -0700139 // Verify getID returns the expected value.
140 EXPECT_EQ(id, passive->getID());
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700141}
142
Patrick Venture6b9f5992019-09-10 09:18:28 -0700143TEST_F(DbusPassiveTestObj, GetMinValueReturnsExpectedValue)
144{
145 EXPECT_DOUBLE_EQ(0, passive->getMin());
146}
147
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700148TEST_F(DbusPassiveTestObj, VerifyHandlesDbusSignal)
149{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700150 // The dbus passive sensor listens for updates and if it's the Value
151 // property, it needs to handle it.
152
153 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
154 .WillOnce(Return(nullptr));
155 sdbusplus::message::message msg(nullptr, &sdbus_mock);
156
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700157 const char* Value = "Value";
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700158 int64_t xValue = 10000;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700159 const char* intf = "xyz.openbmc_project.Sensor.Value";
James Feist1f802f52019-02-08 13:51:43 -0800160 // string, std::map<std::string, std::variant<int64_t>>
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700161 // msg.read(msgSensor, msgData);
162
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700163 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700164 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
165 const char** s = static_cast<const char**>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700166 // Read the first parameter, the string.
167 *s = intf;
168 return 0;
169 }))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700170 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
171 const char** s = static_cast<const char**>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700172 *s = Value;
173 // Read the string in the pair (dictionary).
174 return 0;
175 }));
176
177 // std::map
178 EXPECT_CALL(sdbus_mock,
179 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
180 .WillOnce(Return(0));
181
182 // while !at_end()
183 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
184 .WillOnce(Return(0))
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700185 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700186
187 // std::pair
188 EXPECT_CALL(sdbus_mock,
189 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
190 .WillOnce(Return(0));
191
192 EXPECT_CALL(sdbus_mock,
193 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
194 .WillOnce(Return(1));
195 EXPECT_CALL(sdbus_mock,
196 sd_bus_message_enter_container(IsNull(), 'v', StrEq("x")))
197 .WillOnce(Return(0));
198
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700199 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'x', NotNull()))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700200 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
201 int64_t* s = static_cast<int64_t*>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700202 *s = xValue;
203 return 0;
204 }));
205
206 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
207 .WillOnce(Return(0)) /* variant. */
208 .WillOnce(Return(0)) /* std::pair */
209 .WillOnce(Return(0)); /* std::map */
210
Patrick Venture7af157b2018-10-30 11:24:40 -0700211 int rv = handleSensorValue(msg, passive);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700212 EXPECT_EQ(rv, 0); // It's always 0.
213
214 ReadReturn r = passive->read();
215 EXPECT_EQ(10, r.value);
216}
217
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700218TEST_F(DbusPassiveTestObj, VerifyIgnoresOtherPropertySignal)
219{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700220 // The dbus passive sensor listens for updates and if it's the Value
221 // property, it needs to handle it. In this case, it won't be.
222
223 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
224 .WillOnce(Return(nullptr));
225 sdbusplus::message::message msg(nullptr, &sdbus_mock);
226
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700227 const char* Scale = "Scale";
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700228 int64_t xScale = -6;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700229 const char* intf = "xyz.openbmc_project.Sensor.Value";
James Feist1f802f52019-02-08 13:51:43 -0800230 // string, std::map<std::string, std::variant<int64_t>>
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700231 // msg.read(msgSensor, msgData);
232
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700233 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700234 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
235 const char** s = static_cast<const char**>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700236 // Read the first parameter, the string.
237 *s = intf;
238 return 0;
239 }))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700240 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
241 const char** s = static_cast<const char**>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700242 *s = Scale;
243 // Read the string in the pair (dictionary).
244 return 0;
245 }));
246
247 // std::map
248 EXPECT_CALL(sdbus_mock,
249 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
250 .WillOnce(Return(0));
251
252 // while !at_end()
253 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
254 .WillOnce(Return(0))
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700255 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700256
257 // std::pair
258 EXPECT_CALL(sdbus_mock,
259 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
260 .WillOnce(Return(0));
261
262 EXPECT_CALL(sdbus_mock,
263 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
264 .WillOnce(Return(1));
265 EXPECT_CALL(sdbus_mock,
266 sd_bus_message_enter_container(IsNull(), 'v', StrEq("x")))
267 .WillOnce(Return(0));
268
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700269 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'x', NotNull()))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700270 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
271 int64_t* s = static_cast<int64_t*>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700272 *s = xScale;
273 return 0;
274 }));
275
276 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
277 .WillOnce(Return(0)) /* variant. */
278 .WillOnce(Return(0)) /* std::pair */
279 .WillOnce(Return(0)); /* std::map */
280
Patrick Venture7af157b2018-10-30 11:24:40 -0700281 int rv = handleSensorValue(msg, passive);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700282 EXPECT_EQ(rv, 0); // It's always 0.
283
284 ReadReturn r = passive->read();
285 EXPECT_EQ(0.01, r.value);
286}
James Feist36b7d8e2018-10-05 15:39:01 -0700287TEST_F(DbusPassiveTestObj, VerifyCriticalThresholdAssert)
288{
289
290 // Verifies when a threshold is crossed the sensor goes into error state
291 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
292 .WillOnce(Return(nullptr));
293 sdbusplus::message::message msg(nullptr, &sdbus_mock);
294
295 const char* criticalAlarm = "CriticalAlarmHigh";
296 bool alarm = true;
297 const char* intf = "xyz.openbmc_project.Sensor.Threshold.Critical";
298
299 passive->setFailed(false);
300
301 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
302 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
303 const char** s = static_cast<const char**>(p);
304 // Read the first parameter, the string.
305 *s = intf;
306 return 0;
307 }))
308 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
309 const char** s = static_cast<const char**>(p);
310 *s = criticalAlarm;
311 // Read the string in the pair (dictionary).
312 return 0;
313 }));
314
315 // std::map
316 EXPECT_CALL(sdbus_mock,
317 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
318 .WillOnce(Return(0));
319
320 // while !at_end()
321 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
322 .WillOnce(Return(0))
323 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
324
325 // std::pair
326 EXPECT_CALL(sdbus_mock,
327 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
328 .WillOnce(Return(0));
329
330 EXPECT_CALL(sdbus_mock,
331 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
332 .WillOnce(Return(0));
333 EXPECT_CALL(sdbus_mock,
334 sd_bus_message_verify_type(IsNull(), 'v', StrEq("d")))
335 .WillOnce(Return(0));
336 EXPECT_CALL(sdbus_mock,
337 sd_bus_message_verify_type(IsNull(), 'v', StrEq("b")))
338 .WillOnce(Return(1));
339 EXPECT_CALL(sdbus_mock,
340 sd_bus_message_enter_container(IsNull(), 'v', StrEq("b")))
341 .WillOnce(Return(0));
342
343 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull()))
344 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
345 bool* s = static_cast<bool*>(p);
346 *s = alarm;
347 return 0;
348 }));
349
350 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
351 .WillOnce(Return(0)) /* variant. */
352 .WillOnce(Return(0)) /* std::pair */
353 .WillOnce(Return(0)); /* std::map */
354
Patrick Venture7af157b2018-10-30 11:24:40 -0700355 int rv = handleSensorValue(msg, passive);
James Feist36b7d8e2018-10-05 15:39:01 -0700356 EXPECT_EQ(rv, 0); // It's always 0.
357 bool failed = passive->getFailed();
358 EXPECT_EQ(failed, true);
359}
360
361TEST_F(DbusPassiveTestObj, VerifyCriticalThresholdDeassert)
362{
363
364 // Verifies when a threshold is deasserted a failed sensor goes back into
365 // the normal state
366 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
367 .WillOnce(Return(nullptr));
368 sdbusplus::message::message msg(nullptr, &sdbus_mock);
369
370 const char* criticalAlarm = "CriticalAlarmHigh";
371 bool alarm = false;
372 const char* intf = "xyz.openbmc_project.Sensor.Threshold.Critical";
373
374 passive->setFailed(true);
375
376 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
377 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
378 const char** s = static_cast<const char**>(p);
379 // Read the first parameter, the string.
380 *s = intf;
381 return 0;
382 }))
383 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
384 const char** s = static_cast<const char**>(p);
385 *s = criticalAlarm;
386 // Read the string in the pair (dictionary).
387 return 0;
388 }));
389
390 // std::map
391 EXPECT_CALL(sdbus_mock,
392 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
393 .WillOnce(Return(0));
394
395 // while !at_end()
396 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
397 .WillOnce(Return(0))
398 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
399
400 // std::pair
401 EXPECT_CALL(sdbus_mock,
402 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
403 .WillOnce(Return(0));
404
405 EXPECT_CALL(sdbus_mock,
406 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
407 .WillOnce(Return(0));
408 EXPECT_CALL(sdbus_mock,
409 sd_bus_message_verify_type(IsNull(), 'v', StrEq("d")))
410 .WillOnce(Return(0));
411 EXPECT_CALL(sdbus_mock,
412 sd_bus_message_verify_type(IsNull(), 'v', StrEq("b")))
413 .WillOnce(Return(1));
414 EXPECT_CALL(sdbus_mock,
415 sd_bus_message_enter_container(IsNull(), 'v', StrEq("b")))
416 .WillOnce(Return(0));
417
418 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull()))
419 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
420 bool* s = static_cast<bool*>(p);
421 *s = alarm;
422 return 0;
423 }));
424
425 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
426 .WillOnce(Return(0)) /* variant. */
427 .WillOnce(Return(0)) /* std::pair */
428 .WillOnce(Return(0)); /* std::map */
429
Patrick Venture7af157b2018-10-30 11:24:40 -0700430 int rv = handleSensorValue(msg, passive);
James Feist36b7d8e2018-10-05 15:39:01 -0700431 EXPECT_EQ(rv, 0); // It's always 0.
432 bool failed = passive->getFailed();
433 EXPECT_EQ(failed, false);
Patrick Venture563a3562018-10-30 09:31:26 -0700434}
Patrick Venture6b9f5992019-09-10 09:18:28 -0700435
436void GetPropertiesMax3k(sdbusplus::bus::bus& bus, const std::string& service,
437 const std::string& path, SensorProperties* prop)
438{
439 prop->scale = -3;
440 prop->value = 10;
441 prop->unit = "x";
442 prop->min = 0;
443 prop->max = 3000;
444}
445
446using GetPropertiesFunction =
447 std::function<void(sdbusplus::bus::bus&, const std::string&,
448 const std::string&, SensorProperties*)>;
449
450// TODO: There is definitely a cleaner way to do this.
451class DbusPassiveTest3kMaxObj : public ::testing::Test
452{
453 protected:
454 DbusPassiveTest3kMaxObj() :
455 sdbus_mock(),
456 bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), helper()
457 {
458 EXPECT_CALL(helper, getService(_, StrEq(SensorIntf), StrEq(path)))
459 .WillOnce(Return("asdf"));
460
461 EXPECT_CALL(helper,
462 getProperties(_, StrEq("asdf"), StrEq(path), NotNull()))
463 .WillOnce(_getProps);
464 EXPECT_CALL(helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path)))
465 .WillOnce(Return(false));
466
467 auto info = conf::SensorConfig();
468 ri = DbusPassive::createDbusPassive(bus_mock, type, id, &helper, &info,
469 nullptr);
470 passive = reinterpret_cast<DbusPassive*>(ri.get());
471 EXPECT_FALSE(passive == nullptr);
472 }
473
474 sdbusplus::SdBusMock sdbus_mock;
475 sdbusplus::bus::bus bus_mock;
476 DbusHelperMock helper;
477 std::string type = "temp";
478 std::string id = "id";
479 std::string path = "/xyz/openbmc_project/sensors/temperature/id";
480 int64_t _scale = -3;
481 int64_t _value = 10;
482
483 std::unique_ptr<ReadInterface> ri;
484 DbusPassive* passive;
485 GetPropertiesFunction _getProps = &GetPropertiesMax3k;
486};
487
488TEST_F(DbusPassiveTest3kMaxObj, ReadMinAndMaxReturnsExpected)
489{
490 EXPECT_DOUBLE_EQ(0, passive->getMin());
491 EXPECT_DOUBLE_EQ(3, passive->getMax());
492}
493
494class DbusPassiveTest3kMaxIgnoredObj : public ::testing::Test
495{
496 protected:
497 DbusPassiveTest3kMaxIgnoredObj() :
498 sdbus_mock(),
499 bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), helper()
500 {
501 EXPECT_CALL(helper, getService(_, StrEq(SensorIntf), StrEq(path)))
502 .WillOnce(Return("asdf"));
503
504 EXPECT_CALL(helper,
505 getProperties(_, StrEq("asdf"), StrEq(path), NotNull()))
506 .WillOnce(_getProps);
507 EXPECT_CALL(helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path)))
508 .WillOnce(Return(false));
509
510 auto info = conf::SensorConfig();
511 info.ignoreDbusMinMax = true;
512 ri = DbusPassive::createDbusPassive(bus_mock, type, id, &helper, &info,
513 nullptr);
514 passive = reinterpret_cast<DbusPassive*>(ri.get());
515 EXPECT_FALSE(passive == nullptr);
516 }
517
518 sdbusplus::SdBusMock sdbus_mock;
519 sdbusplus::bus::bus bus_mock;
520 DbusHelperMock helper;
521 std::string type = "temp";
522 std::string id = "id";
523 std::string path = "/xyz/openbmc_project/sensors/temperature/id";
524 int64_t _scale = -3;
525 int64_t _value = 10;
526
527 std::unique_ptr<ReadInterface> ri;
528 DbusPassive* passive;
529 GetPropertiesFunction _getProps = &GetPropertiesMax3k;
530};
531
532TEST_F(DbusPassiveTest3kMaxIgnoredObj, ReadMinAndMaxReturnsExpected)
533{
534 EXPECT_DOUBLE_EQ(0, passive->getMin());
535 EXPECT_DOUBLE_EQ(0, passive->getMax());
536}
Patrick Venturea0764872020-08-08 07:48:43 -0700537
538} // namespace
539} // namespace pid_control