blob: 18be5db8c6e99d641a79883d1458028064bdb420 [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
Matt Spinlere6b48f12020-04-02 09:51:39 -050021#include <sdbusplus/test/sdbus_mock.hpp>
Matt Spinlera34ab722019-12-16 10:39:32 -060022#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinler89fa0822019-07-17 13:54:30 -050023
Patrick Williams2544b412022-10-04 08:41:06 -050024#include <fstream>
25#include <regex>
26
Matt Spinler89fa0822019-07-17 13:54:30 -050027#include <gtest/gtest.h>
28
29using namespace openpower::pels;
30namespace fs = std::filesystem;
31
Matt Spinlere6b48f12020-04-02 09:51:39 -050032using ::testing::NiceMock;
Matt Spinler3dd17e92020-08-05 15:04:27 -050033using ::testing::Return;
harsh-agarwal1d763db32024-09-03 09:18:50 -050034using json = nlohmann::json;
Matt Spinlere6b48f12020-04-02 09:51:39 -050035
Matt Spinler05c2c6c2019-12-18 14:02:09 -060036class TestLogger
37{
38 public:
39 void log(const std::string& name, phosphor::logging::Entry::Level level,
40 const EventLogger::ADMap& additionalData)
41 {
42 errName = name;
43 errLevel = level;
44 ad = additionalData;
45 }
46
47 std::string errName;
48 phosphor::logging::Entry::Level errLevel;
49 EventLogger::ADMap ad;
50};
51
Matt Spinler89fa0822019-07-17 13:54:30 -050052class ManagerTest : public CleanPELFiles
53{
Matt Spinler6b1a5c82020-01-07 08:48:53 -060054 public:
Matt Spinlere6b48f12020-04-02 09:51:39 -050055 ManagerTest() :
56 bus(sdbusplus::get_mocked_new(&sdbusInterface)),
57 logManager(bus, "logging_path")
Matt Spinler6b1a5c82020-01-07 08:48:53 -060058 {
59 sd_event_default(&sdEvent);
Matt Spinler6b1a5c82020-01-07 08:48:53 -060060 }
61
Matt Spinler32e36b82023-04-25 10:57:15 -050062 fs::path makeTempDir()
63 {
64 char path[] = "/tmp/tempnameXXXXXX";
65 std::filesystem::path dir = mkdtemp(path);
66 dirsToRemove.push_back(dir);
67 return dir;
68 }
69
Matt Spinler6b1a5c82020-01-07 08:48:53 -060070 ~ManagerTest()
71 {
Matt Spinler32e36b82023-04-25 10:57:15 -050072 for (const auto& d : dirsToRemove)
73 {
74 std::filesystem::remove_all(d);
75 }
Matt Spinler6b1a5c82020-01-07 08:48:53 -060076 sd_event_unref(sdEvent);
77 }
78
Matt Spinlere6b48f12020-04-02 09:51:39 -050079 NiceMock<sdbusplus::SdBusMock> sdbusInterface;
Patrick Williams45e83522022-07-22 19:26:52 -050080 sdbusplus::bus_t bus;
Matt Spinler6b1a5c82020-01-07 08:48:53 -060081 phosphor::logging::internal::Manager logManager;
82 sd_event* sdEvent;
Matt Spinler05c2c6c2019-12-18 14:02:09 -060083 TestLogger logger;
Matt Spinler32e36b82023-04-25 10:57:15 -050084 std::vector<std::filesystem::path> dirsToRemove;
Matt Spinler89fa0822019-07-17 13:54:30 -050085};
86
Matt Spinler67456c22019-10-21 12:22:49 -050087std::optional<fs::path> findAnyPELInRepo()
88{
89 // PELs are named <timestamp>_<ID>
90 std::regex expr{"\\d+_\\d+"};
91
92 for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs"))
93 {
94 if (std::regex_search(f.path().string(), expr))
95 {
96 return f.path();
97 }
98 }
99 return std::nullopt;
100}
101
Matt Spinler7e727a32020-07-07 15:00:17 -0500102size_t countPELsInRepo()
103{
104 size_t count = 0;
105 std::regex expr{"\\d+_\\d+"};
106
107 for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs"))
108 {
109 if (std::regex_search(f.path().string(), expr))
110 {
111 count++;
112 }
113 }
114 return count;
115}
116
Matt Spinlerff9cec22020-07-15 13:06:35 -0500117void deletePELFile(uint32_t id)
118{
119 char search[20];
120
121 sprintf(search, "\\d+_%.8X", id);
122 std::regex expr{search};
123
124 for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs"))
125 {
126 if (std::regex_search(f.path().string(), expr))
127 {
128 fs::remove(f.path());
129 break;
130 }
131 }
132}
133
Matt Spinler89fa0822019-07-17 13:54:30 -0500134// Test that using the RAWPEL=<file> with the Manager::create() call gets
135// a PEL saved in the repository.
136TEST_F(ManagerTest, TestCreateWithPEL)
137{
Matt Spinlerc8705e22019-09-11 12:36:07 -0500138 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500139 std::make_unique<MockDataInterface>();
Matt Spinler89fa0822019-07-17 13:54:30 -0500140
Matt Spinlerd96fa602022-12-15 11:11:26 -0600141 std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
142
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600143 openpower::pels::Manager manager{
144 logManager, std::move(dataIface),
145 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
Matt Spinlerd96fa602022-12-15 11:11:26 -0600146 std::placeholders::_2, std::placeholders::_3),
147 std::move(journal)};
Matt Spinler89fa0822019-07-17 13:54:30 -0500148
149 // Create a PEL, write it to a file, and pass that filename into
150 // the create function.
Matt Spinler42828bd2019-10-11 10:39:30 -0500151 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler89fa0822019-07-17 13:54:30 -0500152
153 fs::path pelFilename = makeTempDir() / "rawpel";
154 std::ofstream pelFile{pelFilename};
Matt Spinler42828bd2019-10-11 10:39:30 -0500155 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
Matt Spinler89fa0822019-07-17 13:54:30 -0500156 pelFile.close();
157
Patrick Williamse5940632024-11-22 20:47:58 -0500158 std::map<std::string, std::string> additionalData{
159 {"RAWPEL", pelFilename.string()}};
Matt Spinler89fa0822019-07-17 13:54:30 -0500160 std::vector<std::string> associations;
161
Matt Spinler367144c2019-09-19 15:33:52 -0500162 manager.create("error message", 42, 0,
163 phosphor::logging::Entry::Level::Error, additionalData,
Matt Spinler89fa0822019-07-17 13:54:30 -0500164 associations);
165
Matt Spinler67456c22019-10-21 12:22:49 -0500166 // Find the file in the PEL repository directory
167 auto pelPathInRepo = findAnyPELInRepo();
Matt Spinler89fa0822019-07-17 13:54:30 -0500168
Matt Spinler67456c22019-10-21 12:22:49 -0500169 EXPECT_TRUE(pelPathInRepo);
Matt Spinler89fa0822019-07-17 13:54:30 -0500170
Matt Spinler475e5742019-07-18 16:09:49 -0500171 // Now remove it based on its OpenBMC event log ID
172 manager.erase(42);
173
Matt Spinler67456c22019-10-21 12:22:49 -0500174 pelPathInRepo = findAnyPELInRepo();
Matt Spinler475e5742019-07-18 16:09:49 -0500175
Matt Spinler67456c22019-10-21 12:22:49 -0500176 EXPECT_FALSE(pelPathInRepo);
Matt Spinler89fa0822019-07-17 13:54:30 -0500177}
Matt Spinler67456c22019-10-21 12:22:49 -0500178
Matt Spinlere95fd012020-01-07 12:53:16 -0600179TEST_F(ManagerTest, TestCreateWithInvalidPEL)
180{
181 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500182 std::make_unique<MockDataInterface>();
Matt Spinlere95fd012020-01-07 12:53:16 -0600183
Matt Spinlerd96fa602022-12-15 11:11:26 -0600184 std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
185
Matt Spinlere95fd012020-01-07 12:53:16 -0600186 openpower::pels::Manager manager{
187 logManager, std::move(dataIface),
188 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
Matt Spinlerd96fa602022-12-15 11:11:26 -0600189 std::placeholders::_2, std::placeholders::_3),
190 std::move(journal)};
Matt Spinlere95fd012020-01-07 12:53:16 -0600191
192 // Create a PEL, write it to a file, and pass that filename into
193 // the create function.
194 auto data = pelDataFactory(TestPELType::pelSimple);
195
196 // Truncate it to make it invalid.
197 data.resize(200);
198
199 fs::path pelFilename = makeTempDir() / "rawpel";
200 std::ofstream pelFile{pelFilename};
201 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
202 pelFile.close();
203
Patrick Williamse5940632024-11-22 20:47:58 -0500204 std::map<std::string, std::string> additionalData{
205 {"RAWPEL", pelFilename.string()}};
Matt Spinlere95fd012020-01-07 12:53:16 -0600206 std::vector<std::string> associations;
207
208 manager.create("error message", 42, 0,
209 phosphor::logging::Entry::Level::Error, additionalData,
210 associations);
211
212 // Run the event loop to log the bad PEL event
213 sdeventplus::Event e{sdEvent};
214 e.run(std::chrono::milliseconds(1));
215
216 PEL invalidPEL{data};
217 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.BadHostPEL");
218 EXPECT_EQ(logger.errLevel, phosphor::logging::Entry::Level::Error);
219 EXPECT_EQ(std::stoi(logger.ad["PLID"], nullptr, 16), invalidPEL.plid());
220 EXPECT_EQ(logger.ad["OBMC_LOG_ID"], "42");
221 EXPECT_EQ(logger.ad["SRC"], (*invalidPEL.primarySRC())->asciiString());
222 EXPECT_EQ(logger.ad["PEL_SIZE"], std::to_string(data.size()));
223
Matt Spinlerfe721892020-04-02 10:28:08 -0500224 // Check that the bad PEL data was saved to a file.
225 auto badPELData = readPELFile(getPELRepoPath() / "badPEL");
226 EXPECT_EQ(*badPELData, data);
Matt Spinlere95fd012020-01-07 12:53:16 -0600227}
228
Matt Spinler67456c22019-10-21 12:22:49 -0500229// Test that the message registry can be used to build a PEL.
230TEST_F(ManagerTest, TestCreateWithMessageRegistry)
231{
232 const auto registry = R"(
233{
234 "PELs":
235 [
236 {
237 "Name": "xyz.openbmc_project.Error.Test",
238 "Subsystem": "power_supply",
239 "ActionFlags": ["service_action", "report"],
240 "SRC":
241 {
242 "ReasonCode": "0x2030"
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800243 },
Vijay Lobo593a4c62021-06-16 14:25:26 -0500244 "Callouts": [
245 {
246 "CalloutList": [
Matt Spinler2edce4e2024-01-17 11:13:51 -0600247 {"Priority": "high", "Procedure": "BMC0001"},
Vijay Lobo593a4c62021-06-16 14:25:26 -0500248 {"Priority": "medium", "SymbolicFRU": "service_docs"}
249 ]
250 }
251 ],
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800252 "Documentation":
253 {
254 "Description": "A PGOOD Fault",
255 "Message": "PS had a PGOOD Fault"
Matt Spinler67456c22019-10-21 12:22:49 -0500256 }
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500257 },
258 {
259 "Name": "xyz.openbmc_project.Logging.Error.Default",
260 "Subsystem": "bmc_firmware",
261 "SRC":
262 {
263 "ReasonCode": "0x2031"
264 },
265 "Documentation":
266 {
267 "Description": "The entry used when no match found",
268 "Message": "This is a generic SRC"
269 }
Matt Spinler67456c22019-10-21 12:22:49 -0500270 }
271 ]
272}
273)";
274
Matt Spinler0d804ef2020-05-12 16:16:26 -0500275 auto path = getPELReadOnlyDataPath();
Matt Spinlerd4ffb652019-11-12 14:16:14 -0600276 fs::create_directories(path);
277 path /= "message_registry.json";
278
Matt Spinler67456c22019-10-21 12:22:49 -0500279 std::ofstream registryFile{path};
280 registryFile << registry;
281 registryFile.close();
282
Matt Spinler67456c22019-10-21 12:22:49 -0500283 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500284 std::make_unique<MockDataInterface>();
Matt Spinler67456c22019-10-21 12:22:49 -0500285
Matt Spinlerd96fa602022-12-15 11:11:26 -0600286 std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
287
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600288 openpower::pels::Manager manager{
289 logManager, std::move(dataIface),
290 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
Matt Spinlerd96fa602022-12-15 11:11:26 -0600291 std::placeholders::_2, std::placeholders::_3),
292 std::move(journal)};
Matt Spinler67456c22019-10-21 12:22:49 -0500293
Patrick Williamse5940632024-11-22 20:47:58 -0500294 std::map<std::string, std::string> additionalData{{"FOO", "BAR"}};
Matt Spinler67456c22019-10-21 12:22:49 -0500295 std::vector<std::string> associations;
296
297 // Create the event log to create the PEL from.
298 manager.create("xyz.openbmc_project.Error.Test", 33, 0,
299 phosphor::logging::Entry::Level::Error, additionalData,
300 associations);
301
302 // Ensure a PEL was created in the repository
303 auto pelFile = findAnyPELInRepo();
304 ASSERT_TRUE(pelFile);
305
306 auto data = readPELFile(*pelFile);
307 PEL pel(*data);
308
309 // Spot check it. Other testcases cover the details.
310 EXPECT_TRUE(pel.valid());
311 EXPECT_EQ(pel.obmcLogID(), 33);
312 EXPECT_EQ(pel.primarySRC().value()->asciiString(),
313 "BD612030 ");
Vijay Lobod354a392021-06-01 16:21:02 -0500314 // Check if the eventId creation is good
315 EXPECT_EQ(manager.getEventId(pel),
316 "BD612030 00000055 00000010 00000000 00000000 00000000 00000000 "
317 "00000000 00000000");
Vijay Lobo593a4c62021-06-16 14:25:26 -0500318 // Check if resolution property creation is good
319 EXPECT_EQ(manager.getResolution(pel),
Matt Spinlerea2873d2021-08-18 10:35:40 -0500320 "1. Priority: High, Procedure: BMC0001\n2. Priority: Medium, PN: "
Vijay Lobo593a4c62021-06-16 14:25:26 -0500321 "SVCDOCS\n");
Matt Spinler67456c22019-10-21 12:22:49 -0500322
323 // Remove it
324 manager.erase(33);
325 pelFile = findAnyPELInRepo();
326 EXPECT_FALSE(pelFile);
327
328 // Create an event log that can't be found in the registry.
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500329 // In this case, xyz.openbmc_project.Logging.Error.Default will
330 // be used as the key instead to find a registry match.
331 manager.create("xyz.openbmc_project.Error.Foo", 42, 0,
Matt Spinler67456c22019-10-21 12:22:49 -0500332 phosphor::logging::Entry::Level::Error, additionalData,
333 associations);
334
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500335 // Ensure a PEL was still created in the repository
Matt Spinler67456c22019-10-21 12:22:49 -0500336 pelFile = findAnyPELInRepo();
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500337 ASSERT_TRUE(pelFile);
338
339 data = readPELFile(*pelFile);
340 PEL newPEL(*data);
341
342 EXPECT_TRUE(newPEL.valid());
343 EXPECT_EQ(newPEL.obmcLogID(), 42);
344 EXPECT_EQ(newPEL.primarySRC().value()->asciiString(),
345 "BD8D2031 ");
346
347 // Check for both the original AdditionalData item as well as
348 // the ERROR_NAME item that should contain the error message
349 // property that wasn't found.
350 std::string errorName;
351 std::string adItem;
352
353 for (const auto& section : newPEL.optionalSections())
354 {
355 if (SectionID::userData == static_cast<SectionID>(section->header().id))
356 {
357 if (UserDataFormat::json ==
358 static_cast<UserDataFormat>(section->header().subType))
359 {
360 auto ud = static_cast<UserData*>(section.get());
361
362 // Check that there was a UserData section added that
363 // contains debug details about the device.
364 const auto& d = ud->data();
365 std::string jsonString{d.begin(), d.end()};
366 auto json = nlohmann::json::parse(jsonString);
367
368 if (json.contains("ERROR_NAME"))
369 {
370 errorName = json["ERROR_NAME"].get<std::string>();
371 }
372
373 if (json.contains("FOO"))
374 {
375 adItem = json["FOO"].get<std::string>();
376 }
377 }
378 }
379 if (!errorName.empty())
380 {
381 break;
382 }
383 }
384
385 EXPECT_EQ(errorName, "xyz.openbmc_project.Error.Foo");
386 EXPECT_EQ(adItem, "BAR");
Matt Spinler67456c22019-10-21 12:22:49 -0500387}
Matt Spinlera34ab722019-12-16 10:39:32 -0600388
389TEST_F(ManagerTest, TestDBusMethods)
390{
Matt Spinlera34ab722019-12-16 10:39:32 -0600391 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500392 std::make_unique<MockDataInterface>();
Matt Spinlera34ab722019-12-16 10:39:32 -0600393
Matt Spinlerd96fa602022-12-15 11:11:26 -0600394 std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
395
Patrick Williams075c7922024-08-16 15:19:49 -0400396 Manager manager{
397 logManager, std::move(dataIface),
398 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
399 std::placeholders::_2, std::placeholders::_3),
400 std::move(journal)};
Matt Spinlera34ab722019-12-16 10:39:32 -0600401
402 // Create a PEL, write it to a file, and pass that filename into
403 // the create function so there's one in the repo.
404 auto data = pelDataFactory(TestPELType::pelSimple);
405
406 fs::path pelFilename = makeTempDir() / "rawpel";
407 std::ofstream pelFile{pelFilename};
408 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
409 pelFile.close();
410
Patrick Williamse5940632024-11-22 20:47:58 -0500411 std::map<std::string, std::string> additionalData{
412 {"RAWPEL", pelFilename.string()}};
Matt Spinlera34ab722019-12-16 10:39:32 -0600413 std::vector<std::string> associations;
414
415 manager.create("error message", 42, 0,
416 phosphor::logging::Entry::Level::Error, additionalData,
417 associations);
418
419 // getPELFromOBMCID
420 auto newData = manager.getPELFromOBMCID(42);
421 EXPECT_EQ(newData.size(), data.size());
422
423 // Read the PEL to get the ID for later
424 PEL pel{newData};
425 auto id = pel.id();
426
427 EXPECT_THROW(
428 manager.getPELFromOBMCID(id + 1),
429 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
430
431 // getPEL
432 auto unixfd = manager.getPEL(id);
433
434 // Get the size
435 struct stat s;
436 int r = fstat(unixfd, &s);
437 ASSERT_EQ(r, 0);
438 auto size = s.st_size;
439
440 // Open the FD and check the contents
441 FILE* fp = fdopen(unixfd, "r");
442 ASSERT_NE(fp, nullptr);
443
444 std::vector<uint8_t> fdData;
445 fdData.resize(size);
446 r = fread(fdData.data(), 1, size, fp);
447 EXPECT_EQ(r, size);
448
449 EXPECT_EQ(newData, fdData);
450
451 fclose(fp);
452
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600453 // Run the event loop to close the FD
454 sdeventplus::Event e{sdEvent};
455 e.run(std::chrono::milliseconds(1));
456
Matt Spinlera34ab722019-12-16 10:39:32 -0600457 EXPECT_THROW(
458 manager.getPEL(id + 1),
459 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
460
461 // hostAck
462 manager.hostAck(id);
463
464 EXPECT_THROW(
465 manager.hostAck(id + 1),
466 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
467
468 // hostReject
469 manager.hostReject(id, Manager::RejectionReason::BadPEL);
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600470
471 // Run the event loop to log the bad PEL event
472 e.run(std::chrono::milliseconds(1));
473
474 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.SentBadPELToHost");
475 EXPECT_EQ(id, std::stoi(logger.ad["BAD_ID"], nullptr, 16));
476
Matt Spinlera34ab722019-12-16 10:39:32 -0600477 manager.hostReject(id, Manager::RejectionReason::HostFull);
478
479 EXPECT_THROW(
480 manager.hostReject(id + 1, Manager::RejectionReason::BadPEL),
481 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
482
Ramesh Iyyarf4203c42021-06-24 06:09:23 -0500483 // GetPELIdFromBMCLogId
484 EXPECT_EQ(pel.id(), manager.getPELIdFromBMCLogId(pel.obmcLogID()));
485 EXPECT_THROW(
486 manager.getPELIdFromBMCLogId(pel.obmcLogID() + 1),
487 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
Ramesh Iyyar530efbf2021-06-24 06:22:22 -0500488
489 // GetBMCLogIdFromPELId
490 EXPECT_EQ(pel.obmcLogID(), manager.getBMCLogIdFromPELId(pel.id()));
491 EXPECT_THROW(
492 manager.getBMCLogIdFromPELId(pel.id() + 1),
493 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
Matt Spinlera34ab722019-12-16 10:39:32 -0600494}
Matt Spinler19e72902020-01-24 11:05:20 -0600495
496// An ESEL from the wild
497const std::string esel{
498 "00 00 df 00 00 00 00 20 00 04 12 01 6f aa 00 00 "
Matt Spinler0bf04b52023-04-28 10:30:26 -0500499 "50 48 00 30 01 00 33 00 20 23 05 11 10 20 20 00 00 00 00 07 5c d5 50 db "
Matt Spinler19e72902020-01-24 11:05:20 -0600500 "42 00 00 10 00 00 00 00 00 00 00 00 00 00 00 00 90 00 00 4e 90 00 00 4e "
501 "55 48 00 18 01 00 09 00 8a 03 40 00 00 00 00 00 ff ff 00 00 00 00 00 00 "
502 "50 53 00 50 01 01 00 00 02 00 00 09 33 2d 00 48 00 00 00 e0 00 00 10 00 "
503 "00 00 00 00 00 20 00 00 00 0c 00 02 00 00 00 fa 00 00 0c e4 00 00 00 12 "
504 "42 43 38 41 33 33 32 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
505 "20 20 20 20 20 20 20 20 55 44 00 1c 01 06 01 00 02 54 41 4b 00 00 00 06 "
506 "00 00 00 55 00 01 f9 20 00 00 00 00 55 44 00 24 01 06 01 00 01 54 41 4b "
507 "00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 00 23 01 00 02 00 05 00 00 "
508 "55 44 00 0c 01 0b 01 00 0f 01 00 00 55 44 00 10 01 04 01 00 0f 9f de 6a "
509 "00 01 00 00 55 44 00 7c 00 0c 01 00 00 13 0c 02 00 fa 0c e4 16 00 01 2c "
510 "0c 1c 16 00 00 fa 0a f0 14 00 00 fa 0b b8 14 00 00 be 09 60 12 00 01 2c "
511 "0d 7a 12 00 00 fa 0c 4e 10 00 00 fa 0c e4 10 00 00 be 0a 8c 16 00 01 2c "
512 "0c 1c 16 00 01 09 09 f6 16 00 00 fa 09 f6 14 00 00 fa 0b b8 14 00 00 fa "
513 "0a f0 14 00 00 be 08 ca 12 00 01 2c 0c e4 12 00 00 fa 0b 54 10 00 00 fa "
514 "0c 2d 10 00 00 be 08 ca 55 44 00 58 01 03 01 00 00 00 00 00 00 05 31 64 "
515 "00 00 00 00 00 05 0d d4 00 00 00 00 40 5f 06 e0 00 00 00 00 40 5d d2 00 "
516 "00 00 00 00 40 57 d3 d0 00 00 00 00 40 58 f6 a0 00 00 00 00 40 54 c9 34 "
517 "00 00 00 00 40 55 9a 10 00 00 00 00 40 4c 0a 80 00 00 00 00 00 00 27 14 "
518 "55 44 01 84 01 01 01 00 48 6f 73 74 62 6f 6f 74 20 42 75 69 6c 64 20 49 "
519 "44 3a 20 68 6f 73 74 62 6f 6f 74 2d 66 65 63 37 34 64 66 2d 70 30 61 38 "
520 "37 64 63 34 2f 68 62 69 63 6f 72 65 2e 62 69 6e 00 49 42 4d 2d 77 69 74 "
521 "68 65 72 73 70 6f 6f 6e 2d 4f 50 39 2d 76 32 2e 34 2d 39 2e 32 33 34 0a "
522 "09 6f 70 2d 62 75 69 6c 64 2d 38 32 66 34 63 66 30 0a 09 62 75 69 6c 64 "
523 "72 6f 6f 74 2d 32 30 31 39 2e 30 35 2e 32 2d 31 30 2d 67 38 39 35 39 31 "
524 "31 34 0a 09 73 6b 69 62 6f 6f 74 2d 76 36 2e 35 2d 31 38 2d 67 34 37 30 "
525 "66 66 62 35 66 32 39 64 37 0a 09 68 6f 73 74 62 6f 6f 74 2d 66 65 63 37 "
526 "34 64 66 2d 70 30 61 38 37 64 63 34 0a 09 6f 63 63 2d 65 34 35 39 37 61 "
527 "62 0a 09 6c 69 6e 75 78 2d 35 2e 32 2e 31 37 2d 6f 70 65 6e 70 6f 77 65 "
528 "72 31 2d 70 64 64 63 63 30 33 33 0a 09 70 65 74 69 74 62 6f 6f 74 2d 76 "
529 "31 2e 31 30 2e 34 0a 09 6d 61 63 68 69 6e 65 2d 78 6d 6c 2d 63 36 32 32 "
530 "63 62 35 2d 70 37 65 63 61 62 33 64 0a 09 68 6f 73 74 62 6f 6f 74 2d 62 "
531 "69 6e 61 72 69 65 73 2d 36 36 65 39 61 36 30 0a 09 63 61 70 70 2d 75 63 "
532 "6f 64 65 2d 70 39 2d 64 64 32 2d 76 34 0a 09 73 62 65 2d 36 30 33 33 30 "
533 "65 30 0a 09 68 63 6f 64 65 2d 68 77 30 39 32 31 31 39 61 2e 6f 70 6d 73 "
534 "74 0a 00 00 55 44 00 70 01 04 01 00 0f 9f de 6a 00 05 00 00 07 5f 1d f4 "
535 "30 32 43 59 34 37 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
536 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
537 "0b ac 54 02 59 41 31 39 33 34 36 39 37 30 35 38 00 00 00 00 00 00 05 22 "
538 "a1 58 01 8a 00 58 40 20 17 18 4d 2c 00 00 00 fc 01 a1 00 00 55 44 00 14 "
539 "01 08 01 00 00 00 00 01 00 00 00 5a 00 00 00 05 55 44 03 fc 01 15 31 00 "
540 "01 28 00 42 46 41 50 49 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 f4 "
541 "00 00 00 00 00 00 03 f4 00 00 00 0b 00 00 00 00 00 00 00 3d 2c 9b c2 84 "
542 "00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 09 "
543 "00 00 00 00 00 11 bd 20 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 "
544 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 00 00 00 00 00 00 01 2c "
545 "00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c 1c 00 00 00 64 00 00 00 3d "
546 "2c 9b d1 11 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 "
547 "00 00 00 0a 00 00 00 00 00 13 b5 a0 00 00 00 00 00 01 f8 80 00 00 00 00 "
548 "00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 00 00 00 00 "
549 "00 00 00 be 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0a 8c 00 00 00 64 "
550 "00 00 00 3d 2c 9b df 98 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 "
551 "00 00 00 00 00 00 00 0b 00 00 00 00 00 15 ae 20 00 00 00 00 00 01 f8 80 "
552 "00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 "
553 "00 00 00 00 00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c e4 "
554 "00 00 00 64 00 00 00 3d 2c 9b ea b7 00 00 01 e4 00 48 43 4f fb ed 70 b1 "
555 "00 00 02 01 00 00 00 00 00 00 00 0c 00 00 00 00 00 17 a6 a0 00 00 00 00 "
556 "00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 "
557 "00 00 00 12 00 00 00 00 00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 "
558 "00 00 0c 4e 00 00 00 64 00 00 00 3d 2c 9b f6 27 00 00 01 e4 00 48 43 4f "
559 "fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0d 00 00 00 00 00 19 9f 20 "
560 "00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 "
561 "00 00 00 00 00 00 00 12 00 00 00 00 00 00 01 2c 00 00 00 00 00 00 07 d0 "
562 "00 00 00 00 00 00 0d 7a 00 00 00 64 00 00 00 3d 2c 9c 05 75 00 00 01 e4 "
563 "00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0e 00 00 00 00 "
564 "00 1b 97 a0 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 "
565 "00 00 00 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 be 00 00 00 00 "
566 "00 00 07 d0 00 00 00 00 00 00 09 60 00 00 00 64 00 00 00 3d 2c 9c 11 29 "
567 "00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0f "
568 "00 00 00 00 00 1d 90 20 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 "
569 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 fa "
570 "00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0b b8 00 00 00 64 00 00 00 3d "
571 "2c 9c 1c 45 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 "
572 "00 00 00 10 00 00 00 00 00 1f 88 a0 00 00 00 00 00 01 f8 80 00 00 00 00 "
573 "00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 00 00 00 00 "
574 "00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0a f0 00 00 00 64 "
575 "00 00 00 3d 2c 9c 2b 14 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 "
576 "00 00 00 00 00 00 00 11 00 00 00 00 00 21 81 20 00 00 00 00 00 01 f8 80 "
577 "00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 "
578 "00 00 00 00 00 00 01 2c 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c 1c "
579 "00 00 00 64 00 00 00 3d 2d 6d 8f 9e 00 00 01 e4 00 00 43 4f 52 d7 9c 36 "
580 "00 00 04 73 00 00 00 1c 00 00 00 3d 2d 6d 99 ac 00 00 01 e4 00 10 43 4f "
581 "3f f2 02 3d 00 00 05 58 00 00 00 00 02 00 00 01 00 00 00 00 00 00 00 40 "
582 "00 00 00 2c 55 44 00 30 01 15 31 00 01 28 00 42 46 41 50 49 5f 44 42 47 "
583 "00 00 00 00 00 00 00 00 00 00 00 28 00 00 00 00 00 00 00 28 00 00 00 00 "
584 "00 00 00 00 55 44 01 74 01 15 31 00 01 28 00 42 46 41 50 49 5f 49 00 00 "
585 "00 00 00 00 00 00 00 00 00 00 01 6c 00 00 00 00 00 00 01 6c 00 00 00 0b "
586 "00 00 00 00 00 00 00 3c 0d 52 18 5e 00 00 01 e4 00 08 43 4f 46 79 94 13 "
587 "00 00 0a 5b 00 00 00 00 00 00 2c 00 00 00 00 24 00 00 00 3c 0d 6b 26 6c "
588 "00 00 01 e4 00 00 43 4f 4e 9b 18 74 00 00 01 03 00 00 00 1c 00 00 00 3c "
589 "12 b9 2d 13 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 00 00 00 1c "
590 "00 00 00 3c 13 02 73 53 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 "
591 "00 00 00 1c 00 00 00 3c 13 04 7c 94 00 00 01 e4 00 00 43 4f ea 31 ed d4 "
592 "00 00 05 c4 00 00 00 1c 00 00 00 3c 13 06 ad e1 00 00 01 e4 00 00 43 4f "
593 "ea 31 ed d4 00 00 05 c4 00 00 00 1c 00 00 00 3c 13 07 3f 77 00 00 01 e4 "
594 "00 00 43 4f 5e 4a 55 32 00 00 10 f2 00 00 00 1c 00 00 00 3c 13 07 4e e4 "
595 "00 00 01 e4 00 00 43 4f 5e 4a 55 32 00 00 0d 68 00 00 00 1c 00 00 00 3c "
596 "13 36 79 18 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 00 00 00 1c "
597 "00 00 00 3d 2c 9c 36 70 00 00 01 e4 00 00 43 4f 23 45 90 97 00 00 02 47 "
598 "00 00 00 1c 00 00 00 3d 2d 6d a3 ed 00 00 01 e4 00 08 43 4f 74 3a 5b 1a "
599 "00 00 04 cc 00 00 00 00 02 00 00 01 00 00 00 24 55 44 00 30 01 15 31 00 "
600 "01 28 00 42 53 43 41 4e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 28 "
601 "00 00 00 00 00 00 00 28 00 00 00 00 00 00 00 00"};
602
603TEST_F(ManagerTest, TestESELToRawData)
604{
605 auto data = Manager::eselToRawData(esel);
606
607 EXPECT_EQ(data.size(), 2464);
608
609 PEL pel{data};
610 EXPECT_TRUE(pel.valid());
611}
612
613TEST_F(ManagerTest, TestCreateWithESEL)
614{
615 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500616 std::make_unique<MockDataInterface>();
Matt Spinler19e72902020-01-24 11:05:20 -0600617
Matt Spinlerd96fa602022-12-15 11:11:26 -0600618 std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
619
Matt Spinler19e72902020-01-24 11:05:20 -0600620 openpower::pels::Manager manager{
621 logManager, std::move(dataIface),
622 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
Matt Spinlerd96fa602022-12-15 11:11:26 -0600623 std::placeholders::_2, std::placeholders::_3),
624 std::move(journal)};
Matt Spinler19e72902020-01-24 11:05:20 -0600625
626 {
Patrick Williamse5940632024-11-22 20:47:58 -0500627 std::map<std::string, std::string> additionalData{{"ESEL", esel}};
Matt Spinler19e72902020-01-24 11:05:20 -0600628 std::vector<std::string> associations;
629
630 manager.create("error message", 37, 0,
631 phosphor::logging::Entry::Level::Error, additionalData,
632 associations);
633
634 auto data = manager.getPELFromOBMCID(37);
635 PEL pel{data};
636 EXPECT_TRUE(pel.valid());
637 }
638
639 // Now an invalid one
640 {
Patrick Williamse5940632024-11-22 20:47:58 -0500641 std::string adItem = esel;
Matt Spinler19e72902020-01-24 11:05:20 -0600642
643 // Crop it
644 adItem.resize(adItem.size() - 300);
645
Patrick Williamse5940632024-11-22 20:47:58 -0500646 std::map<std::string, std::string> additionalData{{"ESEL", adItem}};
Matt Spinler19e72902020-01-24 11:05:20 -0600647 std::vector<std::string> associations;
648
649 manager.create("error message", 38, 0,
650 phosphor::logging::Entry::Level::Error, additionalData,
651 associations);
652
653 EXPECT_THROW(
654 manager.getPELFromOBMCID(38),
655 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
656
657 // Run the event loop to log the bad PEL event
658 sdeventplus::Event e{sdEvent};
659 e.run(std::chrono::milliseconds(1));
660
661 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.BadHostPEL");
662 EXPECT_EQ(logger.errLevel, phosphor::logging::Entry::Level::Error);
663 }
664}
Matt Spinler7e727a32020-07-07 15:00:17 -0500665
666// Test that PELs will be pruned when necessary
667TEST_F(ManagerTest, TestPruning)
668{
669 sdeventplus::Event e{sdEvent};
670
671 std::unique_ptr<DataInterfaceBase> dataIface =
672 std::make_unique<MockDataInterface>();
673
Matt Spinlerd96fa602022-12-15 11:11:26 -0600674 std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
675
Matt Spinler7e727a32020-07-07 15:00:17 -0500676 openpower::pels::Manager manager{
677 logManager, std::move(dataIface),
678 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
Matt Spinlerd96fa602022-12-15 11:11:26 -0600679 std::placeholders::_2, std::placeholders::_3),
680 std::move(journal)};
Matt Spinler7e727a32020-07-07 15:00:17 -0500681
Patrick Williamse5940632024-11-22 20:47:58 -0500682 // Create 25 1000B (4096B on disk each, which is what is used for
683 // pruning) BMC non-informational PELs in the 100KB repository. After
684 // the 24th one, the repo will be 96% full and a prune should be
685 // triggered to remove all but 7 to get under 30% full. Then when the
686 // 25th is added there will be 8 left.
Matt Spinler7e727a32020-07-07 15:00:17 -0500687
688 auto dir = makeTempDir();
689 for (int i = 1; i <= 25; i++)
690 {
691 auto data = pelFactory(42, 'O', 0x40, 0x8800, 1000);
692
693 fs::path pelFilename = dir / "rawpel";
694 std::ofstream pelFile{pelFilename};
695 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
696 pelFile.close();
697
Patrick Williamse5940632024-11-22 20:47:58 -0500698 std::map<std::string, std::string> additionalData{
699 {"RAWPEL", pelFilename.string()}};
Matt Spinler7e727a32020-07-07 15:00:17 -0500700 std::vector<std::string> associations;
701
702 manager.create("error message", 42, 0,
703 phosphor::logging::Entry::Level::Error, additionalData,
704 associations);
705
706 // Simulate the code getting back to the event loop
707 // after each create.
708 e.run(std::chrono::milliseconds(1));
709
710 if (i < 24)
711 {
712 EXPECT_EQ(countPELsInRepo(), i);
713 }
714 else if (i == 24)
715 {
716 // Prune occured
717 EXPECT_EQ(countPELsInRepo(), 7);
718 }
719 else // i == 25
720 {
721 EXPECT_EQ(countPELsInRepo(), 8);
722 }
723 }
724
725 try
726 {
727 // Make sure the 8 newest ones are still found.
728 for (uint32_t i = 0; i < 8; i++)
729 {
730 manager.getPEL(0x50000012 + i);
731 }
732 }
Patrick Williams66491c62021-10-06 12:23:37 -0500733 catch (
734 const sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument&
Matt Spinlerbe952d22022-07-01 11:30:11 -0500735 ex)
Matt Spinler7e727a32020-07-07 15:00:17 -0500736 {
737 ADD_FAILURE() << "PELs should have all been found";
738 }
Matt Spinler7e727a32020-07-07 15:00:17 -0500739}
Matt Spinlerff9cec22020-07-15 13:06:35 -0500740
741// Test that manually deleting a PEL file will be recognized by the code.
742TEST_F(ManagerTest, TestPELManualDelete)
743{
744 sdeventplus::Event e{sdEvent};
745
746 std::unique_ptr<DataInterfaceBase> dataIface =
747 std::make_unique<MockDataInterface>();
748
Matt Spinlerd96fa602022-12-15 11:11:26 -0600749 std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
750
Matt Spinlerff9cec22020-07-15 13:06:35 -0500751 openpower::pels::Manager manager{
752 logManager, std::move(dataIface),
753 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
Matt Spinlerd96fa602022-12-15 11:11:26 -0600754 std::placeholders::_2, std::placeholders::_3),
755 std::move(journal)};
Matt Spinlerff9cec22020-07-15 13:06:35 -0500756
757 auto data = pelDataFactory(TestPELType::pelSimple);
758 auto dir = makeTempDir();
759 fs::path pelFilename = dir / "rawpel";
760
Patrick Williamse5940632024-11-22 20:47:58 -0500761 std::map<std::string, std::string> additionalData{
762 {"RAWPEL", pelFilename.string()}};
Matt Spinlerff9cec22020-07-15 13:06:35 -0500763 std::vector<std::string> associations;
764
765 // Add 20 PELs, they will get incrementing IDs like
766 // 0x50000001, 0x50000002, etc.
767 for (int i = 1; i <= 20; i++)
768 {
769 std::ofstream pelFile{pelFilename};
770 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
771 pelFile.close();
772
773 manager.create("error message", 42, 0,
774 phosphor::logging::Entry::Level::Error, additionalData,
775 associations);
776
777 // Sanity check this ID is really there so we can test
778 // it was deleted later. This will throw an exception if
779 // not present.
780 manager.getPEL(0x50000000 + i);
781
782 // Run an event loop pass where the internal FD is deleted
783 // after the getPEL function call.
784 e.run(std::chrono::milliseconds(1));
785 }
786
787 EXPECT_EQ(countPELsInRepo(), 20);
788
789 deletePELFile(0x50000001);
790
791 // Run a single event loop pass so the inotify event can run
792 e.run(std::chrono::milliseconds(1));
793
794 EXPECT_EQ(countPELsInRepo(), 19);
795
796 EXPECT_THROW(
797 manager.getPEL(0x50000001),
798 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
799
800 // Delete a few more, they should all get handled in the same
801 // event loop pass
802 std::vector<uint32_t> toDelete{0x50000002, 0x50000003, 0x50000004,
803 0x50000005, 0x50000006};
804 std::for_each(toDelete.begin(), toDelete.end(),
805 [](auto i) { deletePELFile(i); });
806
807 e.run(std::chrono::milliseconds(1));
808
809 EXPECT_EQ(countPELsInRepo(), 14);
810
811 std::for_each(toDelete.begin(), toDelete.end(), [&manager](const auto i) {
812 EXPECT_THROW(
813 manager.getPEL(i),
814 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
815 });
Matt Spinlerff9cec22020-07-15 13:06:35 -0500816}
817
818// Test that deleting all PELs at once is handled OK.
819TEST_F(ManagerTest, TestPELManualDeleteAll)
820{
821 sdeventplus::Event e{sdEvent};
822
823 std::unique_ptr<DataInterfaceBase> dataIface =
824 std::make_unique<MockDataInterface>();
825
Matt Spinlerd96fa602022-12-15 11:11:26 -0600826 std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
827
Matt Spinlerff9cec22020-07-15 13:06:35 -0500828 openpower::pels::Manager manager{
829 logManager, std::move(dataIface),
830 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
Matt Spinlerd96fa602022-12-15 11:11:26 -0600831 std::placeholders::_2, std::placeholders::_3),
832 std::move(journal)};
Matt Spinlerff9cec22020-07-15 13:06:35 -0500833
834 auto data = pelDataFactory(TestPELType::pelSimple);
835 auto dir = makeTempDir();
836 fs::path pelFilename = dir / "rawpel";
837
Patrick Williamse5940632024-11-22 20:47:58 -0500838 std::map<std::string, std::string> additionalData{
839 {"RAWPEL", pelFilename.string()}};
Matt Spinlerff9cec22020-07-15 13:06:35 -0500840 std::vector<std::string> associations;
841
842 // Add 200 PELs, they will get incrementing IDs like
843 // 0x50000001, 0x50000002, etc.
844 for (int i = 1; i <= 200; i++)
845 {
846 std::ofstream pelFile{pelFilename};
847 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
848 pelFile.close();
849
850 manager.create("error message", 42, 0,
851 phosphor::logging::Entry::Level::Error, additionalData,
852 associations);
853
854 // Sanity check this ID is really there so we can test
855 // it was deleted later. This will throw an exception if
856 // not present.
857 manager.getPEL(0x50000000 + i);
858
859 // Run an event loop pass where the internal FD is deleted
860 // after the getPEL function call.
861 e.run(std::chrono::milliseconds(1));
862 }
863
864 // Delete them all at once
865 auto logPath = getPELRepoPath() / "logs";
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500866 std::string cmd = "rm " + logPath.string() + "/*_*";
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500867
868 {
869 auto rc = system(cmd.c_str());
870 EXPECT_EQ(rc, 0);
871 }
Matt Spinlerff9cec22020-07-15 13:06:35 -0500872
873 EXPECT_EQ(countPELsInRepo(), 0);
874
875 // It will take 5 event loop passes to process them all
876 for (int i = 0; i < 5; i++)
877 {
878 e.run(std::chrono::milliseconds(1));
879 }
880
881 for (int i = 1; i <= 200; i++)
882 {
883 EXPECT_THROW(
884 manager.getPEL(0x50000000 + i),
885 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
886 }
Matt Spinlerff9cec22020-07-15 13:06:35 -0500887}
Matt Spinler3dd17e92020-08-05 15:04:27 -0500888
889// Test that fault LEDs are turned on when PELs are created
890TEST_F(ManagerTest, TestServiceIndicators)
891{
892 std::unique_ptr<DataInterfaceBase> dataIface =
893 std::make_unique<MockDataInterface>();
894
895 MockDataInterface* mockIface =
896 reinterpret_cast<MockDataInterface*>(dataIface.get());
897
Matt Spinlerd96fa602022-12-15 11:11:26 -0600898 std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
899
Matt Spinler3dd17e92020-08-05 15:04:27 -0500900 openpower::pels::Manager manager{
901 logManager, std::move(dataIface),
902 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
Matt Spinlerd96fa602022-12-15 11:11:26 -0600903 std::placeholders::_2, std::placeholders::_3),
904 std::move(journal)};
Matt Spinler3dd17e92020-08-05 15:04:27 -0500905
906 // Add a PEL with a callout as if hostboot added it
907 {
908 EXPECT_CALL(*mockIface, getInventoryFromLocCode("U42", 0, true))
Matt Spinlerbad056b2023-01-25 14:16:57 -0600909 .WillOnce(
910 Return(std::vector<std::string>{"/system/chassis/processor"}));
Matt Spinler3dd17e92020-08-05 15:04:27 -0500911
Matt Spinler993168d2021-04-07 16:05:03 -0500912 EXPECT_CALL(*mockIface,
913 setFunctional("/system/chassis/processor", false))
Matt Spinler3dd17e92020-08-05 15:04:27 -0500914 .Times(1);
915
916 // This hostboot PEL has a single hardware callout in it.
917 auto data = pelFactory(1, 'B', 0x20, 0xA400, 500);
918
919 fs::path pelFilename = makeTempDir() / "rawpel";
920 std::ofstream pelFile{pelFilename};
921 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
922 pelFile.close();
923
Patrick Williamse5940632024-11-22 20:47:58 -0500924 std::map<std::string, std::string> additionalData{
925 {"RAWPEL", pelFilename.string()}};
Matt Spinler3dd17e92020-08-05 15:04:27 -0500926 std::vector<std::string> associations;
927
928 manager.create("error message", 42, 0,
929 phosphor::logging::Entry::Level::Error, additionalData,
930 associations);
Matt Spinler3dd17e92020-08-05 15:04:27 -0500931 }
932
933 // Add a BMC PEL with a callout that uses the message registry
934 {
935 std::vector<std::string> names{"systemA"};
936 EXPECT_CALL(*mockIface, getSystemNames)
937 .Times(1)
Matt Spinler1ab66962020-10-29 13:21:44 -0500938 .WillOnce(Return(names));
Matt Spinler3dd17e92020-08-05 15:04:27 -0500939
940 EXPECT_CALL(*mockIface, expandLocationCode("P42-C23", 0))
941 .WillOnce(Return("U42-P42-C23"));
942
943 // First call to this is when building the Callout section
944 EXPECT_CALL(*mockIface, getInventoryFromLocCode("P42-C23", 0, false))
Matt Spinlerbad056b2023-01-25 14:16:57 -0600945 .WillOnce(
946 Return(std::vector<std::string>{"/system/chassis/processor"}));
Matt Spinler3dd17e92020-08-05 15:04:27 -0500947
948 // Second call to this is finding the associated LED group
949 EXPECT_CALL(*mockIface, getInventoryFromLocCode("U42-P42-C23", 0, true))
Matt Spinlerbad056b2023-01-25 14:16:57 -0600950 .WillOnce(
951 Return(std::vector<std::string>{"/system/chassis/processor"}));
Matt Spinler3dd17e92020-08-05 15:04:27 -0500952
Matt Spinler993168d2021-04-07 16:05:03 -0500953 EXPECT_CALL(*mockIface,
954 setFunctional("/system/chassis/processor", false))
Matt Spinler3dd17e92020-08-05 15:04:27 -0500955 .Times(1);
956
957 const auto registry = R"(
958 {
959 "PELs":
960 [
961 {
962 "Name": "xyz.openbmc_project.Error.Test",
963 "Subsystem": "power_supply",
964 "ActionFlags": ["service_action", "report"],
965 "SRC":
966 {
967 "ReasonCode": "0x2030"
968 },
969 "Callouts": [
970 {
971 "CalloutList": [
972 {"Priority": "high", "LocCode": "P42-C23"}
973 ]
974 }
975 ],
976 "Documentation":
977 {
978 "Description": "Test Error",
979 "Message": "Test Error"
980 }
981 }
982 ]
983 })";
984
985 auto path = getPELReadOnlyDataPath();
986 fs::create_directories(path);
987 path /= "message_registry.json";
988
989 std::ofstream registryFile{path};
990 registryFile << registry;
991 registryFile.close();
992
Patrick Williamse5940632024-11-22 20:47:58 -0500993 std::map<std::string, std::string> additionalData;
Matt Spinler3dd17e92020-08-05 15:04:27 -0500994 std::vector<std::string> associations;
995
996 manager.create("xyz.openbmc_project.Error.Test", 42, 0,
997 phosphor::logging::Entry::Level::Error, additionalData,
998 associations);
999 }
1000}
Sumit Kumar2ccdcef2021-07-31 10:04:58 -05001001
1002// Test for duplicate PELs moved to archive folder
1003TEST_F(ManagerTest, TestDuplicatePEL)
1004{
1005 sdeventplus::Event e{sdEvent};
1006 size_t count = 0;
1007
1008 std::unique_ptr<DataInterfaceBase> dataIface =
1009 std::make_unique<MockDataInterface>();
1010
Matt Spinlerd96fa602022-12-15 11:11:26 -06001011 std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
1012
Sumit Kumar2ccdcef2021-07-31 10:04:58 -05001013 openpower::pels::Manager manager{
1014 logManager, std::move(dataIface),
1015 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
Matt Spinlerd96fa602022-12-15 11:11:26 -06001016 std::placeholders::_2, std::placeholders::_3),
1017 std::move(journal)};
Sumit Kumar2ccdcef2021-07-31 10:04:58 -05001018
1019 for (int i = 0; i < 2; i++)
1020 {
1021 // This hostboot PEL has a single hardware callout in it.
1022 auto data = pelFactory(1, 'B', 0x20, 0xA400, 500);
1023
1024 fs::path pelFilename = makeTempDir() / "rawpel";
1025 std::ofstream pelFile{pelFilename};
1026 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
1027 pelFile.close();
1028
Patrick Williamse5940632024-11-22 20:47:58 -05001029 std::map<std::string, std::string> additionalData{
1030 {"RAWPEL", pelFilename.string()}};
Sumit Kumar2ccdcef2021-07-31 10:04:58 -05001031 std::vector<std::string> associations;
1032
1033 manager.create("error message", 42, 0,
1034 phosphor::logging::Entry::Level::Error, additionalData,
1035 associations);
1036
1037 e.run(std::chrono::milliseconds(1));
1038 }
1039
1040 for (auto& f :
1041 fs::directory_iterator(getPELRepoPath() / "logs" / "archive"))
1042 {
1043 if (fs::is_regular_file(f.path()))
1044 {
1045 count++;
1046 }
1047 }
1048
1049 // Get count of PELs in the repository & in archive directtory
1050 EXPECT_EQ(countPELsInRepo(), 1);
1051 EXPECT_EQ(count, 1);
1052}
Sumit Kumar3e274432021-09-14 06:37:56 -05001053
Patrick Williamse5940632024-11-22 20:47:58 -05001054// Test termination bit set for pel with critical system termination
1055// severity
Sumit Kumar3e274432021-09-14 06:37:56 -05001056TEST_F(ManagerTest, TestTerminateBitWithPELSevCriticalSysTerminate)
1057{
1058 const auto registry = R"(
1059{
1060 "PELs":
1061 [
1062 {
1063 "Name": "xyz.openbmc_project.Error.Test",
1064 "Subsystem": "power_supply",
1065 "Severity": "critical_system_term",
1066 "ActionFlags": ["service_action", "report"],
1067 "SRC":
1068 {
1069 "ReasonCode": "0x2030"
1070 },
1071 "Documentation":
1072 {
1073 "Description": "A PGOOD Fault",
1074 "Message": "PS had a PGOOD Fault"
1075 }
1076 }
1077 ]
1078}
1079)";
1080
1081 auto path = getPELReadOnlyDataPath();
1082 fs::create_directories(path);
1083 path /= "message_registry.json";
1084
1085 std::ofstream registryFile{path};
1086 registryFile << registry;
1087 registryFile.close();
1088
1089 std::unique_ptr<DataInterfaceBase> dataIface =
1090 std::make_unique<MockDataInterface>();
1091
Matt Spinlerd96fa602022-12-15 11:11:26 -06001092 std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
1093
Sumit Kumar3e274432021-09-14 06:37:56 -05001094 openpower::pels::Manager manager{
1095 logManager, std::move(dataIface),
1096 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
Matt Spinlerd96fa602022-12-15 11:11:26 -06001097 std::placeholders::_2, std::placeholders::_3),
1098 std::move(journal)};
Sumit Kumar3e274432021-09-14 06:37:56 -05001099
Patrick Williamse5940632024-11-22 20:47:58 -05001100 std::map<std::string, std::string> additionalData{{"FOO", "BAR"}};
Sumit Kumar3e274432021-09-14 06:37:56 -05001101 std::vector<std::string> associations;
1102
1103 // Create the event log to create the PEL from.
1104 manager.create("xyz.openbmc_project.Error.Test", 33, 0,
1105 phosphor::logging::Entry::Level::Error, additionalData,
1106 associations);
1107
1108 // Ensure a PEL was created in the repository
1109 auto pelData = findAnyPELInRepo();
1110 ASSERT_TRUE(pelData);
1111
1112 auto getPELData = readPELFile(*pelData);
1113 PEL pel(*getPELData);
1114
1115 // Spot check it. Other testcases cover the details.
1116 EXPECT_TRUE(pel.valid());
1117
1118 // Check for terminate bit set
1119 auto& hexwords = pel.primarySRC().value()->hexwordData();
1120 EXPECT_EQ(hexwords[3] & 0x20000000, 0x20000000);
1121}
Matt Spinler0003af12022-06-08 10:46:17 -05001122
1123TEST_F(ManagerTest, TestSanitizeFieldforDBus)
1124{
1125 std::string base{"(test0!}\n\t ~"};
1126 auto string = base;
1127 string += char{' ' - 1};
1128 string += char{'~' + 1};
1129 string += char{0};
1130 string += char{static_cast<char>(0xFF)};
1131
1132 // convert the last four chars to spaces
1133 EXPECT_EQ(Manager::sanitizeFieldForDBus(string), base + " ");
1134}
Matt Spinler0dd22c82023-05-04 15:28:12 -05001135
1136TEST_F(ManagerTest, TestFruPlug)
1137{
1138 const auto registry = R"(
1139{
1140 "PELs":
1141 [{
1142 "Name": "xyz.openbmc_project.Fan.Error.Fault",
1143 "Subsystem": "power_fans",
1144 "ComponentID": "0x2800",
1145 "SRC":
1146 {
1147 "Type": "11",
1148 "ReasonCode": "0x76F0",
1149 "Words6To9": {},
1150 "DeconfigFlag": true
1151 },
1152 "Callouts": [{
1153 "CalloutList": [
1154 {"Priority": "low", "LocCode": "P0"},
1155 {"Priority": "high", "LocCode": "A3"}
1156 ]
1157 }],
1158 "Documentation": {
1159 "Description": "A Fan Fault",
1160 "Message": "Fan had a Fault"
1161 }
1162 }]
1163}
1164)";
1165
1166 auto path = getPELReadOnlyDataPath();
1167 fs::create_directories(path);
1168 path /= "message_registry.json";
1169
1170 std::ofstream registryFile{path};
1171 registryFile << registry;
1172 registryFile.close();
1173
1174 std::unique_ptr<DataInterfaceBase> dataIface =
1175 std::make_unique<MockDataInterface>();
1176
1177 MockDataInterface* mockIface =
1178 reinterpret_cast<MockDataInterface*>(dataIface.get());
1179
1180 // Set up the mock calls used when building callouts
1181 EXPECT_CALL(*mockIface, getInventoryFromLocCode("P0", 0, false))
1182 .WillRepeatedly(Return(std::vector<std::string>{"motherboard"}));
1183 EXPECT_CALL(*mockIface, expandLocationCode("P0", 0))
1184 .WillRepeatedly(Return("U1234-P0"));
1185 EXPECT_CALL(*mockIface, getInventoryFromLocCode("U1234-P0", 0, true))
1186 .WillRepeatedly(Return(std::vector<std::string>{"motherboard"}));
1187
1188 EXPECT_CALL(*mockIface, getInventoryFromLocCode("A3", 0, false))
1189 .WillRepeatedly(Return(std::vector<std::string>{"fan"}));
1190 EXPECT_CALL(*mockIface, expandLocationCode("A3", 0))
1191 .WillRepeatedly(Return("U1234-A3"));
1192 EXPECT_CALL(*mockIface, getInventoryFromLocCode("U1234-A3", 0, true))
1193 .WillRepeatedly(Return(std::vector<std::string>{"fan"}));
1194
1195 std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
1196
1197 openpower::pels::Manager manager{
1198 logManager, std::move(dataIface),
1199 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
1200 std::placeholders::_2, std::placeholders::_3),
1201 std::move(journal)};
1202
Patrick Williamse5940632024-11-22 20:47:58 -05001203 std::map<std::string, std::string> additionalData;
Matt Spinler0dd22c82023-05-04 15:28:12 -05001204 std::vector<std::string> associations;
1205
1206 auto checkDeconfigured = [](bool deconfigured) {
1207 auto pelFile = findAnyPELInRepo();
1208 ASSERT_TRUE(pelFile);
1209
1210 auto data = readPELFile(*pelFile);
1211 PEL pel(*data);
1212 ASSERT_TRUE(pel.valid());
1213
1214 EXPECT_EQ(pel.primarySRC().value()->getErrorStatusFlag(
1215 SRC::ErrorStatusFlags::deconfigured),
1216 deconfigured);
1217 };
1218
1219 manager.create("xyz.openbmc_project.Fan.Error.Fault", 42, 0,
1220 phosphor::logging::Entry::Level::Error, additionalData,
1221 associations);
1222 checkDeconfigured(true);
1223
1224 // Replace A3 so PEL deconfigured flag should be set to false
1225 mockIface->fruPresent("U1234-A3");
1226 checkDeconfigured(false);
1227
1228 manager.erase(42);
1229
1230 // Create it again and replace a FRU not in the callout list.
1231 // Deconfig flag should stay on.
1232 manager.create("xyz.openbmc_project.Fan.Error.Fault", 43, 0,
1233 phosphor::logging::Entry::Level::Error, additionalData,
1234 associations);
1235 checkDeconfigured(true);
1236 mockIface->fruPresent("U1234-A4");
1237 checkDeconfigured(true);
1238}
harsh-agarwal1d763db32024-09-03 09:18:50 -05001239
1240int createHWIsolatedCalloutFile()
1241{
1242 json jsonCalloutDataList(nlohmann::json::value_t::array);
1243 json jsonDimmCallout;
1244
1245 jsonDimmCallout["LocationCode"] = "Ufcs-DIMM0";
1246 jsonDimmCallout["EntityPath"] = {35, 1, 0, 2, 0, 3, 0, 0, 0, 0, 0,
1247 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1248 jsonDimmCallout["GuardType"] = "GARD_Predictive";
1249 jsonDimmCallout["Deconfigured"] = false;
1250 jsonDimmCallout["Guarded"] = true;
1251 jsonDimmCallout["Priority"] = "M";
1252 jsonCalloutDataList.emplace_back(std::move(jsonDimmCallout));
1253
1254 std::string calloutData(jsonCalloutDataList.dump());
1255 std::string calloutFile("/tmp/phalPELCalloutsJson.XXXXXX");
1256 int fileFD = -1;
1257
1258 fileFD = mkostemp(calloutFile.data(), O_RDWR);
1259 if (fileFD == -1)
1260 {
1261 perror("Failed to create PELCallouts file");
1262 return -1;
1263 }
1264
1265 ssize_t rc = write(fileFD, calloutData.c_str(), calloutData.size());
1266 if (rc == -1)
1267 {
1268 perror("Failed to write PELCallouts file");
1269 close(fileFD);
1270 return -1;
1271 }
1272
1273 // Ensure we seek to the beginning of the file
1274 rc = lseek(fileFD, 0, SEEK_SET);
1275 if (rc == -1)
1276 {
1277 perror("Failed to set SEEK_SET for PELCallouts file");
1278 close(fileFD);
1279 return -1;
1280 }
1281 return fileFD;
1282}
1283
1284void appendFFDCEntry(int fd, uint8_t subTypeJson, uint8_t version,
1285 phosphor::logging::FFDCEntries& ffdcEntries)
1286{
1287 phosphor::logging::FFDCEntry ffdcEntry =
1288 std::make_tuple(sdbusplus::xyz::openbmc_project::Logging::server::
1289 Create::FFDCFormat::JSON,
1290 subTypeJson, version, fd);
1291 ffdcEntries.push_back(ffdcEntry);
1292}
1293
1294TEST_F(ManagerTest, TestPELDeleteWithoutHWIsolation)
1295{
1296 const auto registry = R"(
1297 {
1298 "PELs":
1299 [{
1300 "Name": "xyz.openbmc_project.Error.Test",
1301 "SRC":
1302 {
1303 "ReasonCode": "0x2030"
1304 },
1305 "Documentation": {
1306 "Description": "Test Error",
1307 "Message": "Test Error"
1308 }
1309 }]
1310 }
1311 )";
1312
1313 auto path = getPELReadOnlyDataPath();
1314 fs::create_directories(path);
1315 path /= "message_registry.json";
1316
1317 std::ofstream registryFile{path};
1318 registryFile << registry;
1319 registryFile.close();
1320
1321 std::unique_ptr<DataInterfaceBase> dataIface =
1322 std::make_unique<MockDataInterface>();
1323
1324 MockDataInterface* mockIface =
1325 reinterpret_cast<MockDataInterface*>(dataIface.get());
1326
1327 EXPECT_CALL(*mockIface, getInventoryFromLocCode("Ufcs-DIMM0", 0, false))
1328 .WillOnce(Return(std::vector<std::string>{
1329 "/xyz/openbmc_project/inventory/system/chassis/motherboard/dimm0"}));
1330
1331 // Mock the scenario where the hardware isolation guard is flagged
1332 // but is not associated, resulting in an empty list being returned.
1333 EXPECT_CALL(
1334 *mockIface,
1335 getAssociatedPaths(
1336 ::testing::StrEq(
1337 "/xyz/openbmc_project/logging/entry/42/isolated_hw_entry"),
1338 ::testing::StrEq("/"), 0,
1339 ::testing::ElementsAre(
1340 "xyz.openbmc_project.HardwareIsolation.Entry")))
1341 .WillRepeatedly(Return(std::vector<std::string>{}));
1342
1343 std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
1344 openpower::pels::Manager manager{
1345 logManager, std::move(dataIface),
1346 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
1347 std::placeholders::_2, std::placeholders::_3),
1348 std::move(journal)};
Patrick Williamse5940632024-11-22 20:47:58 -05001349 std::map<std::string, std::string> additionalData;
harsh-agarwal1d763db32024-09-03 09:18:50 -05001350 std::vector<std::string> associations;
1351
1352 // Check when there's no PEL with given id.
1353 {
1354 EXPECT_FALSE(manager.isDeleteProhibited(42));
1355 }
1356 // creating without ffdcEntries
1357 manager.create("xyz.openbmc_project.Error.Test", 42, 0,
1358 phosphor::logging::Entry::Level::Error, additionalData,
1359 associations);
1360 auto pelFile = findAnyPELInRepo();
1361 auto data = readPELFile(*pelFile);
1362 PEL pel_unguarded(*data);
1363 {
1364 // Verify that the guard flag is false.
1365 EXPECT_FALSE(pel_unguarded.getGuardFlag());
Patrick Williamse5940632024-11-22 20:47:58 -05001366 // Check that `isDeleteProhibited` returns false when the guard flag
1367 // is false.
harsh-agarwal1d763db32024-09-03 09:18:50 -05001368 EXPECT_FALSE(manager.isDeleteProhibited(42));
1369 }
1370 manager.erase(42);
1371 EXPECT_FALSE(findAnyPELInRepo());
1372
1373 int fd = createHWIsolatedCalloutFile();
1374 ASSERT_NE(fd, -1);
1375 uint8_t subTypeJson = 0xCA;
1376 uint8_t version = 0x01;
1377 phosphor::logging::FFDCEntries ffdcEntries;
1378 appendFFDCEntry(fd, subTypeJson, version, ffdcEntries);
1379 manager.create("xyz.openbmc_project.Error.Test", 42, 0,
1380 phosphor::logging::Entry::Level::Error, additionalData,
1381 associations, ffdcEntries);
1382 close(fd);
1383
1384 auto pelPathInRepo = findAnyPELInRepo();
1385 auto unguardedData = readPELFile(*pelPathInRepo);
1386 PEL pel(*unguardedData);
1387 {
1388 // Verify guard flag set to true
1389 EXPECT_TRUE(pel.getGuardFlag());
1390 // Check even if guard flag is true, if dbus call returns empty
1391 // array list then `isDeleteProhibited` returns false
1392 EXPECT_FALSE(manager.isDeleteProhibited(42));
1393 }
1394 manager.erase(42);
1395}
1396
1397TEST_F(ManagerTest, TestPELDeleteWithHWIsolation)
1398{
1399 const auto registry = R"(
1400 {
1401 "PELs":
1402 [{
1403 "Name": "xyz.openbmc_project.Error.Test",
1404 "Severity": "critical_system_term",
1405 "SRC":
1406 {
1407 "ReasonCode": "0x2030"
1408 },
1409 "Documentation": {
1410 "Description": "Test Error",
1411 "Message": "Test Error"
1412 }
1413 }]
1414 }
1415 )";
1416
1417 auto path = getPELReadOnlyDataPath();
1418 fs::create_directories(path);
1419 path /= "message_registry.json";
1420
1421 std::ofstream registryFile{path};
1422 registryFile << registry;
1423 registryFile.close();
1424
1425 std::unique_ptr<DataInterfaceBase> dataIface =
1426 std::make_unique<MockDataInterface>();
1427
1428 MockDataInterface* mockIface =
1429 reinterpret_cast<MockDataInterface*>(dataIface.get());
1430
1431 EXPECT_CALL(*mockIface, getInventoryFromLocCode("Ufcs-DIMM0", 0, false))
1432 .WillOnce(Return(std::vector<std::string>{
1433 "/xyz/openbmc_project/inventory/system/chassis/motherboard/dimm0"}));
1434
1435 EXPECT_CALL(
1436 *mockIface,
1437 getAssociatedPaths(
1438 ::testing::StrEq(
1439 "/xyz/openbmc_project/logging/entry/42/isolated_hw_entry"),
1440 ::testing::StrEq("/"), 0,
1441 ::testing::ElementsAre(
1442 "xyz.openbmc_project.HardwareIsolation.Entry")))
1443 .WillRepeatedly(Return(std::vector<std::string>{
1444 "/xyz/openbmc_project/hardware_isolation/entry/1"}));
1445
1446 std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
1447 openpower::pels::Manager manager{
1448 logManager, std::move(dataIface),
1449 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
1450 std::placeholders::_2, std::placeholders::_3),
1451 std::move(journal)};
Patrick Williamse5940632024-11-22 20:47:58 -05001452 std::map<std::string, std::string> additionalData;
harsh-agarwal1d763db32024-09-03 09:18:50 -05001453 std::vector<std::string> associations;
1454
1455 int fd = createHWIsolatedCalloutFile();
1456 ASSERT_NE(fd, -1);
1457 uint8_t subTypeJson = 0xCA;
1458 uint8_t version = 0x01;
1459 phosphor::logging::FFDCEntries ffdcEntries;
1460 appendFFDCEntry(fd, subTypeJson, version, ffdcEntries);
1461 manager.create("xyz.openbmc_project.Error.Test", 42, 0,
1462 phosphor::logging::Entry::Level::Error, additionalData,
1463 associations, ffdcEntries);
1464 close(fd);
1465
1466 auto pelFile = findAnyPELInRepo();
1467 EXPECT_TRUE(pelFile);
1468 auto data = readPELFile(*pelFile);
1469 PEL pel(*data);
1470 EXPECT_TRUE(pel.valid());
Patrick Williamse5940632024-11-22 20:47:58 -05001471 // Test case where the guard flag is set to true and the hardware
1472 // isolation guard is associated, which should result in
1473 // `isDeleteProhibited` returning true as expected.
harsh-agarwal1d763db32024-09-03 09:18:50 -05001474 EXPECT_TRUE(pel.getGuardFlag());
1475 EXPECT_TRUE(manager.isDeleteProhibited(42));
1476 manager.erase(42);
1477}