Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 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 | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 16 | #include "chassis.hpp" |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 17 | #include "device.hpp" |
| 18 | #include "id_map.hpp" |
| 19 | #include "rail.hpp" |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 20 | #include "rule.hpp" |
| 21 | #include "system.hpp" |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 22 | #include "test_utils.hpp" |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 23 | |
| 24 | #include <memory> |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 25 | #include <stdexcept> |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 26 | #include <utility> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include <gtest/gtest.h> |
| 30 | |
| 31 | using namespace phosphor::power::regulators; |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 32 | using namespace phosphor::power::regulators::test_utils; |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 33 | |
| 34 | TEST(SystemTests, Constructor) |
| 35 | { |
| 36 | // Create Rules |
| 37 | std::vector<std::unique_ptr<Rule>> rules{}; |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 38 | rules.emplace_back(createRule("set_voltage_rule")); |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 39 | |
| 40 | // Create Chassis |
| 41 | std::vector<std::unique_ptr<Chassis>> chassis{}; |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 42 | std::vector<std::unique_ptr<Device>> devices{}; |
| 43 | devices.emplace_back(createDevice("reg1", {"rail1"})); |
| 44 | chassis.emplace_back(std::make_unique<Chassis>(1, std::move(devices))); |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 45 | |
| 46 | // Create System |
| 47 | System system{std::move(rules), std::move(chassis)}; |
| 48 | EXPECT_EQ(system.getChassis().size(), 1); |
| 49 | EXPECT_EQ(system.getChassis()[0]->getNumber(), 1); |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 50 | EXPECT_NO_THROW(system.getIDMap().getRule("set_voltage_rule")); |
| 51 | EXPECT_NO_THROW(system.getIDMap().getDevice("reg1")); |
| 52 | EXPECT_NO_THROW(system.getIDMap().getRail("rail1")); |
| 53 | EXPECT_THROW(system.getIDMap().getRail("rail2"), std::invalid_argument); |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 54 | EXPECT_EQ(system.getRules().size(), 1); |
| 55 | EXPECT_EQ(system.getRules()[0]->getID(), "set_voltage_rule"); |
| 56 | } |
| 57 | |
| 58 | TEST(SystemTests, GetChassis) |
| 59 | { |
| 60 | // Specify an empty rules vector |
| 61 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 62 | |
| 63 | // Create Chassis |
| 64 | std::vector<std::unique_ptr<Chassis>> chassis{}; |
| 65 | chassis.emplace_back(std::make_unique<Chassis>(1)); |
| 66 | chassis.emplace_back(std::make_unique<Chassis>(3)); |
| 67 | |
| 68 | // Create System |
| 69 | System system{std::move(rules), std::move(chassis)}; |
| 70 | EXPECT_EQ(system.getChassis().size(), 2); |
| 71 | EXPECT_EQ(system.getChassis()[0]->getNumber(), 1); |
| 72 | EXPECT_EQ(system.getChassis()[1]->getNumber(), 3); |
| 73 | } |
| 74 | |
| 75 | TEST(SystemTests, GetIDMap) |
| 76 | { |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 77 | // Create Rules |
| 78 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 79 | rules.emplace_back(createRule("set_voltage_rule")); |
| 80 | rules.emplace_back(createRule("read_sensors_rule")); |
| 81 | |
| 82 | // Create Chassis |
| 83 | std::vector<std::unique_ptr<Chassis>> chassis{}; |
| 84 | { |
| 85 | // Chassis 1 |
| 86 | std::vector<std::unique_ptr<Device>> devices{}; |
| 87 | devices.emplace_back(createDevice("reg1", {"rail1"})); |
| 88 | devices.emplace_back(createDevice("reg2", {"rail2a", "rail2b"})); |
| 89 | chassis.emplace_back(std::make_unique<Chassis>(1, std::move(devices))); |
| 90 | } |
| 91 | { |
| 92 | // Chassis 2 |
| 93 | std::vector<std::unique_ptr<Device>> devices{}; |
| 94 | devices.emplace_back(createDevice("reg3", {"rail3a", "rail3b"})); |
| 95 | devices.emplace_back(createDevice("reg4")); |
| 96 | chassis.emplace_back(std::make_unique<Chassis>(2, std::move(devices))); |
| 97 | } |
| 98 | |
| 99 | // Create System |
| 100 | System system{std::move(rules), std::move(chassis)}; |
| 101 | const IDMap& idMap = system.getIDMap(); |
| 102 | |
| 103 | // Verify all Rules are in the IDMap |
| 104 | EXPECT_NO_THROW(idMap.getRule("set_voltage_rule")); |
| 105 | EXPECT_NO_THROW(idMap.getRule("read_sensors_rule")); |
| 106 | EXPECT_THROW(idMap.getRule("set_voltage_rule2"), std::invalid_argument); |
| 107 | |
| 108 | // Verify all Devices are in the IDMap |
| 109 | EXPECT_NO_THROW(idMap.getDevice("reg1")); |
| 110 | EXPECT_NO_THROW(idMap.getDevice("reg2")); |
| 111 | EXPECT_NO_THROW(idMap.getDevice("reg3")); |
| 112 | EXPECT_NO_THROW(idMap.getDevice("reg4")); |
| 113 | EXPECT_THROW(idMap.getDevice("reg5"), std::invalid_argument); |
| 114 | |
| 115 | // Verify all Rails are in the IDMap |
| 116 | EXPECT_NO_THROW(idMap.getRail("rail1")); |
| 117 | EXPECT_NO_THROW(idMap.getRail("rail2a")); |
| 118 | EXPECT_NO_THROW(idMap.getRail("rail2b")); |
| 119 | EXPECT_NO_THROW(idMap.getRail("rail3a")); |
| 120 | EXPECT_NO_THROW(idMap.getRail("rail3b")); |
| 121 | EXPECT_THROW(idMap.getRail("rail4"), std::invalid_argument); |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | TEST(SystemTests, GetRules) |
| 125 | { |
| 126 | // Create Rules |
| 127 | std::vector<std::unique_ptr<Rule>> rules{}; |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 128 | rules.emplace_back(createRule("set_voltage_rule")); |
| 129 | rules.emplace_back(createRule("read_sensors_rule")); |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 130 | |
| 131 | // Create Chassis |
| 132 | std::vector<std::unique_ptr<Chassis>> chassis{}; |
| 133 | chassis.emplace_back(std::make_unique<Chassis>(1)); |
| 134 | |
| 135 | // Create System |
| 136 | System system{std::move(rules), std::move(chassis)}; |
| 137 | EXPECT_EQ(system.getRules().size(), 2); |
| 138 | EXPECT_EQ(system.getRules()[0]->getID(), "set_voltage_rule"); |
| 139 | EXPECT_EQ(system.getRules()[1]->getID(), "read_sensors_rule"); |
| 140 | } |