blob: 1e0e1a9923fd370b4589e10b8d9adb4203bf7d74 [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"
18#include "pel_utils.hpp"
19
20#include <fstream>
21#include <regex>
Matt Spinlera34ab722019-12-16 10:39:32 -060022#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinler89fa0822019-07-17 13:54:30 -050023
24#include <gtest/gtest.h>
25
26using namespace openpower::pels;
27namespace fs = std::filesystem;
28
Matt Spinler05c2c6c2019-12-18 14:02:09 -060029class TestLogger
30{
31 public:
32 void log(const std::string& name, phosphor::logging::Entry::Level level,
33 const EventLogger::ADMap& additionalData)
34 {
35 errName = name;
36 errLevel = level;
37 ad = additionalData;
38 }
39
40 std::string errName;
41 phosphor::logging::Entry::Level errLevel;
42 EventLogger::ADMap ad;
43};
44
Matt Spinler89fa0822019-07-17 13:54:30 -050045class ManagerTest : public CleanPELFiles
46{
Matt Spinler6b1a5c82020-01-07 08:48:53 -060047 public:
48 ManagerTest() : logManager(bus, "logging_path")
49 {
50 sd_event_default(&sdEvent);
51 bus.attach_event(sdEvent, SD_EVENT_PRIORITY_NORMAL);
52 }
53
54 ~ManagerTest()
55 {
56 sd_event_unref(sdEvent);
57 }
58
59 sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
60 phosphor::logging::internal::Manager logManager;
61 sd_event* sdEvent;
Matt Spinler05c2c6c2019-12-18 14:02:09 -060062 TestLogger logger;
Matt Spinler89fa0822019-07-17 13:54:30 -050063};
64
65fs::path makeTempDir()
66{
67 char path[] = "/tmp/tempnameXXXXXX";
68 std::filesystem::path dir = mkdtemp(path);
69 return dir;
70}
71
Matt Spinler67456c22019-10-21 12:22:49 -050072std::optional<fs::path> findAnyPELInRepo()
73{
74 // PELs are named <timestamp>_<ID>
75 std::regex expr{"\\d+_\\d+"};
76
77 for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs"))
78 {
79 if (std::regex_search(f.path().string(), expr))
80 {
81 return f.path();
82 }
83 }
84 return std::nullopt;
85}
86
Matt Spinler89fa0822019-07-17 13:54:30 -050087// Test that using the RAWPEL=<file> with the Manager::create() call gets
88// a PEL saved in the repository.
89TEST_F(ManagerTest, TestCreateWithPEL)
90{
Matt Spinlerc8705e22019-09-11 12:36:07 -050091 std::unique_ptr<DataInterfaceBase> dataIface =
92 std::make_unique<DataInterface>(bus);
Matt Spinler89fa0822019-07-17 13:54:30 -050093
Matt Spinler05c2c6c2019-12-18 14:02:09 -060094 openpower::pels::Manager manager{
95 logManager, std::move(dataIface),
96 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
97 std::placeholders::_2, std::placeholders::_3)};
Matt Spinler89fa0822019-07-17 13:54:30 -050098
99 // Create a PEL, write it to a file, and pass that filename into
100 // the create function.
Matt Spinler42828bd2019-10-11 10:39:30 -0500101 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler89fa0822019-07-17 13:54:30 -0500102
103 fs::path pelFilename = makeTempDir() / "rawpel";
104 std::ofstream pelFile{pelFilename};
Matt Spinler42828bd2019-10-11 10:39:30 -0500105 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
Matt Spinler89fa0822019-07-17 13:54:30 -0500106 pelFile.close();
107
108 std::string adItem = "RAWPEL=" + pelFilename.string();
109 std::vector<std::string> additionalData{adItem};
110 std::vector<std::string> associations;
111
Matt Spinler367144c2019-09-19 15:33:52 -0500112 manager.create("error message", 42, 0,
113 phosphor::logging::Entry::Level::Error, additionalData,
Matt Spinler89fa0822019-07-17 13:54:30 -0500114 associations);
115
Matt Spinler67456c22019-10-21 12:22:49 -0500116 // Find the file in the PEL repository directory
117 auto pelPathInRepo = findAnyPELInRepo();
Matt Spinler89fa0822019-07-17 13:54:30 -0500118
Matt Spinler67456c22019-10-21 12:22:49 -0500119 EXPECT_TRUE(pelPathInRepo);
Matt Spinler89fa0822019-07-17 13:54:30 -0500120
Matt Spinler475e5742019-07-18 16:09:49 -0500121 // Now remove it based on its OpenBMC event log ID
122 manager.erase(42);
123
Matt Spinler67456c22019-10-21 12:22:49 -0500124 pelPathInRepo = findAnyPELInRepo();
Matt Spinler475e5742019-07-18 16:09:49 -0500125
Matt Spinler67456c22019-10-21 12:22:49 -0500126 EXPECT_FALSE(pelPathInRepo);
Matt Spinler475e5742019-07-18 16:09:49 -0500127
Matt Spinler89fa0822019-07-17 13:54:30 -0500128 fs::remove_all(pelFilename.parent_path());
129}
Matt Spinler67456c22019-10-21 12:22:49 -0500130
Matt Spinlere95fd012020-01-07 12:53:16 -0600131TEST_F(ManagerTest, TestCreateWithInvalidPEL)
132{
133 std::unique_ptr<DataInterfaceBase> dataIface =
134 std::make_unique<DataInterface>(bus);
135
136 openpower::pels::Manager manager{
137 logManager, std::move(dataIface),
138 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
139 std::placeholders::_2, std::placeholders::_3)};
140
141 // Create a PEL, write it to a file, and pass that filename into
142 // the create function.
143 auto data = pelDataFactory(TestPELType::pelSimple);
144
145 // Truncate it to make it invalid.
146 data.resize(200);
147
148 fs::path pelFilename = makeTempDir() / "rawpel";
149 std::ofstream pelFile{pelFilename};
150 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
151 pelFile.close();
152
153 std::string adItem = "RAWPEL=" + pelFilename.string();
154 std::vector<std::string> additionalData{adItem};
155 std::vector<std::string> associations;
156
157 manager.create("error message", 42, 0,
158 phosphor::logging::Entry::Level::Error, additionalData,
159 associations);
160
161 // Run the event loop to log the bad PEL event
162 sdeventplus::Event e{sdEvent};
163 e.run(std::chrono::milliseconds(1));
164
165 PEL invalidPEL{data};
166 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.BadHostPEL");
167 EXPECT_EQ(logger.errLevel, phosphor::logging::Entry::Level::Error);
168 EXPECT_EQ(std::stoi(logger.ad["PLID"], nullptr, 16), invalidPEL.plid());
169 EXPECT_EQ(logger.ad["OBMC_LOG_ID"], "42");
170 EXPECT_EQ(logger.ad["SRC"], (*invalidPEL.primarySRC())->asciiString());
171 EXPECT_EQ(logger.ad["PEL_SIZE"], std::to_string(data.size()));
172
173 fs::remove_all(pelFilename.parent_path());
174}
175
Matt Spinler67456c22019-10-21 12:22:49 -0500176// Test that the message registry can be used to build a PEL.
177TEST_F(ManagerTest, TestCreateWithMessageRegistry)
178{
179 const auto registry = R"(
180{
181 "PELs":
182 [
183 {
184 "Name": "xyz.openbmc_project.Error.Test",
185 "Subsystem": "power_supply",
186 "ActionFlags": ["service_action", "report"],
187 "SRC":
188 {
189 "ReasonCode": "0x2030"
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800190 },
191 "Documentation":
192 {
193 "Description": "A PGOOD Fault",
194 "Message": "PS had a PGOOD Fault"
Matt Spinler67456c22019-10-21 12:22:49 -0500195 }
196 }
197 ]
198}
199)";
200
Matt Spinlerd4ffb652019-11-12 14:16:14 -0600201 auto path = getMessageRegistryPath();
202 fs::create_directories(path);
203 path /= "message_registry.json";
204
Matt Spinler67456c22019-10-21 12:22:49 -0500205 std::ofstream registryFile{path};
206 registryFile << registry;
207 registryFile.close();
208
Matt Spinler67456c22019-10-21 12:22:49 -0500209 std::unique_ptr<DataInterfaceBase> dataIface =
210 std::make_unique<DataInterface>(logManager.getBus());
211
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600212 openpower::pels::Manager manager{
213 logManager, std::move(dataIface),
214 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
215 std::placeholders::_2, std::placeholders::_3)};
Matt Spinler67456c22019-10-21 12:22:49 -0500216
217 std::vector<std::string> additionalData;
218 std::vector<std::string> associations;
219
220 // Create the event log to create the PEL from.
221 manager.create("xyz.openbmc_project.Error.Test", 33, 0,
222 phosphor::logging::Entry::Level::Error, additionalData,
223 associations);
224
225 // Ensure a PEL was created in the repository
226 auto pelFile = findAnyPELInRepo();
227 ASSERT_TRUE(pelFile);
228
229 auto data = readPELFile(*pelFile);
230 PEL pel(*data);
231
232 // Spot check it. Other testcases cover the details.
233 EXPECT_TRUE(pel.valid());
234 EXPECT_EQ(pel.obmcLogID(), 33);
235 EXPECT_EQ(pel.primarySRC().value()->asciiString(),
236 "BD612030 ");
237
238 // Remove it
239 manager.erase(33);
240 pelFile = findAnyPELInRepo();
241 EXPECT_FALSE(pelFile);
242
243 // Create an event log that can't be found in the registry.
244 manager.create("xyz.openbmc_project.Error.Foo", 33, 0,
245 phosphor::logging::Entry::Level::Error, additionalData,
246 associations);
247
248 // Currently, no PEL should be created. Eventually, a 'missing registry
249 // entry' PEL will be there.
250 pelFile = findAnyPELInRepo();
251 EXPECT_FALSE(pelFile);
252}
Matt Spinlera34ab722019-12-16 10:39:32 -0600253
254TEST_F(ManagerTest, TestDBusMethods)
255{
Matt Spinlera34ab722019-12-16 10:39:32 -0600256 std::unique_ptr<DataInterfaceBase> dataIface =
257 std::make_unique<DataInterface>(bus);
258
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600259 Manager manager{logManager, std::move(dataIface),
260 std::bind(std::mem_fn(&TestLogger::log), &logger,
261 std::placeholders::_1, std::placeholders::_2,
262 std::placeholders::_3)};
Matt Spinlera34ab722019-12-16 10:39:32 -0600263
264 // Create a PEL, write it to a file, and pass that filename into
265 // the create function so there's one in the repo.
266 auto data = pelDataFactory(TestPELType::pelSimple);
267
268 fs::path pelFilename = makeTempDir() / "rawpel";
269 std::ofstream pelFile{pelFilename};
270 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
271 pelFile.close();
272
273 std::string adItem = "RAWPEL=" + pelFilename.string();
274 std::vector<std::string> additionalData{adItem};
275 std::vector<std::string> associations;
276
277 manager.create("error message", 42, 0,
278 phosphor::logging::Entry::Level::Error, additionalData,
279 associations);
280
281 // getPELFromOBMCID
282 auto newData = manager.getPELFromOBMCID(42);
283 EXPECT_EQ(newData.size(), data.size());
284
285 // Read the PEL to get the ID for later
286 PEL pel{newData};
287 auto id = pel.id();
288
289 EXPECT_THROW(
290 manager.getPELFromOBMCID(id + 1),
291 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
292
293 // getPEL
294 auto unixfd = manager.getPEL(id);
295
296 // Get the size
297 struct stat s;
298 int r = fstat(unixfd, &s);
299 ASSERT_EQ(r, 0);
300 auto size = s.st_size;
301
302 // Open the FD and check the contents
303 FILE* fp = fdopen(unixfd, "r");
304 ASSERT_NE(fp, nullptr);
305
306 std::vector<uint8_t> fdData;
307 fdData.resize(size);
308 r = fread(fdData.data(), 1, size, fp);
309 EXPECT_EQ(r, size);
310
311 EXPECT_EQ(newData, fdData);
312
313 fclose(fp);
314
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600315 // Run the event loop to close the FD
316 sdeventplus::Event e{sdEvent};
317 e.run(std::chrono::milliseconds(1));
318
Matt Spinlera34ab722019-12-16 10:39:32 -0600319 EXPECT_THROW(
320 manager.getPEL(id + 1),
321 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
322
323 // hostAck
324 manager.hostAck(id);
325
326 EXPECT_THROW(
327 manager.hostAck(id + 1),
328 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
329
330 // hostReject
331 manager.hostReject(id, Manager::RejectionReason::BadPEL);
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600332
333 // Run the event loop to log the bad PEL event
334 e.run(std::chrono::milliseconds(1));
335
336 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.SentBadPELToHost");
337 EXPECT_EQ(id, std::stoi(logger.ad["BAD_ID"], nullptr, 16));
338
Matt Spinlera34ab722019-12-16 10:39:32 -0600339 manager.hostReject(id, Manager::RejectionReason::HostFull);
340
341 EXPECT_THROW(
342 manager.hostReject(id + 1, Manager::RejectionReason::BadPEL),
343 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
344
345 fs::remove_all(pelFilename.parent_path());
346}