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");
30     std::unique_ptr<DataInterfaceBase> dataIface =
31         std::make_unique<DataInterface>(bus);
32 
33     openpower::pels::Manager manager{logManager, std::move(dataIface)};
34 
35     // Create a PEL, write it to a file, and pass that filename into
36     // the create function.
37     auto data = pelDataFactory(TestPelType::pelSimple);
38 
39     fs::path pelFilename = makeTempDir() / "rawpel";
40     std::ofstream pelFile{pelFilename};
41     pelFile.write(reinterpret_cast<const char*>(data->data()), data->size());
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 
48     manager.create("error message", 42, 0, Entry::Level::Error, additionalData,
49                    associations);
50 
51     // We don't know the exact name, but a file should have been added to the
52     // repo of the form <timestamp>_<ID>
53     std::regex expr{"\\d+_\\d+"};
54 
55     bool found = false;
56     for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs"))
57     {
58         if (std::regex_search(f.path().string(), expr))
59         {
60             found = true;
61             break;
62         }
63     }
64 
65     EXPECT_TRUE(found);
66 
67     // Now remove it based on its OpenBMC event log ID
68     manager.erase(42);
69 
70     found = false;
71 
72     for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs"))
73     {
74         if (std::regex_search(f.path().string(), expr))
75         {
76             found = true;
77             break;
78         }
79     }
80 
81     EXPECT_FALSE(found);
82 
83     fs::remove_all(pelFilename.parent_path());
84 }
85