Matt Spinler | 09d6400 | 2019-09-11 14:29:46 -0500 | [diff] [blame] | 1 | #include "extensions/openpower-pels/data_interface.hpp" |
Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 2 | #include "extensions/openpower-pels/host_interface.hpp" |
| 3 | |
| 4 | #include <fcntl.h> |
| 5 | |
Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 6 | #include <sdeventplus/source/io.hpp> |
Matt Spinler | 09d6400 | 2019-09-11 14:29:46 -0500 | [diff] [blame] | 7 | |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 8 | #include <filesystem> |
| 9 | |
Matt Spinler | 09d6400 | 2019-09-11 14:29:46 -0500 | [diff] [blame] | 10 | #include <gmock/gmock.h> |
| 11 | |
| 12 | namespace openpower |
| 13 | { |
| 14 | namespace pels |
| 15 | { |
| 16 | |
| 17 | class MockDataInterface : public DataInterfaceBase |
| 18 | { |
| 19 | public: |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 20 | MockDataInterface() {} |
Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 21 | MOCK_METHOD(std::string, getMachineTypeModel, (), (const override)); |
| 22 | MOCK_METHOD(std::string, getMachineSerialNumber, (), (const override)); |
| 23 | MOCK_METHOD(std::string, getServerFWVersion, (), (const override)); |
| 24 | MOCK_METHOD(std::string, getBMCFWVersion, (), (const override)); |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 25 | MOCK_METHOD(std::string, getBMCFWVersionID, (), (const override)); |
Matt Spinler | 24a8558 | 2020-01-27 16:40:21 -0600 | [diff] [blame] | 26 | MOCK_METHOD(bool, getHostPELEnablement, (), (const override)); |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 27 | MOCK_METHOD(std::string, getBMCState, (), (const override)); |
| 28 | MOCK_METHOD(std::string, getChassisState, (), (const override)); |
| 29 | MOCK_METHOD(std::string, getHostState, (), (const override)); |
Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 30 | MOCK_METHOD(std::string, getMotherboardCCIN, (), (const override)); |
Matt Spinler | 60c4e79 | 2020-03-13 13:45:36 -0500 | [diff] [blame] | 31 | MOCK_METHOD(void, getHWCalloutFields, |
Matt Spinler | 9b90e2a | 2020-04-14 10:59:04 -0500 | [diff] [blame] | 32 | (const std::string&, std::string&, std::string&, std::string&), |
Matt Spinler | 60c4e79 | 2020-03-13 13:45:36 -0500 | [diff] [blame] | 33 | (const override)); |
Matt Spinler | 9b90e2a | 2020-04-14 10:59:04 -0500 | [diff] [blame] | 34 | MOCK_METHOD(std::string, getLocationCode, (const std::string&), |
| 35 | (const override)); |
Matt Spinler | 1ab6696 | 2020-10-29 13:21:44 -0500 | [diff] [blame] | 36 | MOCK_METHOD(std::vector<std::string>, getSystemNames, (), (const override)); |
Matt Spinler | 5fb24c1 | 2020-06-04 11:21:33 -0500 | [diff] [blame] | 37 | MOCK_METHOD(std::string, expandLocationCode, (const std::string&, uint16_t), |
| 38 | (const override)); |
| 39 | MOCK_METHOD(std::string, getInventoryFromLocCode, |
Matt Spinler | 2f9225a | 2020-08-05 12:58:49 -0500 | [diff] [blame] | 40 | (const std::string&, uint16_t, bool), (const override)); |
Matt Spinler | 34a904c | 2020-08-05 14:53:28 -0500 | [diff] [blame] | 41 | MOCK_METHOD(void, assertLEDGroup, (const std::string&, bool), |
| 42 | (const override)); |
Matt Spinler | 993168d | 2021-04-07 16:05:03 -0500 | [diff] [blame] | 43 | MOCK_METHOD(void, setFunctional, (const std::string&, bool), |
| 44 | (const override)); |
Ben Tyner | e32b7e7 | 2021-05-18 12:38:40 -0500 | [diff] [blame] | 45 | MOCK_METHOD(std::vector<uint8_t>, getSystemIMKeyword, (), (const override)); |
Sumit Kumar | 3b8ed7f | 2021-05-18 12:38:35 -0500 | [diff] [blame] | 46 | MOCK_METHOD(bool, getQuiesceOnError, (), (const override)); |
Sumit Kumar | 76198a2 | 2021-07-15 05:59:57 -0500 | [diff] [blame] | 47 | MOCK_METHOD(void, setCriticalAssociation, (const std::string&), |
| 48 | (const override)); |
Sumit Kumar | 9d43a72 | 2021-08-24 09:46:19 -0500 | [diff] [blame] | 49 | MOCK_METHOD(std::vector<bool>, checkDumpStatus, |
| 50 | (const std::vector<std::string>&), (const override)); |
Sumit Kumar | 2c36fdd | 2021-09-21 03:12:11 -0500 | [diff] [blame] | 51 | MOCK_METHOD(std::string, getBootState, (), (const override)); |
Jayanth Othayoth | ecaa2fc | 2021-11-05 02:02:52 -0500 | [diff] [blame] | 52 | MOCK_METHOD(void, createGuardRecord, |
| 53 | (const std::vector<uint8_t>&, const std::string&, |
| 54 | const std::string&), |
| 55 | (const override)); |
Sumit Kumar | 3e27443 | 2021-09-14 06:37:56 -0500 | [diff] [blame] | 56 | MOCK_METHOD(void, createProgressSRC, |
| 57 | (const uint64_t&, const std::vector<uint8_t>&), |
| 58 | (const override)); |
Sumit Kumar | 027bf28 | 2022-01-24 11:25:19 -0600 | [diff] [blame] | 59 | MOCK_METHOD(std::vector<uint32_t>, getLogIDWithHwIsolation, (), |
| 60 | (const override)); |
Vijay Lobo | 875b6c7 | 2021-10-20 17:38:56 -0500 | [diff] [blame] | 61 | MOCK_METHOD(std::vector<uint8_t>, getRawProgressSRC, (), (const override)); |
Matt Spinler | 34a904c | 2020-08-05 14:53:28 -0500 | [diff] [blame] | 62 | |
Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 63 | void changeHostState(bool newState) |
| 64 | { |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 65 | setHostUp(newState); |
Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void setHMCManaged(bool managed) |
| 69 | { |
| 70 | _hmcManaged = managed; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * @brief The mock HostInterface class |
Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 76 | * |
| 77 | * This replaces the PLDM calls with a FIFO for the asynchronous |
| 78 | * responses. |
Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 79 | */ |
| 80 | class MockHostInterface : public HostInterface |
| 81 | { |
| 82 | public: |
Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 83 | /** |
| 84 | * @brief Constructor |
| 85 | * |
| 86 | * @param[in] event - The sd_event object |
| 87 | * @param[in] dataIface - The DataInterface class |
| 88 | */ |
Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 89 | MockHostInterface(sd_event* event, DataInterfaceBase& dataIface) : |
| 90 | HostInterface(event, dataIface) |
| 91 | { |
Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 92 | char templ[] = "/tmp/cmdfifoXXXXXX"; |
| 93 | std::filesystem::path dir = mkdtemp(templ); |
| 94 | _fifo = dir / "fifo"; |
Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 95 | } |
| 96 | |
Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 97 | /** |
| 98 | * @brief Destructor |
| 99 | */ |
Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 100 | virtual ~MockHostInterface() |
| 101 | { |
Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 102 | std::filesystem::remove_all(_fifo.parent_path()); |
Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | MOCK_METHOD(CmdStatus, sendNewLogCmd, (uint32_t, uint32_t), (override)); |
| 106 | |
Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 107 | /** |
| 108 | * @brief Cancels waiting for a command response |
| 109 | */ |
| 110 | virtual void cancelCmd() override |
| 111 | { |
| 112 | _inProgress = false; |
| 113 | _source = nullptr; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @brief Returns the amount of time to wait before retrying after |
| 118 | * a failed send command. |
| 119 | * |
| 120 | * @return milliseconds - The amount of time to wait |
| 121 | */ |
| 122 | virtual std::chrono::milliseconds getSendRetryDelay() const override |
| 123 | { |
| 124 | return std::chrono::milliseconds(2); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @brief Returns the amount of time to wait before retrying after |
| 129 | * a command receive. |
| 130 | * |
| 131 | * @return milliseconds - The amount of time to wait |
| 132 | */ |
| 133 | virtual std::chrono::milliseconds getReceiveRetryDelay() const override |
| 134 | { |
| 135 | return std::chrono::milliseconds(2); |
| 136 | } |
| 137 | |
| 138 | /** |
Matt Spinler | 41293cb | 2019-12-12 13:11:09 -0600 | [diff] [blame] | 139 | * @brief Returns the amount of time to wait before retrying if the |
| 140 | * host firmware's PEL storage was full and it can't store |
| 141 | * any more logs until it is freed up somehow. |
| 142 | * |
| 143 | * @return milliseconds - The amount of time to wait |
| 144 | */ |
| 145 | virtual std::chrono::milliseconds getHostFullRetryDelay() const override |
| 146 | { |
Matt Spinler | 6aae6a0 | 2020-02-07 12:46:07 -0600 | [diff] [blame] | 147 | return std::chrono::milliseconds(400); |
Matt Spinler | 41293cb | 2019-12-12 13:11:09 -0600 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
Matt Spinler | e5f7508 | 2022-01-24 16:09:51 -0600 | [diff] [blame] | 151 | * @brief Returns the amount of time to wait after the host is up |
| 152 | * before sending commands. |
| 153 | * |
| 154 | * @return milliseconds - The amount of time to wait |
| 155 | */ |
| 156 | virtual std::chrono::milliseconds getHostUpDelay() const override |
| 157 | { |
| 158 | return std::chrono::milliseconds(0); |
| 159 | } |
| 160 | |
| 161 | /** |
Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 162 | * @brief Returns the number of commands processed |
| 163 | */ |
| 164 | size_t numCmdsProcessed() const |
| 165 | { |
| 166 | return _cmdsProcessed; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @brief Writes the data passed in to the FIFO |
| 171 | * |
| 172 | * @param[in] hostResponse - use a 0 to indicate success |
| 173 | * |
| 174 | * @return CmdStatus - success or failure |
| 175 | */ |
| 176 | CmdStatus send(uint8_t hostResponse) |
| 177 | { |
| 178 | // Create a FIFO once. |
| 179 | if (!std::filesystem::exists(_fifo)) |
| 180 | { |
| 181 | if (mkfifo(_fifo.c_str(), 0622)) |
| 182 | { |
| 183 | ADD_FAILURE() << "Failed mkfifo " << _fifo << strerror(errno); |
| 184 | exit(-1); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Open it and register the reponse callback to |
| 189 | // be used on FD activity. |
| 190 | int fd = open(_fifo.c_str(), O_NONBLOCK | O_RDWR); |
| 191 | EXPECT_TRUE(fd >= 0) << "Unable to open FIFO"; |
| 192 | |
| 193 | auto callback = [this](sdeventplus::source::IO& source, int fd, |
| 194 | uint32_t events) { |
| 195 | this->receive(source, fd, events); |
| 196 | }; |
| 197 | |
| 198 | try |
| 199 | { |
| 200 | _source = std::make_unique<sdeventplus::source::IO>( |
| 201 | _event, fd, EPOLLIN, |
| 202 | std::bind(callback, std::placeholders::_1, |
| 203 | std::placeholders::_2, std::placeholders::_3)); |
| 204 | } |
Patrick Williams | 66491c6 | 2021-10-06 12:23:37 -0500 | [diff] [blame] | 205 | catch (const std::exception& e) |
Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 206 | { |
| 207 | ADD_FAILURE() << "Event exception: " << e.what(); |
| 208 | close(fd); |
| 209 | return CmdStatus::failure; |
| 210 | } |
| 211 | |
| 212 | // Write the fake host reponse to the FIFO |
| 213 | auto bytesWritten = write(fd, &hostResponse, sizeof(hostResponse)); |
| 214 | EXPECT_EQ(bytesWritten, sizeof(hostResponse)); |
| 215 | |
| 216 | _inProgress = true; |
| 217 | |
| 218 | return CmdStatus::success; |
| 219 | } |
| 220 | |
Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 221 | protected: |
Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 222 | /** |
| 223 | * @brief Reads the data written to the fifo and then calls |
| 224 | * the subscriber's callback. |
| 225 | * |
| 226 | * Nonzero data indicates a command failure (for testing bad path). |
| 227 | * |
| 228 | * @param[in] source - The event source object |
| 229 | * @param[in] fd - The file descriptor used |
| 230 | * @param[in] events - The event bits |
| 231 | */ |
Patrick Williams | d26fa3e | 2021-04-21 15:22:23 -0500 | [diff] [blame] | 232 | void receive(sdeventplus::source::IO& /*source*/, int /*fd*/, |
Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 233 | uint32_t events) override |
| 234 | { |
Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 235 | if (!(events & EPOLLIN)) |
| 236 | { |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | _inProgress = false; |
| 241 | |
| 242 | int newFD = open(_fifo.c_str(), O_NONBLOCK | O_RDONLY); |
| 243 | ASSERT_TRUE(newFD >= 0) << "Failed to open FIFO"; |
| 244 | |
| 245 | // Read the host success/failure response from the FIFO. |
| 246 | uint8_t data; |
| 247 | auto bytesRead = read(newFD, &data, sizeof(data)); |
| 248 | EXPECT_EQ(bytesRead, sizeof(data)); |
| 249 | |
| 250 | close(newFD); |
| 251 | |
| 252 | ResponseStatus status = ResponseStatus::success; |
| 253 | if (data != 0) |
| 254 | { |
| 255 | status = ResponseStatus::failure; |
| 256 | } |
| 257 | |
Matt Spinler | a44efe4 | 2020-03-03 10:30:16 -0600 | [diff] [blame] | 258 | callResponseFunc(status); |
Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 259 | |
Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 260 | // Keep account of the number of commands responses for testing. |
| 261 | _cmdsProcessed++; |
| 262 | } |
| 263 | |
| 264 | private: |
Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 265 | /** |
| 266 | * @brief The event source for the fifo |
| 267 | */ |
| 268 | std::unique_ptr<sdeventplus::source::IO> _source; |
| 269 | |
| 270 | /** |
| 271 | * @brief the path to the fifo |
| 272 | */ |
| 273 | std::filesystem::path _fifo; |
| 274 | |
| 275 | /** |
| 276 | * @brief The number of commands processed |
| 277 | */ |
Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 278 | size_t _cmdsProcessed = 0; |
Matt Spinler | 09d6400 | 2019-09-11 14:29:46 -0500 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | } // namespace pels |
| 282 | } // namespace openpower |