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 "presence_detection.hpp" 23 #include "rail.hpp" 24 #include "rule.hpp" 25 26 #include <filesystem> 27 #include <fstream> 28 #include <memory> 29 #include <string> 30 #include <utility> 31 #include <vector> 32 33 namespace phosphor::power::regulators::test_utils 34 { 35 36 namespace fs = std::filesystem; 37 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 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; 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 */ 86 inline 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 96 /** 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 */ 103 inline 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 */ 124 inline 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 135 } // namespace phosphor::power::regulators::test_utils 136