blob: f56dc1898d52a16e7ee49221b4ad006cade9d5ac [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"
22#include "presence_detection.hpp"
23#include "rail.hpp"
24#include "rule.hpp"
Shawn McCarney8a3afd72020-03-12 14:28:44 -050025
Shawn McCarneyf3633f62020-09-02 10:34:52 -050026#include <filesystem>
27#include <fstream>
Shawn McCarney8a3afd72020-03-12 14:28:44 -050028#include <memory>
Shawn McCarneydb0b8332020-04-06 14:13:04 -050029#include <string>
30#include <utility>
31#include <vector>
Shawn McCarney8a3afd72020-03-12 14:28:44 -050032
33namespace phosphor::power::regulators::test_utils
34{
35
Shawn McCarneyf3633f62020-09-02 10:34:52 -050036namespace fs = std::filesystem;
37
Shawn McCarney8a3afd72020-03-12 14:28:44 -050038/**
39 * Create an I2CInterface object with hard-coded bus and address values.
40 *
41 * @return I2CInterface object wrapped in a unique_ptr
42 */
43inline std::unique_ptr<i2c::I2CInterface> createI2CInterface()
44{
45 return i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
46}
47
Shawn McCarneydb0b8332020-04-06 14:13:04 -050048/**
49 * Creates a Device object with the specified ID.
50 *
51 * Creates Rail objects within the Device if railIDs is specified.
52 *
53 * @param id device ID
54 * @param railIDs rail IDs (optional)
55 * @return Device object
56 */
57inline std::unique_ptr<Device>
58 createDevice(const std::string& id,
59 const std::vector<std::string>& railIDs = {})
60{
61 // Create Rails (if any)
62 std::vector<std::unique_ptr<Rail>> rails{};
63 for (const std::string& railID : railIDs)
64 {
65 rails.emplace_back(std::make_unique<Rail>(railID));
66 }
67
68 // Create Device
69 bool isRegulator = true;
70 std::string fru = "/system/chassis/motherboard/reg1";
71 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
72 std::unique_ptr<PresenceDetection> presenceDetection{};
73 std::unique_ptr<Configuration> configuration{};
74 return std::make_unique<Device>(id, isRegulator, fru,
75 std::move(i2cInterface),
76 std::move(presenceDetection),
77 std::move(configuration), std::move(rails));
78}
79
80/**
81 * Creates a Rule object with the specified ID.
82 *
83 * @param id rule ID
84 * @return Rule object
85 */
86inline std::unique_ptr<Rule> createRule(const std::string& id)
87{
88 // Create actions
89 std::vector<std::unique_ptr<Action>> actions{};
90 actions.emplace_back(std::make_unique<MockAction>());
91
92 // Create Rule
93 return std::make_unique<Rule>(id, std::move(actions));
94}
95
Shawn McCarneyf3633f62020-09-02 10:34:52 -050096/**
97 * Modify the specified file so that fs::remove() fails with an exception.
98 *
99 * The file will be renamed and can be restored by calling makeFileRemovable().
100 *
101 * @param path path to the file
102 */
103inline void makeFileUnRemovable(const fs::path& path)
104{
105 // Rename the file to save its contents
106 fs::path savePath{path.native() + ".save"};
107 fs::rename(path, savePath);
108
109 // Create a directory at the original file path
110 fs::create_directory(path);
111
112 // Create a file within the directory. fs::remove() will throw an exception
113 // if the path is a non-empty directory.
114 std::ofstream childFile{path / "childFile"};
115}
116
117/**
118 * Modify the specified file so that fs::remove() can successfully delete it.
119 *
120 * Undo the modifications from an earlier call to makeFileUnRemovable().
121 *
122 * @param path path to the file
123 */
124inline void makeFileRemovable(const fs::path& path)
125{
126 // makeFileUnRemovable() creates a directory at the file path. Remove the
127 // directory and all of its contents.
128 fs::remove_all(path);
129
130 // Rename the file back to the original path to restore its contents
131 fs::path savePath{path.native() + ".save"};
132 fs::rename(savePath, path);
133}
134
Shawn McCarney8a3afd72020-03-12 14:28:44 -0500135} // namespace phosphor::power::regulators::test_utils