blob: 1d4d3b7036507bd97cf376f22ef147ed4b1fad5d [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
117 fs::path path = getMessageRegistryPath() / "message_registry.json";
118 std::ofstream registryFile{path};
119 registryFile << registry;
120 registryFile.close();
121
122 auto bus = sdbusplus::bus::new_default();
123 phosphor::logging::internal::Manager logManager(bus, "logging_path");
124
125 std::unique_ptr<DataInterfaceBase> dataIface =
126 std::make_unique<DataInterface>(logManager.getBus());
127
128 openpower::pels::Manager manager{logManager, std::move(dataIface)};
129
130 std::vector<std::string> additionalData;
131 std::vector<std::string> associations;
132
133 // Create the event log to create the PEL from.
134 manager.create("xyz.openbmc_project.Error.Test", 33, 0,
135 phosphor::logging::Entry::Level::Error, additionalData,
136 associations);
137
138 // Ensure a PEL was created in the repository
139 auto pelFile = findAnyPELInRepo();
140 ASSERT_TRUE(pelFile);
141
142 auto data = readPELFile(*pelFile);
143 PEL pel(*data);
144
145 // Spot check it. Other testcases cover the details.
146 EXPECT_TRUE(pel.valid());
147 EXPECT_EQ(pel.obmcLogID(), 33);
148 EXPECT_EQ(pel.primarySRC().value()->asciiString(),
149 "BD612030 ");
150
151 // Remove it
152 manager.erase(33);
153 pelFile = findAnyPELInRepo();
154 EXPECT_FALSE(pelFile);
155
156 // Create an event log that can't be found in the registry.
157 manager.create("xyz.openbmc_project.Error.Foo", 33, 0,
158 phosphor::logging::Entry::Level::Error, additionalData,
159 associations);
160
161 // Currently, no PEL should be created. Eventually, a 'missing registry
162 // entry' PEL will be there.
163 pelFile = findAnyPELInRepo();
164 EXPECT_FALSE(pelFile);
165}