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 #include "action.hpp" 17 #include "chassis.hpp" 18 #include "configuration.hpp" 19 #include "device.hpp" 20 #include "i2c_interface.hpp" 21 #include "mock_action.hpp" 22 #include "phase_fault_detection.hpp" 23 #include "presence_detection.hpp" 24 #include "rail.hpp" 25 #include "rule.hpp" 26 27 #include <filesystem> 28 #include <fstream> 29 #include <memory> 30 #include <string> 31 #include <utility> 32 #include <vector> 33 34 namespace phosphor::power::regulators::test_utils 35 { 36 37 namespace fs = std::filesystem; 38 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 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> createDevice( 59 const std::string& id, 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 = 71 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1"; 72 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); 73 std::unique_ptr<PresenceDetection> presenceDetection{}; 74 std::unique_ptr<Configuration> configuration{}; 75 std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{}; 76 return std::make_unique<Device>( 77 id, isRegulator, fru, std::move(i2cInterface), 78 std::move(presenceDetection), std::move(configuration), 79 std::move(phaseFaultDetection), std::move(rails)); 80 } 81 82 /** 83 * Creates a Rule object with the specified ID. 84 * 85 * @param id rule ID 86 * @return Rule object 87 */ 88 inline std::unique_ptr<Rule> createRule(const std::string& id) 89 { 90 // Create actions 91 std::vector<std::unique_ptr<Action>> actions{}; 92 actions.emplace_back(std::make_unique<MockAction>()); 93 94 // Create Rule 95 return std::make_unique<Rule>(id, std::move(actions)); 96 } 97 98 /** 99 * Modify the specified file so that fs::remove() fails with an exception. 100 * 101 * The file will be renamed and can be restored by calling makeFileRemovable(). 102 * 103 * @param path path to the file 104 */ 105 inline void makeFileUnRemovable(const fs::path& path) 106 { 107 // Rename the file to save its contents 108 fs::path savePath{path.native() + ".save"}; 109 fs::rename(path, savePath); 110 111 // Create a directory at the original file path 112 fs::create_directory(path); 113 114 // Create a file within the directory. fs::remove() will throw an exception 115 // if the path is a non-empty directory. 116 std::ofstream childFile{path / "childFile"}; 117 } 118 119 /** 120 * Modify the specified file so that fs::remove() can successfully delete it. 121 * 122 * Undo the modifications from an earlier call to makeFileUnRemovable(). 123 * 124 * @param path path to the file 125 */ 126 inline void makeFileRemovable(const fs::path& path) 127 { 128 // makeFileUnRemovable() creates a directory at the file path. Remove the 129 // directory and all of its contents. 130 fs::remove_all(path); 131 132 // Rename the file back to the original path to restore its contents 133 fs::path savePath{path.native() + ".save"}; 134 fs::rename(savePath, path); 135 } 136 137 } // namespace phosphor::power::regulators::test_utils 138