blob: 61fbedd201f0e16dcb292020c3b6e7d7c5a85826 [file] [log] [blame]
Matt Spinler89fa0822019-07-17 13:54:30 -05001#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
10using namespace openpower::pels;
11namespace fs = std::filesystem;
12
13class ManagerTest : public CleanPELFiles
14{
15};
16
17fs::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.
26TEST_F(ManagerTest, TestCreateWithPEL)
27{
28 auto bus = sdbusplus::bus::new_default();
29 phosphor::logging::internal::Manager logManager(bus, "logging_path");
Matt Spinlerc8705e22019-09-11 12:36:07 -050030 std::unique_ptr<DataInterfaceBase> dataIface =
31 std::make_unique<DataInterface>(bus);
Matt Spinler89fa0822019-07-17 13:54:30 -050032
Matt Spinlerc8705e22019-09-11 12:36:07 -050033 openpower::pels::Manager manager{logManager, std::move(dataIface)};
Matt Spinler89fa0822019-07-17 13:54:30 -050034
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
Matt Spinler367144c2019-09-19 15:33:52 -050048 manager.create("error message", 42, 0,
49 phosphor::logging::Entry::Level::Error, additionalData,
Matt Spinler89fa0822019-07-17 13:54:30 -050050 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 Spinler475e5742019-07-18 16:09:49 -050068 // 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 Spinler89fa0822019-07-17 13:54:30 -050084 fs::remove_all(pelFilename.parent_path());
85}