blob: dfc22a0bb722af9b26f6651277250da52b83a3d7 [file] [log] [blame]
Shawn McCarneyc3991f12020-04-05 13:16:06 -05001/**
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 McCarneyc3991f12020-04-05 13:16:06 -050016#include "chassis.hpp"
Shawn McCarneydb0b8332020-04-06 14:13:04 -050017#include "device.hpp"
18#include "id_map.hpp"
Shawn McCarney2af52892020-04-14 11:54:45 -050019#include "journal.hpp"
20#include "mock_journal.hpp"
Shawn McCarneydb0b8332020-04-06 14:13:04 -050021#include "rail.hpp"
Shawn McCarneyc3991f12020-04-05 13:16:06 -050022#include "rule.hpp"
23#include "system.hpp"
Shawn McCarneydb0b8332020-04-06 14:13:04 -050024#include "test_utils.hpp"
Shawn McCarneyc3991f12020-04-05 13:16:06 -050025
26#include <memory>
Shawn McCarneydb0b8332020-04-06 14:13:04 -050027#include <stdexcept>
Shawn McCarney2af52892020-04-14 11:54:45 -050028#include <string>
Shawn McCarneyc3991f12020-04-05 13:16:06 -050029#include <utility>
30#include <vector>
31
32#include <gtest/gtest.h>
33
34using namespace phosphor::power::regulators;
Shawn McCarneydb0b8332020-04-06 14:13:04 -050035using namespace phosphor::power::regulators::test_utils;
Shawn McCarneyc3991f12020-04-05 13:16:06 -050036
37TEST(SystemTests, Constructor)
38{
39 // Create Rules
40 std::vector<std::unique_ptr<Rule>> rules{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -050041 rules.emplace_back(createRule("set_voltage_rule"));
Shawn McCarneyc3991f12020-04-05 13:16:06 -050042
43 // Create Chassis
44 std::vector<std::unique_ptr<Chassis>> chassis{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -050045 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)));
Shawn McCarneyc3991f12020-04-05 13:16:06 -050048
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);
Shawn McCarneydb0b8332020-04-06 14:13:04 -050053 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);
Shawn McCarneyc3991f12020-04-05 13:16:06 -050057 EXPECT_EQ(system.getRules().size(), 1);
58 EXPECT_EQ(system.getRules()[0]->getID(), "set_voltage_rule");
59}
60
Shawn McCarney5b19ea52020-06-02 18:52:56 -050061TEST(SystemTests, CloseDevices)
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 closeDevices()
75 journal::clear();
76 system.closeDevices();
77 EXPECT_EQ(journal::getErrMessages().size(), 0);
78 EXPECT_EQ(journal::getInfoMessages().size(), 0);
79 std::vector<std::string> expectedDebugMessages{
80 "Closing devices in chassis 1", "Closing devices in chassis 3"};
81 EXPECT_EQ(journal::getDebugMessages(), expectedDebugMessages);
82}
83
Shawn McCarney2af52892020-04-14 11:54:45 -050084TEST(SystemTests, Configure)
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
97 // Call configure()
98 journal::clear();
99 system.configure();
100 EXPECT_EQ(journal::getDebugMessages().size(), 0);
101 EXPECT_EQ(journal::getErrMessages().size(), 0);
102 std::vector<std::string> expectedInfoMessages{"Configuring chassis 1",
103 "Configuring chassis 3"};
104 EXPECT_EQ(journal::getInfoMessages(), expectedInfoMessages);
105}
106
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500107TEST(SystemTests, GetChassis)
108{
109 // Specify an empty rules vector
110 std::vector<std::unique_ptr<Rule>> rules{};
111
112 // Create Chassis
113 std::vector<std::unique_ptr<Chassis>> chassis{};
114 chassis.emplace_back(std::make_unique<Chassis>(1));
115 chassis.emplace_back(std::make_unique<Chassis>(3));
116
117 // Create System
118 System system{std::move(rules), std::move(chassis)};
119 EXPECT_EQ(system.getChassis().size(), 2);
120 EXPECT_EQ(system.getChassis()[0]->getNumber(), 1);
121 EXPECT_EQ(system.getChassis()[1]->getNumber(), 3);
122}
123
124TEST(SystemTests, GetIDMap)
125{
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500126 // Create Rules
127 std::vector<std::unique_ptr<Rule>> rules{};
128 rules.emplace_back(createRule("set_voltage_rule"));
129 rules.emplace_back(createRule("read_sensors_rule"));
130
131 // Create Chassis
132 std::vector<std::unique_ptr<Chassis>> chassis{};
133 {
134 // Chassis 1
135 std::vector<std::unique_ptr<Device>> devices{};
136 devices.emplace_back(createDevice("reg1", {"rail1"}));
137 devices.emplace_back(createDevice("reg2", {"rail2a", "rail2b"}));
138 chassis.emplace_back(std::make_unique<Chassis>(1, std::move(devices)));
139 }
140 {
141 // Chassis 2
142 std::vector<std::unique_ptr<Device>> devices{};
143 devices.emplace_back(createDevice("reg3", {"rail3a", "rail3b"}));
144 devices.emplace_back(createDevice("reg4"));
145 chassis.emplace_back(std::make_unique<Chassis>(2, std::move(devices)));
146 }
147
148 // Create System
149 System system{std::move(rules), std::move(chassis)};
150 const IDMap& idMap = system.getIDMap();
151
152 // Verify all Rules are in the IDMap
153 EXPECT_NO_THROW(idMap.getRule("set_voltage_rule"));
154 EXPECT_NO_THROW(idMap.getRule("read_sensors_rule"));
155 EXPECT_THROW(idMap.getRule("set_voltage_rule2"), std::invalid_argument);
156
157 // Verify all Devices are in the IDMap
158 EXPECT_NO_THROW(idMap.getDevice("reg1"));
159 EXPECT_NO_THROW(idMap.getDevice("reg2"));
160 EXPECT_NO_THROW(idMap.getDevice("reg3"));
161 EXPECT_NO_THROW(idMap.getDevice("reg4"));
162 EXPECT_THROW(idMap.getDevice("reg5"), std::invalid_argument);
163
164 // Verify all Rails are in the IDMap
165 EXPECT_NO_THROW(idMap.getRail("rail1"));
166 EXPECT_NO_THROW(idMap.getRail("rail2a"));
167 EXPECT_NO_THROW(idMap.getRail("rail2b"));
168 EXPECT_NO_THROW(idMap.getRail("rail3a"));
169 EXPECT_NO_THROW(idMap.getRail("rail3b"));
170 EXPECT_THROW(idMap.getRail("rail4"), std::invalid_argument);
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500171}
172
173TEST(SystemTests, GetRules)
174{
175 // Create Rules
176 std::vector<std::unique_ptr<Rule>> rules{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500177 rules.emplace_back(createRule("set_voltage_rule"));
178 rules.emplace_back(createRule("read_sensors_rule"));
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500179
180 // Create Chassis
181 std::vector<std::unique_ptr<Chassis>> chassis{};
182 chassis.emplace_back(std::make_unique<Chassis>(1));
183
184 // Create System
185 System system{std::move(rules), std::move(chassis)};
186 EXPECT_EQ(system.getRules().size(), 2);
187 EXPECT_EQ(system.getRules()[0]->getID(), "set_voltage_rule");
188 EXPECT_EQ(system.getRules()[1]->getID(), "read_sensors_rule");
189}