blob: 2903d3c28c66899ff104b49f4a0ccc6909cc7692 [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;
Bob Kinga76898f2020-10-13 15:08:33 +080070 std::string fru =
71 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1";
Shawn McCarneydb0b8332020-04-06 14:13:04 -050072 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
73 std::unique_ptr<PresenceDetection> presenceDetection{};
74 std::unique_ptr<Configuration> configuration{};
75 return std::make_unique<Device>(id, isRegulator, fru,
76 std::move(i2cInterface),
77 std::move(presenceDetection),
78 std::move(configuration), std::move(rails));
79}
80
81/**
82 * Creates a Rule object with the specified ID.
83 *
84 * @param id rule ID
85 * @return Rule object
86 */
87inline std::unique_ptr<Rule> createRule(const std::string& id)
88{
89 // Create actions
90 std::vector<std::unique_ptr<Action>> actions{};
91 actions.emplace_back(std::make_unique<MockAction>());
92
93 // Create Rule
94 return std::make_unique<Rule>(id, std::move(actions));
95}
96
Shawn McCarneyf3633f62020-09-02 10:34:52 -050097/**
98 * Modify the specified file so that fs::remove() fails with an exception.
99 *
100 * The file will be renamed and can be restored by calling makeFileRemovable().
101 *
102 * @param path path to the file
103 */
104inline void makeFileUnRemovable(const fs::path& path)
105{
106 // Rename the file to save its contents
107 fs::path savePath{path.native() + ".save"};
108 fs::rename(path, savePath);
109
110 // Create a directory at the original file path
111 fs::create_directory(path);
112
113 // Create a file within the directory. fs::remove() will throw an exception
114 // if the path is a non-empty directory.
115 std::ofstream childFile{path / "childFile"};
116}
117
118/**
119 * Modify the specified file so that fs::remove() can successfully delete it.
120 *
121 * Undo the modifications from an earlier call to makeFileUnRemovable().
122 *
123 * @param path path to the file
124 */
125inline void makeFileRemovable(const fs::path& path)
126{
127 // makeFileUnRemovable() creates a directory at the file path. Remove the
128 // directory and all of its contents.
129 fs::remove_all(path);
130
131 // Rename the file back to the original path to restore its contents
132 fs::path savePath{path.native() + ".save"};
133 fs::rename(savePath, path);
134}
135
Shawn McCarney8a3afd72020-03-12 14:28:44 -0500136} // namespace phosphor::power::regulators::test_utils