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