blob: 71314fad2ef8a108d3bded04a5c907abad199679 [file] [log] [blame]
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01001#include "dbus_environment.hpp"
2#include "helpers.hpp"
3#include "mocks/trigger_factory_mock.hpp"
4#include "mocks/trigger_mock.hpp"
5#include "params/trigger_params.hpp"
6#include "trigger_manager.hpp"
7
8using namespace testing;
9
10class TestTriggerManager : public Test
11{
12 public:
13 std::pair<boost::system::error_code, std::string>
14 addTrigger(const TriggerParams& params)
15 {
16 std::promise<std::pair<boost::system::error_code, std::string>>
17 addTriggerPromise;
18 DbusEnvironment::getBus()->async_method_call(
19 [&addTriggerPromise](boost::system::error_code ec,
20 const std::string& path) {
21 addTriggerPromise.set_value({ec, path});
22 },
23 DbusEnvironment::serviceName(), TriggerManager::triggerManagerPath,
24 TriggerManager::triggerManagerIfaceName, "AddTrigger",
25 params.name(), params.isDiscrete(), params.logToJournal(),
26 params.logToRedfish(), params.updateReport(), params.sensors(),
27 params.reportNames(), params.thresholds());
28 return DbusEnvironment::waitForFuture(addTriggerPromise.get_future());
29 }
30
31 TriggerParams triggerParams;
32 std::unique_ptr<TriggerFactoryMock> triggerFactoryMockPtr =
33 std::make_unique<NiceMock<TriggerFactoryMock>>();
34 TriggerFactoryMock& triggerFactoryMock = *triggerFactoryMockPtr;
35 std::unique_ptr<TriggerMock> triggerMockPtr =
36 std::make_unique<NiceMock<TriggerMock>>(triggerParams.name());
37 TriggerMock& triggerMock = *triggerMockPtr;
38 std::unique_ptr<TriggerManager> sut = std::make_unique<TriggerManager>(
39 std::move(triggerFactoryMockPtr),
40 std::move(DbusEnvironment::getObjServer()));
41 MockFunction<void(std::string)> checkPoint;
42};
43
44TEST_F(TestTriggerManager, addTrigger)
45{
46 triggerFactoryMock.expectMake(triggerParams, Ref(*sut))
47 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
48
49 auto [ec, path] = addTrigger(triggerParams);
50 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
51 EXPECT_THAT(path, Eq(triggerMock.getPath()));
52}
53
54TEST_F(TestTriggerManager, failToAddTriggerTwice)
55{
56 triggerFactoryMock.expectMake(triggerParams, Ref(*sut))
57 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
58
59 addTrigger(triggerParams);
60
61 auto [ec, path] = addTrigger(triggerParams);
62 EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists));
63 EXPECT_THAT(path, Eq(std::string()));
64}
65
66TEST_F(TestTriggerManager, failToAddTriggerWhenMaxTriggerIsReached)
67{
68 triggerFactoryMock.expectMake(std::nullopt, Ref(*sut))
69 .Times(TriggerManager::maxTriggers);
70
71 for (size_t i = 0; i < TriggerManager::maxTriggers; i++)
72 {
73 triggerParams.name(triggerParams.name() + std::to_string(i));
74
75 auto [ec, path] = addTrigger(triggerParams);
76 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
77 }
78
79 triggerParams.name(triggerParams.name() +
80 std::to_string(TriggerManager::maxTriggers));
81 auto [ec, path] = addTrigger(triggerParams);
82 EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open));
83 EXPECT_THAT(path, Eq(std::string()));
84}
85
86TEST_F(TestTriggerManager, removeTrigger)
87{
88 {
89 InSequence seq;
90 triggerFactoryMock.expectMake(triggerParams, Ref(*sut))
91 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
92 EXPECT_CALL(triggerMock, Die());
93 EXPECT_CALL(checkPoint, Call("end"));
94 }
95
96 addTrigger(triggerParams);
97 sut->removeTrigger(&triggerMock);
98 checkPoint.Call("end");
99}
100
101TEST_F(TestTriggerManager, removingTriggerThatIsNotInContainerHasNoEffect)
102{
103 {
104 InSequence seq;
105 EXPECT_CALL(checkPoint, Call("end"));
106 EXPECT_CALL(triggerMock, Die());
107 }
108
109 sut->removeTrigger(&triggerMock);
110 checkPoint.Call("end");
111}
112
113TEST_F(TestTriggerManager, removingSameTriggerTwiceHasNoSideEffect)
114{
115 {
116 InSequence seq;
117 triggerFactoryMock.expectMake(triggerParams, Ref(*sut))
118 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
119 EXPECT_CALL(triggerMock, Die());
120 EXPECT_CALL(checkPoint, Call("end"));
121 }
122
123 addTrigger(triggerParams);
124 sut->removeTrigger(&triggerMock);
125 sut->removeTrigger(&triggerMock);
126 checkPoint.Call("end");
127}