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 
31     openpower::pels::Manager manager{logManager};
32 
33     // Create a PEL, write it to a file, and pass that filename into
34     // the create function.
35     auto data = pelDataFactory(TestPelType::pelSimple);
36 
37     fs::path pelFilename = makeTempDir() / "rawpel";
38     std::ofstream pelFile{pelFilename};
39     pelFile.write(reinterpret_cast<const char*>(data->data()), data->size());
40     pelFile.close();
41 
42     std::string adItem = "RAWPEL=" + pelFilename.string();
43     std::vector<std::string> additionalData{adItem};
44     std::vector<std::string> associations;
45 
46     manager.create("error message", 42, 0, Entry::Level::Error, additionalData,
47                    associations);
48 
49     // We don't know the exact name, but a file should have been added to the
50     // repo of the form <timestamp>_<ID>
51     std::regex expr{"\\d+_\\d+"};
52 
53     bool found = false;
54     for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs"))
55     {
56         if (std::regex_search(f.path().string(), expr))
57         {
58             found = true;
59             break;
60         }
61     }
62 
63     EXPECT_TRUE(found);
64 
65     fs::remove_all(pelFilename.parent_path());
66 }
67