Matt Spinler | 97f7abc | 2019-11-06 09:40:23 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 16 | #include "extensions/openpower-pels/manager.hpp" |
| 17 | #include "log_manager.hpp" |
| 18 | #include "pel_utils.hpp" |
| 19 | |
| 20 | #include <fstream> |
| 21 | #include <regex> |
Matt Spinler | a34ab72 | 2019-12-16 10:39:32 -0600 | [diff] [blame] | 22 | #include <xyz/openbmc_project/Common/error.hpp> |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | using namespace openpower::pels; |
| 27 | namespace fs = std::filesystem; |
| 28 | |
Matt Spinler | 05c2c6c | 2019-12-18 14:02:09 -0600 | [diff] [blame] | 29 | class TestLogger |
| 30 | { |
| 31 | public: |
| 32 | void log(const std::string& name, phosphor::logging::Entry::Level level, |
| 33 | const EventLogger::ADMap& additionalData) |
| 34 | { |
| 35 | errName = name; |
| 36 | errLevel = level; |
| 37 | ad = additionalData; |
| 38 | } |
| 39 | |
| 40 | std::string errName; |
| 41 | phosphor::logging::Entry::Level errLevel; |
| 42 | EventLogger::ADMap ad; |
| 43 | }; |
| 44 | |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 45 | class ManagerTest : public CleanPELFiles |
| 46 | { |
Matt Spinler | 6b1a5c8 | 2020-01-07 08:48:53 -0600 | [diff] [blame] | 47 | public: |
| 48 | ManagerTest() : logManager(bus, "logging_path") |
| 49 | { |
| 50 | sd_event_default(&sdEvent); |
| 51 | bus.attach_event(sdEvent, SD_EVENT_PRIORITY_NORMAL); |
| 52 | } |
| 53 | |
| 54 | ~ManagerTest() |
| 55 | { |
| 56 | sd_event_unref(sdEvent); |
| 57 | } |
| 58 | |
| 59 | sdbusplus::bus::bus bus = sdbusplus::bus::new_default(); |
| 60 | phosphor::logging::internal::Manager logManager; |
| 61 | sd_event* sdEvent; |
Matt Spinler | 05c2c6c | 2019-12-18 14:02:09 -0600 | [diff] [blame] | 62 | TestLogger logger; |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | fs::path makeTempDir() |
| 66 | { |
| 67 | char path[] = "/tmp/tempnameXXXXXX"; |
| 68 | std::filesystem::path dir = mkdtemp(path); |
| 69 | return dir; |
| 70 | } |
| 71 | |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 72 | std::optional<fs::path> findAnyPELInRepo() |
| 73 | { |
| 74 | // PELs are named <timestamp>_<ID> |
| 75 | std::regex expr{"\\d+_\\d+"}; |
| 76 | |
| 77 | for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs")) |
| 78 | { |
| 79 | if (std::regex_search(f.path().string(), expr)) |
| 80 | { |
| 81 | return f.path(); |
| 82 | } |
| 83 | } |
| 84 | return std::nullopt; |
| 85 | } |
| 86 | |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 87 | // Test that using the RAWPEL=<file> with the Manager::create() call gets |
| 88 | // a PEL saved in the repository. |
| 89 | TEST_F(ManagerTest, TestCreateWithPEL) |
| 90 | { |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 91 | std::unique_ptr<DataInterfaceBase> dataIface = |
| 92 | std::make_unique<DataInterface>(bus); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 93 | |
Matt Spinler | 05c2c6c | 2019-12-18 14:02:09 -0600 | [diff] [blame] | 94 | openpower::pels::Manager manager{ |
| 95 | logManager, std::move(dataIface), |
| 96 | std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1, |
| 97 | std::placeholders::_2, std::placeholders::_3)}; |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 98 | |
| 99 | // Create a PEL, write it to a file, and pass that filename into |
| 100 | // the create function. |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 101 | auto data = pelDataFactory(TestPELType::pelSimple); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 102 | |
| 103 | fs::path pelFilename = makeTempDir() / "rawpel"; |
| 104 | std::ofstream pelFile{pelFilename}; |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 105 | pelFile.write(reinterpret_cast<const char*>(data.data()), data.size()); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 106 | pelFile.close(); |
| 107 | |
| 108 | std::string adItem = "RAWPEL=" + pelFilename.string(); |
| 109 | std::vector<std::string> additionalData{adItem}; |
| 110 | std::vector<std::string> associations; |
| 111 | |
Matt Spinler | 367144c | 2019-09-19 15:33:52 -0500 | [diff] [blame] | 112 | manager.create("error message", 42, 0, |
| 113 | phosphor::logging::Entry::Level::Error, additionalData, |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 114 | associations); |
| 115 | |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 116 | // Find the file in the PEL repository directory |
| 117 | auto pelPathInRepo = findAnyPELInRepo(); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 118 | |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 119 | EXPECT_TRUE(pelPathInRepo); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 120 | |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 121 | // Now remove it based on its OpenBMC event log ID |
| 122 | manager.erase(42); |
| 123 | |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 124 | pelPathInRepo = findAnyPELInRepo(); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 125 | |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 126 | EXPECT_FALSE(pelPathInRepo); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 127 | |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 128 | fs::remove_all(pelFilename.parent_path()); |
| 129 | } |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 130 | |
Matt Spinler | e95fd01 | 2020-01-07 12:53:16 -0600 | [diff] [blame^] | 131 | TEST_F(ManagerTest, TestCreateWithInvalidPEL) |
| 132 | { |
| 133 | std::unique_ptr<DataInterfaceBase> dataIface = |
| 134 | std::make_unique<DataInterface>(bus); |
| 135 | |
| 136 | openpower::pels::Manager manager{ |
| 137 | logManager, std::move(dataIface), |
| 138 | std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1, |
| 139 | std::placeholders::_2, std::placeholders::_3)}; |
| 140 | |
| 141 | // Create a PEL, write it to a file, and pass that filename into |
| 142 | // the create function. |
| 143 | auto data = pelDataFactory(TestPELType::pelSimple); |
| 144 | |
| 145 | // Truncate it to make it invalid. |
| 146 | data.resize(200); |
| 147 | |
| 148 | fs::path pelFilename = makeTempDir() / "rawpel"; |
| 149 | std::ofstream pelFile{pelFilename}; |
| 150 | pelFile.write(reinterpret_cast<const char*>(data.data()), data.size()); |
| 151 | pelFile.close(); |
| 152 | |
| 153 | std::string adItem = "RAWPEL=" + pelFilename.string(); |
| 154 | std::vector<std::string> additionalData{adItem}; |
| 155 | std::vector<std::string> associations; |
| 156 | |
| 157 | manager.create("error message", 42, 0, |
| 158 | phosphor::logging::Entry::Level::Error, additionalData, |
| 159 | associations); |
| 160 | |
| 161 | // Run the event loop to log the bad PEL event |
| 162 | sdeventplus::Event e{sdEvent}; |
| 163 | e.run(std::chrono::milliseconds(1)); |
| 164 | |
| 165 | PEL invalidPEL{data}; |
| 166 | EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.BadHostPEL"); |
| 167 | EXPECT_EQ(logger.errLevel, phosphor::logging::Entry::Level::Error); |
| 168 | EXPECT_EQ(std::stoi(logger.ad["PLID"], nullptr, 16), invalidPEL.plid()); |
| 169 | EXPECT_EQ(logger.ad["OBMC_LOG_ID"], "42"); |
| 170 | EXPECT_EQ(logger.ad["SRC"], (*invalidPEL.primarySRC())->asciiString()); |
| 171 | EXPECT_EQ(logger.ad["PEL_SIZE"], std::to_string(data.size())); |
| 172 | |
| 173 | fs::remove_all(pelFilename.parent_path()); |
| 174 | } |
| 175 | |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 176 | // Test that the message registry can be used to build a PEL. |
| 177 | TEST_F(ManagerTest, TestCreateWithMessageRegistry) |
| 178 | { |
| 179 | const auto registry = R"( |
| 180 | { |
| 181 | "PELs": |
| 182 | [ |
| 183 | { |
| 184 | "Name": "xyz.openbmc_project.Error.Test", |
| 185 | "Subsystem": "power_supply", |
| 186 | "ActionFlags": ["service_action", "report"], |
| 187 | "SRC": |
| 188 | { |
| 189 | "ReasonCode": "0x2030" |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 190 | }, |
| 191 | "Documentation": |
| 192 | { |
| 193 | "Description": "A PGOOD Fault", |
| 194 | "Message": "PS had a PGOOD Fault" |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | ] |
| 198 | } |
| 199 | )"; |
| 200 | |
Matt Spinler | d4ffb65 | 2019-11-12 14:16:14 -0600 | [diff] [blame] | 201 | auto path = getMessageRegistryPath(); |
| 202 | fs::create_directories(path); |
| 203 | path /= "message_registry.json"; |
| 204 | |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 205 | std::ofstream registryFile{path}; |
| 206 | registryFile << registry; |
| 207 | registryFile.close(); |
| 208 | |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 209 | std::unique_ptr<DataInterfaceBase> dataIface = |
| 210 | std::make_unique<DataInterface>(logManager.getBus()); |
| 211 | |
Matt Spinler | 05c2c6c | 2019-12-18 14:02:09 -0600 | [diff] [blame] | 212 | openpower::pels::Manager manager{ |
| 213 | logManager, std::move(dataIface), |
| 214 | std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1, |
| 215 | std::placeholders::_2, std::placeholders::_3)}; |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 216 | |
| 217 | std::vector<std::string> additionalData; |
| 218 | std::vector<std::string> associations; |
| 219 | |
| 220 | // Create the event log to create the PEL from. |
| 221 | manager.create("xyz.openbmc_project.Error.Test", 33, 0, |
| 222 | phosphor::logging::Entry::Level::Error, additionalData, |
| 223 | associations); |
| 224 | |
| 225 | // Ensure a PEL was created in the repository |
| 226 | auto pelFile = findAnyPELInRepo(); |
| 227 | ASSERT_TRUE(pelFile); |
| 228 | |
| 229 | auto data = readPELFile(*pelFile); |
| 230 | PEL pel(*data); |
| 231 | |
| 232 | // Spot check it. Other testcases cover the details. |
| 233 | EXPECT_TRUE(pel.valid()); |
| 234 | EXPECT_EQ(pel.obmcLogID(), 33); |
| 235 | EXPECT_EQ(pel.primarySRC().value()->asciiString(), |
| 236 | "BD612030 "); |
| 237 | |
| 238 | // Remove it |
| 239 | manager.erase(33); |
| 240 | pelFile = findAnyPELInRepo(); |
| 241 | EXPECT_FALSE(pelFile); |
| 242 | |
| 243 | // Create an event log that can't be found in the registry. |
| 244 | manager.create("xyz.openbmc_project.Error.Foo", 33, 0, |
| 245 | phosphor::logging::Entry::Level::Error, additionalData, |
| 246 | associations); |
| 247 | |
| 248 | // Currently, no PEL should be created. Eventually, a 'missing registry |
| 249 | // entry' PEL will be there. |
| 250 | pelFile = findAnyPELInRepo(); |
| 251 | EXPECT_FALSE(pelFile); |
| 252 | } |
Matt Spinler | a34ab72 | 2019-12-16 10:39:32 -0600 | [diff] [blame] | 253 | |
| 254 | TEST_F(ManagerTest, TestDBusMethods) |
| 255 | { |
Matt Spinler | a34ab72 | 2019-12-16 10:39:32 -0600 | [diff] [blame] | 256 | std::unique_ptr<DataInterfaceBase> dataIface = |
| 257 | std::make_unique<DataInterface>(bus); |
| 258 | |
Matt Spinler | 05c2c6c | 2019-12-18 14:02:09 -0600 | [diff] [blame] | 259 | Manager manager{logManager, std::move(dataIface), |
| 260 | std::bind(std::mem_fn(&TestLogger::log), &logger, |
| 261 | std::placeholders::_1, std::placeholders::_2, |
| 262 | std::placeholders::_3)}; |
Matt Spinler | a34ab72 | 2019-12-16 10:39:32 -0600 | [diff] [blame] | 263 | |
| 264 | // Create a PEL, write it to a file, and pass that filename into |
| 265 | // the create function so there's one in the repo. |
| 266 | auto data = pelDataFactory(TestPELType::pelSimple); |
| 267 | |
| 268 | fs::path pelFilename = makeTempDir() / "rawpel"; |
| 269 | std::ofstream pelFile{pelFilename}; |
| 270 | pelFile.write(reinterpret_cast<const char*>(data.data()), data.size()); |
| 271 | pelFile.close(); |
| 272 | |
| 273 | std::string adItem = "RAWPEL=" + pelFilename.string(); |
| 274 | std::vector<std::string> additionalData{adItem}; |
| 275 | std::vector<std::string> associations; |
| 276 | |
| 277 | manager.create("error message", 42, 0, |
| 278 | phosphor::logging::Entry::Level::Error, additionalData, |
| 279 | associations); |
| 280 | |
| 281 | // getPELFromOBMCID |
| 282 | auto newData = manager.getPELFromOBMCID(42); |
| 283 | EXPECT_EQ(newData.size(), data.size()); |
| 284 | |
| 285 | // Read the PEL to get the ID for later |
| 286 | PEL pel{newData}; |
| 287 | auto id = pel.id(); |
| 288 | |
| 289 | EXPECT_THROW( |
| 290 | manager.getPELFromOBMCID(id + 1), |
| 291 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument); |
| 292 | |
| 293 | // getPEL |
| 294 | auto unixfd = manager.getPEL(id); |
| 295 | |
| 296 | // Get the size |
| 297 | struct stat s; |
| 298 | int r = fstat(unixfd, &s); |
| 299 | ASSERT_EQ(r, 0); |
| 300 | auto size = s.st_size; |
| 301 | |
| 302 | // Open the FD and check the contents |
| 303 | FILE* fp = fdopen(unixfd, "r"); |
| 304 | ASSERT_NE(fp, nullptr); |
| 305 | |
| 306 | std::vector<uint8_t> fdData; |
| 307 | fdData.resize(size); |
| 308 | r = fread(fdData.data(), 1, size, fp); |
| 309 | EXPECT_EQ(r, size); |
| 310 | |
| 311 | EXPECT_EQ(newData, fdData); |
| 312 | |
| 313 | fclose(fp); |
| 314 | |
Matt Spinler | 05c2c6c | 2019-12-18 14:02:09 -0600 | [diff] [blame] | 315 | // Run the event loop to close the FD |
| 316 | sdeventplus::Event e{sdEvent}; |
| 317 | e.run(std::chrono::milliseconds(1)); |
| 318 | |
Matt Spinler | a34ab72 | 2019-12-16 10:39:32 -0600 | [diff] [blame] | 319 | EXPECT_THROW( |
| 320 | manager.getPEL(id + 1), |
| 321 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument); |
| 322 | |
| 323 | // hostAck |
| 324 | manager.hostAck(id); |
| 325 | |
| 326 | EXPECT_THROW( |
| 327 | manager.hostAck(id + 1), |
| 328 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument); |
| 329 | |
| 330 | // hostReject |
| 331 | manager.hostReject(id, Manager::RejectionReason::BadPEL); |
Matt Spinler | 05c2c6c | 2019-12-18 14:02:09 -0600 | [diff] [blame] | 332 | |
| 333 | // Run the event loop to log the bad PEL event |
| 334 | e.run(std::chrono::milliseconds(1)); |
| 335 | |
| 336 | EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.SentBadPELToHost"); |
| 337 | EXPECT_EQ(id, std::stoi(logger.ad["BAD_ID"], nullptr, 16)); |
| 338 | |
Matt Spinler | a34ab72 | 2019-12-16 10:39:32 -0600 | [diff] [blame] | 339 | manager.hostReject(id, Manager::RejectionReason::HostFull); |
| 340 | |
| 341 | EXPECT_THROW( |
| 342 | manager.hostReject(id + 1, Manager::RejectionReason::BadPEL), |
| 343 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument); |
| 344 | |
| 345 | fs::remove_all(pelFilename.parent_path()); |
| 346 | } |