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
17 #include "action.hpp"
18 #include "chassis.hpp"
19 #include "configuration.hpp"
20 #include "device.hpp"
21 #include "i2c_interface.hpp"
22 #include "mock_action.hpp"
23 #include "phase_fault_detection.hpp"
24 #include "presence_detection.hpp"
25 #include "rail.hpp"
26 #include "rule.hpp"
27
28 #include <filesystem>
29 #include <fstream>
30 #include <memory>
31 #include <string>
32 #include <utility>
33 #include <vector>
34
35 namespace phosphor::power::regulators::test_utils
36 {
37
38 namespace fs = std::filesystem;
39
40 /**
41 * Create an I2CInterface object with hard-coded bus and address values.
42 *
43 * @return I2CInterface object wrapped in a unique_ptr
44 */
createI2CInterface()45 inline std::unique_ptr<i2c::I2CInterface> createI2CInterface()
46 {
47 return i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
48 }
49
50 /**
51 * Creates a Device object with the specified ID.
52 *
53 * Creates Rail objects within the Device if railIDs is specified.
54 *
55 * @param id device ID
56 * @param railIDs rail IDs (optional)
57 * @return Device object
58 */
createDevice(const std::string & id,const std::vector<std::string> & railIDs={})59 inline std::unique_ptr<Device> createDevice(
60 const std::string& id, 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;
71 std::string fru =
72 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1";
73 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
74 std::unique_ptr<PresenceDetection> presenceDetection{};
75 std::unique_ptr<Configuration> configuration{};
76 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));
81 }
82
83 /**
84 * Creates a Rule object with the specified ID.
85 *
86 * @param id rule ID
87 * @return Rule object
88 */
createRule(const std::string & id)89 inline 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
99 /**
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 */
makeFileUnRemovable(const fs::path & path)106 inline 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 */
makeFileRemovable(const fs::path & path)127 inline 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
138 } // namespace phosphor::power::regulators::test_utils
139