blob: e04dd6d32a8ab403c1ebaa5b0a1acc5f9b0161f0 [file] [log] [blame]
Shawn McCarney8a3afd72020-03-12 14:28:44 -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 McCarneydb0b8332020-04-06 14:13:04 -050016#include "action.hpp"
17#include "chassis.hpp"
18#include "configuration.hpp"
19#include "device.hpp"
Shawn McCarney8a3afd72020-03-12 14:28:44 -050020#include "i2c_interface.hpp"
Shawn McCarneydb0b8332020-04-06 14:13:04 -050021#include "mock_action.hpp"
Shawn McCarney32252592021-09-08 15:29:36 -050022#include "phase_fault_detection.hpp"
Shawn McCarneydb0b8332020-04-06 14:13:04 -050023#include "presence_detection.hpp"
24#include "rail.hpp"
25#include "rule.hpp"
Shawn McCarney8a3afd72020-03-12 14:28:44 -050026
Shawn McCarneyf3633f62020-09-02 10:34:52 -050027#include <filesystem>
28#include <fstream>
Shawn McCarney8a3afd72020-03-12 14:28:44 -050029#include <memory>
Shawn McCarneydb0b8332020-04-06 14:13:04 -050030#include <string>
31#include <utility>
32#include <vector>
Shawn McCarney8a3afd72020-03-12 14:28:44 -050033
34namespace phosphor::power::regulators::test_utils
35{
36
Shawn McCarneyf3633f62020-09-02 10:34:52 -050037namespace fs = std::filesystem;
38
Shawn McCarney8a3afd72020-03-12 14:28:44 -050039/**
40 * Create an I2CInterface object with hard-coded bus and address values.
41 *
42 * @return I2CInterface object wrapped in a unique_ptr
43 */
44inline std::unique_ptr<i2c::I2CInterface> createI2CInterface()
45{
46 return i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
47}
48
Shawn McCarneydb0b8332020-04-06 14:13:04 -050049/**
50 * Creates a Device object with the specified ID.
51 *
52 * Creates Rail objects within the Device if railIDs is specified.
53 *
54 * @param id device ID
55 * @param railIDs rail IDs (optional)
56 * @return Device object
57 */
58inline std::unique_ptr<Device>
59 createDevice(const std::string& id,
60 const std::vector<std::string>& railIDs = {})
61{
62 // Create Rails (if any)
63 std::vector<std::unique_ptr<Rail>> rails{};
64 for (const std::string& railID : railIDs)
65 {
66 rails.emplace_back(std::make_unique<Rail>(railID));
67 }
68
69 // Create Device
70 bool isRegulator = true;
Bob Kinga76898f2020-10-13 15:08:33 +080071 std::string fru =
72 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1";
Shawn McCarneydb0b8332020-04-06 14:13:04 -050073 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
74 std::unique_ptr<PresenceDetection> presenceDetection{};
75 std::unique_ptr<Configuration> configuration{};
Shawn McCarney32252592021-09-08 15:29:36 -050076 std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{};
77 return std::make_unique<Device>(
78 id, isRegulator, fru, std::move(i2cInterface),
79 std::move(presenceDetection), std::move(configuration),
80 std::move(phaseFaultDetection), std::move(rails));
Shawn McCarneydb0b8332020-04-06 14:13:04 -050081}
82
83/**
84 * Creates a Rule object with the specified ID.
85 *
86 * @param id rule ID
87 * @return Rule object
88 */
89inline std::unique_ptr<Rule> createRule(const std::string& id)
90{
91 // Create actions
92 std::vector<std::unique_ptr<Action>> actions{};
93 actions.emplace_back(std::make_unique<MockAction>());
94
95 // Create Rule
96 return std::make_unique<Rule>(id, std::move(actions));
97}
98
Shawn McCarneyf3633f62020-09-02 10:34:52 -050099/**
100 * Modify the specified file so that fs::remove() fails with an exception.
101 *
102 * The file will be renamed and can be restored by calling makeFileRemovable().
103 *
104 * @param path path to the file
105 */
106inline void makeFileUnRemovable(const fs::path& path)
107{
108 // Rename the file to save its contents
109 fs::path savePath{path.native() + ".save"};
110 fs::rename(path, savePath);
111
112 // Create a directory at the original file path
113 fs::create_directory(path);
114
115 // Create a file within the directory. fs::remove() will throw an exception
116 // if the path is a non-empty directory.
117 std::ofstream childFile{path / "childFile"};
118}
119
120/**
121 * Modify the specified file so that fs::remove() can successfully delete it.
122 *
123 * Undo the modifications from an earlier call to makeFileUnRemovable().
124 *
125 * @param path path to the file
126 */
127inline void makeFileRemovable(const fs::path& path)
128{
129 // makeFileUnRemovable() creates a directory at the file path. Remove the
130 // directory and all of its contents.
131 fs::remove_all(path);
132
133 // Rename the file back to the original path to restore its contents
134 fs::path savePath{path.native() + ".save"};
135 fs::rename(savePath, path);
136}
137
Shawn McCarney8a3afd72020-03-12 14:28:44 -0500138} // namespace phosphor::power::regulators::test_utils