blob: 63786c47d61e21580b3260af42a912324177a9a5 [file] [log] [blame]
Matt Spinler09d64002019-09-11 14:29:46 -05001#include "extensions/openpower-pels/data_interface.hpp"
Matt Spinlerf60ac272019-12-11 13:47:50 -06002#include "extensions/openpower-pels/host_interface.hpp"
3
4#include <fcntl.h>
5
6#include <filesystem>
7#include <sdeventplus/source/io.hpp>
Matt Spinler09d64002019-09-11 14:29:46 -05008
9#include <gmock/gmock.h>
10
11namespace openpower
12{
13namespace pels
14{
15
16class MockDataInterface : public DataInterfaceBase
17{
18 public:
19 MockDataInterface()
20 {
21 }
Matt Spinlerf60ac272019-12-11 13:47:50 -060022 MOCK_METHOD(std::string, getMachineTypeModel, (), (const override));
23 MOCK_METHOD(std::string, getMachineSerialNumber, (), (const override));
24 MOCK_METHOD(std::string, getServerFWVersion, (), (const override));
25 MOCK_METHOD(std::string, getBMCFWVersion, (), (const override));
Matt Spinler677381b2020-01-23 10:04:29 -060026 MOCK_METHOD(std::string, getBMCFWVersionID, (), (const override));
Matt Spinler24a85582020-01-27 16:40:21 -060027 MOCK_METHOD(bool, getHostPELEnablement, (), (const override));
Matt Spinler4aa23a12020-02-03 15:05:09 -060028 MOCK_METHOD(std::string, getBMCState, (), (const override));
29 MOCK_METHOD(std::string, getChassisState, (), (const override));
30 MOCK_METHOD(std::string, getHostState, (), (const override));
Matt Spinler075e5ba2020-02-21 15:46:00 -060031 MOCK_METHOD(std::string, getMotherboardCCIN, (), (const override));
Matt Spinler60c4e792020-03-13 13:45:36 -050032 MOCK_METHOD(void, getHWCalloutFields,
Matt Spinler9b90e2a2020-04-14 10:59:04 -050033 (const std::string&, std::string&, std::string&, std::string&),
Matt Spinler60c4e792020-03-13 13:45:36 -050034 (const override));
Matt Spinler9b90e2a2020-04-14 10:59:04 -050035 MOCK_METHOD(std::string, getLocationCode, (const std::string&),
36 (const override));
Matt Spinler1ab66962020-10-29 13:21:44 -050037 MOCK_METHOD(std::vector<std::string>, getSystemNames, (), (const override));
Matt Spinler5fb24c12020-06-04 11:21:33 -050038 MOCK_METHOD(std::string, expandLocationCode, (const std::string&, uint16_t),
39 (const override));
40 MOCK_METHOD(std::string, getInventoryFromLocCode,
Matt Spinler2f9225a2020-08-05 12:58:49 -050041 (const std::string&, uint16_t, bool), (const override));
Matt Spinler34a904c2020-08-05 14:53:28 -050042 MOCK_METHOD(void, assertLEDGroup, (const std::string&, bool),
43 (const override));
Matt Spinler993168d2021-04-07 16:05:03 -050044 MOCK_METHOD(void, setFunctional, (const std::string&, bool),
45 (const override));
Ben Tynere32b7e72021-05-18 12:38:40 -050046 MOCK_METHOD(std::vector<uint8_t>, getSystemIMKeyword, (), (const override));
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -050047 MOCK_METHOD(bool, getQuiesceOnError, (), (const override));
Sumit Kumar76198a22021-07-15 05:59:57 -050048 MOCK_METHOD(void, setCriticalAssociation, (const std::string&),
49 (const override));
Sumit Kumar9d43a722021-08-24 09:46:19 -050050 MOCK_METHOD(std::vector<bool>, checkDumpStatus,
51 (const std::vector<std::string>&), (const override));
Sumit Kumar2c36fdd2021-09-21 03:12:11 -050052 MOCK_METHOD(std::string, getBootState, (), (const override));
Jayanth Othayothecaa2fc2021-11-05 02:02:52 -050053 MOCK_METHOD(void, createGuardRecord,
54 (const std::vector<uint8_t>&, const std::string&,
55 const std::string&),
56 (const override));
Sumit Kumar3e274432021-09-14 06:37:56 -050057 MOCK_METHOD(void, createProgressSRC,
58 (const uint64_t&, const std::vector<uint8_t>&),
59 (const override));
Matt Spinler34a904c2020-08-05 14:53:28 -050060
Matt Spinlerf60ac272019-12-11 13:47:50 -060061 void changeHostState(bool newState)
62 {
Matt Spinler4aa23a12020-02-03 15:05:09 -060063 setHostUp(newState);
Matt Spinlerf60ac272019-12-11 13:47:50 -060064 }
65
66 void setHMCManaged(bool managed)
67 {
68 _hmcManaged = managed;
69 }
70};
71
72/**
73 * @brief The mock HostInterface class
Matt Spinler5342d9a2019-12-12 11:03:39 -060074 *
75 * This replaces the PLDM calls with a FIFO for the asynchronous
76 * responses.
Matt Spinlerf60ac272019-12-11 13:47:50 -060077 */
78class MockHostInterface : public HostInterface
79{
80 public:
Matt Spinler5342d9a2019-12-12 11:03:39 -060081 /**
82 * @brief Constructor
83 *
84 * @param[in] event - The sd_event object
85 * @param[in] dataIface - The DataInterface class
86 */
Matt Spinlerf60ac272019-12-11 13:47:50 -060087 MockHostInterface(sd_event* event, DataInterfaceBase& dataIface) :
88 HostInterface(event, dataIface)
89 {
Matt Spinler5342d9a2019-12-12 11:03:39 -060090 char templ[] = "/tmp/cmdfifoXXXXXX";
91 std::filesystem::path dir = mkdtemp(templ);
92 _fifo = dir / "fifo";
Matt Spinlerf60ac272019-12-11 13:47:50 -060093 }
94
Matt Spinler5342d9a2019-12-12 11:03:39 -060095 /**
96 * @brief Destructor
97 */
Matt Spinlerf60ac272019-12-11 13:47:50 -060098 virtual ~MockHostInterface()
99 {
Matt Spinler5342d9a2019-12-12 11:03:39 -0600100 std::filesystem::remove_all(_fifo.parent_path());
Matt Spinlerf60ac272019-12-11 13:47:50 -0600101 }
102
103 MOCK_METHOD(CmdStatus, sendNewLogCmd, (uint32_t, uint32_t), (override));
104
Matt Spinler5342d9a2019-12-12 11:03:39 -0600105 /**
106 * @brief Cancels waiting for a command response
107 */
108 virtual void cancelCmd() override
109 {
110 _inProgress = false;
111 _source = nullptr;
112 }
113
114 /**
115 * @brief Returns the amount of time to wait before retrying after
116 * a failed send command.
117 *
118 * @return milliseconds - The amount of time to wait
119 */
120 virtual std::chrono::milliseconds getSendRetryDelay() const override
121 {
122 return std::chrono::milliseconds(2);
123 }
124
125 /**
126 * @brief Returns the amount of time to wait before retrying after
127 * a command receive.
128 *
129 * @return milliseconds - The amount of time to wait
130 */
131 virtual std::chrono::milliseconds getReceiveRetryDelay() const override
132 {
133 return std::chrono::milliseconds(2);
134 }
135
136 /**
Matt Spinler41293cb2019-12-12 13:11:09 -0600137 * @brief Returns the amount of time to wait before retrying if the
138 * host firmware's PEL storage was full and it can't store
139 * any more logs until it is freed up somehow.
140 *
141 * @return milliseconds - The amount of time to wait
142 */
143 virtual std::chrono::milliseconds getHostFullRetryDelay() const override
144 {
Matt Spinler6aae6a02020-02-07 12:46:07 -0600145 return std::chrono::milliseconds(400);
Matt Spinler41293cb2019-12-12 13:11:09 -0600146 }
147
148 /**
Matt Spinlere5f75082022-01-24 16:09:51 -0600149 * @brief Returns the amount of time to wait after the host is up
150 * before sending commands.
151 *
152 * @return milliseconds - The amount of time to wait
153 */
154 virtual std::chrono::milliseconds getHostUpDelay() const override
155 {
156 return std::chrono::milliseconds(0);
157 }
158
159 /**
Matt Spinler5342d9a2019-12-12 11:03:39 -0600160 * @brief Returns the number of commands processed
161 */
162 size_t numCmdsProcessed() const
163 {
164 return _cmdsProcessed;
165 }
166
167 /**
168 * @brief Writes the data passed in to the FIFO
169 *
170 * @param[in] hostResponse - use a 0 to indicate success
171 *
172 * @return CmdStatus - success or failure
173 */
174 CmdStatus send(uint8_t hostResponse)
175 {
176 // Create a FIFO once.
177 if (!std::filesystem::exists(_fifo))
178 {
179 if (mkfifo(_fifo.c_str(), 0622))
180 {
181 ADD_FAILURE() << "Failed mkfifo " << _fifo << strerror(errno);
182 exit(-1);
183 }
184 }
185
186 // Open it and register the reponse callback to
187 // be used on FD activity.
188 int fd = open(_fifo.c_str(), O_NONBLOCK | O_RDWR);
189 EXPECT_TRUE(fd >= 0) << "Unable to open FIFO";
190
191 auto callback = [this](sdeventplus::source::IO& source, int fd,
192 uint32_t events) {
193 this->receive(source, fd, events);
194 };
195
196 try
197 {
198 _source = std::make_unique<sdeventplus::source::IO>(
199 _event, fd, EPOLLIN,
200 std::bind(callback, std::placeholders::_1,
201 std::placeholders::_2, std::placeholders::_3));
202 }
Patrick Williams66491c62021-10-06 12:23:37 -0500203 catch (const std::exception& e)
Matt Spinler5342d9a2019-12-12 11:03:39 -0600204 {
205 ADD_FAILURE() << "Event exception: " << e.what();
206 close(fd);
207 return CmdStatus::failure;
208 }
209
210 // Write the fake host reponse to the FIFO
211 auto bytesWritten = write(fd, &hostResponse, sizeof(hostResponse));
212 EXPECT_EQ(bytesWritten, sizeof(hostResponse));
213
214 _inProgress = true;
215
216 return CmdStatus::success;
217 }
218
Matt Spinlerf60ac272019-12-11 13:47:50 -0600219 protected:
Matt Spinler5342d9a2019-12-12 11:03:39 -0600220 /**
221 * @brief Reads the data written to the fifo and then calls
222 * the subscriber's callback.
223 *
224 * Nonzero data indicates a command failure (for testing bad path).
225 *
226 * @param[in] source - The event source object
227 * @param[in] fd - The file descriptor used
228 * @param[in] events - The event bits
229 */
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500230 void receive(sdeventplus::source::IO& /*source*/, int /*fd*/,
Matt Spinlerf60ac272019-12-11 13:47:50 -0600231 uint32_t events) override
232 {
Matt Spinler5342d9a2019-12-12 11:03:39 -0600233 if (!(events & EPOLLIN))
234 {
235 return;
236 }
237
238 _inProgress = false;
239
240 int newFD = open(_fifo.c_str(), O_NONBLOCK | O_RDONLY);
241 ASSERT_TRUE(newFD >= 0) << "Failed to open FIFO";
242
243 // Read the host success/failure response from the FIFO.
244 uint8_t data;
245 auto bytesRead = read(newFD, &data, sizeof(data));
246 EXPECT_EQ(bytesRead, sizeof(data));
247
248 close(newFD);
249
250 ResponseStatus status = ResponseStatus::success;
251 if (data != 0)
252 {
253 status = ResponseStatus::failure;
254 }
255
Matt Spinlera44efe42020-03-03 10:30:16 -0600256 callResponseFunc(status);
Matt Spinler5342d9a2019-12-12 11:03:39 -0600257
Matt Spinlerf60ac272019-12-11 13:47:50 -0600258 // Keep account of the number of commands responses for testing.
259 _cmdsProcessed++;
260 }
261
262 private:
Matt Spinler5342d9a2019-12-12 11:03:39 -0600263 /**
264 * @brief The event source for the fifo
265 */
266 std::unique_ptr<sdeventplus::source::IO> _source;
267
268 /**
269 * @brief the path to the fifo
270 */
271 std::filesystem::path _fifo;
272
273 /**
274 * @brief The number of commands processed
275 */
Matt Spinlerf60ac272019-12-11 13:47:50 -0600276 size_t _cmdsProcessed = 0;
Matt Spinler09d64002019-09-11 14:29:46 -0500277};
278
279} // namespace pels
280} // namespace openpower