Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 1 | #include "extensions/openpower-pels/manager.hpp" |
| 2 | #include "log_manager.hpp" |
| 3 | #include "pel_utils.hpp" |
| 4 | |
| 5 | #include <fstream> |
| 6 | #include <regex> |
| 7 | |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | using namespace openpower::pels; |
| 11 | namespace fs = std::filesystem; |
| 12 | |
| 13 | class ManagerTest : public CleanPELFiles |
| 14 | { |
| 15 | }; |
| 16 | |
| 17 | fs::path makeTempDir() |
| 18 | { |
| 19 | char path[] = "/tmp/tempnameXXXXXX"; |
| 20 | std::filesystem::path dir = mkdtemp(path); |
| 21 | return dir; |
| 22 | } |
| 23 | |
| 24 | // Test that using the RAWPEL=<file> with the Manager::create() call gets |
| 25 | // a PEL saved in the repository. |
| 26 | TEST_F(ManagerTest, TestCreateWithPEL) |
| 27 | { |
| 28 | auto bus = sdbusplus::bus::new_default(); |
| 29 | phosphor::logging::internal::Manager logManager(bus, "logging_path"); |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 30 | std::unique_ptr<DataInterfaceBase> dataIface = |
| 31 | std::make_unique<DataInterface>(bus); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 32 | |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 33 | openpower::pels::Manager manager{logManager, std::move(dataIface)}; |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 34 | |
| 35 | // Create a PEL, write it to a file, and pass that filename into |
| 36 | // the create function. |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 37 | auto data = pelDataFactory(TestPELType::pelSimple); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 38 | |
| 39 | fs::path pelFilename = makeTempDir() / "rawpel"; |
| 40 | std::ofstream pelFile{pelFilename}; |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 41 | pelFile.write(reinterpret_cast<const char*>(data.data()), data.size()); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 42 | pelFile.close(); |
| 43 | |
| 44 | std::string adItem = "RAWPEL=" + pelFilename.string(); |
| 45 | std::vector<std::string> additionalData{adItem}; |
| 46 | std::vector<std::string> associations; |
| 47 | |
Matt Spinler | 367144c | 2019-09-19 15:33:52 -0500 | [diff] [blame] | 48 | manager.create("error message", 42, 0, |
| 49 | phosphor::logging::Entry::Level::Error, additionalData, |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 50 | associations); |
| 51 | |
| 52 | // We don't know the exact name, but a file should have been added to the |
| 53 | // repo of the form <timestamp>_<ID> |
| 54 | std::regex expr{"\\d+_\\d+"}; |
| 55 | |
| 56 | bool found = false; |
| 57 | for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs")) |
| 58 | { |
| 59 | if (std::regex_search(f.path().string(), expr)) |
| 60 | { |
| 61 | found = true; |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | EXPECT_TRUE(found); |
| 67 | |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 68 | // Now remove it based on its OpenBMC event log ID |
| 69 | manager.erase(42); |
| 70 | |
| 71 | found = false; |
| 72 | |
| 73 | for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs")) |
| 74 | { |
| 75 | if (std::regex_search(f.path().string(), expr)) |
| 76 | { |
| 77 | found = true; |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | EXPECT_FALSE(found); |
| 83 | |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 84 | fs::remove_all(pelFilename.parent_path()); |
| 85 | } |