1 #include <iostream> 2 #include <string> 3 #include <gtest/gtest.h> 4 #include "../fallback.hpp" 5 #include "../psensor.hpp" 6 7 int pstate = -1; 8 9 namespace phosphor 10 { 11 namespace fan 12 { 13 namespace presence 14 { 15 void setPresence(const Fan& fan, bool newState) 16 { 17 if (newState) 18 { 19 pstate = 1; 20 } 21 else 22 { 23 pstate = 0; 24 } 25 } 26 27 class TestSensor : public PresenceSensor 28 { 29 public: 30 bool start() override 31 { 32 ++started; 33 return _present; 34 } 35 36 void stop() override 37 { 38 ++stopped; 39 } 40 41 bool present() override 42 { 43 return _present; 44 } 45 46 void fail() override 47 { 48 ++failed; 49 } 50 51 bool _present = false; 52 size_t started = 0; 53 size_t stopped = 0; 54 size_t failed = 0; 55 }; 56 57 } // namespace presence 58 } // namespace fan 59 } // namespace phosphor 60 61 using namespace phosphor::fan::presence; 62 63 TEST(FallbackTest, TestOne) 64 { 65 // Validate a single sensor. 66 // Validate on->off->on. 67 pstate = -1; 68 Fan fan{"/path", "name"}; 69 TestSensor ts; 70 ts._present = true; 71 std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts}; 72 Fallback f{fan, sensors}; 73 74 f.monitor(); 75 ASSERT_EQ(pstate, 1); 76 ASSERT_EQ(ts.failed, 0); 77 ASSERT_EQ(ts.stopped, 0); 78 ASSERT_EQ(ts.started, 1); 79 80 f.stateChanged(false); 81 ASSERT_EQ(pstate, 0); 82 ASSERT_EQ(ts.failed, 0); 83 ASSERT_EQ(ts.stopped, 0); 84 ASSERT_EQ(ts.started, 1); 85 86 f.stateChanged(true); 87 ASSERT_EQ(pstate, 1); 88 ASSERT_EQ(ts.failed, 0); 89 ASSERT_EQ(ts.stopped, 0); 90 ASSERT_EQ(ts.started, 1); 91 } 92 93 TEST(FallbackTest, TestTwoA) 94 { 95 // Validate two sensors. 96 // Validate both sensors on->off->on 97 98 pstate = -1; 99 Fan fan{"/path", "name"}; 100 TestSensor ts1, ts2; 101 ts1._present = true; 102 ts2._present = true; 103 104 std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts1, ts2}; 105 Fallback f{fan, sensors}; 106 107 f.monitor(); 108 ASSERT_EQ(pstate, 1); 109 ASSERT_EQ(ts1.failed, 0); 110 ASSERT_EQ(ts1.stopped, 0); 111 ASSERT_EQ(ts1.started, 1); 112 ASSERT_EQ(ts2.failed, 0); 113 ASSERT_EQ(ts2.stopped, 0); 114 ASSERT_EQ(ts2.started, 0); 115 116 ts1._present = false; 117 ts2._present = false; 118 f.stateChanged(false); 119 ASSERT_EQ(pstate, 0); 120 ASSERT_EQ(ts1.failed, 0); 121 ASSERT_EQ(ts1.stopped, 0); 122 ASSERT_EQ(ts1.started, 1); 123 ASSERT_EQ(ts2.failed, 0); 124 ASSERT_EQ(ts2.stopped, 0); 125 ASSERT_EQ(ts2.started, 0); 126 127 ts1._present = true; 128 ts2._present = true; 129 f.stateChanged(true); 130 ASSERT_EQ(pstate, 1); 131 ASSERT_EQ(ts1.failed, 0); 132 ASSERT_EQ(ts1.stopped, 0); 133 ASSERT_EQ(ts1.started, 1); 134 ASSERT_EQ(ts2.failed, 0); 135 ASSERT_EQ(ts2.stopped, 0); 136 ASSERT_EQ(ts2.started, 0); 137 } 138 139 140 TEST(FallbackTest, TestTwoB) 141 { 142 // Validate two sensors. 143 // Validate first sensor on->off. 144 145 pstate = -1; 146 Fan fan{"/path", "name"}; 147 TestSensor ts1, ts2; 148 ts1._present = true; 149 ts2._present = true; 150 151 std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts1, ts2}; 152 Fallback f{fan, sensors}; 153 154 f.monitor(); 155 ASSERT_EQ(pstate, 1); 156 ts1._present = false; 157 f.stateChanged(false); 158 ASSERT_EQ(pstate, 1); 159 ASSERT_EQ(ts1.failed, 1); 160 ASSERT_EQ(ts1.stopped, 1); 161 ASSERT_EQ(ts1.started, 1); 162 ASSERT_EQ(ts2.failed, 0); 163 ASSERT_EQ(ts2.stopped, 0); 164 ASSERT_EQ(ts2.started, 1); 165 166 // Flip the state of both sensors. 167 ts1._present = true; 168 ts2._present = false; 169 f.stateChanged(false); 170 ASSERT_EQ(pstate, 0); 171 ASSERT_EQ(ts1.failed, 1); 172 ASSERT_EQ(ts1.stopped, 1); 173 ASSERT_EQ(ts1.started, 1); 174 ASSERT_EQ(ts2.failed, 0); 175 ASSERT_EQ(ts2.stopped, 0); 176 ASSERT_EQ(ts2.started, 1); 177 } 178 179 TEST(FallbackTest, TestTwoC) 180 { 181 // Validate two sensors. 182 // Validate first in bad state. 183 184 pstate = -1; 185 Fan fan{"/path", "name"}; 186 TestSensor ts1, ts2; 187 ts1._present = false; 188 ts2._present = true; 189 190 std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts1, ts2}; 191 Fallback f{fan, sensors}; 192 193 f.monitor(); 194 ASSERT_EQ(pstate, 1); 195 ASSERT_EQ(ts1.failed, 1); 196 ASSERT_EQ(ts1.stopped, 0); 197 ASSERT_EQ(ts1.started, 0); 198 ASSERT_EQ(ts2.failed, 0); 199 ASSERT_EQ(ts2.stopped, 0); 200 ASSERT_EQ(ts2.started, 1); 201 202 f.stateChanged(false); 203 ASSERT_EQ(pstate, 0); 204 ASSERT_EQ(ts1.failed, 1); 205 ASSERT_EQ(ts1.stopped, 0); 206 ASSERT_EQ(ts1.started, 0); 207 ASSERT_EQ(ts2.failed, 0); 208 ASSERT_EQ(ts2.stopped, 0); 209 ASSERT_EQ(ts2.started, 1); 210 } 211 212 TEST(FallbackTest, TestTwoD) 213 { 214 // Validate two sensors. 215 // Validate both in bad state. 216 217 pstate = -1; 218 Fan fan{"/path", "name"}; 219 TestSensor ts1, ts2; 220 ts1._present = false; 221 ts2._present = false; 222 223 std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts1, ts2}; 224 Fallback f{fan, sensors}; 225 226 f.monitor(); 227 ASSERT_EQ(pstate, 0); 228 ASSERT_EQ(ts1.failed, 0); 229 ASSERT_EQ(ts1.stopped, 0); 230 ASSERT_EQ(ts1.started, 1); 231 ASSERT_EQ(ts2.failed, 0); 232 ASSERT_EQ(ts2.stopped, 0); 233 ASSERT_EQ(ts2.started, 0); 234 } 235