1 #include "../fallback.hpp"
2 
3 #include "../psensor.hpp"
4 
5 #include <iostream>
6 #include <string>
7 
8 #include <gtest/gtest.h>
9 
10 int pstate = -1;
11 
12 namespace phosphor
13 {
14 namespace fan
15 {
16 namespace presence
17 {
setPresence(const Fan &,bool newState)18 void setPresence(const Fan&, bool newState)
19 {
20     if (newState)
21     {
22         pstate = 1;
23     }
24     else
25     {
26         pstate = 0;
27     }
28 }
29 
30 class TestSensor : public PresenceSensor
31 {
32   public:
start()33     bool start() override
34     {
35         ++started;
36         return _present;
37     }
38 
stop()39     void stop() override
40     {
41         ++stopped;
42     }
43 
present()44     bool present() override
45     {
46         return _present;
47     }
48 
fail()49     void fail() override
50     {
51         ++failed;
52     }
53 
54     bool _present = false;
55     size_t started = 0;
56     size_t stopped = 0;
57     size_t failed = 0;
58 };
59 
60 } // namespace presence
61 } // namespace fan
62 } // namespace phosphor
63 
64 using namespace phosphor::fan::presence;
65 
TEST(FallbackTest,TestOne)66 TEST(FallbackTest, TestOne)
67 {
68     // Validate a single sensor.
69     // Validate on->off->on.
70     pstate = -1;
71     Fan fan{"/path", "name", 0};
72     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 
TEST(FallbackTest,TestTwoA)96 TEST(FallbackTest, TestTwoA)
97 {
98     // Validate two sensors.
99     // Validate both sensors on->off->on
100 
101     pstate = -1;
102     Fan fan{"/path", "name", 0};
103     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 
TEST(FallbackTest,TestTwoB)142 TEST(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 
TEST(FallbackTest,TestTwoC)181 TEST(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 
TEST(FallbackTest,TestTwoD)214 TEST(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 }
237