blob: 5cbfbc70c35427f2c5ac0f2452092bdecd2e9384 [file] [log] [blame]
Matthew Barth2d2caa32020-05-26 11:07:24 -05001#include "../fallback.hpp"
2
3#include "../psensor.hpp"
4
Brad Bishop7cd75d72017-07-13 01:42:26 -04005#include <iostream>
6#include <string>
Matthew Barth2d2caa32020-05-26 11:07:24 -05007
Brad Bishop7cd75d72017-07-13 01:42:26 -04008#include <gtest/gtest.h>
Brad Bishop7cd75d72017-07-13 01:42:26 -04009
10int pstate = -1;
11
12namespace phosphor
13{
14namespace fan
15{
16namespace presence
17{
Mike Cappsa0819562022-06-13 10:17:10 -040018void setPresence(const Fan&, bool newState)
Brad Bishop7cd75d72017-07-13 01:42:26 -040019{
20 if (newState)
21 {
22 pstate = 1;
23 }
24 else
25 {
26 pstate = 0;
27 }
28}
29
30class TestSensor : public PresenceSensor
31{
Matthew Barth2d2caa32020-05-26 11:07:24 -050032 public:
33 bool start() override
34 {
35 ++started;
36 return _present;
37 }
Brad Bishop7cd75d72017-07-13 01:42:26 -040038
Matthew Barth2d2caa32020-05-26 11:07:24 -050039 void stop() override
40 {
41 ++stopped;
42 }
Brad Bishop7cd75d72017-07-13 01:42:26 -040043
Matthew Barth2d2caa32020-05-26 11:07:24 -050044 bool present() override
45 {
46 return _present;
47 }
Brad Bishop7cd75d72017-07-13 01:42:26 -040048
Matthew Barth2d2caa32020-05-26 11:07:24 -050049 void fail() override
50 {
51 ++failed;
52 }
Brad Bishop7cd75d72017-07-13 01:42:26 -040053
Matt Spinlerf27192e2025-08-20 14:51:45 -050054 void logConflict(const std::string&) const override {}
55
Matthew Barth2d2caa32020-05-26 11:07:24 -050056 bool _present = false;
57 size_t started = 0;
58 size_t stopped = 0;
59 size_t failed = 0;
Brad Bishop7cd75d72017-07-13 01:42:26 -040060};
61
62} // namespace presence
63} // namespace fan
64} // namespace phosphor
65
66using namespace phosphor::fan::presence;
67
68TEST(FallbackTest, TestOne)
69{
70 // Validate a single sensor.
71 // Validate on->off->on.
72 pstate = -1;
Mike Cappsa0819562022-06-13 10:17:10 -040073 Fan fan{"/path", "name", 0};
Brad Bishop7cd75d72017-07-13 01:42:26 -040074 TestSensor ts;
75 ts._present = true;
76 std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts};
77 Fallback f{fan, sensors};
78
79 f.monitor();
80 ASSERT_EQ(pstate, 1);
81 ASSERT_EQ(ts.failed, 0);
82 ASSERT_EQ(ts.stopped, 0);
83 ASSERT_EQ(ts.started, 1);
84
Matt Spinlerf27192e2025-08-20 14:51:45 -050085 f.stateChanged(false, ts);
Brad Bishop7cd75d72017-07-13 01:42:26 -040086 ASSERT_EQ(pstate, 0);
87 ASSERT_EQ(ts.failed, 0);
88 ASSERT_EQ(ts.stopped, 0);
89 ASSERT_EQ(ts.started, 1);
90
Matt Spinlerf27192e2025-08-20 14:51:45 -050091 f.stateChanged(true, ts);
Brad Bishop7cd75d72017-07-13 01:42:26 -040092 ASSERT_EQ(pstate, 1);
93 ASSERT_EQ(ts.failed, 0);
94 ASSERT_EQ(ts.stopped, 0);
95 ASSERT_EQ(ts.started, 1);
96}
97
98TEST(FallbackTest, TestTwoA)
99{
100 // Validate two sensors.
101 // Validate both sensors on->off->on
102
103 pstate = -1;
Mike Cappsa0819562022-06-13 10:17:10 -0400104 Fan fan{"/path", "name", 0};
Brad Bishop7cd75d72017-07-13 01:42:26 -0400105 TestSensor ts1, ts2;
106 ts1._present = true;
107 ts2._present = true;
108
109 std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts1, ts2};
110 Fallback f{fan, sensors};
111
112 f.monitor();
113 ASSERT_EQ(pstate, 1);
114 ASSERT_EQ(ts1.failed, 0);
115 ASSERT_EQ(ts1.stopped, 0);
116 ASSERT_EQ(ts1.started, 1);
117 ASSERT_EQ(ts2.failed, 0);
118 ASSERT_EQ(ts2.stopped, 0);
119 ASSERT_EQ(ts2.started, 0);
120
121 ts1._present = false;
122 ts2._present = false;
Matt Spinlerf27192e2025-08-20 14:51:45 -0500123 f.stateChanged(false, ts1);
Brad Bishop7cd75d72017-07-13 01:42:26 -0400124 ASSERT_EQ(pstate, 0);
125 ASSERT_EQ(ts1.failed, 0);
126 ASSERT_EQ(ts1.stopped, 0);
127 ASSERT_EQ(ts1.started, 1);
128 ASSERT_EQ(ts2.failed, 0);
129 ASSERT_EQ(ts2.stopped, 0);
130 ASSERT_EQ(ts2.started, 0);
131
132 ts1._present = true;
133 ts2._present = true;
Matt Spinlerf27192e2025-08-20 14:51:45 -0500134 f.stateChanged(true, ts1);
Brad Bishop7cd75d72017-07-13 01:42:26 -0400135 ASSERT_EQ(pstate, 1);
136 ASSERT_EQ(ts1.failed, 0);
137 ASSERT_EQ(ts1.stopped, 0);
138 ASSERT_EQ(ts1.started, 1);
139 ASSERT_EQ(ts2.failed, 0);
140 ASSERT_EQ(ts2.stopped, 0);
141 ASSERT_EQ(ts2.started, 0);
142}
143
Brad Bishop7cd75d72017-07-13 01:42:26 -0400144TEST(FallbackTest, TestTwoB)
145{
146 // Validate two sensors.
147 // Validate first sensor on->off.
148
149 pstate = -1;
Matt Spinlerf27192e2025-08-20 14:51:45 -0500150 Fan fan{"/path", "name", std::nullopt};
Brad Bishop7cd75d72017-07-13 01:42:26 -0400151 TestSensor ts1, ts2;
152 ts1._present = true;
153 ts2._present = true;
154
155 std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts1, ts2};
156 Fallback f{fan, sensors};
157
158 f.monitor();
159 ASSERT_EQ(pstate, 1);
160 ts1._present = false;
Matt Spinlerf27192e2025-08-20 14:51:45 -0500161 f.stateChanged(false, ts1);
Brad Bishop7cd75d72017-07-13 01:42:26 -0400162 ASSERT_EQ(pstate, 1);
163 ASSERT_EQ(ts1.failed, 1);
164 ASSERT_EQ(ts1.stopped, 1);
165 ASSERT_EQ(ts1.started, 1);
166 ASSERT_EQ(ts2.failed, 0);
167 ASSERT_EQ(ts2.stopped, 0);
168 ASSERT_EQ(ts2.started, 1);
169
170 // Flip the state of both sensors.
171 ts1._present = true;
172 ts2._present = false;
Matt Spinlerf27192e2025-08-20 14:51:45 -0500173 f.stateChanged(false, ts1);
Brad Bishop7cd75d72017-07-13 01:42:26 -0400174 ASSERT_EQ(pstate, 0);
175 ASSERT_EQ(ts1.failed, 1);
176 ASSERT_EQ(ts1.stopped, 1);
177 ASSERT_EQ(ts1.started, 1);
178 ASSERT_EQ(ts2.failed, 0);
179 ASSERT_EQ(ts2.stopped, 0);
180 ASSERT_EQ(ts2.started, 1);
181}
182
183TEST(FallbackTest, TestTwoC)
184{
185 // Validate two sensors.
186 // Validate first in bad state.
187
188 pstate = -1;
Matt Spinlerf27192e2025-08-20 14:51:45 -0500189 Fan fan{"/path", "name", std::nullopt};
Brad Bishop7cd75d72017-07-13 01:42:26 -0400190 TestSensor ts1, ts2;
191 ts1._present = false;
192 ts2._present = true;
193
194 std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts1, ts2};
195 Fallback f{fan, sensors};
196
197 f.monitor();
198 ASSERT_EQ(pstate, 1);
199 ASSERT_EQ(ts1.failed, 1);
200 ASSERT_EQ(ts1.stopped, 0);
201 ASSERT_EQ(ts1.started, 0);
202 ASSERT_EQ(ts2.failed, 0);
203 ASSERT_EQ(ts2.stopped, 0);
204 ASSERT_EQ(ts2.started, 1);
205
Matt Spinlerf27192e2025-08-20 14:51:45 -0500206 f.stateChanged(false, ts1);
Brad Bishop7cd75d72017-07-13 01:42:26 -0400207 ASSERT_EQ(pstate, 0);
208 ASSERT_EQ(ts1.failed, 1);
209 ASSERT_EQ(ts1.stopped, 0);
210 ASSERT_EQ(ts1.started, 0);
211 ASSERT_EQ(ts2.failed, 0);
212 ASSERT_EQ(ts2.stopped, 0);
213 ASSERT_EQ(ts2.started, 1);
214}
215
216TEST(FallbackTest, TestTwoD)
217{
218 // Validate two sensors.
219 // Validate both in bad state.
220
221 pstate = -1;
Matt Spinlerf27192e2025-08-20 14:51:45 -0500222 Fan fan{"/path", "name", std::nullopt};
Brad Bishop7cd75d72017-07-13 01:42:26 -0400223 TestSensor ts1, ts2;
224 ts1._present = false;
225 ts2._present = false;
226
227 std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts1, ts2};
228 Fallback f{fan, sensors};
229
230 f.monitor();
231 ASSERT_EQ(pstate, 0);
232 ASSERT_EQ(ts1.failed, 0);
233 ASSERT_EQ(ts1.stopped, 0);
234 ASSERT_EQ(ts1.started, 1);
235 ASSERT_EQ(ts2.failed, 0);
236 ASSERT_EQ(ts2.stopped, 0);
237 ASSERT_EQ(ts2.started, 0);
238}