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