1 /**
2 * Copyright © 2025 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
17 #include "chassis.hpp"
18 #include "mock_services.hpp"
19 #include "power_sequencer_device.hpp"
20 #include "rail.hpp"
21 #include "services.hpp"
22 #include "ucd90160_device.hpp"
23
24 #include <stddef.h> // for size_t
25
26 #include <cstdint>
27 #include <memory>
28 #include <string>
29 #include <utility>
30 #include <vector>
31
32 #include <gtest/gtest.h>
33
34 using namespace phosphor::power::sequencer;
35
36 /**
37 * Creates a PowerSequencerDevice instance.
38 *
39 * PowerSequencerDevice is an abstract base class. The actual object type
40 * created is a UCD90160Device.
41 *
42 * @param bus I2C bus for the device
43 * @param address I2C address for the device
44 * @param services System services like hardware presence and the journal
45 * @return PowerSequencerDevice instance
46 */
createPowerSequencer(uint8_t bus,uint16_t address,Services & services)47 std::unique_ptr<PowerSequencerDevice> createPowerSequencer(
48 uint8_t bus, uint16_t address, Services& services)
49 {
50 std::string powerControlGPIOName{"power-chassis-control"};
51 std::string powerGoodGPIOName{"power-chassis-good"};
52 std::vector<std::unique_ptr<Rail>> rails;
53 return std::make_unique<UCD90160Device>(
54 bus, address, powerControlGPIOName, powerGoodGPIOName, std::move(rails),
55 services);
56 }
57
TEST(ChassisTests,Constructor)58 TEST(ChassisTests, Constructor)
59 {
60 MockServices services;
61
62 size_t number{1};
63 std::string inventoryPath{"/xyz/openbmc_project/inventory/system/chassis"};
64 std::vector<std::unique_ptr<PowerSequencerDevice>> powerSequencers;
65 powerSequencers.emplace_back(createPowerSequencer(3, 0x70, services));
66 Chassis chassis{number, inventoryPath, std::move(powerSequencers)};
67
68 EXPECT_EQ(chassis.getNumber(), number);
69 EXPECT_EQ(chassis.getInventoryPath(), inventoryPath);
70 EXPECT_EQ(chassis.getPowerSequencers().size(), 1);
71 EXPECT_EQ(chassis.getPowerSequencers()[0]->getBus(), 3);
72 EXPECT_EQ(chassis.getPowerSequencers()[0]->getAddress(), 0x70);
73 }
74
TEST(ChassisTests,GetNumber)75 TEST(ChassisTests, GetNumber)
76 {
77 MockServices services;
78
79 size_t number{2};
80 std::string inventoryPath{"/xyz/openbmc_project/inventory/system/chassis2"};
81 std::vector<std::unique_ptr<PowerSequencerDevice>> powerSequencers;
82 Chassis chassis{number, inventoryPath, std::move(powerSequencers)};
83
84 EXPECT_EQ(chassis.getNumber(), number);
85 }
86
TEST(ChassisTests,GetInventoryPath)87 TEST(ChassisTests, GetInventoryPath)
88 {
89 MockServices services;
90
91 size_t number{3};
92 std::string inventoryPath{
93 "/xyz/openbmc_project/inventory/system/chassis_3"};
94 std::vector<std::unique_ptr<PowerSequencerDevice>> powerSequencers;
95 Chassis chassis{number, inventoryPath, std::move(powerSequencers)};
96
97 EXPECT_EQ(chassis.getInventoryPath(), inventoryPath);
98 }
99
TEST(ChassisTests,GetPowerSequencers)100 TEST(ChassisTests, GetPowerSequencers)
101 {
102 MockServices services;
103
104 size_t number{2};
105 std::string inventoryPath{"/xyz/openbmc_project/inventory/system/chassis2"};
106 std::vector<std::unique_ptr<PowerSequencerDevice>> powerSequencers;
107 powerSequencers.emplace_back(createPowerSequencer(3, 0x70, services));
108 powerSequencers.emplace_back(createPowerSequencer(4, 0x32, services));
109 powerSequencers.emplace_back(createPowerSequencer(10, 0x16, services));
110 Chassis chassis{number, inventoryPath, std::move(powerSequencers)};
111
112 EXPECT_EQ(chassis.getPowerSequencers().size(), 3);
113 EXPECT_EQ(chassis.getPowerSequencers()[0]->getBus(), 3);
114 EXPECT_EQ(chassis.getPowerSequencers()[0]->getAddress(), 0x70);
115 EXPECT_EQ(chassis.getPowerSequencers()[1]->getBus(), 4);
116 EXPECT_EQ(chassis.getPowerSequencers()[1]->getAddress(), 0x32);
117 EXPECT_EQ(chassis.getPowerSequencers()[2]->getBus(), 10);
118 EXPECT_EQ(chassis.getPowerSequencers()[2]->getAddress(), 0x16);
119 }
120