Matt Spinler | 97f7abc | 2019-11-06 09:40:23 -0600 | [diff] [blame] | 1 | /** |
| 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 Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 16 | #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 | |
| 25 | using namespace openpower::pels; |
| 26 | namespace fs = std::filesystem; |
| 27 | |
| 28 | class ManagerTest : public CleanPELFiles |
| 29 | { |
| 30 | }; |
| 31 | |
| 32 | fs::path makeTempDir() |
| 33 | { |
| 34 | char path[] = "/tmp/tempnameXXXXXX"; |
| 35 | std::filesystem::path dir = mkdtemp(path); |
| 36 | return dir; |
| 37 | } |
| 38 | |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 39 | std::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 Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 54 | // Test that using the RAWPEL=<file> with the Manager::create() call gets |
| 55 | // a PEL saved in the repository. |
| 56 | TEST_F(ManagerTest, TestCreateWithPEL) |
| 57 | { |
| 58 | auto bus = sdbusplus::bus::new_default(); |
| 59 | phosphor::logging::internal::Manager logManager(bus, "logging_path"); |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 60 | std::unique_ptr<DataInterfaceBase> dataIface = |
| 61 | std::make_unique<DataInterface>(bus); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 62 | |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 63 | openpower::pels::Manager manager{logManager, std::move(dataIface)}; |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 64 | |
| 65 | // Create a PEL, write it to a file, and pass that filename into |
| 66 | // the create function. |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 67 | auto data = pelDataFactory(TestPELType::pelSimple); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 68 | |
| 69 | fs::path pelFilename = makeTempDir() / "rawpel"; |
| 70 | std::ofstream pelFile{pelFilename}; |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 71 | pelFile.write(reinterpret_cast<const char*>(data.data()), data.size()); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 72 | 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 Spinler | 367144c | 2019-09-19 15:33:52 -0500 | [diff] [blame] | 78 | manager.create("error message", 42, 0, |
| 79 | phosphor::logging::Entry::Level::Error, additionalData, |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 80 | associations); |
| 81 | |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 82 | // Find the file in the PEL repository directory |
| 83 | auto pelPathInRepo = findAnyPELInRepo(); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 84 | |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 85 | EXPECT_TRUE(pelPathInRepo); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 86 | |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 87 | // Now remove it based on its OpenBMC event log ID |
| 88 | manager.erase(42); |
| 89 | |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 90 | pelPathInRepo = findAnyPELInRepo(); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 91 | |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 92 | EXPECT_FALSE(pelPathInRepo); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 93 | |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 94 | fs::remove_all(pelFilename.parent_path()); |
| 95 | } |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 96 | |
| 97 | // Test that the message registry can be used to build a PEL. |
| 98 | TEST_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 Spinler | d4ffb65 | 2019-11-12 14:16:14 -0600 | [diff] [blame] | 117 | auto path = getMessageRegistryPath(); |
| 118 | fs::create_directories(path); |
| 119 | path /= "message_registry.json"; |
| 120 | |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 121 | 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 | } |