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" |
Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 22 | #include "phase_fault_detection.hpp" |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 23 | #include "presence_detection.hpp" |
| 24 | #include "rail.hpp" |
| 25 | #include "rule.hpp" |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 26 | |
Shawn McCarney | f3633f6 | 2020-09-02 10:34:52 -0500 | [diff] [blame] | 27 | #include <filesystem> |
| 28 | #include <fstream> |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 29 | #include <memory> |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <utility> |
| 32 | #include <vector> |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 33 | |
| 34 | namespace phosphor::power::regulators::test_utils |
| 35 | { |
| 36 | |
Shawn McCarney | f3633f6 | 2020-09-02 10:34:52 -0500 | [diff] [blame] | 37 | namespace fs = std::filesystem; |
| 38 | |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 39 | /** |
| 40 | * Create an I2CInterface object with hard-coded bus and address values. |
| 41 | * |
| 42 | * @return I2CInterface object wrapped in a unique_ptr |
| 43 | */ |
| 44 | inline std::unique_ptr<i2c::I2CInterface> createI2CInterface() |
| 45 | { |
| 46 | return i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED); |
| 47 | } |
| 48 | |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 49 | /** |
| 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 | */ |
| 58 | inline 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 King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 71 | std::string fru = |
| 72 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1"; |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 73 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 74 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 75 | std::unique_ptr<Configuration> configuration{}; |
Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 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)); |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Creates a Rule object with the specified ID. |
| 85 | * |
| 86 | * @param id rule ID |
| 87 | * @return Rule object |
| 88 | */ |
| 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 | |
Shawn McCarney | f3633f6 | 2020-09-02 10:34:52 -0500 | [diff] [blame] | 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 | */ |
| 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 | */ |
| 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 | |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 138 | } // namespace phosphor::power::regulators::test_utils |