blob: bbc89a632f4965a05e17f5d8ff7ed77046b35abd [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 Spinler89fa0822019-07-17 13:54:30 -0500108// Test that using the RAWPEL=<file> with the Manager::create() call gets
109// a PEL saved in the repository.
110TEST_F(ManagerTest, TestCreateWithPEL)
111{
Matt Spinlerc8705e22019-09-11 12:36:07 -0500112 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500113 std::make_unique<MockDataInterface>();
Matt Spinler89fa0822019-07-17 13:54:30 -0500114
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600115 openpower::pels::Manager manager{
116 logManager, std::move(dataIface),
117 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
118 std::placeholders::_2, std::placeholders::_3)};
Matt Spinler89fa0822019-07-17 13:54:30 -0500119
120 // Create a PEL, write it to a file, and pass that filename into
121 // the create function.
Matt Spinler42828bd2019-10-11 10:39:30 -0500122 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler89fa0822019-07-17 13:54:30 -0500123
124 fs::path pelFilename = makeTempDir() / "rawpel";
125 std::ofstream pelFile{pelFilename};
Matt Spinler42828bd2019-10-11 10:39:30 -0500126 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
Matt Spinler89fa0822019-07-17 13:54:30 -0500127 pelFile.close();
128
129 std::string adItem = "RAWPEL=" + pelFilename.string();
130 std::vector<std::string> additionalData{adItem};
131 std::vector<std::string> associations;
132
Matt Spinler367144c2019-09-19 15:33:52 -0500133 manager.create("error message", 42, 0,
134 phosphor::logging::Entry::Level::Error, additionalData,
Matt Spinler89fa0822019-07-17 13:54:30 -0500135 associations);
136
Matt Spinler67456c22019-10-21 12:22:49 -0500137 // Find the file in the PEL repository directory
138 auto pelPathInRepo = findAnyPELInRepo();
Matt Spinler89fa0822019-07-17 13:54:30 -0500139
Matt Spinler67456c22019-10-21 12:22:49 -0500140 EXPECT_TRUE(pelPathInRepo);
Matt Spinler89fa0822019-07-17 13:54:30 -0500141
Matt Spinler475e5742019-07-18 16:09:49 -0500142 // Now remove it based on its OpenBMC event log ID
143 manager.erase(42);
144
Matt Spinler67456c22019-10-21 12:22:49 -0500145 pelPathInRepo = findAnyPELInRepo();
Matt Spinler475e5742019-07-18 16:09:49 -0500146
Matt Spinler67456c22019-10-21 12:22:49 -0500147 EXPECT_FALSE(pelPathInRepo);
Matt Spinler475e5742019-07-18 16:09:49 -0500148
Matt Spinler89fa0822019-07-17 13:54:30 -0500149 fs::remove_all(pelFilename.parent_path());
150}
Matt Spinler67456c22019-10-21 12:22:49 -0500151
Matt Spinlere95fd012020-01-07 12:53:16 -0600152TEST_F(ManagerTest, TestCreateWithInvalidPEL)
153{
154 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500155 std::make_unique<MockDataInterface>();
Matt Spinlere95fd012020-01-07 12:53:16 -0600156
157 openpower::pels::Manager manager{
158 logManager, std::move(dataIface),
159 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
160 std::placeholders::_2, std::placeholders::_3)};
161
162 // Create a PEL, write it to a file, and pass that filename into
163 // the create function.
164 auto data = pelDataFactory(TestPELType::pelSimple);
165
166 // Truncate it to make it invalid.
167 data.resize(200);
168
169 fs::path pelFilename = makeTempDir() / "rawpel";
170 std::ofstream pelFile{pelFilename};
171 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
172 pelFile.close();
173
174 std::string adItem = "RAWPEL=" + pelFilename.string();
175 std::vector<std::string> additionalData{adItem};
176 std::vector<std::string> associations;
177
178 manager.create("error message", 42, 0,
179 phosphor::logging::Entry::Level::Error, additionalData,
180 associations);
181
182 // Run the event loop to log the bad PEL event
183 sdeventplus::Event e{sdEvent};
184 e.run(std::chrono::milliseconds(1));
185
186 PEL invalidPEL{data};
187 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.BadHostPEL");
188 EXPECT_EQ(logger.errLevel, phosphor::logging::Entry::Level::Error);
189 EXPECT_EQ(std::stoi(logger.ad["PLID"], nullptr, 16), invalidPEL.plid());
190 EXPECT_EQ(logger.ad["OBMC_LOG_ID"], "42");
191 EXPECT_EQ(logger.ad["SRC"], (*invalidPEL.primarySRC())->asciiString());
192 EXPECT_EQ(logger.ad["PEL_SIZE"], std::to_string(data.size()));
193
Matt Spinlerfe721892020-04-02 10:28:08 -0500194 // Check that the bad PEL data was saved to a file.
195 auto badPELData = readPELFile(getPELRepoPath() / "badPEL");
196 EXPECT_EQ(*badPELData, data);
197
Matt Spinlere95fd012020-01-07 12:53:16 -0600198 fs::remove_all(pelFilename.parent_path());
199}
200
Matt Spinler67456c22019-10-21 12:22:49 -0500201// Test that the message registry can be used to build a PEL.
202TEST_F(ManagerTest, TestCreateWithMessageRegistry)
203{
204 const auto registry = R"(
205{
206 "PELs":
207 [
208 {
209 "Name": "xyz.openbmc_project.Error.Test",
210 "Subsystem": "power_supply",
211 "ActionFlags": ["service_action", "report"],
212 "SRC":
213 {
214 "ReasonCode": "0x2030"
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800215 },
216 "Documentation":
217 {
218 "Description": "A PGOOD Fault",
219 "Message": "PS had a PGOOD Fault"
Matt Spinler67456c22019-10-21 12:22:49 -0500220 }
221 }
222 ]
223}
224)";
225
Matt Spinler0d804ef2020-05-12 16:16:26 -0500226 auto path = getPELReadOnlyDataPath();
Matt Spinlerd4ffb652019-11-12 14:16:14 -0600227 fs::create_directories(path);
228 path /= "message_registry.json";
229
Matt Spinler67456c22019-10-21 12:22:49 -0500230 std::ofstream registryFile{path};
231 registryFile << registry;
232 registryFile.close();
233
Matt Spinler67456c22019-10-21 12:22:49 -0500234 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500235 std::make_unique<MockDataInterface>();
Matt Spinler67456c22019-10-21 12:22:49 -0500236
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600237 openpower::pels::Manager manager{
238 logManager, std::move(dataIface),
239 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
240 std::placeholders::_2, std::placeholders::_3)};
Matt Spinler67456c22019-10-21 12:22:49 -0500241
242 std::vector<std::string> additionalData;
243 std::vector<std::string> associations;
244
245 // Create the event log to create the PEL from.
246 manager.create("xyz.openbmc_project.Error.Test", 33, 0,
247 phosphor::logging::Entry::Level::Error, additionalData,
248 associations);
249
250 // Ensure a PEL was created in the repository
251 auto pelFile = findAnyPELInRepo();
252 ASSERT_TRUE(pelFile);
253
254 auto data = readPELFile(*pelFile);
255 PEL pel(*data);
256
257 // Spot check it. Other testcases cover the details.
258 EXPECT_TRUE(pel.valid());
259 EXPECT_EQ(pel.obmcLogID(), 33);
260 EXPECT_EQ(pel.primarySRC().value()->asciiString(),
261 "BD612030 ");
262
263 // Remove it
264 manager.erase(33);
265 pelFile = findAnyPELInRepo();
266 EXPECT_FALSE(pelFile);
267
268 // Create an event log that can't be found in the registry.
269 manager.create("xyz.openbmc_project.Error.Foo", 33, 0,
270 phosphor::logging::Entry::Level::Error, additionalData,
271 associations);
272
273 // Currently, no PEL should be created. Eventually, a 'missing registry
274 // entry' PEL will be there.
275 pelFile = findAnyPELInRepo();
276 EXPECT_FALSE(pelFile);
277}
Matt Spinlera34ab722019-12-16 10:39:32 -0600278
279TEST_F(ManagerTest, TestDBusMethods)
280{
Matt Spinlera34ab722019-12-16 10:39:32 -0600281 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500282 std::make_unique<MockDataInterface>();
Matt Spinlera34ab722019-12-16 10:39:32 -0600283
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600284 Manager manager{logManager, std::move(dataIface),
285 std::bind(std::mem_fn(&TestLogger::log), &logger,
286 std::placeholders::_1, std::placeholders::_2,
287 std::placeholders::_3)};
Matt Spinlera34ab722019-12-16 10:39:32 -0600288
289 // Create a PEL, write it to a file, and pass that filename into
290 // the create function so there's one in the repo.
291 auto data = pelDataFactory(TestPELType::pelSimple);
292
293 fs::path pelFilename = makeTempDir() / "rawpel";
294 std::ofstream pelFile{pelFilename};
295 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
296 pelFile.close();
297
298 std::string adItem = "RAWPEL=" + pelFilename.string();
299 std::vector<std::string> additionalData{adItem};
300 std::vector<std::string> associations;
301
302 manager.create("error message", 42, 0,
303 phosphor::logging::Entry::Level::Error, additionalData,
304 associations);
305
306 // getPELFromOBMCID
307 auto newData = manager.getPELFromOBMCID(42);
308 EXPECT_EQ(newData.size(), data.size());
309
310 // Read the PEL to get the ID for later
311 PEL pel{newData};
312 auto id = pel.id();
313
314 EXPECT_THROW(
315 manager.getPELFromOBMCID(id + 1),
316 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
317
318 // getPEL
319 auto unixfd = manager.getPEL(id);
320
321 // Get the size
322 struct stat s;
323 int r = fstat(unixfd, &s);
324 ASSERT_EQ(r, 0);
325 auto size = s.st_size;
326
327 // Open the FD and check the contents
328 FILE* fp = fdopen(unixfd, "r");
329 ASSERT_NE(fp, nullptr);
330
331 std::vector<uint8_t> fdData;
332 fdData.resize(size);
333 r = fread(fdData.data(), 1, size, fp);
334 EXPECT_EQ(r, size);
335
336 EXPECT_EQ(newData, fdData);
337
338 fclose(fp);
339
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600340 // Run the event loop to close the FD
341 sdeventplus::Event e{sdEvent};
342 e.run(std::chrono::milliseconds(1));
343
Matt Spinlera34ab722019-12-16 10:39:32 -0600344 EXPECT_THROW(
345 manager.getPEL(id + 1),
346 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
347
348 // hostAck
349 manager.hostAck(id);
350
351 EXPECT_THROW(
352 manager.hostAck(id + 1),
353 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
354
355 // hostReject
356 manager.hostReject(id, Manager::RejectionReason::BadPEL);
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600357
358 // Run the event loop to log the bad PEL event
359 e.run(std::chrono::milliseconds(1));
360
361 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.SentBadPELToHost");
362 EXPECT_EQ(id, std::stoi(logger.ad["BAD_ID"], nullptr, 16));
363
Matt Spinlera34ab722019-12-16 10:39:32 -0600364 manager.hostReject(id, Manager::RejectionReason::HostFull);
365
366 EXPECT_THROW(
367 manager.hostReject(id + 1, Manager::RejectionReason::BadPEL),
368 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
369
370 fs::remove_all(pelFilename.parent_path());
371}
Matt Spinler19e72902020-01-24 11:05:20 -0600372
373// An ESEL from the wild
374const std::string esel{
375 "00 00 df 00 00 00 00 20 00 04 12 01 6f aa 00 00 "
376 "50 48 00 30 01 00 33 00 00 00 00 07 5c 69 cc 0d 00 00 00 07 5c d5 50 db "
377 "42 00 00 10 00 00 00 00 00 00 00 00 00 00 00 00 90 00 00 4e 90 00 00 4e "
378 "55 48 00 18 01 00 09 00 8a 03 40 00 00 00 00 00 ff ff 00 00 00 00 00 00 "
379 "50 53 00 50 01 01 00 00 02 00 00 09 33 2d 00 48 00 00 00 e0 00 00 10 00 "
380 "00 00 00 00 00 20 00 00 00 0c 00 02 00 00 00 fa 00 00 0c e4 00 00 00 12 "
381 "42 43 38 41 33 33 32 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
382 "20 20 20 20 20 20 20 20 55 44 00 1c 01 06 01 00 02 54 41 4b 00 00 00 06 "
383 "00 00 00 55 00 01 f9 20 00 00 00 00 55 44 00 24 01 06 01 00 01 54 41 4b "
384 "00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 00 23 01 00 02 00 05 00 00 "
385 "55 44 00 0c 01 0b 01 00 0f 01 00 00 55 44 00 10 01 04 01 00 0f 9f de 6a "
386 "00 01 00 00 55 44 00 7c 00 0c 01 00 00 13 0c 02 00 fa 0c e4 16 00 01 2c "
387 "0c 1c 16 00 00 fa 0a f0 14 00 00 fa 0b b8 14 00 00 be 09 60 12 00 01 2c "
388 "0d 7a 12 00 00 fa 0c 4e 10 00 00 fa 0c e4 10 00 00 be 0a 8c 16 00 01 2c "
389 "0c 1c 16 00 01 09 09 f6 16 00 00 fa 09 f6 14 00 00 fa 0b b8 14 00 00 fa "
390 "0a f0 14 00 00 be 08 ca 12 00 01 2c 0c e4 12 00 00 fa 0b 54 10 00 00 fa "
391 "0c 2d 10 00 00 be 08 ca 55 44 00 58 01 03 01 00 00 00 00 00 00 05 31 64 "
392 "00 00 00 00 00 05 0d d4 00 00 00 00 40 5f 06 e0 00 00 00 00 40 5d d2 00 "
393 "00 00 00 00 40 57 d3 d0 00 00 00 00 40 58 f6 a0 00 00 00 00 40 54 c9 34 "
394 "00 00 00 00 40 55 9a 10 00 00 00 00 40 4c 0a 80 00 00 00 00 00 00 27 14 "
395 "55 44 01 84 01 01 01 00 48 6f 73 74 62 6f 6f 74 20 42 75 69 6c 64 20 49 "
396 "44 3a 20 68 6f 73 74 62 6f 6f 74 2d 66 65 63 37 34 64 66 2d 70 30 61 38 "
397 "37 64 63 34 2f 68 62 69 63 6f 72 65 2e 62 69 6e 00 49 42 4d 2d 77 69 74 "
398 "68 65 72 73 70 6f 6f 6e 2d 4f 50 39 2d 76 32 2e 34 2d 39 2e 32 33 34 0a "
399 "09 6f 70 2d 62 75 69 6c 64 2d 38 32 66 34 63 66 30 0a 09 62 75 69 6c 64 "
400 "72 6f 6f 74 2d 32 30 31 39 2e 30 35 2e 32 2d 31 30 2d 67 38 39 35 39 31 "
401 "31 34 0a 09 73 6b 69 62 6f 6f 74 2d 76 36 2e 35 2d 31 38 2d 67 34 37 30 "
402 "66 66 62 35 66 32 39 64 37 0a 09 68 6f 73 74 62 6f 6f 74 2d 66 65 63 37 "
403 "34 64 66 2d 70 30 61 38 37 64 63 34 0a 09 6f 63 63 2d 65 34 35 39 37 61 "
404 "62 0a 09 6c 69 6e 75 78 2d 35 2e 32 2e 31 37 2d 6f 70 65 6e 70 6f 77 65 "
405 "72 31 2d 70 64 64 63 63 30 33 33 0a 09 70 65 74 69 74 62 6f 6f 74 2d 76 "
406 "31 2e 31 30 2e 34 0a 09 6d 61 63 68 69 6e 65 2d 78 6d 6c 2d 63 36 32 32 "
407 "63 62 35 2d 70 37 65 63 61 62 33 64 0a 09 68 6f 73 74 62 6f 6f 74 2d 62 "
408 "69 6e 61 72 69 65 73 2d 36 36 65 39 61 36 30 0a 09 63 61 70 70 2d 75 63 "
409 "6f 64 65 2d 70 39 2d 64 64 32 2d 76 34 0a 09 73 62 65 2d 36 30 33 33 30 "
410 "65 30 0a 09 68 63 6f 64 65 2d 68 77 30 39 32 31 31 39 61 2e 6f 70 6d 73 "
411 "74 0a 00 00 55 44 00 70 01 04 01 00 0f 9f de 6a 00 05 00 00 07 5f 1d f4 "
412 "30 32 43 59 34 37 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
413 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
414 "0b ac 54 02 59 41 31 39 33 34 36 39 37 30 35 38 00 00 00 00 00 00 05 22 "
415 "a1 58 01 8a 00 58 40 20 17 18 4d 2c 00 00 00 fc 01 a1 00 00 55 44 00 14 "
416 "01 08 01 00 00 00 00 01 00 00 00 5a 00 00 00 05 55 44 03 fc 01 15 31 00 "
417 "01 28 00 42 46 41 50 49 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 f4 "
418 "00 00 00 00 00 00 03 f4 00 00 00 0b 00 00 00 00 00 00 00 3d 2c 9b c2 84 "
419 "00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 09 "
420 "00 00 00 00 00 11 bd 20 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 "
421 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 00 00 00 00 00 00 01 2c "
422 "00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c 1c 00 00 00 64 00 00 00 3d "
423 "2c 9b d1 11 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 "
424 "00 00 00 0a 00 00 00 00 00 13 b5 a0 00 00 00 00 00 01 f8 80 00 00 00 00 "
425 "00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 00 00 00 00 "
426 "00 00 00 be 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0a 8c 00 00 00 64 "
427 "00 00 00 3d 2c 9b df 98 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 "
428 "00 00 00 00 00 00 00 0b 00 00 00 00 00 15 ae 20 00 00 00 00 00 01 f8 80 "
429 "00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 "
430 "00 00 00 00 00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c e4 "
431 "00 00 00 64 00 00 00 3d 2c 9b ea b7 00 00 01 e4 00 48 43 4f fb ed 70 b1 "
432 "00 00 02 01 00 00 00 00 00 00 00 0c 00 00 00 00 00 17 a6 a0 00 00 00 00 "
433 "00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 "
434 "00 00 00 12 00 00 00 00 00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 "
435 "00 00 0c 4e 00 00 00 64 00 00 00 3d 2c 9b f6 27 00 00 01 e4 00 48 43 4f "
436 "fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0d 00 00 00 00 00 19 9f 20 "
437 "00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 "
438 "00 00 00 00 00 00 00 12 00 00 00 00 00 00 01 2c 00 00 00 00 00 00 07 d0 "
439 "00 00 00 00 00 00 0d 7a 00 00 00 64 00 00 00 3d 2c 9c 05 75 00 00 01 e4 "
440 "00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0e 00 00 00 00 "
441 "00 1b 97 a0 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 "
442 "00 00 00 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 be 00 00 00 00 "
443 "00 00 07 d0 00 00 00 00 00 00 09 60 00 00 00 64 00 00 00 3d 2c 9c 11 29 "
444 "00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0f "
445 "00 00 00 00 00 1d 90 20 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 "
446 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 fa "
447 "00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0b b8 00 00 00 64 00 00 00 3d "
448 "2c 9c 1c 45 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 "
449 "00 00 00 10 00 00 00 00 00 1f 88 a0 00 00 00 00 00 01 f8 80 00 00 00 00 "
450 "00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 00 00 00 00 "
451 "00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0a f0 00 00 00 64 "
452 "00 00 00 3d 2c 9c 2b 14 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 "
453 "00 00 00 00 00 00 00 11 00 00 00 00 00 21 81 20 00 00 00 00 00 01 f8 80 "
454 "00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 "
455 "00 00 00 00 00 00 01 2c 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c 1c "
456 "00 00 00 64 00 00 00 3d 2d 6d 8f 9e 00 00 01 e4 00 00 43 4f 52 d7 9c 36 "
457 "00 00 04 73 00 00 00 1c 00 00 00 3d 2d 6d 99 ac 00 00 01 e4 00 10 43 4f "
458 "3f f2 02 3d 00 00 05 58 00 00 00 00 02 00 00 01 00 00 00 00 00 00 00 40 "
459 "00 00 00 2c 55 44 00 30 01 15 31 00 01 28 00 42 46 41 50 49 5f 44 42 47 "
460 "00 00 00 00 00 00 00 00 00 00 00 28 00 00 00 00 00 00 00 28 00 00 00 00 "
461 "00 00 00 00 55 44 01 74 01 15 31 00 01 28 00 42 46 41 50 49 5f 49 00 00 "
462 "00 00 00 00 00 00 00 00 00 00 01 6c 00 00 00 00 00 00 01 6c 00 00 00 0b "
463 "00 00 00 00 00 00 00 3c 0d 52 18 5e 00 00 01 e4 00 08 43 4f 46 79 94 13 "
464 "00 00 0a 5b 00 00 00 00 00 00 2c 00 00 00 00 24 00 00 00 3c 0d 6b 26 6c "
465 "00 00 01 e4 00 00 43 4f 4e 9b 18 74 00 00 01 03 00 00 00 1c 00 00 00 3c "
466 "12 b9 2d 13 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 00 00 00 1c "
467 "00 00 00 3c 13 02 73 53 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 "
468 "00 00 00 1c 00 00 00 3c 13 04 7c 94 00 00 01 e4 00 00 43 4f ea 31 ed d4 "
469 "00 00 05 c4 00 00 00 1c 00 00 00 3c 13 06 ad e1 00 00 01 e4 00 00 43 4f "
470 "ea 31 ed d4 00 00 05 c4 00 00 00 1c 00 00 00 3c 13 07 3f 77 00 00 01 e4 "
471 "00 00 43 4f 5e 4a 55 32 00 00 10 f2 00 00 00 1c 00 00 00 3c 13 07 4e e4 "
472 "00 00 01 e4 00 00 43 4f 5e 4a 55 32 00 00 0d 68 00 00 00 1c 00 00 00 3c "
473 "13 36 79 18 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 00 00 00 1c "
474 "00 00 00 3d 2c 9c 36 70 00 00 01 e4 00 00 43 4f 23 45 90 97 00 00 02 47 "
475 "00 00 00 1c 00 00 00 3d 2d 6d a3 ed 00 00 01 e4 00 08 43 4f 74 3a 5b 1a "
476 "00 00 04 cc 00 00 00 00 02 00 00 01 00 00 00 24 55 44 00 30 01 15 31 00 "
477 "01 28 00 42 53 43 41 4e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 28 "
478 "00 00 00 00 00 00 00 28 00 00 00 00 00 00 00 00"};
479
480TEST_F(ManagerTest, TestESELToRawData)
481{
482 auto data = Manager::eselToRawData(esel);
483
484 EXPECT_EQ(data.size(), 2464);
485
486 PEL pel{data};
487 EXPECT_TRUE(pel.valid());
488}
489
490TEST_F(ManagerTest, TestCreateWithESEL)
491{
492 std::unique_ptr<DataInterfaceBase> dataIface =
Matt Spinlere6b48f12020-04-02 09:51:39 -0500493 std::make_unique<MockDataInterface>();
Matt Spinler19e72902020-01-24 11:05:20 -0600494
495 openpower::pels::Manager manager{
496 logManager, std::move(dataIface),
497 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
498 std::placeholders::_2, std::placeholders::_3)};
499
500 {
501 std::string adItem = "ESEL=" + esel;
502 std::vector<std::string> additionalData{adItem};
503 std::vector<std::string> associations;
504
505 manager.create("error message", 37, 0,
506 phosphor::logging::Entry::Level::Error, additionalData,
507 associations);
508
509 auto data = manager.getPELFromOBMCID(37);
510 PEL pel{data};
511 EXPECT_TRUE(pel.valid());
512 }
513
514 // Now an invalid one
515 {
516 std::string adItem = "ESEL=" + esel;
517
518 // Crop it
519 adItem.resize(adItem.size() - 300);
520
521 std::vector<std::string> additionalData{adItem};
522 std::vector<std::string> associations;
523
524 manager.create("error message", 38, 0,
525 phosphor::logging::Entry::Level::Error, additionalData,
526 associations);
527
528 EXPECT_THROW(
529 manager.getPELFromOBMCID(38),
530 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
531
532 // Run the event loop to log the bad PEL event
533 sdeventplus::Event e{sdEvent};
534 e.run(std::chrono::milliseconds(1));
535
536 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.BadHostPEL");
537 EXPECT_EQ(logger.errLevel, phosphor::logging::Entry::Level::Error);
538 }
539}
Matt Spinler7e727a32020-07-07 15:00:17 -0500540
541// Test that PELs will be pruned when necessary
542TEST_F(ManagerTest, TestPruning)
543{
544 sdeventplus::Event e{sdEvent};
545
546 std::unique_ptr<DataInterfaceBase> dataIface =
547 std::make_unique<MockDataInterface>();
548
549 openpower::pels::Manager manager{
550 logManager, std::move(dataIface),
551 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
552 std::placeholders::_2, std::placeholders::_3)};
553
554 // Create 25 1000B (4096B on disk each, which is what is used for pruning)
555 // BMC non-informational PELs in the 100KB repository. After the 24th one,
556 // the repo will be 96% full and a prune should be triggered to remove all
557 // but 7 to get under 30% full. Then when the 25th is added there will be
558 // 8 left.
559
560 auto dir = makeTempDir();
561 for (int i = 1; i <= 25; i++)
562 {
563 auto data = pelFactory(42, 'O', 0x40, 0x8800, 1000);
564
565 fs::path pelFilename = dir / "rawpel";
566 std::ofstream pelFile{pelFilename};
567 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
568 pelFile.close();
569
570 std::string adItem = "RAWPEL=" + pelFilename.string();
571 std::vector<std::string> additionalData{adItem};
572 std::vector<std::string> associations;
573
574 manager.create("error message", 42, 0,
575 phosphor::logging::Entry::Level::Error, additionalData,
576 associations);
577
578 // Simulate the code getting back to the event loop
579 // after each create.
580 e.run(std::chrono::milliseconds(1));
581
582 if (i < 24)
583 {
584 EXPECT_EQ(countPELsInRepo(), i);
585 }
586 else if (i == 24)
587 {
588 // Prune occured
589 EXPECT_EQ(countPELsInRepo(), 7);
590 }
591 else // i == 25
592 {
593 EXPECT_EQ(countPELsInRepo(), 8);
594 }
595 }
596
597 try
598 {
599 // Make sure the 8 newest ones are still found.
600 for (uint32_t i = 0; i < 8; i++)
601 {
602 manager.getPEL(0x50000012 + i);
603 }
604 }
605 catch (sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument& e)
606 {
607 ADD_FAILURE() << "PELs should have all been found";
608 }
609
610 fs::remove_all(dir);
611}