blob: a84ea7ab73ce50fa3164737d415542824d010077 [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>
22
23#include <gtest/gtest.h>
24
25using namespace openpower::pels;
26namespace fs = std::filesystem;
27
28class ManagerTest : public CleanPELFiles
29{
30};
31
32fs::path makeTempDir()
33{
34 char path[] = "/tmp/tempnameXXXXXX";
35 std::filesystem::path dir = mkdtemp(path);
36 return dir;
37}
38
Matt Spinler67456c22019-10-21 12:22:49 -050039std::optional<fs::path> findAnyPELInRepo()
40{
41 // PELs are named <timestamp>_<ID>
42 std::regex expr{"\\d+_\\d+"};
43
44 for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs"))
45 {
46 if (std::regex_search(f.path().string(), expr))
47 {
48 return f.path();
49 }
50 }
51 return std::nullopt;
52}
53
Matt Spinler89fa0822019-07-17 13:54:30 -050054// Test that using the RAWPEL=<file> with the Manager::create() call gets
55// a PEL saved in the repository.
56TEST_F(ManagerTest, TestCreateWithPEL)
57{
58 auto bus = sdbusplus::bus::new_default();
59 phosphor::logging::internal::Manager logManager(bus, "logging_path");
Matt Spinlerc8705e22019-09-11 12:36:07 -050060 std::unique_ptr<DataInterfaceBase> dataIface =
61 std::make_unique<DataInterface>(bus);
Matt Spinler89fa0822019-07-17 13:54:30 -050062
Matt Spinlerc8705e22019-09-11 12:36:07 -050063 openpower::pels::Manager manager{logManager, std::move(dataIface)};
Matt Spinler89fa0822019-07-17 13:54:30 -050064
65 // Create a PEL, write it to a file, and pass that filename into
66 // the create function.
Matt Spinler42828bd2019-10-11 10:39:30 -050067 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler89fa0822019-07-17 13:54:30 -050068
69 fs::path pelFilename = makeTempDir() / "rawpel";
70 std::ofstream pelFile{pelFilename};
Matt Spinler42828bd2019-10-11 10:39:30 -050071 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size());
Matt Spinler89fa0822019-07-17 13:54:30 -050072 pelFile.close();
73
74 std::string adItem = "RAWPEL=" + pelFilename.string();
75 std::vector<std::string> additionalData{adItem};
76 std::vector<std::string> associations;
77
Matt Spinler367144c2019-09-19 15:33:52 -050078 manager.create("error message", 42, 0,
79 phosphor::logging::Entry::Level::Error, additionalData,
Matt Spinler89fa0822019-07-17 13:54:30 -050080 associations);
81
Matt Spinler67456c22019-10-21 12:22:49 -050082 // Find the file in the PEL repository directory
83 auto pelPathInRepo = findAnyPELInRepo();
Matt Spinler89fa0822019-07-17 13:54:30 -050084
Matt Spinler67456c22019-10-21 12:22:49 -050085 EXPECT_TRUE(pelPathInRepo);
Matt Spinler89fa0822019-07-17 13:54:30 -050086
Matt Spinler475e5742019-07-18 16:09:49 -050087 // Now remove it based on its OpenBMC event log ID
88 manager.erase(42);
89
Matt Spinler67456c22019-10-21 12:22:49 -050090 pelPathInRepo = findAnyPELInRepo();
Matt Spinler475e5742019-07-18 16:09:49 -050091
Matt Spinler67456c22019-10-21 12:22:49 -050092 EXPECT_FALSE(pelPathInRepo);
Matt Spinler475e5742019-07-18 16:09:49 -050093
Matt Spinler89fa0822019-07-17 13:54:30 -050094 fs::remove_all(pelFilename.parent_path());
95}
Matt Spinler67456c22019-10-21 12:22:49 -050096
97// Test that the message registry can be used to build a PEL.
98TEST_F(ManagerTest, TestCreateWithMessageRegistry)
99{
100 const auto registry = R"(
101{
102 "PELs":
103 [
104 {
105 "Name": "xyz.openbmc_project.Error.Test",
106 "Subsystem": "power_supply",
107 "ActionFlags": ["service_action", "report"],
108 "SRC":
109 {
110 "ReasonCode": "0x2030"
111 }
112 }
113 ]
114}
115)";
116
Matt Spinlerd4ffb652019-11-12 14:16:14 -0600117 auto path = getMessageRegistryPath();
118 fs::create_directories(path);
119 path /= "message_registry.json";
120
Matt Spinler67456c22019-10-21 12:22:49 -0500121 std::ofstream registryFile{path};
122 registryFile << registry;
123 registryFile.close();
124
125 auto bus = sdbusplus::bus::new_default();
126 phosphor::logging::internal::Manager logManager(bus, "logging_path");
127
128 std::unique_ptr<DataInterfaceBase> dataIface =
129 std::make_unique<DataInterface>(logManager.getBus());
130
131 openpower::pels::Manager manager{logManager, std::move(dataIface)};
132
133 std::vector<std::string> additionalData;
134 std::vector<std::string> associations;
135
136 // Create the event log to create the PEL from.
137 manager.create("xyz.openbmc_project.Error.Test", 33, 0,
138 phosphor::logging::Entry::Level::Error, additionalData,
139 associations);
140
141 // Ensure a PEL was created in the repository
142 auto pelFile = findAnyPELInRepo();
143 ASSERT_TRUE(pelFile);
144
145 auto data = readPELFile(*pelFile);
146 PEL pel(*data);
147
148 // Spot check it. Other testcases cover the details.
149 EXPECT_TRUE(pel.valid());
150 EXPECT_EQ(pel.obmcLogID(), 33);
151 EXPECT_EQ(pel.primarySRC().value()->asciiString(),
152 "BD612030 ");
153
154 // Remove it
155 manager.erase(33);
156 pelFile = findAnyPELInRepo();
157 EXPECT_FALSE(pelFile);
158
159 // Create an event log that can't be found in the registry.
160 manager.create("xyz.openbmc_project.Error.Foo", 33, 0,
161 phosphor::logging::Entry::Level::Error, additionalData,
162 associations);
163
164 // Currently, no PEL should be created. Eventually, a 'missing registry
165 // entry' PEL will be there.
166 pelFile = findAnyPELInRepo();
167 EXPECT_FALSE(pelFile);
168}