blob: 0a8214164a158f6269fe01e60720f10a97bf74e1 [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
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
Matt Spinler475e5742019-07-18 16:09:49 -050067 // 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
Matt Spinler89fa0822019-07-17 13:54:30 -050083 fs::remove_all(pelFilename.parent_path());
84}