blob: bf1a18f5a7c6fc30b3a88c9616336fb77dfdbcfe [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;
32
Matt Spinler05c2c6c2019-12-18 14:02:09 -060033class TestLogger
34{
35 public:
36 void log(const std::string& name, phosphor::logging::Entry::Level level,
37 const EventLogger::ADMap& additionalData)
38 {
39 errName = name;
40 errLevel = level;
41 ad = additionalData;
42 }
43
44 std::string errName;
45 phosphor::logging::Entry::Level errLevel;
46 EventLogger::ADMap ad;
47};
48
Matt Spinler89fa0822019-07-17 13:54:30 -050049class ManagerTest : public CleanPELFiles
50{
Matt Spinler6b1a5c82020-01-07 08:48:53 -060051 public:
Matt Spinlere6b48f12020-04-02 09:51:39 -050052 ManagerTest() :
53 bus(sdbusplus::get_mocked_new(&sdbusInterface)),
54 logManager(bus, "logging_path")
Matt Spinler6b1a5c82020-01-07 08:48:53 -060055 {
56 sd_event_default(&sdEvent);
Matt Spinler6b1a5c82020-01-07 08:48:53 -060057 }
58
59 ~ManagerTest()
60 {
61 sd_event_unref(sdEvent);
62 }
63
Matt Spinlere6b48f12020-04-02 09:51:39 -050064 NiceMock<sdbusplus::SdBusMock> sdbusInterface;
65 sdbusplus::bus::bus bus;
Matt Spinler6b1a5c82020-01-07 08:48:53 -060066 phosphor::logging::internal::Manager logManager;
67 sd_event* sdEvent;
Matt Spinler05c2c6c2019-12-18 14:02:09 -060068 TestLogger logger;
Matt Spinler89fa0822019-07-17 13:54:30 -050069};
70
71fs::path makeTempDir()
72{
73 char path[] = "/tmp/tempnameXXXXXX";
74 std::filesystem::path dir = mkdtemp(path);
75 return dir;
76}
77
Matt Spinler67456c22019-10-21 12:22:49 -050078std::optional<fs::path> findAnyPELInRepo()
79{
80 // PELs are named <timestamp>_<ID>
81 std::regex expr{"\\d+_\\d+"};
82
83 for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs"))
84 {
85 if (std::regex_search(f.path().string(), expr))
86 {
87 return f.path();
88 }
89 }
90 return std::nullopt;
91}
92
Matt Spinler7e727a32020-07-07 15:00:17 -050093size_t countPELsInRepo()
94{
95 size_t count = 0;
96 std::regex expr{"\\d+_\\d+"};
97
98 for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs"))
99 {
100 if (std::regex_search(f.path().string(), expr))
101 {
102 count++;
103 }
104 }
105 return count;
106}
107
Matt Spinlerff9cec22020-07-15 13:06:35 -0500108void deletePELFile(uint32_t id)
109{
110 char search[20];
111
112 sprintf(search, "\\d+_%.8X", id);
113 std::regex expr{search};
114
115 for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs"))
116 {
117 if (std::regex_search(f.path().string(), expr))
118 {
119 fs::remove(f.path());
120 break;
121 }
122 }
123}
124
Matt Spinler89fa0822019-07-17 13:54:30 -0500125// Test that using the RAWPEL=<file> with the Manager::create() call gets
126// a PEL saved in the repository.
127TEST_F(ManagerTest, TestCreateWithPEL)
128{
Matt Spinlerc8705e22019-09-11 12:36:07 -0500129 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500130 std::make_unique<MockDataInterface>();
Matt Spinler89fa0822019-07-17 13:54:30 -0500131
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600132 openpower::pels::Manager manager{
133 logManager, std::move(dataIface),
134 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
135 std::placeholders::_2, std::placeholders::_3)};
Matt Spinler89fa0822019-07-17 13:54:30 -0500136
137 // Create a PEL, write it to a file, and pass that filename into
138 // the create function.
Matt Spinler42828bd2019-10-11 10:39:30 -0500139 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler89fa0822019-07-17 13:54:30 -0500140
141 fs::path pelFilename = makeTempDir() / "rawpel";
142 std::ofstream pelFile{pelFilename};
Matt Spinler42828bd2019-10-11 10:39:30 -0500143 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
Matt Spinler89fa0822019-07-17 13:54:30 -0500144 pelFile.close();
145
146 std::string adItem = "RAWPEL=" + pelFilename.string();
147 std::vector<std::string> additionalData{adItem};
148 std::vector<std::string> associations;
149
Matt Spinler367144c2019-09-19 15:33:52 -0500150 manager.create("error message", 42, 0,
151 phosphor::logging::Entry::Level::Error, additionalData,
Matt Spinler89fa0822019-07-17 13:54:30 -0500152 associations);
153
Matt Spinler67456c22019-10-21 12:22:49 -0500154 // Find the file in the PEL repository directory
155 auto pelPathInRepo = findAnyPELInRepo();
Matt Spinler89fa0822019-07-17 13:54:30 -0500156
Matt Spinler67456c22019-10-21 12:22:49 -0500157 EXPECT_TRUE(pelPathInRepo);
Matt Spinler89fa0822019-07-17 13:54:30 -0500158
Matt Spinler475e5742019-07-18 16:09:49 -0500159 // Now remove it based on its OpenBMC event log ID
160 manager.erase(42);
161
Matt Spinler67456c22019-10-21 12:22:49 -0500162 pelPathInRepo = findAnyPELInRepo();
Matt Spinler475e5742019-07-18 16:09:49 -0500163
Matt Spinler67456c22019-10-21 12:22:49 -0500164 EXPECT_FALSE(pelPathInRepo);
Matt Spinler475e5742019-07-18 16:09:49 -0500165
Matt Spinler89fa0822019-07-17 13:54:30 -0500166 fs::remove_all(pelFilename.parent_path());
167}
Matt Spinler67456c22019-10-21 12:22:49 -0500168
Matt Spinlere95fd012020-01-07 12:53:16 -0600169TEST_F(ManagerTest, TestCreateWithInvalidPEL)
170{
171 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500172 std::make_unique<MockDataInterface>();
Matt Spinlere95fd012020-01-07 12:53:16 -0600173
174 openpower::pels::Manager manager{
175 logManager, std::move(dataIface),
176 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
177 std::placeholders::_2, std::placeholders::_3)};
178
179 // Create a PEL, write it to a file, and pass that filename into
180 // the create function.
181 auto data = pelDataFactory(TestPELType::pelSimple);
182
183 // Truncate it to make it invalid.
184 data.resize(200);
185
186 fs::path pelFilename = makeTempDir() / "rawpel";
187 std::ofstream pelFile{pelFilename};
188 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
189 pelFile.close();
190
191 std::string adItem = "RAWPEL=" + pelFilename.string();
192 std::vector<std::string> additionalData{adItem};
193 std::vector<std::string> associations;
194
195 manager.create("error message", 42, 0,
196 phosphor::logging::Entry::Level::Error, additionalData,
197 associations);
198
199 // Run the event loop to log the bad PEL event
200 sdeventplus::Event e{sdEvent};
201 e.run(std::chrono::milliseconds(1));
202
203 PEL invalidPEL{data};
204 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.BadHostPEL");
205 EXPECT_EQ(logger.errLevel, phosphor::logging::Entry::Level::Error);
206 EXPECT_EQ(std::stoi(logger.ad["PLID"], nullptr, 16), invalidPEL.plid());
207 EXPECT_EQ(logger.ad["OBMC_LOG_ID"], "42");
208 EXPECT_EQ(logger.ad["SRC"], (*invalidPEL.primarySRC())->asciiString());
209 EXPECT_EQ(logger.ad["PEL_SIZE"], std::to_string(data.size()));
210
Matt Spinlerfe721892020-04-02 10:28:08 -0500211 // Check that the bad PEL data was saved to a file.
212 auto badPELData = readPELFile(getPELRepoPath() / "badPEL");
213 EXPECT_EQ(*badPELData, data);
214
Matt Spinlere95fd012020-01-07 12:53:16 -0600215 fs::remove_all(pelFilename.parent_path());
216}
217
Matt Spinler67456c22019-10-21 12:22:49 -0500218// Test that the message registry can be used to build a PEL.
219TEST_F(ManagerTest, TestCreateWithMessageRegistry)
220{
221 const auto registry = R"(
222{
223 "PELs":
224 [
225 {
226 "Name": "xyz.openbmc_project.Error.Test",
227 "Subsystem": "power_supply",
228 "ActionFlags": ["service_action", "report"],
229 "SRC":
230 {
231 "ReasonCode": "0x2030"
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800232 },
233 "Documentation":
234 {
235 "Description": "A PGOOD Fault",
236 "Message": "PS had a PGOOD Fault"
Matt Spinler67456c22019-10-21 12:22:49 -0500237 }
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500238 },
239 {
240 "Name": "xyz.openbmc_project.Logging.Error.Default",
241 "Subsystem": "bmc_firmware",
242 "SRC":
243 {
244 "ReasonCode": "0x2031"
245 },
246 "Documentation":
247 {
248 "Description": "The entry used when no match found",
249 "Message": "This is a generic SRC"
250 }
Matt Spinler67456c22019-10-21 12:22:49 -0500251 }
252 ]
253}
254)";
255
Matt Spinler0d804ef2020-05-12 16:16:26 -0500256 auto path = getPELReadOnlyDataPath();
Matt Spinlerd4ffb652019-11-12 14:16:14 -0600257 fs::create_directories(path);
258 path /= "message_registry.json";
259
Matt Spinler67456c22019-10-21 12:22:49 -0500260 std::ofstream registryFile{path};
261 registryFile << registry;
262 registryFile.close();
263
Matt Spinler67456c22019-10-21 12:22:49 -0500264 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500265 std::make_unique<MockDataInterface>();
Matt Spinler67456c22019-10-21 12:22:49 -0500266
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600267 openpower::pels::Manager manager{
268 logManager, std::move(dataIface),
269 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
270 std::placeholders::_2, std::placeholders::_3)};
Matt Spinler67456c22019-10-21 12:22:49 -0500271
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500272 std::vector<std::string> additionalData{"FOO=BAR"};
Matt Spinler67456c22019-10-21 12:22:49 -0500273 std::vector<std::string> associations;
274
275 // Create the event log to create the PEL from.
276 manager.create("xyz.openbmc_project.Error.Test", 33, 0,
277 phosphor::logging::Entry::Level::Error, additionalData,
278 associations);
279
280 // Ensure a PEL was created in the repository
281 auto pelFile = findAnyPELInRepo();
282 ASSERT_TRUE(pelFile);
283
284 auto data = readPELFile(*pelFile);
285 PEL pel(*data);
286
287 // Spot check it. Other testcases cover the details.
288 EXPECT_TRUE(pel.valid());
289 EXPECT_EQ(pel.obmcLogID(), 33);
290 EXPECT_EQ(pel.primarySRC().value()->asciiString(),
291 "BD612030 ");
292
293 // Remove it
294 manager.erase(33);
295 pelFile = findAnyPELInRepo();
296 EXPECT_FALSE(pelFile);
297
298 // Create an event log that can't be found in the registry.
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500299 // In this case, xyz.openbmc_project.Logging.Error.Default will
300 // be used as the key instead to find a registry match.
301 manager.create("xyz.openbmc_project.Error.Foo", 42, 0,
Matt Spinler67456c22019-10-21 12:22:49 -0500302 phosphor::logging::Entry::Level::Error, additionalData,
303 associations);
304
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500305 // Ensure a PEL was still created in the repository
Matt Spinler67456c22019-10-21 12:22:49 -0500306 pelFile = findAnyPELInRepo();
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500307 ASSERT_TRUE(pelFile);
308
309 data = readPELFile(*pelFile);
310 PEL newPEL(*data);
311
312 EXPECT_TRUE(newPEL.valid());
313 EXPECT_EQ(newPEL.obmcLogID(), 42);
314 EXPECT_EQ(newPEL.primarySRC().value()->asciiString(),
315 "BD8D2031 ");
316
317 // Check for both the original AdditionalData item as well as
318 // the ERROR_NAME item that should contain the error message
319 // property that wasn't found.
320 std::string errorName;
321 std::string adItem;
322
323 for (const auto& section : newPEL.optionalSections())
324 {
325 if (SectionID::userData == static_cast<SectionID>(section->header().id))
326 {
327 if (UserDataFormat::json ==
328 static_cast<UserDataFormat>(section->header().subType))
329 {
330 auto ud = static_cast<UserData*>(section.get());
331
332 // Check that there was a UserData section added that
333 // contains debug details about the device.
334 const auto& d = ud->data();
335 std::string jsonString{d.begin(), d.end()};
336 auto json = nlohmann::json::parse(jsonString);
337
338 if (json.contains("ERROR_NAME"))
339 {
340 errorName = json["ERROR_NAME"].get<std::string>();
341 }
342
343 if (json.contains("FOO"))
344 {
345 adItem = json["FOO"].get<std::string>();
346 }
347 }
348 }
349 if (!errorName.empty())
350 {
351 break;
352 }
353 }
354
355 EXPECT_EQ(errorName, "xyz.openbmc_project.Error.Foo");
356 EXPECT_EQ(adItem, "BAR");
Matt Spinler67456c22019-10-21 12:22:49 -0500357}
Matt Spinlera34ab722019-12-16 10:39:32 -0600358
359TEST_F(ManagerTest, TestDBusMethods)
360{
Matt Spinlera34ab722019-12-16 10:39:32 -0600361 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500362 std::make_unique<MockDataInterface>();
Matt Spinlera34ab722019-12-16 10:39:32 -0600363
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600364 Manager manager{logManager, std::move(dataIface),
365 std::bind(std::mem_fn(&TestLogger::log), &logger,
366 std::placeholders::_1, std::placeholders::_2,
367 std::placeholders::_3)};
Matt Spinlera34ab722019-12-16 10:39:32 -0600368
369 // Create a PEL, write it to a file, and pass that filename into
370 // the create function so there's one in the repo.
371 auto data = pelDataFactory(TestPELType::pelSimple);
372
373 fs::path pelFilename = makeTempDir() / "rawpel";
374 std::ofstream pelFile{pelFilename};
375 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
376 pelFile.close();
377
378 std::string adItem = "RAWPEL=" + pelFilename.string();
379 std::vector<std::string> additionalData{adItem};
380 std::vector<std::string> associations;
381
382 manager.create("error message", 42, 0,
383 phosphor::logging::Entry::Level::Error, additionalData,
384 associations);
385
386 // getPELFromOBMCID
387 auto newData = manager.getPELFromOBMCID(42);
388 EXPECT_EQ(newData.size(), data.size());
389
390 // Read the PEL to get the ID for later
391 PEL pel{newData};
392 auto id = pel.id();
393
394 EXPECT_THROW(
395 manager.getPELFromOBMCID(id + 1),
396 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
397
398 // getPEL
399 auto unixfd = manager.getPEL(id);
400
401 // Get the size
402 struct stat s;
403 int r = fstat(unixfd, &s);
404 ASSERT_EQ(r, 0);
405 auto size = s.st_size;
406
407 // Open the FD and check the contents
408 FILE* fp = fdopen(unixfd, "r");
409 ASSERT_NE(fp, nullptr);
410
411 std::vector<uint8_t> fdData;
412 fdData.resize(size);
413 r = fread(fdData.data(), 1, size, fp);
414 EXPECT_EQ(r, size);
415
416 EXPECT_EQ(newData, fdData);
417
418 fclose(fp);
419
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600420 // Run the event loop to close the FD
421 sdeventplus::Event e{sdEvent};
422 e.run(std::chrono::milliseconds(1));
423
Matt Spinlera34ab722019-12-16 10:39:32 -0600424 EXPECT_THROW(
425 manager.getPEL(id + 1),
426 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
427
428 // hostAck
429 manager.hostAck(id);
430
431 EXPECT_THROW(
432 manager.hostAck(id + 1),
433 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
434
435 // hostReject
436 manager.hostReject(id, Manager::RejectionReason::BadPEL);
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600437
438 // Run the event loop to log the bad PEL event
439 e.run(std::chrono::milliseconds(1));
440
441 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.SentBadPELToHost");
442 EXPECT_EQ(id, std::stoi(logger.ad["BAD_ID"], nullptr, 16));
443
Matt Spinlera34ab722019-12-16 10:39:32 -0600444 manager.hostReject(id, Manager::RejectionReason::HostFull);
445
446 EXPECT_THROW(
447 manager.hostReject(id + 1, Manager::RejectionReason::BadPEL),
448 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
449
450 fs::remove_all(pelFilename.parent_path());
451}
Matt Spinler19e72902020-01-24 11:05:20 -0600452
453// An ESEL from the wild
454const std::string esel{
455 "00 00 df 00 00 00 00 20 00 04 12 01 6f aa 00 00 "
456 "50 48 00 30 01 00 33 00 00 00 00 07 5c 69 cc 0d 00 00 00 07 5c d5 50 db "
457 "42 00 00 10 00 00 00 00 00 00 00 00 00 00 00 00 90 00 00 4e 90 00 00 4e "
458 "55 48 00 18 01 00 09 00 8a 03 40 00 00 00 00 00 ff ff 00 00 00 00 00 00 "
459 "50 53 00 50 01 01 00 00 02 00 00 09 33 2d 00 48 00 00 00 e0 00 00 10 00 "
460 "00 00 00 00 00 20 00 00 00 0c 00 02 00 00 00 fa 00 00 0c e4 00 00 00 12 "
461 "42 43 38 41 33 33 32 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
462 "20 20 20 20 20 20 20 20 55 44 00 1c 01 06 01 00 02 54 41 4b 00 00 00 06 "
463 "00 00 00 55 00 01 f9 20 00 00 00 00 55 44 00 24 01 06 01 00 01 54 41 4b "
464 "00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 00 23 01 00 02 00 05 00 00 "
465 "55 44 00 0c 01 0b 01 00 0f 01 00 00 55 44 00 10 01 04 01 00 0f 9f de 6a "
466 "00 01 00 00 55 44 00 7c 00 0c 01 00 00 13 0c 02 00 fa 0c e4 16 00 01 2c "
467 "0c 1c 16 00 00 fa 0a f0 14 00 00 fa 0b b8 14 00 00 be 09 60 12 00 01 2c "
468 "0d 7a 12 00 00 fa 0c 4e 10 00 00 fa 0c e4 10 00 00 be 0a 8c 16 00 01 2c "
469 "0c 1c 16 00 01 09 09 f6 16 00 00 fa 09 f6 14 00 00 fa 0b b8 14 00 00 fa "
470 "0a f0 14 00 00 be 08 ca 12 00 01 2c 0c e4 12 00 00 fa 0b 54 10 00 00 fa "
471 "0c 2d 10 00 00 be 08 ca 55 44 00 58 01 03 01 00 00 00 00 00 00 05 31 64 "
472 "00 00 00 00 00 05 0d d4 00 00 00 00 40 5f 06 e0 00 00 00 00 40 5d d2 00 "
473 "00 00 00 00 40 57 d3 d0 00 00 00 00 40 58 f6 a0 00 00 00 00 40 54 c9 34 "
474 "00 00 00 00 40 55 9a 10 00 00 00 00 40 4c 0a 80 00 00 00 00 00 00 27 14 "
475 "55 44 01 84 01 01 01 00 48 6f 73 74 62 6f 6f 74 20 42 75 69 6c 64 20 49 "
476 "44 3a 20 68 6f 73 74 62 6f 6f 74 2d 66 65 63 37 34 64 66 2d 70 30 61 38 "
477 "37 64 63 34 2f 68 62 69 63 6f 72 65 2e 62 69 6e 00 49 42 4d 2d 77 69 74 "
478 "68 65 72 73 70 6f 6f 6e 2d 4f 50 39 2d 76 32 2e 34 2d 39 2e 32 33 34 0a "
479 "09 6f 70 2d 62 75 69 6c 64 2d 38 32 66 34 63 66 30 0a 09 62 75 69 6c 64 "
480 "72 6f 6f 74 2d 32 30 31 39 2e 30 35 2e 32 2d 31 30 2d 67 38 39 35 39 31 "
481 "31 34 0a 09 73 6b 69 62 6f 6f 74 2d 76 36 2e 35 2d 31 38 2d 67 34 37 30 "
482 "66 66 62 35 66 32 39 64 37 0a 09 68 6f 73 74 62 6f 6f 74 2d 66 65 63 37 "
483 "34 64 66 2d 70 30 61 38 37 64 63 34 0a 09 6f 63 63 2d 65 34 35 39 37 61 "
484 "62 0a 09 6c 69 6e 75 78 2d 35 2e 32 2e 31 37 2d 6f 70 65 6e 70 6f 77 65 "
485 "72 31 2d 70 64 64 63 63 30 33 33 0a 09 70 65 74 69 74 62 6f 6f 74 2d 76 "
486 "31 2e 31 30 2e 34 0a 09 6d 61 63 68 69 6e 65 2d 78 6d 6c 2d 63 36 32 32 "
487 "63 62 35 2d 70 37 65 63 61 62 33 64 0a 09 68 6f 73 74 62 6f 6f 74 2d 62 "
488 "69 6e 61 72 69 65 73 2d 36 36 65 39 61 36 30 0a 09 63 61 70 70 2d 75 63 "
489 "6f 64 65 2d 70 39 2d 64 64 32 2d 76 34 0a 09 73 62 65 2d 36 30 33 33 30 "
490 "65 30 0a 09 68 63 6f 64 65 2d 68 77 30 39 32 31 31 39 61 2e 6f 70 6d 73 "
491 "74 0a 00 00 55 44 00 70 01 04 01 00 0f 9f de 6a 00 05 00 00 07 5f 1d f4 "
492 "30 32 43 59 34 37 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
493 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
494 "0b ac 54 02 59 41 31 39 33 34 36 39 37 30 35 38 00 00 00 00 00 00 05 22 "
495 "a1 58 01 8a 00 58 40 20 17 18 4d 2c 00 00 00 fc 01 a1 00 00 55 44 00 14 "
496 "01 08 01 00 00 00 00 01 00 00 00 5a 00 00 00 05 55 44 03 fc 01 15 31 00 "
497 "01 28 00 42 46 41 50 49 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 f4 "
498 "00 00 00 00 00 00 03 f4 00 00 00 0b 00 00 00 00 00 00 00 3d 2c 9b c2 84 "
499 "00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 09 "
500 "00 00 00 00 00 11 bd 20 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 "
501 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 00 00 00 00 00 00 01 2c "
502 "00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c 1c 00 00 00 64 00 00 00 3d "
503 "2c 9b d1 11 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 "
504 "00 00 00 0a 00 00 00 00 00 13 b5 a0 00 00 00 00 00 01 f8 80 00 00 00 00 "
505 "00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 00 00 00 00 "
506 "00 00 00 be 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0a 8c 00 00 00 64 "
507 "00 00 00 3d 2c 9b df 98 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 "
508 "00 00 00 00 00 00 00 0b 00 00 00 00 00 15 ae 20 00 00 00 00 00 01 f8 80 "
509 "00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 "
510 "00 00 00 00 00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c e4 "
511 "00 00 00 64 00 00 00 3d 2c 9b ea b7 00 00 01 e4 00 48 43 4f fb ed 70 b1 "
512 "00 00 02 01 00 00 00 00 00 00 00 0c 00 00 00 00 00 17 a6 a0 00 00 00 00 "
513 "00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 "
514 "00 00 00 12 00 00 00 00 00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 "
515 "00 00 0c 4e 00 00 00 64 00 00 00 3d 2c 9b f6 27 00 00 01 e4 00 48 43 4f "
516 "fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0d 00 00 00 00 00 19 9f 20 "
517 "00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 "
518 "00 00 00 00 00 00 00 12 00 00 00 00 00 00 01 2c 00 00 00 00 00 00 07 d0 "
519 "00 00 00 00 00 00 0d 7a 00 00 00 64 00 00 00 3d 2c 9c 05 75 00 00 01 e4 "
520 "00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0e 00 00 00 00 "
521 "00 1b 97 a0 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 "
522 "00 00 00 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 be 00 00 00 00 "
523 "00 00 07 d0 00 00 00 00 00 00 09 60 00 00 00 64 00 00 00 3d 2c 9c 11 29 "
524 "00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0f "
525 "00 00 00 00 00 1d 90 20 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 "
526 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 fa "
527 "00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0b b8 00 00 00 64 00 00 00 3d "
528 "2c 9c 1c 45 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 "
529 "00 00 00 10 00 00 00 00 00 1f 88 a0 00 00 00 00 00 01 f8 80 00 00 00 00 "
530 "00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 00 00 00 00 "
531 "00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0a f0 00 00 00 64 "
532 "00 00 00 3d 2c 9c 2b 14 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 "
533 "00 00 00 00 00 00 00 11 00 00 00 00 00 21 81 20 00 00 00 00 00 01 f8 80 "
534 "00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 "
535 "00 00 00 00 00 00 01 2c 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c 1c "
536 "00 00 00 64 00 00 00 3d 2d 6d 8f 9e 00 00 01 e4 00 00 43 4f 52 d7 9c 36 "
537 "00 00 04 73 00 00 00 1c 00 00 00 3d 2d 6d 99 ac 00 00 01 e4 00 10 43 4f "
538 "3f f2 02 3d 00 00 05 58 00 00 00 00 02 00 00 01 00 00 00 00 00 00 00 40 "
539 "00 00 00 2c 55 44 00 30 01 15 31 00 01 28 00 42 46 41 50 49 5f 44 42 47 "
540 "00 00 00 00 00 00 00 00 00 00 00 28 00 00 00 00 00 00 00 28 00 00 00 00 "
541 "00 00 00 00 55 44 01 74 01 15 31 00 01 28 00 42 46 41 50 49 5f 49 00 00 "
542 "00 00 00 00 00 00 00 00 00 00 01 6c 00 00 00 00 00 00 01 6c 00 00 00 0b "
543 "00 00 00 00 00 00 00 3c 0d 52 18 5e 00 00 01 e4 00 08 43 4f 46 79 94 13 "
544 "00 00 0a 5b 00 00 00 00 00 00 2c 00 00 00 00 24 00 00 00 3c 0d 6b 26 6c "
545 "00 00 01 e4 00 00 43 4f 4e 9b 18 74 00 00 01 03 00 00 00 1c 00 00 00 3c "
546 "12 b9 2d 13 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 00 00 00 1c "
547 "00 00 00 3c 13 02 73 53 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 "
548 "00 00 00 1c 00 00 00 3c 13 04 7c 94 00 00 01 e4 00 00 43 4f ea 31 ed d4 "
549 "00 00 05 c4 00 00 00 1c 00 00 00 3c 13 06 ad e1 00 00 01 e4 00 00 43 4f "
550 "ea 31 ed d4 00 00 05 c4 00 00 00 1c 00 00 00 3c 13 07 3f 77 00 00 01 e4 "
551 "00 00 43 4f 5e 4a 55 32 00 00 10 f2 00 00 00 1c 00 00 00 3c 13 07 4e e4 "
552 "00 00 01 e4 00 00 43 4f 5e 4a 55 32 00 00 0d 68 00 00 00 1c 00 00 00 3c "
553 "13 36 79 18 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 00 00 00 1c "
554 "00 00 00 3d 2c 9c 36 70 00 00 01 e4 00 00 43 4f 23 45 90 97 00 00 02 47 "
555 "00 00 00 1c 00 00 00 3d 2d 6d a3 ed 00 00 01 e4 00 08 43 4f 74 3a 5b 1a "
556 "00 00 04 cc 00 00 00 00 02 00 00 01 00 00 00 24 55 44 00 30 01 15 31 00 "
557 "01 28 00 42 53 43 41 4e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 28 "
558 "00 00 00 00 00 00 00 28 00 00 00 00 00 00 00 00"};
559
560TEST_F(ManagerTest, TestESELToRawData)
561{
562 auto data = Manager::eselToRawData(esel);
563
564 EXPECT_EQ(data.size(), 2464);
565
566 PEL pel{data};
567 EXPECT_TRUE(pel.valid());
568}
569
570TEST_F(ManagerTest, TestCreateWithESEL)
571{
572 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500573 std::make_unique<MockDataInterface>();
Matt Spinler19e72902020-01-24 11:05:20 -0600574
575 openpower::pels::Manager manager{
576 logManager, std::move(dataIface),
577 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
578 std::placeholders::_2, std::placeholders::_3)};
579
580 {
581 std::string adItem = "ESEL=" + esel;
582 std::vector<std::string> additionalData{adItem};
583 std::vector<std::string> associations;
584
585 manager.create("error message", 37, 0,
586 phosphor::logging::Entry::Level::Error, additionalData,
587 associations);
588
589 auto data = manager.getPELFromOBMCID(37);
590 PEL pel{data};
591 EXPECT_TRUE(pel.valid());
592 }
593
594 // Now an invalid one
595 {
596 std::string adItem = "ESEL=" + esel;
597
598 // Crop it
599 adItem.resize(adItem.size() - 300);
600
601 std::vector<std::string> additionalData{adItem};
602 std::vector<std::string> associations;
603
604 manager.create("error message", 38, 0,
605 phosphor::logging::Entry::Level::Error, additionalData,
606 associations);
607
608 EXPECT_THROW(
609 manager.getPELFromOBMCID(38),
610 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
611
612 // Run the event loop to log the bad PEL event
613 sdeventplus::Event e{sdEvent};
614 e.run(std::chrono::milliseconds(1));
615
616 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.BadHostPEL");
617 EXPECT_EQ(logger.errLevel, phosphor::logging::Entry::Level::Error);
618 }
619}
Matt Spinler7e727a32020-07-07 15:00:17 -0500620
621// Test that PELs will be pruned when necessary
622TEST_F(ManagerTest, TestPruning)
623{
624 sdeventplus::Event e{sdEvent};
625
626 std::unique_ptr<DataInterfaceBase> dataIface =
627 std::make_unique<MockDataInterface>();
628
629 openpower::pels::Manager manager{
630 logManager, std::move(dataIface),
631 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
632 std::placeholders::_2, std::placeholders::_3)};
633
634 // Create 25 1000B (4096B on disk each, which is what is used for pruning)
635 // BMC non-informational PELs in the 100KB repository. After the 24th one,
636 // the repo will be 96% full and a prune should be triggered to remove all
637 // but 7 to get under 30% full. Then when the 25th is added there will be
638 // 8 left.
639
640 auto dir = makeTempDir();
641 for (int i = 1; i <= 25; i++)
642 {
643 auto data = pelFactory(42, 'O', 0x40, 0x8800, 1000);
644
645 fs::path pelFilename = dir / "rawpel";
646 std::ofstream pelFile{pelFilename};
647 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
648 pelFile.close();
649
650 std::string adItem = "RAWPEL=" + pelFilename.string();
651 std::vector<std::string> additionalData{adItem};
652 std::vector<std::string> associations;
653
654 manager.create("error message", 42, 0,
655 phosphor::logging::Entry::Level::Error, additionalData,
656 associations);
657
658 // Simulate the code getting back to the event loop
659 // after each create.
660 e.run(std::chrono::milliseconds(1));
661
662 if (i < 24)
663 {
664 EXPECT_EQ(countPELsInRepo(), i);
665 }
666 else if (i == 24)
667 {
668 // Prune occured
669 EXPECT_EQ(countPELsInRepo(), 7);
670 }
671 else // i == 25
672 {
673 EXPECT_EQ(countPELsInRepo(), 8);
674 }
675 }
676
677 try
678 {
679 // Make sure the 8 newest ones are still found.
680 for (uint32_t i = 0; i < 8; i++)
681 {
682 manager.getPEL(0x50000012 + i);
683 }
684 }
685 catch (sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument& e)
686 {
687 ADD_FAILURE() << "PELs should have all been found";
688 }
689
690 fs::remove_all(dir);
691}
Matt Spinlerff9cec22020-07-15 13:06:35 -0500692
693// Test that manually deleting a PEL file will be recognized by the code.
694TEST_F(ManagerTest, TestPELManualDelete)
695{
696 sdeventplus::Event e{sdEvent};
697
698 std::unique_ptr<DataInterfaceBase> dataIface =
699 std::make_unique<MockDataInterface>();
700
701 openpower::pels::Manager manager{
702 logManager, std::move(dataIface),
703 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
704 std::placeholders::_2, std::placeholders::_3)};
705
706 auto data = pelDataFactory(TestPELType::pelSimple);
707 auto dir = makeTempDir();
708 fs::path pelFilename = dir / "rawpel";
709
710 std::string adItem = "RAWPEL=" + pelFilename.string();
711 std::vector<std::string> additionalData{adItem};
712 std::vector<std::string> associations;
713
714 // Add 20 PELs, they will get incrementing IDs like
715 // 0x50000001, 0x50000002, etc.
716 for (int i = 1; i <= 20; i++)
717 {
718 std::ofstream pelFile{pelFilename};
719 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
720 pelFile.close();
721
722 manager.create("error message", 42, 0,
723 phosphor::logging::Entry::Level::Error, additionalData,
724 associations);
725
726 // Sanity check this ID is really there so we can test
727 // it was deleted later. This will throw an exception if
728 // not present.
729 manager.getPEL(0x50000000 + i);
730
731 // Run an event loop pass where the internal FD is deleted
732 // after the getPEL function call.
733 e.run(std::chrono::milliseconds(1));
734 }
735
736 EXPECT_EQ(countPELsInRepo(), 20);
737
738 deletePELFile(0x50000001);
739
740 // Run a single event loop pass so the inotify event can run
741 e.run(std::chrono::milliseconds(1));
742
743 EXPECT_EQ(countPELsInRepo(), 19);
744
745 EXPECT_THROW(
746 manager.getPEL(0x50000001),
747 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
748
749 // Delete a few more, they should all get handled in the same
750 // event loop pass
751 std::vector<uint32_t> toDelete{0x50000002, 0x50000003, 0x50000004,
752 0x50000005, 0x50000006};
753 std::for_each(toDelete.begin(), toDelete.end(),
754 [](auto i) { deletePELFile(i); });
755
756 e.run(std::chrono::milliseconds(1));
757
758 EXPECT_EQ(countPELsInRepo(), 14);
759
760 std::for_each(toDelete.begin(), toDelete.end(), [&manager](const auto i) {
761 EXPECT_THROW(
762 manager.getPEL(i),
763 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
764 });
765
766 fs::remove_all(dir);
767}
768
769// Test that deleting all PELs at once is handled OK.
770TEST_F(ManagerTest, TestPELManualDeleteAll)
771{
772 sdeventplus::Event e{sdEvent};
773
774 std::unique_ptr<DataInterfaceBase> dataIface =
775 std::make_unique<MockDataInterface>();
776
777 openpower::pels::Manager manager{
778 logManager, std::move(dataIface),
779 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
780 std::placeholders::_2, std::placeholders::_3)};
781
782 auto data = pelDataFactory(TestPELType::pelSimple);
783 auto dir = makeTempDir();
784 fs::path pelFilename = dir / "rawpel";
785
786 std::string adItem = "RAWPEL=" + pelFilename.string();
787 std::vector<std::string> additionalData{adItem};
788 std::vector<std::string> associations;
789
790 // Add 200 PELs, they will get incrementing IDs like
791 // 0x50000001, 0x50000002, etc.
792 for (int i = 1; i <= 200; i++)
793 {
794 std::ofstream pelFile{pelFilename};
795 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
796 pelFile.close();
797
798 manager.create("error message", 42, 0,
799 phosphor::logging::Entry::Level::Error, additionalData,
800 associations);
801
802 // Sanity check this ID is really there so we can test
803 // it was deleted later. This will throw an exception if
804 // not present.
805 manager.getPEL(0x50000000 + i);
806
807 // Run an event loop pass where the internal FD is deleted
808 // after the getPEL function call.
809 e.run(std::chrono::milliseconds(1));
810 }
811
812 // Delete them all at once
813 auto logPath = getPELRepoPath() / "logs";
814 std::string cmd = "rm " + logPath.string() + "/*";
815 system(cmd.c_str());
816
817 EXPECT_EQ(countPELsInRepo(), 0);
818
819 // It will take 5 event loop passes to process them all
820 for (int i = 0; i < 5; i++)
821 {
822 e.run(std::chrono::milliseconds(1));
823 }
824
825 for (int i = 1; i <= 200; i++)
826 {
827 EXPECT_THROW(
828 manager.getPEL(0x50000000 + i),
829 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
830 }
831
832 fs::remove_all(dir);
833}