18a3afd70SShawn McCarney /**
28a3afd70SShawn McCarney * Copyright © 2020 IBM Corporation
38a3afd70SShawn McCarney *
48a3afd70SShawn McCarney * Licensed under the Apache License, Version 2.0 (the "License");
58a3afd70SShawn McCarney * you may not use this file except in compliance with the License.
68a3afd70SShawn McCarney * You may obtain a copy of the License at
78a3afd70SShawn McCarney *
88a3afd70SShawn McCarney * http://www.apache.org/licenses/LICENSE-2.0
98a3afd70SShawn McCarney *
108a3afd70SShawn McCarney * Unless required by applicable law or agreed to in writing, software
118a3afd70SShawn McCarney * distributed under the License is distributed on an "AS IS" BASIS,
128a3afd70SShawn McCarney * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138a3afd70SShawn McCarney * See the License for the specific language governing permissions and
148a3afd70SShawn McCarney * limitations under the License.
158a3afd70SShawn McCarney */
16db0b833cSShawn McCarney #include "action.hpp"
17db0b833cSShawn McCarney #include "chassis.hpp"
18db0b833cSShawn McCarney #include "configuration.hpp"
19db0b833cSShawn McCarney #include "device.hpp"
208a3afd70SShawn McCarney #include "i2c_interface.hpp"
21db0b833cSShawn McCarney #include "mock_action.hpp"
2232252599SShawn McCarney #include "phase_fault_detection.hpp"
23db0b833cSShawn McCarney #include "presence_detection.hpp"
24db0b833cSShawn McCarney #include "rail.hpp"
25db0b833cSShawn McCarney #include "rule.hpp"
268a3afd70SShawn McCarney
27f3633f63SShawn McCarney #include <filesystem>
28f3633f63SShawn McCarney #include <fstream>
298a3afd70SShawn McCarney #include <memory>
30db0b833cSShawn McCarney #include <string>
31db0b833cSShawn McCarney #include <utility>
32db0b833cSShawn McCarney #include <vector>
338a3afd70SShawn McCarney
348a3afd70SShawn McCarney namespace phosphor::power::regulators::test_utils
358a3afd70SShawn McCarney {
368a3afd70SShawn McCarney
37f3633f63SShawn McCarney namespace fs = std::filesystem;
38f3633f63SShawn McCarney
398a3afd70SShawn McCarney /**
408a3afd70SShawn McCarney * Create an I2CInterface object with hard-coded bus and address values.
418a3afd70SShawn McCarney *
428a3afd70SShawn McCarney * @return I2CInterface object wrapped in a unique_ptr
438a3afd70SShawn McCarney */
createI2CInterface()448a3afd70SShawn McCarney inline std::unique_ptr<i2c::I2CInterface> createI2CInterface()
458a3afd70SShawn McCarney {
468a3afd70SShawn McCarney return i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
478a3afd70SShawn McCarney }
488a3afd70SShawn McCarney
49db0b833cSShawn McCarney /**
50db0b833cSShawn McCarney * Creates a Device object with the specified ID.
51db0b833cSShawn McCarney *
52db0b833cSShawn McCarney * Creates Rail objects within the Device if railIDs is specified.
53db0b833cSShawn McCarney *
54db0b833cSShawn McCarney * @param id device ID
55db0b833cSShawn McCarney * @param railIDs rail IDs (optional)
56db0b833cSShawn McCarney * @return Device object
57db0b833cSShawn McCarney */
createDevice(const std::string & id,const std::vector<std::string> & railIDs={})58*f5402197SPatrick Williams inline std::unique_ptr<Device> createDevice(
59*f5402197SPatrick Williams const std::string& id, const std::vector<std::string>& railIDs = {})
60db0b833cSShawn McCarney {
61db0b833cSShawn McCarney // Create Rails (if any)
62db0b833cSShawn McCarney std::vector<std::unique_ptr<Rail>> rails{};
63db0b833cSShawn McCarney for (const std::string& railID : railIDs)
64db0b833cSShawn McCarney {
65db0b833cSShawn McCarney rails.emplace_back(std::make_unique<Rail>(railID));
66db0b833cSShawn McCarney }
67db0b833cSShawn McCarney
68db0b833cSShawn McCarney // Create Device
69db0b833cSShawn McCarney bool isRegulator = true;
70a76898f1SBob King std::string fru =
71a76898f1SBob King "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1";
72db0b833cSShawn McCarney std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
73db0b833cSShawn McCarney std::unique_ptr<PresenceDetection> presenceDetection{};
74db0b833cSShawn McCarney std::unique_ptr<Configuration> configuration{};
7532252599SShawn McCarney std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{};
7632252599SShawn McCarney return std::make_unique<Device>(
7732252599SShawn McCarney id, isRegulator, fru, std::move(i2cInterface),
7832252599SShawn McCarney std::move(presenceDetection), std::move(configuration),
7932252599SShawn McCarney std::move(phaseFaultDetection), std::move(rails));
80db0b833cSShawn McCarney }
81db0b833cSShawn McCarney
82db0b833cSShawn McCarney /**
83db0b833cSShawn McCarney * Creates a Rule object with the specified ID.
84db0b833cSShawn McCarney *
85db0b833cSShawn McCarney * @param id rule ID
86db0b833cSShawn McCarney * @return Rule object
87db0b833cSShawn McCarney */
createRule(const std::string & id)88db0b833cSShawn McCarney inline std::unique_ptr<Rule> createRule(const std::string& id)
89db0b833cSShawn McCarney {
90db0b833cSShawn McCarney // Create actions
91db0b833cSShawn McCarney std::vector<std::unique_ptr<Action>> actions{};
92db0b833cSShawn McCarney actions.emplace_back(std::make_unique<MockAction>());
93db0b833cSShawn McCarney
94db0b833cSShawn McCarney // Create Rule
95db0b833cSShawn McCarney return std::make_unique<Rule>(id, std::move(actions));
96db0b833cSShawn McCarney }
97db0b833cSShawn McCarney
98f3633f63SShawn McCarney /**
99f3633f63SShawn McCarney * Modify the specified file so that fs::remove() fails with an exception.
100f3633f63SShawn McCarney *
101f3633f63SShawn McCarney * The file will be renamed and can be restored by calling makeFileRemovable().
102f3633f63SShawn McCarney *
103f3633f63SShawn McCarney * @param path path to the file
104f3633f63SShawn McCarney */
makeFileUnRemovable(const fs::path & path)105f3633f63SShawn McCarney inline void makeFileUnRemovable(const fs::path& path)
106f3633f63SShawn McCarney {
107f3633f63SShawn McCarney // Rename the file to save its contents
108f3633f63SShawn McCarney fs::path savePath{path.native() + ".save"};
109f3633f63SShawn McCarney fs::rename(path, savePath);
110f3633f63SShawn McCarney
111f3633f63SShawn McCarney // Create a directory at the original file path
112f3633f63SShawn McCarney fs::create_directory(path);
113f3633f63SShawn McCarney
114f3633f63SShawn McCarney // Create a file within the directory. fs::remove() will throw an exception
115f3633f63SShawn McCarney // if the path is a non-empty directory.
116f3633f63SShawn McCarney std::ofstream childFile{path / "childFile"};
117f3633f63SShawn McCarney }
118f3633f63SShawn McCarney
119f3633f63SShawn McCarney /**
120f3633f63SShawn McCarney * Modify the specified file so that fs::remove() can successfully delete it.
121f3633f63SShawn McCarney *
122f3633f63SShawn McCarney * Undo the modifications from an earlier call to makeFileUnRemovable().
123f3633f63SShawn McCarney *
124f3633f63SShawn McCarney * @param path path to the file
125f3633f63SShawn McCarney */
makeFileRemovable(const fs::path & path)126f3633f63SShawn McCarney inline void makeFileRemovable(const fs::path& path)
127f3633f63SShawn McCarney {
128f3633f63SShawn McCarney // makeFileUnRemovable() creates a directory at the file path. Remove the
129f3633f63SShawn McCarney // directory and all of its contents.
130f3633f63SShawn McCarney fs::remove_all(path);
131f3633f63SShawn McCarney
132f3633f63SShawn McCarney // Rename the file back to the original path to restore its contents
133f3633f63SShawn McCarney fs::path savePath{path.native() + ".save"};
134f3633f63SShawn McCarney fs::rename(savePath, path);
135f3633f63SShawn McCarney }
136f3633f63SShawn McCarney
1378a3afd70SShawn McCarney } // namespace phosphor::power::regulators::test_utils
138