Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 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 | */ |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 16 | #include "action.hpp" |
| 17 | #include "chassis.hpp" |
| 18 | #include "configuration.hpp" |
| 19 | #include "device.hpp" |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 20 | #include "i2c_interface.hpp" |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 21 | #include "mock_action.hpp" |
| 22 | #include "presence_detection.hpp" |
| 23 | #include "rail.hpp" |
| 24 | #include "rule.hpp" |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 25 | |
Shawn McCarney | f3633f6 | 2020-09-02 10:34:52 -0500 | [diff] [blame] | 26 | #include <filesystem> |
| 27 | #include <fstream> |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 28 | #include <memory> |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 29 | #include <string> |
| 30 | #include <utility> |
| 31 | #include <vector> |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 32 | |
| 33 | namespace phosphor::power::regulators::test_utils |
| 34 | { |
| 35 | |
Shawn McCarney | f3633f6 | 2020-09-02 10:34:52 -0500 | [diff] [blame] | 36 | namespace fs = std::filesystem; |
| 37 | |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 38 | /** |
| 39 | * Create an I2CInterface object with hard-coded bus and address values. |
| 40 | * |
| 41 | * @return I2CInterface object wrapped in a unique_ptr |
| 42 | */ |
| 43 | inline std::unique_ptr<i2c::I2CInterface> createI2CInterface() |
| 44 | { |
| 45 | return i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED); |
| 46 | } |
| 47 | |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 48 | /** |
| 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 | */ |
| 57 | inline 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 King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 70 | std::string fru = |
| 71 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1"; |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 72 | 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 | */ |
| 87 | inline 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 McCarney | f3633f6 | 2020-09-02 10:34:52 -0500 | [diff] [blame] | 97 | /** |
| 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 | */ |
| 104 | inline 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 | */ |
| 125 | inline 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 McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 136 | } // namespace phosphor::power::regulators::test_utils |