Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 16 | #include "action.hpp" |
| 17 | #include "configuration.hpp" |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 18 | #include "device.hpp" |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 19 | #include "i2c_interface.hpp" |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 20 | #include "id_map.hpp" |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 21 | #include "mock_action.hpp" |
| 22 | #include "presence_detection.hpp" |
| 23 | #include "rail.hpp" |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 24 | #include "test_utils.hpp" |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 25 | |
| 26 | #include <memory> |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 27 | #include <optional> |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 28 | #include <utility> |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 29 | #include <vector> |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 30 | |
| 31 | #include <gtest/gtest.h> |
| 32 | |
| 33 | using namespace phosphor::power::regulators; |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 34 | using namespace phosphor::power::regulators::test_utils; |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 35 | |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 36 | TEST(DeviceTests, Constructor) |
| 37 | { |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 38 | // Test where only required parameters are specified |
| 39 | { |
| 40 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 41 | i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); |
| 42 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 43 | std::move(i2cInterface)}; |
| 44 | EXPECT_EQ(device.getID(), "vdd_reg"); |
| 45 | EXPECT_EQ(device.isRegulator(), true); |
| 46 | EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg2"); |
| 47 | EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); |
| 48 | EXPECT_EQ(device.getPresenceDetection(), nullptr); |
| 49 | EXPECT_EQ(device.getConfiguration(), nullptr); |
| 50 | EXPECT_EQ(device.getRails().size(), 0); |
| 51 | } |
| 52 | |
| 53 | // Test where all parameters are specified |
| 54 | { |
| 55 | // Create I2CInterface |
| 56 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 57 | i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); |
| 58 | |
| 59 | // Create PresenceDetection |
| 60 | std::vector<std::unique_ptr<Action>> actions{}; |
| 61 | actions.push_back(std::make_unique<MockAction>()); |
| 62 | std::unique_ptr<PresenceDetection> presenceDetection = |
| 63 | std::make_unique<PresenceDetection>(std::move(actions)); |
| 64 | |
| 65 | // Create Configuration |
| 66 | std::optional<double> volts{}; |
| 67 | actions.clear(); |
| 68 | actions.push_back(std::make_unique<MockAction>()); |
| 69 | actions.push_back(std::make_unique<MockAction>()); |
| 70 | std::unique_ptr<Configuration> configuration = |
| 71 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 72 | |
| 73 | // Create vector of Rail objects |
| 74 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 75 | rails.push_back(std::make_unique<Rail>("vdd0")); |
| 76 | rails.push_back(std::make_unique<Rail>("vdd1")); |
| 77 | |
| 78 | // Create Device |
| 79 | Device device{"vdd_reg", |
| 80 | false, |
| 81 | "/system/chassis/motherboard/reg1", |
| 82 | std::move(i2cInterface), |
| 83 | std::move(presenceDetection), |
| 84 | std::move(configuration), |
| 85 | std::move(rails)}; |
| 86 | EXPECT_EQ(device.getID(), "vdd_reg"); |
| 87 | EXPECT_EQ(device.isRegulator(), false); |
| 88 | EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg1"); |
| 89 | EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); |
| 90 | EXPECT_NE(device.getPresenceDetection(), nullptr); |
| 91 | EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1); |
| 92 | EXPECT_NE(device.getConfiguration(), nullptr); |
| 93 | EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), false); |
| 94 | EXPECT_EQ(device.getConfiguration()->getActions().size(), 2); |
| 95 | EXPECT_EQ(device.getRails().size(), 2); |
| 96 | } |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 99 | TEST(DeviceTests, AddToIDMap) |
| 100 | { |
| 101 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 102 | std::unique_ptr<Configuration> configuration{}; |
| 103 | |
| 104 | // Create vector of Rail objects |
| 105 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 106 | rails.push_back(std::make_unique<Rail>("vdd0")); |
| 107 | rails.push_back(std::make_unique<Rail>("vdd1")); |
| 108 | |
| 109 | // Create Device |
| 110 | Device device{"vdd_reg", |
| 111 | false, |
| 112 | "/system/chassis/motherboard/reg2", |
| 113 | std::move(createI2CInterface()), |
| 114 | std::move(presenceDetection), |
| 115 | std::move(configuration), |
| 116 | std::move(rails)}; |
| 117 | |
| 118 | // Add Device and Rail objects to an IDMap |
| 119 | IDMap idMap{}; |
| 120 | device.addToIDMap(idMap); |
| 121 | |
| 122 | // Verify Device is in the IDMap |
| 123 | EXPECT_NO_THROW(idMap.getDevice("vdd_reg")); |
| 124 | EXPECT_THROW(idMap.getDevice("vio_reg"), std::invalid_argument); |
| 125 | |
| 126 | // Verify all Rails are in the IDMap |
| 127 | EXPECT_NO_THROW(idMap.getRail("vdd0")); |
| 128 | EXPECT_NO_THROW(idMap.getRail("vdd1")); |
| 129 | EXPECT_THROW(idMap.getRail("vdd2"), std::invalid_argument); |
| 130 | } |
| 131 | |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 132 | TEST(DeviceTests, GetConfiguration) |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 133 | { |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 134 | // Test where Configuration was not specified in constructor |
| 135 | { |
| 136 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 137 | std::move(createI2CInterface())}; |
| 138 | EXPECT_EQ(device.getConfiguration(), nullptr); |
| 139 | } |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 140 | |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 141 | // Test where Configuration was specified in constructor |
| 142 | { |
| 143 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 144 | |
| 145 | // Create Configuration |
| 146 | std::optional<double> volts{3.2}; |
| 147 | std::vector<std::unique_ptr<Action>> actions{}; |
| 148 | actions.push_back(std::make_unique<MockAction>()); |
| 149 | actions.push_back(std::make_unique<MockAction>()); |
| 150 | std::unique_ptr<Configuration> configuration = |
| 151 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 152 | |
| 153 | // Create Device |
| 154 | Device device{"vdd_reg", |
| 155 | true, |
| 156 | "/system/chassis/motherboard/reg2", |
| 157 | std::move(createI2CInterface()), |
| 158 | std::move(presenceDetection), |
| 159 | std::move(configuration)}; |
| 160 | EXPECT_NE(device.getConfiguration(), nullptr); |
| 161 | EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), true); |
| 162 | EXPECT_EQ(device.getConfiguration()->getVolts().value(), 3.2); |
| 163 | EXPECT_EQ(device.getConfiguration()->getActions().size(), 2); |
| 164 | } |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | TEST(DeviceTests, GetFRU) |
| 168 | { |
| 169 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 170 | std::move(createI2CInterface())}; |
| 171 | EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg2"); |
| 172 | } |
| 173 | |
| 174 | TEST(DeviceTests, GetI2CInterface) |
| 175 | { |
| 176 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 177 | i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); |
| 178 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 179 | std::move(i2cInterface)}; |
| 180 | EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 181 | } |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 182 | |
| 183 | TEST(DeviceTests, GetID) |
| 184 | { |
| 185 | Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2", |
| 186 | std::move(createI2CInterface())}; |
| 187 | EXPECT_EQ(device.getID(), "vdd_reg"); |
| 188 | } |
| 189 | |
| 190 | TEST(DeviceTests, GetPresenceDetection) |
| 191 | { |
| 192 | // Test where PresenceDetection was not specified in constructor |
| 193 | { |
| 194 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 195 | std::move(createI2CInterface())}; |
| 196 | EXPECT_EQ(device.getPresenceDetection(), nullptr); |
| 197 | } |
| 198 | |
| 199 | // Test where PresenceDetection was specified in constructor |
| 200 | { |
| 201 | // Create PresenceDetection |
| 202 | std::vector<std::unique_ptr<Action>> actions{}; |
| 203 | actions.push_back(std::make_unique<MockAction>()); |
| 204 | std::unique_ptr<PresenceDetection> presenceDetection = |
| 205 | std::make_unique<PresenceDetection>(std::move(actions)); |
| 206 | |
| 207 | // Create Device |
| 208 | Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2", |
| 209 | std::move(createI2CInterface()), |
| 210 | std::move(presenceDetection)}; |
| 211 | EXPECT_NE(device.getPresenceDetection(), nullptr); |
| 212 | EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | TEST(DeviceTests, GetRails) |
| 217 | { |
| 218 | // Test where no rails were specified in constructor |
| 219 | { |
| 220 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 221 | std::move(createI2CInterface())}; |
| 222 | EXPECT_EQ(device.getRails().size(), 0); |
| 223 | } |
| 224 | |
| 225 | // Test where rails were specified in constructor |
| 226 | { |
| 227 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 228 | std::unique_ptr<Configuration> configuration{}; |
| 229 | |
| 230 | // Create vector of Rail objects |
| 231 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 232 | rails.push_back(std::make_unique<Rail>("vdd0")); |
| 233 | rails.push_back(std::make_unique<Rail>("vdd1")); |
| 234 | |
| 235 | // Create Device |
| 236 | Device device{"vdd_reg", |
| 237 | false, |
| 238 | "/system/chassis/motherboard/reg2", |
| 239 | std::move(createI2CInterface()), |
| 240 | std::move(presenceDetection), |
| 241 | std::move(configuration), |
| 242 | std::move(rails)}; |
| 243 | EXPECT_EQ(device.getRails().size(), 2); |
| 244 | EXPECT_EQ(device.getRails()[0]->getID(), "vdd0"); |
| 245 | EXPECT_EQ(device.getRails()[1]->getID(), "vdd1"); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | TEST(DeviceTests, IsRegulator) |
| 250 | { |
| 251 | Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2", |
| 252 | std::move(createI2CInterface())}; |
| 253 | EXPECT_EQ(device.isRegulator(), false); |
| 254 | } |