blob: 85a1da635469bccfd55209d246839e60bee93301 [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 McCarney2af52892020-04-14 11:54:45 -050061TEST(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
Shawn McCarneyc3991f12020-04-05 13:16:06 -050084TEST(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
101TEST(SystemTests, GetIDMap)
102{
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500103 // 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);
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500148}
149
150TEST(SystemTests, GetRules)
151{
152 // Create Rules
153 std::vector<std::unique_ptr<Rule>> rules{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500154 rules.emplace_back(createRule("set_voltage_rule"));
155 rules.emplace_back(createRule("read_sensors_rule"));
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500156
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}