blob: de4a54d11ea194afd115a4f1726b28c5c34ebf2b [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
Matthew Barth2d2caa32020-05-26 11:07:24 -050054 bool _present = false;
55 size_t started = 0;
56 size_t stopped = 0;
57 size_t failed = 0;
Brad Bishop7cd75d72017-07-13 01:42:26 -040058};
59
60} // namespace presence
61} // namespace fan
62} // namespace phosphor
63
64using namespace phosphor::fan::presence;
65
66TEST(FallbackTest, TestOne)
67{
68 // Validate a single sensor.
69 // Validate on->off->on.
70 pstate = -1;
Mike Cappsa0819562022-06-13 10:17:10 -040071 Fan fan{"/path", "name", 0};
Brad Bishop7cd75d72017-07-13 01:42:26 -040072 TestSensor ts;
73 ts._present = true;
74 std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts};
75 Fallback f{fan, sensors};
76
77 f.monitor();
78 ASSERT_EQ(pstate, 1);
79 ASSERT_EQ(ts.failed, 0);
80 ASSERT_EQ(ts.stopped, 0);
81 ASSERT_EQ(ts.started, 1);
82
83 f.stateChanged(false);
84 ASSERT_EQ(pstate, 0);
85 ASSERT_EQ(ts.failed, 0);
86 ASSERT_EQ(ts.stopped, 0);
87 ASSERT_EQ(ts.started, 1);
88
89 f.stateChanged(true);
90 ASSERT_EQ(pstate, 1);
91 ASSERT_EQ(ts.failed, 0);
92 ASSERT_EQ(ts.stopped, 0);
93 ASSERT_EQ(ts.started, 1);
94}
95
96TEST(FallbackTest, TestTwoA)
97{
98 // Validate two sensors.
99 // Validate both sensors on->off->on
100
101 pstate = -1;
Mike Cappsa0819562022-06-13 10:17:10 -0400102 Fan fan{"/path", "name", 0};
Brad Bishop7cd75d72017-07-13 01:42:26 -0400103 TestSensor ts1, ts2;
104 ts1._present = true;
105 ts2._present = true;
106
107 std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts1, ts2};
108 Fallback f{fan, sensors};
109
110 f.monitor();
111 ASSERT_EQ(pstate, 1);
112 ASSERT_EQ(ts1.failed, 0);
113 ASSERT_EQ(ts1.stopped, 0);
114 ASSERT_EQ(ts1.started, 1);
115 ASSERT_EQ(ts2.failed, 0);
116 ASSERT_EQ(ts2.stopped, 0);
117 ASSERT_EQ(ts2.started, 0);
118
119 ts1._present = false;
120 ts2._present = false;
121 f.stateChanged(false);
122 ASSERT_EQ(pstate, 0);
123 ASSERT_EQ(ts1.failed, 0);
124 ASSERT_EQ(ts1.stopped, 0);
125 ASSERT_EQ(ts1.started, 1);
126 ASSERT_EQ(ts2.failed, 0);
127 ASSERT_EQ(ts2.stopped, 0);
128 ASSERT_EQ(ts2.started, 0);
129
130 ts1._present = true;
131 ts2._present = true;
132 f.stateChanged(true);
133 ASSERT_EQ(pstate, 1);
134 ASSERT_EQ(ts1.failed, 0);
135 ASSERT_EQ(ts1.stopped, 0);
136 ASSERT_EQ(ts1.started, 1);
137 ASSERT_EQ(ts2.failed, 0);
138 ASSERT_EQ(ts2.stopped, 0);
139 ASSERT_EQ(ts2.started, 0);
140}
141
Brad Bishop7cd75d72017-07-13 01:42:26 -0400142TEST(FallbackTest, TestTwoB)
143{
144 // Validate two sensors.
145 // Validate first sensor on->off.
146
147 pstate = -1;
148 Fan fan{"/path", "name"};
149 TestSensor ts1, ts2;
150 ts1._present = true;
151 ts2._present = true;
152
153 std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts1, ts2};
154 Fallback f{fan, sensors};
155
156 f.monitor();
157 ASSERT_EQ(pstate, 1);
158 ts1._present = false;
159 f.stateChanged(false);
160 ASSERT_EQ(pstate, 1);
161 ASSERT_EQ(ts1.failed, 1);
162 ASSERT_EQ(ts1.stopped, 1);
163 ASSERT_EQ(ts1.started, 1);
164 ASSERT_EQ(ts2.failed, 0);
165 ASSERT_EQ(ts2.stopped, 0);
166 ASSERT_EQ(ts2.started, 1);
167
168 // Flip the state of both sensors.
169 ts1._present = true;
170 ts2._present = false;
171 f.stateChanged(false);
172 ASSERT_EQ(pstate, 0);
173 ASSERT_EQ(ts1.failed, 1);
174 ASSERT_EQ(ts1.stopped, 1);
175 ASSERT_EQ(ts1.started, 1);
176 ASSERT_EQ(ts2.failed, 0);
177 ASSERT_EQ(ts2.stopped, 0);
178 ASSERT_EQ(ts2.started, 1);
179}
180
181TEST(FallbackTest, TestTwoC)
182{
183 // Validate two sensors.
184 // Validate first in bad state.
185
186 pstate = -1;
187 Fan fan{"/path", "name"};
188 TestSensor ts1, ts2;
189 ts1._present = false;
190 ts2._present = true;
191
192 std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts1, ts2};
193 Fallback f{fan, sensors};
194
195 f.monitor();
196 ASSERT_EQ(pstate, 1);
197 ASSERT_EQ(ts1.failed, 1);
198 ASSERT_EQ(ts1.stopped, 0);
199 ASSERT_EQ(ts1.started, 0);
200 ASSERT_EQ(ts2.failed, 0);
201 ASSERT_EQ(ts2.stopped, 0);
202 ASSERT_EQ(ts2.started, 1);
203
204 f.stateChanged(false);
205 ASSERT_EQ(pstate, 0);
206 ASSERT_EQ(ts1.failed, 1);
207 ASSERT_EQ(ts1.stopped, 0);
208 ASSERT_EQ(ts1.started, 0);
209 ASSERT_EQ(ts2.failed, 0);
210 ASSERT_EQ(ts2.stopped, 0);
211 ASSERT_EQ(ts2.started, 1);
212}
213
214TEST(FallbackTest, TestTwoD)
215{
216 // Validate two sensors.
217 // Validate both in bad state.
218
219 pstate = -1;
220 Fan fan{"/path", "name"};
221 TestSensor ts1, ts2;
222 ts1._present = false;
223 ts2._present = false;
224
225 std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts1, ts2};
226 Fallback f{fan, sensors};
227
228 f.monitor();
229 ASSERT_EQ(pstate, 0);
230 ASSERT_EQ(ts1.failed, 0);
231 ASSERT_EQ(ts1.stopped, 0);
232 ASSERT_EQ(ts1.started, 1);
233 ASSERT_EQ(ts2.failed, 0);
234 ASSERT_EQ(ts2.stopped, 0);
235 ASSERT_EQ(ts2.started, 0);
236}