blob: 2247a7162806c97f7222a8d710dfb9012ab09b41 [file] [log] [blame]
Matt Spinler97f7abc2019-11-06 09:40:23 -06001/**
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 Spinler89fa0822019-07-17 13:54:30 -050016#include "extensions/openpower-pels/manager.hpp"
17#include "log_manager.hpp"
Matt Spinlere6b48f12020-04-02 09:51:39 -050018#include "mocks.hpp"
Matt Spinler89fa0822019-07-17 13:54:30 -050019#include "pel_utils.hpp"
20
21#include <fstream>
22#include <regex>
Matt Spinlere6b48f12020-04-02 09:51:39 -050023#include <sdbusplus/test/sdbus_mock.hpp>
Matt Spinlera34ab722019-12-16 10:39:32 -060024#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinler89fa0822019-07-17 13:54:30 -050025
26#include <gtest/gtest.h>
27
28using namespace openpower::pels;
29namespace fs = std::filesystem;
30
Matt Spinlere6b48f12020-04-02 09:51:39 -050031using ::testing::NiceMock;
Matt Spinler3dd17e92020-08-05 15:04:27 -050032using ::testing::Return;
33using ::testing::ReturnRef;
Matt Spinlere6b48f12020-04-02 09:51:39 -050034
Matt Spinler05c2c6c2019-12-18 14:02:09 -060035class TestLogger
36{
37 public:
38 void log(const std::string& name, phosphor::logging::Entry::Level level,
39 const EventLogger::ADMap& additionalData)
40 {
41 errName = name;
42 errLevel = level;
43 ad = additionalData;
44 }
45
46 std::string errName;
47 phosphor::logging::Entry::Level errLevel;
48 EventLogger::ADMap ad;
49};
50
Matt Spinler89fa0822019-07-17 13:54:30 -050051class ManagerTest : public CleanPELFiles
52{
Matt Spinler6b1a5c82020-01-07 08:48:53 -060053 public:
Matt Spinlere6b48f12020-04-02 09:51:39 -050054 ManagerTest() :
55 bus(sdbusplus::get_mocked_new(&sdbusInterface)),
56 logManager(bus, "logging_path")
Matt Spinler6b1a5c82020-01-07 08:48:53 -060057 {
58 sd_event_default(&sdEvent);
Matt Spinler6b1a5c82020-01-07 08:48:53 -060059 }
60
61 ~ManagerTest()
62 {
63 sd_event_unref(sdEvent);
64 }
65
Matt Spinlere6b48f12020-04-02 09:51:39 -050066 NiceMock<sdbusplus::SdBusMock> sdbusInterface;
67 sdbusplus::bus::bus bus;
Matt Spinler6b1a5c82020-01-07 08:48:53 -060068 phosphor::logging::internal::Manager logManager;
69 sd_event* sdEvent;
Matt Spinler05c2c6c2019-12-18 14:02:09 -060070 TestLogger logger;
Matt Spinler89fa0822019-07-17 13:54:30 -050071};
72
73fs::path makeTempDir()
74{
75 char path[] = "/tmp/tempnameXXXXXX";
76 std::filesystem::path dir = mkdtemp(path);
77 return dir;
78}
79
Matt Spinler67456c22019-10-21 12:22:49 -050080std::optional<fs::path> findAnyPELInRepo()
81{
82 // PELs are named <timestamp>_<ID>
83 std::regex expr{"\\d+_\\d+"};
84
85 for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs"))
86 {
87 if (std::regex_search(f.path().string(), expr))
88 {
89 return f.path();
90 }
91 }
92 return std::nullopt;
93}
94
Matt Spinler7e727a32020-07-07 15:00:17 -050095size_t countPELsInRepo()
96{
97 size_t count = 0;
98 std::regex expr{"\\d+_\\d+"};
99
100 for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs"))
101 {
102 if (std::regex_search(f.path().string(), expr))
103 {
104 count++;
105 }
106 }
107 return count;
108}
109
Matt Spinlerff9cec22020-07-15 13:06:35 -0500110void deletePELFile(uint32_t id)
111{
112 char search[20];
113
114 sprintf(search, "\\d+_%.8X", id);
115 std::regex expr{search};
116
117 for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs"))
118 {
119 if (std::regex_search(f.path().string(), expr))
120 {
121 fs::remove(f.path());
122 break;
123 }
124 }
125}
126
Matt Spinler89fa0822019-07-17 13:54:30 -0500127// Test that using the RAWPEL=<file> with the Manager::create() call gets
128// a PEL saved in the repository.
129TEST_F(ManagerTest, TestCreateWithPEL)
130{
Matt Spinlerc8705e22019-09-11 12:36:07 -0500131 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500132 std::make_unique<MockDataInterface>();
Matt Spinler89fa0822019-07-17 13:54:30 -0500133
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600134 openpower::pels::Manager manager{
135 logManager, std::move(dataIface),
136 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
137 std::placeholders::_2, std::placeholders::_3)};
Matt Spinler89fa0822019-07-17 13:54:30 -0500138
139 // Create a PEL, write it to a file, and pass that filename into
140 // the create function.
Matt Spinler42828bd2019-10-11 10:39:30 -0500141 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler89fa0822019-07-17 13:54:30 -0500142
143 fs::path pelFilename = makeTempDir() / "rawpel";
144 std::ofstream pelFile{pelFilename};
Matt Spinler42828bd2019-10-11 10:39:30 -0500145 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
Matt Spinler89fa0822019-07-17 13:54:30 -0500146 pelFile.close();
147
148 std::string adItem = "RAWPEL=" + pelFilename.string();
149 std::vector<std::string> additionalData{adItem};
150 std::vector<std::string> associations;
151
Matt Spinler367144c2019-09-19 15:33:52 -0500152 manager.create("error message", 42, 0,
153 phosphor::logging::Entry::Level::Error, additionalData,
Matt Spinler89fa0822019-07-17 13:54:30 -0500154 associations);
155
Matt Spinler67456c22019-10-21 12:22:49 -0500156 // Find the file in the PEL repository directory
157 auto pelPathInRepo = findAnyPELInRepo();
Matt Spinler89fa0822019-07-17 13:54:30 -0500158
Matt Spinler67456c22019-10-21 12:22:49 -0500159 EXPECT_TRUE(pelPathInRepo);
Matt Spinler89fa0822019-07-17 13:54:30 -0500160
Matt Spinler475e5742019-07-18 16:09:49 -0500161 // Now remove it based on its OpenBMC event log ID
162 manager.erase(42);
163
Matt Spinler67456c22019-10-21 12:22:49 -0500164 pelPathInRepo = findAnyPELInRepo();
Matt Spinler475e5742019-07-18 16:09:49 -0500165
Matt Spinler67456c22019-10-21 12:22:49 -0500166 EXPECT_FALSE(pelPathInRepo);
Matt Spinler475e5742019-07-18 16:09:49 -0500167
Matt Spinler89fa0822019-07-17 13:54:30 -0500168 fs::remove_all(pelFilename.parent_path());
169}
Matt Spinler67456c22019-10-21 12:22:49 -0500170
Matt Spinlere95fd012020-01-07 12:53:16 -0600171TEST_F(ManagerTest, TestCreateWithInvalidPEL)
172{
173 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500174 std::make_unique<MockDataInterface>();
Matt Spinlere95fd012020-01-07 12:53:16 -0600175
176 openpower::pels::Manager manager{
177 logManager, std::move(dataIface),
178 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
179 std::placeholders::_2, std::placeholders::_3)};
180
181 // Create a PEL, write it to a file, and pass that filename into
182 // the create function.
183 auto data = pelDataFactory(TestPELType::pelSimple);
184
185 // Truncate it to make it invalid.
186 data.resize(200);
187
188 fs::path pelFilename = makeTempDir() / "rawpel";
189 std::ofstream pelFile{pelFilename};
190 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
191 pelFile.close();
192
193 std::string adItem = "RAWPEL=" + pelFilename.string();
194 std::vector<std::string> additionalData{adItem};
195 std::vector<std::string> associations;
196
197 manager.create("error message", 42, 0,
198 phosphor::logging::Entry::Level::Error, additionalData,
199 associations);
200
201 // Run the event loop to log the bad PEL event
202 sdeventplus::Event e{sdEvent};
203 e.run(std::chrono::milliseconds(1));
204
205 PEL invalidPEL{data};
206 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.BadHostPEL");
207 EXPECT_EQ(logger.errLevel, phosphor::logging::Entry::Level::Error);
208 EXPECT_EQ(std::stoi(logger.ad["PLID"], nullptr, 16), invalidPEL.plid());
209 EXPECT_EQ(logger.ad["OBMC_LOG_ID"], "42");
210 EXPECT_EQ(logger.ad["SRC"], (*invalidPEL.primarySRC())->asciiString());
211 EXPECT_EQ(logger.ad["PEL_SIZE"], std::to_string(data.size()));
212
Matt Spinlerfe721892020-04-02 10:28:08 -0500213 // Check that the bad PEL data was saved to a file.
214 auto badPELData = readPELFile(getPELRepoPath() / "badPEL");
215 EXPECT_EQ(*badPELData, data);
216
Matt Spinlere95fd012020-01-07 12:53:16 -0600217 fs::remove_all(pelFilename.parent_path());
218}
219
Matt Spinler67456c22019-10-21 12:22:49 -0500220// Test that the message registry can be used to build a PEL.
221TEST_F(ManagerTest, TestCreateWithMessageRegistry)
222{
223 const auto registry = R"(
224{
225 "PELs":
226 [
227 {
228 "Name": "xyz.openbmc_project.Error.Test",
229 "Subsystem": "power_supply",
230 "ActionFlags": ["service_action", "report"],
231 "SRC":
232 {
233 "ReasonCode": "0x2030"
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800234 },
235 "Documentation":
236 {
237 "Description": "A PGOOD Fault",
238 "Message": "PS had a PGOOD Fault"
Matt Spinler67456c22019-10-21 12:22:49 -0500239 }
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500240 },
241 {
242 "Name": "xyz.openbmc_project.Logging.Error.Default",
243 "Subsystem": "bmc_firmware",
244 "SRC":
245 {
246 "ReasonCode": "0x2031"
247 },
248 "Documentation":
249 {
250 "Description": "The entry used when no match found",
251 "Message": "This is a generic SRC"
252 }
Matt Spinler67456c22019-10-21 12:22:49 -0500253 }
254 ]
255}
256)";
257
Matt Spinler0d804ef2020-05-12 16:16:26 -0500258 auto path = getPELReadOnlyDataPath();
Matt Spinlerd4ffb652019-11-12 14:16:14 -0600259 fs::create_directories(path);
260 path /= "message_registry.json";
261
Matt Spinler67456c22019-10-21 12:22:49 -0500262 std::ofstream registryFile{path};
263 registryFile << registry;
264 registryFile.close();
265
Matt Spinler67456c22019-10-21 12:22:49 -0500266 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500267 std::make_unique<MockDataInterface>();
Matt Spinler67456c22019-10-21 12:22:49 -0500268
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600269 openpower::pels::Manager manager{
270 logManager, std::move(dataIface),
271 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
272 std::placeholders::_2, std::placeholders::_3)};
Matt Spinler67456c22019-10-21 12:22:49 -0500273
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500274 std::vector<std::string> additionalData{"FOO=BAR"};
Matt Spinler67456c22019-10-21 12:22:49 -0500275 std::vector<std::string> associations;
276
277 // Create the event log to create the PEL from.
278 manager.create("xyz.openbmc_project.Error.Test", 33, 0,
279 phosphor::logging::Entry::Level::Error, additionalData,
280 associations);
281
282 // Ensure a PEL was created in the repository
283 auto pelFile = findAnyPELInRepo();
284 ASSERT_TRUE(pelFile);
285
286 auto data = readPELFile(*pelFile);
287 PEL pel(*data);
288
289 // Spot check it. Other testcases cover the details.
290 EXPECT_TRUE(pel.valid());
291 EXPECT_EQ(pel.obmcLogID(), 33);
292 EXPECT_EQ(pel.primarySRC().value()->asciiString(),
293 "BD612030 ");
294
295 // Remove it
296 manager.erase(33);
297 pelFile = findAnyPELInRepo();
298 EXPECT_FALSE(pelFile);
299
300 // Create an event log that can't be found in the registry.
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500301 // In this case, xyz.openbmc_project.Logging.Error.Default will
302 // be used as the key instead to find a registry match.
303 manager.create("xyz.openbmc_project.Error.Foo", 42, 0,
Matt Spinler67456c22019-10-21 12:22:49 -0500304 phosphor::logging::Entry::Level::Error, additionalData,
305 associations);
306
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500307 // Ensure a PEL was still created in the repository
Matt Spinler67456c22019-10-21 12:22:49 -0500308 pelFile = findAnyPELInRepo();
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500309 ASSERT_TRUE(pelFile);
310
311 data = readPELFile(*pelFile);
312 PEL newPEL(*data);
313
314 EXPECT_TRUE(newPEL.valid());
315 EXPECT_EQ(newPEL.obmcLogID(), 42);
316 EXPECT_EQ(newPEL.primarySRC().value()->asciiString(),
317 "BD8D2031 ");
318
319 // Check for both the original AdditionalData item as well as
320 // the ERROR_NAME item that should contain the error message
321 // property that wasn't found.
322 std::string errorName;
323 std::string adItem;
324
325 for (const auto& section : newPEL.optionalSections())
326 {
327 if (SectionID::userData == static_cast<SectionID>(section->header().id))
328 {
329 if (UserDataFormat::json ==
330 static_cast<UserDataFormat>(section->header().subType))
331 {
332 auto ud = static_cast<UserData*>(section.get());
333
334 // Check that there was a UserData section added that
335 // contains debug details about the device.
336 const auto& d = ud->data();
337 std::string jsonString{d.begin(), d.end()};
338 auto json = nlohmann::json::parse(jsonString);
339
340 if (json.contains("ERROR_NAME"))
341 {
342 errorName = json["ERROR_NAME"].get<std::string>();
343 }
344
345 if (json.contains("FOO"))
346 {
347 adItem = json["FOO"].get<std::string>();
348 }
349 }
350 }
351 if (!errorName.empty())
352 {
353 break;
354 }
355 }
356
357 EXPECT_EQ(errorName, "xyz.openbmc_project.Error.Foo");
358 EXPECT_EQ(adItem, "BAR");
Matt Spinler67456c22019-10-21 12:22:49 -0500359}
Matt Spinlera34ab722019-12-16 10:39:32 -0600360
361TEST_F(ManagerTest, TestDBusMethods)
362{
Matt Spinlera34ab722019-12-16 10:39:32 -0600363 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500364 std::make_unique<MockDataInterface>();
Matt Spinlera34ab722019-12-16 10:39:32 -0600365
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600366 Manager manager{logManager, std::move(dataIface),
367 std::bind(std::mem_fn(&TestLogger::log), &logger,
368 std::placeholders::_1, std::placeholders::_2,
369 std::placeholders::_3)};
Matt Spinlera34ab722019-12-16 10:39:32 -0600370
371 // Create a PEL, write it to a file, and pass that filename into
372 // the create function so there's one in the repo.
373 auto data = pelDataFactory(TestPELType::pelSimple);
374
375 fs::path pelFilename = makeTempDir() / "rawpel";
376 std::ofstream pelFile{pelFilename};
377 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
378 pelFile.close();
379
380 std::string adItem = "RAWPEL=" + pelFilename.string();
381 std::vector<std::string> additionalData{adItem};
382 std::vector<std::string> associations;
383
384 manager.create("error message", 42, 0,
385 phosphor::logging::Entry::Level::Error, additionalData,
386 associations);
387
388 // getPELFromOBMCID
389 auto newData = manager.getPELFromOBMCID(42);
390 EXPECT_EQ(newData.size(), data.size());
391
392 // Read the PEL to get the ID for later
393 PEL pel{newData};
394 auto id = pel.id();
395
396 EXPECT_THROW(
397 manager.getPELFromOBMCID(id + 1),
398 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
399
400 // getPEL
401 auto unixfd = manager.getPEL(id);
402
403 // Get the size
404 struct stat s;
405 int r = fstat(unixfd, &s);
406 ASSERT_EQ(r, 0);
407 auto size = s.st_size;
408
409 // Open the FD and check the contents
410 FILE* fp = fdopen(unixfd, "r");
411 ASSERT_NE(fp, nullptr);
412
413 std::vector<uint8_t> fdData;
414 fdData.resize(size);
415 r = fread(fdData.data(), 1, size, fp);
416 EXPECT_EQ(r, size);
417
418 EXPECT_EQ(newData, fdData);
419
420 fclose(fp);
421
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600422 // Run the event loop to close the FD
423 sdeventplus::Event e{sdEvent};
424 e.run(std::chrono::milliseconds(1));
425
Matt Spinlera34ab722019-12-16 10:39:32 -0600426 EXPECT_THROW(
427 manager.getPEL(id + 1),
428 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
429
430 // hostAck
431 manager.hostAck(id);
432
433 EXPECT_THROW(
434 manager.hostAck(id + 1),
435 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
436
437 // hostReject
438 manager.hostReject(id, Manager::RejectionReason::BadPEL);
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600439
440 // Run the event loop to log the bad PEL event
441 e.run(std::chrono::milliseconds(1));
442
443 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.SentBadPELToHost");
444 EXPECT_EQ(id, std::stoi(logger.ad["BAD_ID"], nullptr, 16));
445
Matt Spinlera34ab722019-12-16 10:39:32 -0600446 manager.hostReject(id, Manager::RejectionReason::HostFull);
447
448 EXPECT_THROW(
449 manager.hostReject(id + 1, Manager::RejectionReason::BadPEL),
450 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
451
452 fs::remove_all(pelFilename.parent_path());
453}
Matt Spinler19e72902020-01-24 11:05:20 -0600454
455// An ESEL from the wild
456const std::string esel{
457 "00 00 df 00 00 00 00 20 00 04 12 01 6f aa 00 00 "
458 "50 48 00 30 01 00 33 00 00 00 00 07 5c 69 cc 0d 00 00 00 07 5c d5 50 db "
459 "42 00 00 10 00 00 00 00 00 00 00 00 00 00 00 00 90 00 00 4e 90 00 00 4e "
460 "55 48 00 18 01 00 09 00 8a 03 40 00 00 00 00 00 ff ff 00 00 00 00 00 00 "
461 "50 53 00 50 01 01 00 00 02 00 00 09 33 2d 00 48 00 00 00 e0 00 00 10 00 "
462 "00 00 00 00 00 20 00 00 00 0c 00 02 00 00 00 fa 00 00 0c e4 00 00 00 12 "
463 "42 43 38 41 33 33 32 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
464 "20 20 20 20 20 20 20 20 55 44 00 1c 01 06 01 00 02 54 41 4b 00 00 00 06 "
465 "00 00 00 55 00 01 f9 20 00 00 00 00 55 44 00 24 01 06 01 00 01 54 41 4b "
466 "00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 00 23 01 00 02 00 05 00 00 "
467 "55 44 00 0c 01 0b 01 00 0f 01 00 00 55 44 00 10 01 04 01 00 0f 9f de 6a "
468 "00 01 00 00 55 44 00 7c 00 0c 01 00 00 13 0c 02 00 fa 0c e4 16 00 01 2c "
469 "0c 1c 16 00 00 fa 0a f0 14 00 00 fa 0b b8 14 00 00 be 09 60 12 00 01 2c "
470 "0d 7a 12 00 00 fa 0c 4e 10 00 00 fa 0c e4 10 00 00 be 0a 8c 16 00 01 2c "
471 "0c 1c 16 00 01 09 09 f6 16 00 00 fa 09 f6 14 00 00 fa 0b b8 14 00 00 fa "
472 "0a f0 14 00 00 be 08 ca 12 00 01 2c 0c e4 12 00 00 fa 0b 54 10 00 00 fa "
473 "0c 2d 10 00 00 be 08 ca 55 44 00 58 01 03 01 00 00 00 00 00 00 05 31 64 "
474 "00 00 00 00 00 05 0d d4 00 00 00 00 40 5f 06 e0 00 00 00 00 40 5d d2 00 "
475 "00 00 00 00 40 57 d3 d0 00 00 00 00 40 58 f6 a0 00 00 00 00 40 54 c9 34 "
476 "00 00 00 00 40 55 9a 10 00 00 00 00 40 4c 0a 80 00 00 00 00 00 00 27 14 "
477 "55 44 01 84 01 01 01 00 48 6f 73 74 62 6f 6f 74 20 42 75 69 6c 64 20 49 "
478 "44 3a 20 68 6f 73 74 62 6f 6f 74 2d 66 65 63 37 34 64 66 2d 70 30 61 38 "
479 "37 64 63 34 2f 68 62 69 63 6f 72 65 2e 62 69 6e 00 49 42 4d 2d 77 69 74 "
480 "68 65 72 73 70 6f 6f 6e 2d 4f 50 39 2d 76 32 2e 34 2d 39 2e 32 33 34 0a "
481 "09 6f 70 2d 62 75 69 6c 64 2d 38 32 66 34 63 66 30 0a 09 62 75 69 6c 64 "
482 "72 6f 6f 74 2d 32 30 31 39 2e 30 35 2e 32 2d 31 30 2d 67 38 39 35 39 31 "
483 "31 34 0a 09 73 6b 69 62 6f 6f 74 2d 76 36 2e 35 2d 31 38 2d 67 34 37 30 "
484 "66 66 62 35 66 32 39 64 37 0a 09 68 6f 73 74 62 6f 6f 74 2d 66 65 63 37 "
485 "34 64 66 2d 70 30 61 38 37 64 63 34 0a 09 6f 63 63 2d 65 34 35 39 37 61 "
486 "62 0a 09 6c 69 6e 75 78 2d 35 2e 32 2e 31 37 2d 6f 70 65 6e 70 6f 77 65 "
487 "72 31 2d 70 64 64 63 63 30 33 33 0a 09 70 65 74 69 74 62 6f 6f 74 2d 76 "
488 "31 2e 31 30 2e 34 0a 09 6d 61 63 68 69 6e 65 2d 78 6d 6c 2d 63 36 32 32 "
489 "63 62 35 2d 70 37 65 63 61 62 33 64 0a 09 68 6f 73 74 62 6f 6f 74 2d 62 "
490 "69 6e 61 72 69 65 73 2d 36 36 65 39 61 36 30 0a 09 63 61 70 70 2d 75 63 "
491 "6f 64 65 2d 70 39 2d 64 64 32 2d 76 34 0a 09 73 62 65 2d 36 30 33 33 30 "
492 "65 30 0a 09 68 63 6f 64 65 2d 68 77 30 39 32 31 31 39 61 2e 6f 70 6d 73 "
493 "74 0a 00 00 55 44 00 70 01 04 01 00 0f 9f de 6a 00 05 00 00 07 5f 1d f4 "
494 "30 32 43 59 34 37 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
495 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
496 "0b ac 54 02 59 41 31 39 33 34 36 39 37 30 35 38 00 00 00 00 00 00 05 22 "
497 "a1 58 01 8a 00 58 40 20 17 18 4d 2c 00 00 00 fc 01 a1 00 00 55 44 00 14 "
498 "01 08 01 00 00 00 00 01 00 00 00 5a 00 00 00 05 55 44 03 fc 01 15 31 00 "
499 "01 28 00 42 46 41 50 49 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 f4 "
500 "00 00 00 00 00 00 03 f4 00 00 00 0b 00 00 00 00 00 00 00 3d 2c 9b c2 84 "
501 "00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 09 "
502 "00 00 00 00 00 11 bd 20 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 "
503 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 00 00 00 00 00 00 01 2c "
504 "00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c 1c 00 00 00 64 00 00 00 3d "
505 "2c 9b d1 11 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 "
506 "00 00 00 0a 00 00 00 00 00 13 b5 a0 00 00 00 00 00 01 f8 80 00 00 00 00 "
507 "00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 00 00 00 00 "
508 "00 00 00 be 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0a 8c 00 00 00 64 "
509 "00 00 00 3d 2c 9b df 98 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 "
510 "00 00 00 00 00 00 00 0b 00 00 00 00 00 15 ae 20 00 00 00 00 00 01 f8 80 "
511 "00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 "
512 "00 00 00 00 00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c e4 "
513 "00 00 00 64 00 00 00 3d 2c 9b ea b7 00 00 01 e4 00 48 43 4f fb ed 70 b1 "
514 "00 00 02 01 00 00 00 00 00 00 00 0c 00 00 00 00 00 17 a6 a0 00 00 00 00 "
515 "00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 "
516 "00 00 00 12 00 00 00 00 00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 "
517 "00 00 0c 4e 00 00 00 64 00 00 00 3d 2c 9b f6 27 00 00 01 e4 00 48 43 4f "
518 "fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0d 00 00 00 00 00 19 9f 20 "
519 "00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 "
520 "00 00 00 00 00 00 00 12 00 00 00 00 00 00 01 2c 00 00 00 00 00 00 07 d0 "
521 "00 00 00 00 00 00 0d 7a 00 00 00 64 00 00 00 3d 2c 9c 05 75 00 00 01 e4 "
522 "00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0e 00 00 00 00 "
523 "00 1b 97 a0 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 "
524 "00 00 00 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 be 00 00 00 00 "
525 "00 00 07 d0 00 00 00 00 00 00 09 60 00 00 00 64 00 00 00 3d 2c 9c 11 29 "
526 "00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0f "
527 "00 00 00 00 00 1d 90 20 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 "
528 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 fa "
529 "00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0b b8 00 00 00 64 00 00 00 3d "
530 "2c 9c 1c 45 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 "
531 "00 00 00 10 00 00 00 00 00 1f 88 a0 00 00 00 00 00 01 f8 80 00 00 00 00 "
532 "00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 00 00 00 00 "
533 "00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0a f0 00 00 00 64 "
534 "00 00 00 3d 2c 9c 2b 14 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 "
535 "00 00 00 00 00 00 00 11 00 00 00 00 00 21 81 20 00 00 00 00 00 01 f8 80 "
536 "00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 "
537 "00 00 00 00 00 00 01 2c 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c 1c "
538 "00 00 00 64 00 00 00 3d 2d 6d 8f 9e 00 00 01 e4 00 00 43 4f 52 d7 9c 36 "
539 "00 00 04 73 00 00 00 1c 00 00 00 3d 2d 6d 99 ac 00 00 01 e4 00 10 43 4f "
540 "3f f2 02 3d 00 00 05 58 00 00 00 00 02 00 00 01 00 00 00 00 00 00 00 40 "
541 "00 00 00 2c 55 44 00 30 01 15 31 00 01 28 00 42 46 41 50 49 5f 44 42 47 "
542 "00 00 00 00 00 00 00 00 00 00 00 28 00 00 00 00 00 00 00 28 00 00 00 00 "
543 "00 00 00 00 55 44 01 74 01 15 31 00 01 28 00 42 46 41 50 49 5f 49 00 00 "
544 "00 00 00 00 00 00 00 00 00 00 01 6c 00 00 00 00 00 00 01 6c 00 00 00 0b "
545 "00 00 00 00 00 00 00 3c 0d 52 18 5e 00 00 01 e4 00 08 43 4f 46 79 94 13 "
546 "00 00 0a 5b 00 00 00 00 00 00 2c 00 00 00 00 24 00 00 00 3c 0d 6b 26 6c "
547 "00 00 01 e4 00 00 43 4f 4e 9b 18 74 00 00 01 03 00 00 00 1c 00 00 00 3c "
548 "12 b9 2d 13 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 00 00 00 1c "
549 "00 00 00 3c 13 02 73 53 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 "
550 "00 00 00 1c 00 00 00 3c 13 04 7c 94 00 00 01 e4 00 00 43 4f ea 31 ed d4 "
551 "00 00 05 c4 00 00 00 1c 00 00 00 3c 13 06 ad e1 00 00 01 e4 00 00 43 4f "
552 "ea 31 ed d4 00 00 05 c4 00 00 00 1c 00 00 00 3c 13 07 3f 77 00 00 01 e4 "
553 "00 00 43 4f 5e 4a 55 32 00 00 10 f2 00 00 00 1c 00 00 00 3c 13 07 4e e4 "
554 "00 00 01 e4 00 00 43 4f 5e 4a 55 32 00 00 0d 68 00 00 00 1c 00 00 00 3c "
555 "13 36 79 18 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 00 00 00 1c "
556 "00 00 00 3d 2c 9c 36 70 00 00 01 e4 00 00 43 4f 23 45 90 97 00 00 02 47 "
557 "00 00 00 1c 00 00 00 3d 2d 6d a3 ed 00 00 01 e4 00 08 43 4f 74 3a 5b 1a "
558 "00 00 04 cc 00 00 00 00 02 00 00 01 00 00 00 24 55 44 00 30 01 15 31 00 "
559 "01 28 00 42 53 43 41 4e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 28 "
560 "00 00 00 00 00 00 00 28 00 00 00 00 00 00 00 00"};
561
562TEST_F(ManagerTest, TestESELToRawData)
563{
564 auto data = Manager::eselToRawData(esel);
565
566 EXPECT_EQ(data.size(), 2464);
567
568 PEL pel{data};
569 EXPECT_TRUE(pel.valid());
570}
571
572TEST_F(ManagerTest, TestCreateWithESEL)
573{
574 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500575 std::make_unique<MockDataInterface>();
Matt Spinler19e72902020-01-24 11:05:20 -0600576
577 openpower::pels::Manager manager{
578 logManager, std::move(dataIface),
579 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
580 std::placeholders::_2, std::placeholders::_3)};
581
582 {
583 std::string adItem = "ESEL=" + esel;
584 std::vector<std::string> additionalData{adItem};
585 std::vector<std::string> associations;
586
587 manager.create("error message", 37, 0,
588 phosphor::logging::Entry::Level::Error, additionalData,
589 associations);
590
591 auto data = manager.getPELFromOBMCID(37);
592 PEL pel{data};
593 EXPECT_TRUE(pel.valid());
594 }
595
596 // Now an invalid one
597 {
598 std::string adItem = "ESEL=" + esel;
599
600 // Crop it
601 adItem.resize(adItem.size() - 300);
602
603 std::vector<std::string> additionalData{adItem};
604 std::vector<std::string> associations;
605
606 manager.create("error message", 38, 0,
607 phosphor::logging::Entry::Level::Error, additionalData,
608 associations);
609
610 EXPECT_THROW(
611 manager.getPELFromOBMCID(38),
612 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
613
614 // Run the event loop to log the bad PEL event
615 sdeventplus::Event e{sdEvent};
616 e.run(std::chrono::milliseconds(1));
617
618 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.BadHostPEL");
619 EXPECT_EQ(logger.errLevel, phosphor::logging::Entry::Level::Error);
620 }
621}
Matt Spinler7e727a32020-07-07 15:00:17 -0500622
623// Test that PELs will be pruned when necessary
624TEST_F(ManagerTest, TestPruning)
625{
626 sdeventplus::Event e{sdEvent};
627
628 std::unique_ptr<DataInterfaceBase> dataIface =
629 std::make_unique<MockDataInterface>();
630
631 openpower::pels::Manager manager{
632 logManager, std::move(dataIface),
633 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
634 std::placeholders::_2, std::placeholders::_3)};
635
636 // Create 25 1000B (4096B on disk each, which is what is used for pruning)
637 // BMC non-informational PELs in the 100KB repository. After the 24th one,
638 // the repo will be 96% full and a prune should be triggered to remove all
639 // but 7 to get under 30% full. Then when the 25th is added there will be
640 // 8 left.
641
642 auto dir = makeTempDir();
643 for (int i = 1; i <= 25; i++)
644 {
645 auto data = pelFactory(42, 'O', 0x40, 0x8800, 1000);
646
647 fs::path pelFilename = dir / "rawpel";
648 std::ofstream pelFile{pelFilename};
649 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
650 pelFile.close();
651
652 std::string adItem = "RAWPEL=" + pelFilename.string();
653 std::vector<std::string> additionalData{adItem};
654 std::vector<std::string> associations;
655
656 manager.create("error message", 42, 0,
657 phosphor::logging::Entry::Level::Error, additionalData,
658 associations);
659
660 // Simulate the code getting back to the event loop
661 // after each create.
662 e.run(std::chrono::milliseconds(1));
663
664 if (i < 24)
665 {
666 EXPECT_EQ(countPELsInRepo(), i);
667 }
668 else if (i == 24)
669 {
670 // Prune occured
671 EXPECT_EQ(countPELsInRepo(), 7);
672 }
673 else // i == 25
674 {
675 EXPECT_EQ(countPELsInRepo(), 8);
676 }
677 }
678
679 try
680 {
681 // Make sure the 8 newest ones are still found.
682 for (uint32_t i = 0; i < 8; i++)
683 {
684 manager.getPEL(0x50000012 + i);
685 }
686 }
687 catch (sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument& e)
688 {
689 ADD_FAILURE() << "PELs should have all been found";
690 }
691
692 fs::remove_all(dir);
693}
Matt Spinlerff9cec22020-07-15 13:06:35 -0500694
695// Test that manually deleting a PEL file will be recognized by the code.
696TEST_F(ManagerTest, TestPELManualDelete)
697{
698 sdeventplus::Event e{sdEvent};
699
700 std::unique_ptr<DataInterfaceBase> dataIface =
701 std::make_unique<MockDataInterface>();
702
703 openpower::pels::Manager manager{
704 logManager, std::move(dataIface),
705 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
706 std::placeholders::_2, std::placeholders::_3)};
707
708 auto data = pelDataFactory(TestPELType::pelSimple);
709 auto dir = makeTempDir();
710 fs::path pelFilename = dir / "rawpel";
711
712 std::string adItem = "RAWPEL=" + pelFilename.string();
713 std::vector<std::string> additionalData{adItem};
714 std::vector<std::string> associations;
715
716 // Add 20 PELs, they will get incrementing IDs like
717 // 0x50000001, 0x50000002, etc.
718 for (int i = 1; i <= 20; i++)
719 {
720 std::ofstream pelFile{pelFilename};
721 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
722 pelFile.close();
723
724 manager.create("error message", 42, 0,
725 phosphor::logging::Entry::Level::Error, additionalData,
726 associations);
727
728 // Sanity check this ID is really there so we can test
729 // it was deleted later. This will throw an exception if
730 // not present.
731 manager.getPEL(0x50000000 + i);
732
733 // Run an event loop pass where the internal FD is deleted
734 // after the getPEL function call.
735 e.run(std::chrono::milliseconds(1));
736 }
737
738 EXPECT_EQ(countPELsInRepo(), 20);
739
740 deletePELFile(0x50000001);
741
742 // Run a single event loop pass so the inotify event can run
743 e.run(std::chrono::milliseconds(1));
744
745 EXPECT_EQ(countPELsInRepo(), 19);
746
747 EXPECT_THROW(
748 manager.getPEL(0x50000001),
749 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
750
751 // Delete a few more, they should all get handled in the same
752 // event loop pass
753 std::vector<uint32_t> toDelete{0x50000002, 0x50000003, 0x50000004,
754 0x50000005, 0x50000006};
755 std::for_each(toDelete.begin(), toDelete.end(),
756 [](auto i) { deletePELFile(i); });
757
758 e.run(std::chrono::milliseconds(1));
759
760 EXPECT_EQ(countPELsInRepo(), 14);
761
762 std::for_each(toDelete.begin(), toDelete.end(), [&manager](const auto i) {
763 EXPECT_THROW(
764 manager.getPEL(i),
765 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
766 });
767
768 fs::remove_all(dir);
769}
770
771// Test that deleting all PELs at once is handled OK.
772TEST_F(ManagerTest, TestPELManualDeleteAll)
773{
774 sdeventplus::Event e{sdEvent};
775
776 std::unique_ptr<DataInterfaceBase> dataIface =
777 std::make_unique<MockDataInterface>();
778
779 openpower::pels::Manager manager{
780 logManager, std::move(dataIface),
781 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
782 std::placeholders::_2, std::placeholders::_3)};
783
784 auto data = pelDataFactory(TestPELType::pelSimple);
785 auto dir = makeTempDir();
786 fs::path pelFilename = dir / "rawpel";
787
788 std::string adItem = "RAWPEL=" + pelFilename.string();
789 std::vector<std::string> additionalData{adItem};
790 std::vector<std::string> associations;
791
792 // Add 200 PELs, they will get incrementing IDs like
793 // 0x50000001, 0x50000002, etc.
794 for (int i = 1; i <= 200; i++)
795 {
796 std::ofstream pelFile{pelFilename};
797 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
798 pelFile.close();
799
800 manager.create("error message", 42, 0,
801 phosphor::logging::Entry::Level::Error, additionalData,
802 associations);
803
804 // Sanity check this ID is really there so we can test
805 // it was deleted later. This will throw an exception if
806 // not present.
807 manager.getPEL(0x50000000 + i);
808
809 // Run an event loop pass where the internal FD is deleted
810 // after the getPEL function call.
811 e.run(std::chrono::milliseconds(1));
812 }
813
814 // Delete them all at once
815 auto logPath = getPELRepoPath() / "logs";
816 std::string cmd = "rm " + logPath.string() + "/*";
817 system(cmd.c_str());
818
819 EXPECT_EQ(countPELsInRepo(), 0);
820
821 // It will take 5 event loop passes to process them all
822 for (int i = 0; i < 5; i++)
823 {
824 e.run(std::chrono::milliseconds(1));
825 }
826
827 for (int i = 1; i <= 200; i++)
828 {
829 EXPECT_THROW(
830 manager.getPEL(0x50000000 + i),
831 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
832 }
833
834 fs::remove_all(dir);
835}
Matt Spinler3dd17e92020-08-05 15:04:27 -0500836
837// Test that fault LEDs are turned on when PELs are created
838TEST_F(ManagerTest, TestServiceIndicators)
839{
840 std::unique_ptr<DataInterfaceBase> dataIface =
841 std::make_unique<MockDataInterface>();
842
843 MockDataInterface* mockIface =
844 reinterpret_cast<MockDataInterface*>(dataIface.get());
845
846 openpower::pels::Manager manager{
847 logManager, std::move(dataIface),
848 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
849 std::placeholders::_2, std::placeholders::_3)};
850
851 // Add a PEL with a callout as if hostboot added it
852 {
853 EXPECT_CALL(*mockIface, getInventoryFromLocCode("U42", 0, true))
854 .WillOnce(Return("/system/chassis/processor"));
855
856 EXPECT_CALL(*mockIface, getFaultLEDGroup("/system/chassis/processor"))
857 .WillOnce(Return("/led/groups/cpu0"));
858
859 EXPECT_CALL(*mockIface, assertLEDGroup("/led/groups/cpu0", true))
860 .Times(1);
861
862 // This hostboot PEL has a single hardware callout in it.
863 auto data = pelFactory(1, 'B', 0x20, 0xA400, 500);
864
865 fs::path pelFilename = makeTempDir() / "rawpel";
866 std::ofstream pelFile{pelFilename};
867 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
868 pelFile.close();
869
870 std::string adItem = "RAWPEL=" + pelFilename.string();
871 std::vector<std::string> additionalData{adItem};
872 std::vector<std::string> associations;
873
874 manager.create("error message", 42, 0,
875 phosphor::logging::Entry::Level::Error, additionalData,
876 associations);
877
878 fs::remove_all(pelFilename.parent_path());
879 }
880
881 // Add a BMC PEL with a callout that uses the message registry
882 {
883 std::vector<std::string> names{"systemA"};
884 EXPECT_CALL(*mockIface, getSystemNames)
885 .Times(1)
886 .WillOnce(ReturnRef(names));
887
888 EXPECT_CALL(*mockIface, expandLocationCode("P42-C23", 0))
889 .WillOnce(Return("U42-P42-C23"));
890
891 // First call to this is when building the Callout section
892 EXPECT_CALL(*mockIface, getInventoryFromLocCode("P42-C23", 0, false))
893 .WillOnce(Return("/system/chassis/processor"));
894
895 // Second call to this is finding the associated LED group
896 EXPECT_CALL(*mockIface, getInventoryFromLocCode("U42-P42-C23", 0, true))
897 .WillOnce(Return("/system/chassis/processor"));
898
899 EXPECT_CALL(*mockIface, getFaultLEDGroup("/system/chassis/processor"))
900 .WillOnce(Return("/led/groups/cpu0"));
901
902 EXPECT_CALL(*mockIface, assertLEDGroup("/led/groups/cpu0", true))
903 .Times(1);
904
905 const auto registry = R"(
906 {
907 "PELs":
908 [
909 {
910 "Name": "xyz.openbmc_project.Error.Test",
911 "Subsystem": "power_supply",
912 "ActionFlags": ["service_action", "report"],
913 "SRC":
914 {
915 "ReasonCode": "0x2030"
916 },
917 "Callouts": [
918 {
919 "CalloutList": [
920 {"Priority": "high", "LocCode": "P42-C23"}
921 ]
922 }
923 ],
924 "Documentation":
925 {
926 "Description": "Test Error",
927 "Message": "Test Error"
928 }
929 }
930 ]
931 })";
932
933 auto path = getPELReadOnlyDataPath();
934 fs::create_directories(path);
935 path /= "message_registry.json";
936
937 std::ofstream registryFile{path};
938 registryFile << registry;
939 registryFile.close();
940
941 std::vector<std::string> additionalData;
942 std::vector<std::string> associations;
943
944 manager.create("xyz.openbmc_project.Error.Test", 42, 0,
945 phosphor::logging::Entry::Level::Error, additionalData,
946 associations);
947 }
948}