blob: e291966edddfe6a543803098b52a46f13b9fb4fc [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 Spinler6ea4d5f2020-05-20 13:31:07 -050037 MOCK_METHOD(const std::vector<std::string>&, getSystemNames, (),
38 (const override));
Matt Spinler5fb24c12020-06-04 11:21:33 -050039 MOCK_METHOD(std::string, expandLocationCode, (const std::string&, uint16_t),
40 (const override));
41 MOCK_METHOD(std::string, getInventoryFromLocCode,
42 (const std::string&, uint16_t), (const override));
Matt Spinlerf60ac272019-12-11 13:47:50 -060043
44 void changeHostState(bool newState)
45 {
Matt Spinler4aa23a12020-02-03 15:05:09 -060046 setHostUp(newState);
Matt Spinlerf60ac272019-12-11 13:47:50 -060047 }
48
49 void setHMCManaged(bool managed)
50 {
51 _hmcManaged = managed;
52 }
53};
54
55/**
56 * @brief The mock HostInterface class
Matt Spinler5342d9a2019-12-12 11:03:39 -060057 *
58 * This replaces the PLDM calls with a FIFO for the asynchronous
59 * responses.
Matt Spinlerf60ac272019-12-11 13:47:50 -060060 */
61class MockHostInterface : public HostInterface
62{
63 public:
Matt Spinler5342d9a2019-12-12 11:03:39 -060064 /**
65 * @brief Constructor
66 *
67 * @param[in] event - The sd_event object
68 * @param[in] dataIface - The DataInterface class
69 */
Matt Spinlerf60ac272019-12-11 13:47:50 -060070 MockHostInterface(sd_event* event, DataInterfaceBase& dataIface) :
71 HostInterface(event, dataIface)
72 {
Matt Spinler5342d9a2019-12-12 11:03:39 -060073 char templ[] = "/tmp/cmdfifoXXXXXX";
74 std::filesystem::path dir = mkdtemp(templ);
75 _fifo = dir / "fifo";
Matt Spinlerf60ac272019-12-11 13:47:50 -060076 }
77
Matt Spinler5342d9a2019-12-12 11:03:39 -060078 /**
79 * @brief Destructor
80 */
Matt Spinlerf60ac272019-12-11 13:47:50 -060081 virtual ~MockHostInterface()
82 {
Matt Spinler5342d9a2019-12-12 11:03:39 -060083 std::filesystem::remove_all(_fifo.parent_path());
Matt Spinlerf60ac272019-12-11 13:47:50 -060084 }
85
86 MOCK_METHOD(CmdStatus, sendNewLogCmd, (uint32_t, uint32_t), (override));
87
Matt Spinler5342d9a2019-12-12 11:03:39 -060088 /**
89 * @brief Cancels waiting for a command response
90 */
91 virtual void cancelCmd() override
92 {
93 _inProgress = false;
94 _source = nullptr;
95 }
96
97 /**
98 * @brief Returns the amount of time to wait before retrying after
99 * a failed send command.
100 *
101 * @return milliseconds - The amount of time to wait
102 */
103 virtual std::chrono::milliseconds getSendRetryDelay() const override
104 {
105 return std::chrono::milliseconds(2);
106 }
107
108 /**
109 * @brief Returns the amount of time to wait before retrying after
110 * a command receive.
111 *
112 * @return milliseconds - The amount of time to wait
113 */
114 virtual std::chrono::milliseconds getReceiveRetryDelay() const override
115 {
116 return std::chrono::milliseconds(2);
117 }
118
119 /**
Matt Spinler41293cb2019-12-12 13:11:09 -0600120 * @brief Returns the amount of time to wait before retrying if the
121 * host firmware's PEL storage was full and it can't store
122 * any more logs until it is freed up somehow.
123 *
124 * @return milliseconds - The amount of time to wait
125 */
126 virtual std::chrono::milliseconds getHostFullRetryDelay() const override
127 {
Matt Spinler6aae6a02020-02-07 12:46:07 -0600128 return std::chrono::milliseconds(400);
Matt Spinler41293cb2019-12-12 13:11:09 -0600129 }
130
131 /**
Matt Spinler5342d9a2019-12-12 11:03:39 -0600132 * @brief Returns the number of commands processed
133 */
134 size_t numCmdsProcessed() const
135 {
136 return _cmdsProcessed;
137 }
138
139 /**
140 * @brief Writes the data passed in to the FIFO
141 *
142 * @param[in] hostResponse - use a 0 to indicate success
143 *
144 * @return CmdStatus - success or failure
145 */
146 CmdStatus send(uint8_t hostResponse)
147 {
148 // Create a FIFO once.
149 if (!std::filesystem::exists(_fifo))
150 {
151 if (mkfifo(_fifo.c_str(), 0622))
152 {
153 ADD_FAILURE() << "Failed mkfifo " << _fifo << strerror(errno);
154 exit(-1);
155 }
156 }
157
158 // Open it and register the reponse callback to
159 // be used on FD activity.
160 int fd = open(_fifo.c_str(), O_NONBLOCK | O_RDWR);
161 EXPECT_TRUE(fd >= 0) << "Unable to open FIFO";
162
163 auto callback = [this](sdeventplus::source::IO& source, int fd,
164 uint32_t events) {
165 this->receive(source, fd, events);
166 };
167
168 try
169 {
170 _source = std::make_unique<sdeventplus::source::IO>(
171 _event, fd, EPOLLIN,
172 std::bind(callback, std::placeholders::_1,
173 std::placeholders::_2, std::placeholders::_3));
174 }
175 catch (std::exception& e)
176 {
177 ADD_FAILURE() << "Event exception: " << e.what();
178 close(fd);
179 return CmdStatus::failure;
180 }
181
182 // Write the fake host reponse to the FIFO
183 auto bytesWritten = write(fd, &hostResponse, sizeof(hostResponse));
184 EXPECT_EQ(bytesWritten, sizeof(hostResponse));
185
186 _inProgress = true;
187
188 return CmdStatus::success;
189 }
190
Matt Spinlerf60ac272019-12-11 13:47:50 -0600191 protected:
Matt Spinler5342d9a2019-12-12 11:03:39 -0600192 /**
193 * @brief Reads the data written to the fifo and then calls
194 * the subscriber's callback.
195 *
196 * Nonzero data indicates a command failure (for testing bad path).
197 *
198 * @param[in] source - The event source object
199 * @param[in] fd - The file descriptor used
200 * @param[in] events - The event bits
201 */
Matt Spinlerf60ac272019-12-11 13:47:50 -0600202 void receive(sdeventplus::source::IO& source, int fd,
203 uint32_t events) override
204 {
Matt Spinler5342d9a2019-12-12 11:03:39 -0600205 if (!(events & EPOLLIN))
206 {
207 return;
208 }
209
210 _inProgress = false;
211
212 int newFD = open(_fifo.c_str(), O_NONBLOCK | O_RDONLY);
213 ASSERT_TRUE(newFD >= 0) << "Failed to open FIFO";
214
215 // Read the host success/failure response from the FIFO.
216 uint8_t data;
217 auto bytesRead = read(newFD, &data, sizeof(data));
218 EXPECT_EQ(bytesRead, sizeof(data));
219
220 close(newFD);
221
222 ResponseStatus status = ResponseStatus::success;
223 if (data != 0)
224 {
225 status = ResponseStatus::failure;
226 }
227
Matt Spinlera44efe42020-03-03 10:30:16 -0600228 callResponseFunc(status);
Matt Spinler5342d9a2019-12-12 11:03:39 -0600229
Matt Spinlerf60ac272019-12-11 13:47:50 -0600230 // Keep account of the number of commands responses for testing.
231 _cmdsProcessed++;
232 }
233
234 private:
Matt Spinler5342d9a2019-12-12 11:03:39 -0600235 /**
236 * @brief The event source for the fifo
237 */
238 std::unique_ptr<sdeventplus::source::IO> _source;
239
240 /**
241 * @brief the path to the fifo
242 */
243 std::filesystem::path _fifo;
244
245 /**
246 * @brief The number of commands processed
247 */
Matt Spinlerf60ac272019-12-11 13:47:50 -0600248 size_t _cmdsProcessed = 0;
Matt Spinler09d64002019-09-11 14:29:46 -0500249};
250
251} // namespace pels
252} // namespace openpower