blob: 5305468f6cc743ca7a0e136e17e953f4ed8d9693 [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
131// Test that the message registry can be used to build a PEL.
132TEST_F(ManagerTest, TestCreateWithMessageRegistry)
133{
134 const auto registry = R"(
135{
136 "PELs":
137 [
138 {
139 "Name": "xyz.openbmc_project.Error.Test",
140 "Subsystem": "power_supply",
141 "ActionFlags": ["service_action", "report"],
142 "SRC":
143 {
144 "ReasonCode": "0x2030"
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800145 },
146 "Documentation":
147 {
148 "Description": "A PGOOD Fault",
149 "Message": "PS had a PGOOD Fault"
Matt Spinler67456c22019-10-21 12:22:49 -0500150 }
151 }
152 ]
153}
154)";
155
Matt Spinlerd4ffb652019-11-12 14:16:14 -0600156 auto path = getMessageRegistryPath();
157 fs::create_directories(path);
158 path /= "message_registry.json";
159
Matt Spinler67456c22019-10-21 12:22:49 -0500160 std::ofstream registryFile{path};
161 registryFile << registry;
162 registryFile.close();
163
Matt Spinler67456c22019-10-21 12:22:49 -0500164 std::unique_ptr<DataInterfaceBase> dataIface =
165 std::make_unique<DataInterface>(logManager.getBus());
166
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600167 openpower::pels::Manager manager{
168 logManager, std::move(dataIface),
169 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
170 std::placeholders::_2, std::placeholders::_3)};
Matt Spinler67456c22019-10-21 12:22:49 -0500171
172 std::vector<std::string> additionalData;
173 std::vector<std::string> associations;
174
175 // Create the event log to create the PEL from.
176 manager.create("xyz.openbmc_project.Error.Test", 33, 0,
177 phosphor::logging::Entry::Level::Error, additionalData,
178 associations);
179
180 // Ensure a PEL was created in the repository
181 auto pelFile = findAnyPELInRepo();
182 ASSERT_TRUE(pelFile);
183
184 auto data = readPELFile(*pelFile);
185 PEL pel(*data);
186
187 // Spot check it. Other testcases cover the details.
188 EXPECT_TRUE(pel.valid());
189 EXPECT_EQ(pel.obmcLogID(), 33);
190 EXPECT_EQ(pel.primarySRC().value()->asciiString(),
191 "BD612030 ");
192
193 // Remove it
194 manager.erase(33);
195 pelFile = findAnyPELInRepo();
196 EXPECT_FALSE(pelFile);
197
198 // Create an event log that can't be found in the registry.
199 manager.create("xyz.openbmc_project.Error.Foo", 33, 0,
200 phosphor::logging::Entry::Level::Error, additionalData,
201 associations);
202
203 // Currently, no PEL should be created. Eventually, a 'missing registry
204 // entry' PEL will be there.
205 pelFile = findAnyPELInRepo();
206 EXPECT_FALSE(pelFile);
207}
Matt Spinlera34ab722019-12-16 10:39:32 -0600208
209TEST_F(ManagerTest, TestDBusMethods)
210{
Matt Spinlera34ab722019-12-16 10:39:32 -0600211 std::unique_ptr<DataInterfaceBase> dataIface =
212 std::make_unique<DataInterface>(bus);
213
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600214 Manager manager{logManager, std::move(dataIface),
215 std::bind(std::mem_fn(&TestLogger::log), &logger,
216 std::placeholders::_1, std::placeholders::_2,
217 std::placeholders::_3)};
Matt Spinlera34ab722019-12-16 10:39:32 -0600218
219 // Create a PEL, write it to a file, and pass that filename into
220 // the create function so there's one in the repo.
221 auto data = pelDataFactory(TestPELType::pelSimple);
222
223 fs::path pelFilename = makeTempDir() / "rawpel";
224 std::ofstream pelFile{pelFilename};
225 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
226 pelFile.close();
227
228 std::string adItem = "RAWPEL=" + pelFilename.string();
229 std::vector<std::string> additionalData{adItem};
230 std::vector<std::string> associations;
231
232 manager.create("error message", 42, 0,
233 phosphor::logging::Entry::Level::Error, additionalData,
234 associations);
235
236 // getPELFromOBMCID
237 auto newData = manager.getPELFromOBMCID(42);
238 EXPECT_EQ(newData.size(), data.size());
239
240 // Read the PEL to get the ID for later
241 PEL pel{newData};
242 auto id = pel.id();
243
244 EXPECT_THROW(
245 manager.getPELFromOBMCID(id + 1),
246 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
247
248 // getPEL
249 auto unixfd = manager.getPEL(id);
250
251 // Get the size
252 struct stat s;
253 int r = fstat(unixfd, &s);
254 ASSERT_EQ(r, 0);
255 auto size = s.st_size;
256
257 // Open the FD and check the contents
258 FILE* fp = fdopen(unixfd, "r");
259 ASSERT_NE(fp, nullptr);
260
261 std::vector<uint8_t> fdData;
262 fdData.resize(size);
263 r = fread(fdData.data(), 1, size, fp);
264 EXPECT_EQ(r, size);
265
266 EXPECT_EQ(newData, fdData);
267
268 fclose(fp);
269
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600270 // Run the event loop to close the FD
271 sdeventplus::Event e{sdEvent};
272 e.run(std::chrono::milliseconds(1));
273
Matt Spinlera34ab722019-12-16 10:39:32 -0600274 EXPECT_THROW(
275 manager.getPEL(id + 1),
276 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
277
278 // hostAck
279 manager.hostAck(id);
280
281 EXPECT_THROW(
282 manager.hostAck(id + 1),
283 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
284
285 // hostReject
286 manager.hostReject(id, Manager::RejectionReason::BadPEL);
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600287
288 // Run the event loop to log the bad PEL event
289 e.run(std::chrono::milliseconds(1));
290
291 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.SentBadPELToHost");
292 EXPECT_EQ(id, std::stoi(logger.ad["BAD_ID"], nullptr, 16));
293
Matt Spinlera34ab722019-12-16 10:39:32 -0600294 manager.hostReject(id, Manager::RejectionReason::HostFull);
295
296 EXPECT_THROW(
297 manager.hostReject(id + 1, Manager::RejectionReason::BadPEL),
298 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
299
300 fs::remove_all(pelFilename.parent_path());
301}