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 | |
| 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 | } |