blob: b7146c93b5761f3da478d52bc4416c1d9a5532f7 [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 }
238 }
239 ]
240}
241)";
242
Matt Spinler0d804ef2020-05-12 16:16:26 -0500243 auto path = getPELReadOnlyDataPath();
Matt Spinlerd4ffb652019-11-12 14:16:14 -0600244 fs::create_directories(path);
245 path /= "message_registry.json";
246
Matt Spinler67456c22019-10-21 12:22:49 -0500247 std::ofstream registryFile{path};
248 registryFile << registry;
249 registryFile.close();
250
Matt Spinler67456c22019-10-21 12:22:49 -0500251 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500252 std::make_unique<MockDataInterface>();
Matt Spinler67456c22019-10-21 12:22:49 -0500253
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600254 openpower::pels::Manager manager{
255 logManager, std::move(dataIface),
256 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
257 std::placeholders::_2, std::placeholders::_3)};
Matt Spinler67456c22019-10-21 12:22:49 -0500258
259 std::vector<std::string> additionalData;
260 std::vector<std::string> associations;
261
262 // Create the event log to create the PEL from.
263 manager.create("xyz.openbmc_project.Error.Test", 33, 0,
264 phosphor::logging::Entry::Level::Error, additionalData,
265 associations);
266
267 // Ensure a PEL was created in the repository
268 auto pelFile = findAnyPELInRepo();
269 ASSERT_TRUE(pelFile);
270
271 auto data = readPELFile(*pelFile);
272 PEL pel(*data);
273
274 // Spot check it. Other testcases cover the details.
275 EXPECT_TRUE(pel.valid());
276 EXPECT_EQ(pel.obmcLogID(), 33);
277 EXPECT_EQ(pel.primarySRC().value()->asciiString(),
278 "BD612030 ");
279
280 // Remove it
281 manager.erase(33);
282 pelFile = findAnyPELInRepo();
283 EXPECT_FALSE(pelFile);
284
285 // Create an event log that can't be found in the registry.
286 manager.create("xyz.openbmc_project.Error.Foo", 33, 0,
287 phosphor::logging::Entry::Level::Error, additionalData,
288 associations);
289
290 // Currently, no PEL should be created. Eventually, a 'missing registry
291 // entry' PEL will be there.
292 pelFile = findAnyPELInRepo();
293 EXPECT_FALSE(pelFile);
294}
Matt Spinlera34ab722019-12-16 10:39:32 -0600295
296TEST_F(ManagerTest, TestDBusMethods)
297{
Matt Spinlera34ab722019-12-16 10:39:32 -0600298 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500299 std::make_unique<MockDataInterface>();
Matt Spinlera34ab722019-12-16 10:39:32 -0600300
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600301 Manager manager{logManager, std::move(dataIface),
302 std::bind(std::mem_fn(&TestLogger::log), &logger,
303 std::placeholders::_1, std::placeholders::_2,
304 std::placeholders::_3)};
Matt Spinlera34ab722019-12-16 10:39:32 -0600305
306 // Create a PEL, write it to a file, and pass that filename into
307 // the create function so there's one in the repo.
308 auto data = pelDataFactory(TestPELType::pelSimple);
309
310 fs::path pelFilename = makeTempDir() / "rawpel";
311 std::ofstream pelFile{pelFilename};
312 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
313 pelFile.close();
314
315 std::string adItem = "RAWPEL=" + pelFilename.string();
316 std::vector<std::string> additionalData{adItem};
317 std::vector<std::string> associations;
318
319 manager.create("error message", 42, 0,
320 phosphor::logging::Entry::Level::Error, additionalData,
321 associations);
322
323 // getPELFromOBMCID
324 auto newData = manager.getPELFromOBMCID(42);
325 EXPECT_EQ(newData.size(), data.size());
326
327 // Read the PEL to get the ID for later
328 PEL pel{newData};
329 auto id = pel.id();
330
331 EXPECT_THROW(
332 manager.getPELFromOBMCID(id + 1),
333 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
334
335 // getPEL
336 auto unixfd = manager.getPEL(id);
337
338 // Get the size
339 struct stat s;
340 int r = fstat(unixfd, &s);
341 ASSERT_EQ(r, 0);
342 auto size = s.st_size;
343
344 // Open the FD and check the contents
345 FILE* fp = fdopen(unixfd, "r");
346 ASSERT_NE(fp, nullptr);
347
348 std::vector<uint8_t> fdData;
349 fdData.resize(size);
350 r = fread(fdData.data(), 1, size, fp);
351 EXPECT_EQ(r, size);
352
353 EXPECT_EQ(newData, fdData);
354
355 fclose(fp);
356
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600357 // Run the event loop to close the FD
358 sdeventplus::Event e{sdEvent};
359 e.run(std::chrono::milliseconds(1));
360
Matt Spinlera34ab722019-12-16 10:39:32 -0600361 EXPECT_THROW(
362 manager.getPEL(id + 1),
363 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
364
365 // hostAck
366 manager.hostAck(id);
367
368 EXPECT_THROW(
369 manager.hostAck(id + 1),
370 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
371
372 // hostReject
373 manager.hostReject(id, Manager::RejectionReason::BadPEL);
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600374
375 // Run the event loop to log the bad PEL event
376 e.run(std::chrono::milliseconds(1));
377
378 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.SentBadPELToHost");
379 EXPECT_EQ(id, std::stoi(logger.ad["BAD_ID"], nullptr, 16));
380
Matt Spinlera34ab722019-12-16 10:39:32 -0600381 manager.hostReject(id, Manager::RejectionReason::HostFull);
382
383 EXPECT_THROW(
384 manager.hostReject(id + 1, Manager::RejectionReason::BadPEL),
385 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
386
387 fs::remove_all(pelFilename.parent_path());
388}
Matt Spinler19e72902020-01-24 11:05:20 -0600389
390// An ESEL from the wild
391const std::string esel{
392 "00 00 df 00 00 00 00 20 00 04 12 01 6f aa 00 00 "
393 "50 48 00 30 01 00 33 00 00 00 00 07 5c 69 cc 0d 00 00 00 07 5c d5 50 db "
394 "42 00 00 10 00 00 00 00 00 00 00 00 00 00 00 00 90 00 00 4e 90 00 00 4e "
395 "55 48 00 18 01 00 09 00 8a 03 40 00 00 00 00 00 ff ff 00 00 00 00 00 00 "
396 "50 53 00 50 01 01 00 00 02 00 00 09 33 2d 00 48 00 00 00 e0 00 00 10 00 "
397 "00 00 00 00 00 20 00 00 00 0c 00 02 00 00 00 fa 00 00 0c e4 00 00 00 12 "
398 "42 43 38 41 33 33 32 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
399 "20 20 20 20 20 20 20 20 55 44 00 1c 01 06 01 00 02 54 41 4b 00 00 00 06 "
400 "00 00 00 55 00 01 f9 20 00 00 00 00 55 44 00 24 01 06 01 00 01 54 41 4b "
401 "00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 00 23 01 00 02 00 05 00 00 "
402 "55 44 00 0c 01 0b 01 00 0f 01 00 00 55 44 00 10 01 04 01 00 0f 9f de 6a "
403 "00 01 00 00 55 44 00 7c 00 0c 01 00 00 13 0c 02 00 fa 0c e4 16 00 01 2c "
404 "0c 1c 16 00 00 fa 0a f0 14 00 00 fa 0b b8 14 00 00 be 09 60 12 00 01 2c "
405 "0d 7a 12 00 00 fa 0c 4e 10 00 00 fa 0c e4 10 00 00 be 0a 8c 16 00 01 2c "
406 "0c 1c 16 00 01 09 09 f6 16 00 00 fa 09 f6 14 00 00 fa 0b b8 14 00 00 fa "
407 "0a f0 14 00 00 be 08 ca 12 00 01 2c 0c e4 12 00 00 fa 0b 54 10 00 00 fa "
408 "0c 2d 10 00 00 be 08 ca 55 44 00 58 01 03 01 00 00 00 00 00 00 05 31 64 "
409 "00 00 00 00 00 05 0d d4 00 00 00 00 40 5f 06 e0 00 00 00 00 40 5d d2 00 "
410 "00 00 00 00 40 57 d3 d0 00 00 00 00 40 58 f6 a0 00 00 00 00 40 54 c9 34 "
411 "00 00 00 00 40 55 9a 10 00 00 00 00 40 4c 0a 80 00 00 00 00 00 00 27 14 "
412 "55 44 01 84 01 01 01 00 48 6f 73 74 62 6f 6f 74 20 42 75 69 6c 64 20 49 "
413 "44 3a 20 68 6f 73 74 62 6f 6f 74 2d 66 65 63 37 34 64 66 2d 70 30 61 38 "
414 "37 64 63 34 2f 68 62 69 63 6f 72 65 2e 62 69 6e 00 49 42 4d 2d 77 69 74 "
415 "68 65 72 73 70 6f 6f 6e 2d 4f 50 39 2d 76 32 2e 34 2d 39 2e 32 33 34 0a "
416 "09 6f 70 2d 62 75 69 6c 64 2d 38 32 66 34 63 66 30 0a 09 62 75 69 6c 64 "
417 "72 6f 6f 74 2d 32 30 31 39 2e 30 35 2e 32 2d 31 30 2d 67 38 39 35 39 31 "
418 "31 34 0a 09 73 6b 69 62 6f 6f 74 2d 76 36 2e 35 2d 31 38 2d 67 34 37 30 "
419 "66 66 62 35 66 32 39 64 37 0a 09 68 6f 73 74 62 6f 6f 74 2d 66 65 63 37 "
420 "34 64 66 2d 70 30 61 38 37 64 63 34 0a 09 6f 63 63 2d 65 34 35 39 37 61 "
421 "62 0a 09 6c 69 6e 75 78 2d 35 2e 32 2e 31 37 2d 6f 70 65 6e 70 6f 77 65 "
422 "72 31 2d 70 64 64 63 63 30 33 33 0a 09 70 65 74 69 74 62 6f 6f 74 2d 76 "
423 "31 2e 31 30 2e 34 0a 09 6d 61 63 68 69 6e 65 2d 78 6d 6c 2d 63 36 32 32 "
424 "63 62 35 2d 70 37 65 63 61 62 33 64 0a 09 68 6f 73 74 62 6f 6f 74 2d 62 "
425 "69 6e 61 72 69 65 73 2d 36 36 65 39 61 36 30 0a 09 63 61 70 70 2d 75 63 "
426 "6f 64 65 2d 70 39 2d 64 64 32 2d 76 34 0a 09 73 62 65 2d 36 30 33 33 30 "
427 "65 30 0a 09 68 63 6f 64 65 2d 68 77 30 39 32 31 31 39 61 2e 6f 70 6d 73 "
428 "74 0a 00 00 55 44 00 70 01 04 01 00 0f 9f de 6a 00 05 00 00 07 5f 1d f4 "
429 "30 32 43 59 34 37 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
430 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
431 "0b ac 54 02 59 41 31 39 33 34 36 39 37 30 35 38 00 00 00 00 00 00 05 22 "
432 "a1 58 01 8a 00 58 40 20 17 18 4d 2c 00 00 00 fc 01 a1 00 00 55 44 00 14 "
433 "01 08 01 00 00 00 00 01 00 00 00 5a 00 00 00 05 55 44 03 fc 01 15 31 00 "
434 "01 28 00 42 46 41 50 49 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 f4 "
435 "00 00 00 00 00 00 03 f4 00 00 00 0b 00 00 00 00 00 00 00 3d 2c 9b c2 84 "
436 "00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 09 "
437 "00 00 00 00 00 11 bd 20 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 "
438 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 00 00 00 00 00 00 01 2c "
439 "00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c 1c 00 00 00 64 00 00 00 3d "
440 "2c 9b d1 11 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 "
441 "00 00 00 0a 00 00 00 00 00 13 b5 a0 00 00 00 00 00 01 f8 80 00 00 00 00 "
442 "00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 00 00 00 00 "
443 "00 00 00 be 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0a 8c 00 00 00 64 "
444 "00 00 00 3d 2c 9b df 98 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 "
445 "00 00 00 00 00 00 00 0b 00 00 00 00 00 15 ae 20 00 00 00 00 00 01 f8 80 "
446 "00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 "
447 "00 00 00 00 00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c e4 "
448 "00 00 00 64 00 00 00 3d 2c 9b ea b7 00 00 01 e4 00 48 43 4f fb ed 70 b1 "
449 "00 00 02 01 00 00 00 00 00 00 00 0c 00 00 00 00 00 17 a6 a0 00 00 00 00 "
450 "00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 "
451 "00 00 00 12 00 00 00 00 00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 "
452 "00 00 0c 4e 00 00 00 64 00 00 00 3d 2c 9b f6 27 00 00 01 e4 00 48 43 4f "
453 "fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0d 00 00 00 00 00 19 9f 20 "
454 "00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 "
455 "00 00 00 00 00 00 00 12 00 00 00 00 00 00 01 2c 00 00 00 00 00 00 07 d0 "
456 "00 00 00 00 00 00 0d 7a 00 00 00 64 00 00 00 3d 2c 9c 05 75 00 00 01 e4 "
457 "00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0e 00 00 00 00 "
458 "00 1b 97 a0 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 "
459 "00 00 00 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 be 00 00 00 00 "
460 "00 00 07 d0 00 00 00 00 00 00 09 60 00 00 00 64 00 00 00 3d 2c 9c 11 29 "
461 "00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0f "
462 "00 00 00 00 00 1d 90 20 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 "
463 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 fa "
464 "00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0b b8 00 00 00 64 00 00 00 3d "
465 "2c 9c 1c 45 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 "
466 "00 00 00 10 00 00 00 00 00 1f 88 a0 00 00 00 00 00 01 f8 80 00 00 00 00 "
467 "00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 00 00 00 00 "
468 "00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0a f0 00 00 00 64 "
469 "00 00 00 3d 2c 9c 2b 14 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 "
470 "00 00 00 00 00 00 00 11 00 00 00 00 00 21 81 20 00 00 00 00 00 01 f8 80 "
471 "00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 "
472 "00 00 00 00 00 00 01 2c 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c 1c "
473 "00 00 00 64 00 00 00 3d 2d 6d 8f 9e 00 00 01 e4 00 00 43 4f 52 d7 9c 36 "
474 "00 00 04 73 00 00 00 1c 00 00 00 3d 2d 6d 99 ac 00 00 01 e4 00 10 43 4f "
475 "3f f2 02 3d 00 00 05 58 00 00 00 00 02 00 00 01 00 00 00 00 00 00 00 40 "
476 "00 00 00 2c 55 44 00 30 01 15 31 00 01 28 00 42 46 41 50 49 5f 44 42 47 "
477 "00 00 00 00 00 00 00 00 00 00 00 28 00 00 00 00 00 00 00 28 00 00 00 00 "
478 "00 00 00 00 55 44 01 74 01 15 31 00 01 28 00 42 46 41 50 49 5f 49 00 00 "
479 "00 00 00 00 00 00 00 00 00 00 01 6c 00 00 00 00 00 00 01 6c 00 00 00 0b "
480 "00 00 00 00 00 00 00 3c 0d 52 18 5e 00 00 01 e4 00 08 43 4f 46 79 94 13 "
481 "00 00 0a 5b 00 00 00 00 00 00 2c 00 00 00 00 24 00 00 00 3c 0d 6b 26 6c "
482 "00 00 01 e4 00 00 43 4f 4e 9b 18 74 00 00 01 03 00 00 00 1c 00 00 00 3c "
483 "12 b9 2d 13 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 00 00 00 1c "
484 "00 00 00 3c 13 02 73 53 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 "
485 "00 00 00 1c 00 00 00 3c 13 04 7c 94 00 00 01 e4 00 00 43 4f ea 31 ed d4 "
486 "00 00 05 c4 00 00 00 1c 00 00 00 3c 13 06 ad e1 00 00 01 e4 00 00 43 4f "
487 "ea 31 ed d4 00 00 05 c4 00 00 00 1c 00 00 00 3c 13 07 3f 77 00 00 01 e4 "
488 "00 00 43 4f 5e 4a 55 32 00 00 10 f2 00 00 00 1c 00 00 00 3c 13 07 4e e4 "
489 "00 00 01 e4 00 00 43 4f 5e 4a 55 32 00 00 0d 68 00 00 00 1c 00 00 00 3c "
490 "13 36 79 18 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 00 00 00 1c "
491 "00 00 00 3d 2c 9c 36 70 00 00 01 e4 00 00 43 4f 23 45 90 97 00 00 02 47 "
492 "00 00 00 1c 00 00 00 3d 2d 6d a3 ed 00 00 01 e4 00 08 43 4f 74 3a 5b 1a "
493 "00 00 04 cc 00 00 00 00 02 00 00 01 00 00 00 24 55 44 00 30 01 15 31 00 "
494 "01 28 00 42 53 43 41 4e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 28 "
495 "00 00 00 00 00 00 00 28 00 00 00 00 00 00 00 00"};
496
497TEST_F(ManagerTest, TestESELToRawData)
498{
499 auto data = Manager::eselToRawData(esel);
500
501 EXPECT_EQ(data.size(), 2464);
502
503 PEL pel{data};
504 EXPECT_TRUE(pel.valid());
505}
506
507TEST_F(ManagerTest, TestCreateWithESEL)
508{
509 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500510 std::make_unique<MockDataInterface>();
Matt Spinler19e72902020-01-24 11:05:20 -0600511
512 openpower::pels::Manager manager{
513 logManager, std::move(dataIface),
514 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
515 std::placeholders::_2, std::placeholders::_3)};
516
517 {
518 std::string adItem = "ESEL=" + esel;
519 std::vector<std::string> additionalData{adItem};
520 std::vector<std::string> associations;
521
522 manager.create("error message", 37, 0,
523 phosphor::logging::Entry::Level::Error, additionalData,
524 associations);
525
526 auto data = manager.getPELFromOBMCID(37);
527 PEL pel{data};
528 EXPECT_TRUE(pel.valid());
529 }
530
531 // Now an invalid one
532 {
533 std::string adItem = "ESEL=" + esel;
534
535 // Crop it
536 adItem.resize(adItem.size() - 300);
537
538 std::vector<std::string> additionalData{adItem};
539 std::vector<std::string> associations;
540
541 manager.create("error message", 38, 0,
542 phosphor::logging::Entry::Level::Error, additionalData,
543 associations);
544
545 EXPECT_THROW(
546 manager.getPELFromOBMCID(38),
547 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
548
549 // Run the event loop to log the bad PEL event
550 sdeventplus::Event e{sdEvent};
551 e.run(std::chrono::milliseconds(1));
552
553 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.BadHostPEL");
554 EXPECT_EQ(logger.errLevel, phosphor::logging::Entry::Level::Error);
555 }
556}
Matt Spinler7e727a32020-07-07 15:00:17 -0500557
558// Test that PELs will be pruned when necessary
559TEST_F(ManagerTest, TestPruning)
560{
561 sdeventplus::Event e{sdEvent};
562
563 std::unique_ptr<DataInterfaceBase> dataIface =
564 std::make_unique<MockDataInterface>();
565
566 openpower::pels::Manager manager{
567 logManager, std::move(dataIface),
568 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
569 std::placeholders::_2, std::placeholders::_3)};
570
571 // Create 25 1000B (4096B on disk each, which is what is used for pruning)
572 // BMC non-informational PELs in the 100KB repository. After the 24th one,
573 // the repo will be 96% full and a prune should be triggered to remove all
574 // but 7 to get under 30% full. Then when the 25th is added there will be
575 // 8 left.
576
577 auto dir = makeTempDir();
578 for (int i = 1; i <= 25; i++)
579 {
580 auto data = pelFactory(42, 'O', 0x40, 0x8800, 1000);
581
582 fs::path pelFilename = dir / "rawpel";
583 std::ofstream pelFile{pelFilename};
584 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
585 pelFile.close();
586
587 std::string adItem = "RAWPEL=" + pelFilename.string();
588 std::vector<std::string> additionalData{adItem};
589 std::vector<std::string> associations;
590
591 manager.create("error message", 42, 0,
592 phosphor::logging::Entry::Level::Error, additionalData,
593 associations);
594
595 // Simulate the code getting back to the event loop
596 // after each create.
597 e.run(std::chrono::milliseconds(1));
598
599 if (i < 24)
600 {
601 EXPECT_EQ(countPELsInRepo(), i);
602 }
603 else if (i == 24)
604 {
605 // Prune occured
606 EXPECT_EQ(countPELsInRepo(), 7);
607 }
608 else // i == 25
609 {
610 EXPECT_EQ(countPELsInRepo(), 8);
611 }
612 }
613
614 try
615 {
616 // Make sure the 8 newest ones are still found.
617 for (uint32_t i = 0; i < 8; i++)
618 {
619 manager.getPEL(0x50000012 + i);
620 }
621 }
622 catch (sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument& e)
623 {
624 ADD_FAILURE() << "PELs should have all been found";
625 }
626
627 fs::remove_all(dir);
628}
Matt Spinlerff9cec22020-07-15 13:06:35 -0500629
630// Test that manually deleting a PEL file will be recognized by the code.
631TEST_F(ManagerTest, TestPELManualDelete)
632{
633 sdeventplus::Event e{sdEvent};
634
635 std::unique_ptr<DataInterfaceBase> dataIface =
636 std::make_unique<MockDataInterface>();
637
638 openpower::pels::Manager manager{
639 logManager, std::move(dataIface),
640 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
641 std::placeholders::_2, std::placeholders::_3)};
642
643 auto data = pelDataFactory(TestPELType::pelSimple);
644 auto dir = makeTempDir();
645 fs::path pelFilename = dir / "rawpel";
646
647 std::string adItem = "RAWPEL=" + pelFilename.string();
648 std::vector<std::string> additionalData{adItem};
649 std::vector<std::string> associations;
650
651 // Add 20 PELs, they will get incrementing IDs like
652 // 0x50000001, 0x50000002, etc.
653 for (int i = 1; i <= 20; i++)
654 {
655 std::ofstream pelFile{pelFilename};
656 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
657 pelFile.close();
658
659 manager.create("error message", 42, 0,
660 phosphor::logging::Entry::Level::Error, additionalData,
661 associations);
662
663 // Sanity check this ID is really there so we can test
664 // it was deleted later. This will throw an exception if
665 // not present.
666 manager.getPEL(0x50000000 + i);
667
668 // Run an event loop pass where the internal FD is deleted
669 // after the getPEL function call.
670 e.run(std::chrono::milliseconds(1));
671 }
672
673 EXPECT_EQ(countPELsInRepo(), 20);
674
675 deletePELFile(0x50000001);
676
677 // Run a single event loop pass so the inotify event can run
678 e.run(std::chrono::milliseconds(1));
679
680 EXPECT_EQ(countPELsInRepo(), 19);
681
682 EXPECT_THROW(
683 manager.getPEL(0x50000001),
684 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
685
686 // Delete a few more, they should all get handled in the same
687 // event loop pass
688 std::vector<uint32_t> toDelete{0x50000002, 0x50000003, 0x50000004,
689 0x50000005, 0x50000006};
690 std::for_each(toDelete.begin(), toDelete.end(),
691 [](auto i) { deletePELFile(i); });
692
693 e.run(std::chrono::milliseconds(1));
694
695 EXPECT_EQ(countPELsInRepo(), 14);
696
697 std::for_each(toDelete.begin(), toDelete.end(), [&manager](const auto i) {
698 EXPECT_THROW(
699 manager.getPEL(i),
700 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
701 });
702
703 fs::remove_all(dir);
704}
705
706// Test that deleting all PELs at once is handled OK.
707TEST_F(ManagerTest, TestPELManualDeleteAll)
708{
709 sdeventplus::Event e{sdEvent};
710
711 std::unique_ptr<DataInterfaceBase> dataIface =
712 std::make_unique<MockDataInterface>();
713
714 openpower::pels::Manager manager{
715 logManager, std::move(dataIface),
716 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
717 std::placeholders::_2, std::placeholders::_3)};
718
719 auto data = pelDataFactory(TestPELType::pelSimple);
720 auto dir = makeTempDir();
721 fs::path pelFilename = dir / "rawpel";
722
723 std::string adItem = "RAWPEL=" + pelFilename.string();
724 std::vector<std::string> additionalData{adItem};
725 std::vector<std::string> associations;
726
727 // Add 200 PELs, they will get incrementing IDs like
728 // 0x50000001, 0x50000002, etc.
729 for (int i = 1; i <= 200; i++)
730 {
731 std::ofstream pelFile{pelFilename};
732 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
733 pelFile.close();
734
735 manager.create("error message", 42, 0,
736 phosphor::logging::Entry::Level::Error, additionalData,
737 associations);
738
739 // Sanity check this ID is really there so we can test
740 // it was deleted later. This will throw an exception if
741 // not present.
742 manager.getPEL(0x50000000 + i);
743
744 // Run an event loop pass where the internal FD is deleted
745 // after the getPEL function call.
746 e.run(std::chrono::milliseconds(1));
747 }
748
749 // Delete them all at once
750 auto logPath = getPELRepoPath() / "logs";
751 std::string cmd = "rm " + logPath.string() + "/*";
752 system(cmd.c_str());
753
754 EXPECT_EQ(countPELsInRepo(), 0);
755
756 // It will take 5 event loop passes to process them all
757 for (int i = 0; i < 5; i++)
758 {
759 e.run(std::chrono::milliseconds(1));
760 }
761
762 for (int i = 1; i <= 200; i++)
763 {
764 EXPECT_THROW(
765 manager.getPEL(0x50000000 + i),
766 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
767 }
768
769 fs::remove_all(dir);
770}