PEL: Add repository to save PELs
Create the Repository class that can save PELs in (and later retrieve
them from) the filesystem. It provides an add() method that can add
a PEL object to the repository.
Now, when the Manager class sees an OpenBMC event log created with the
RAWPEL metadata in the AdditionalData property that points at a file
that contains a PEL, it can save that PEL. Before the PEL is saved, the
log ID and commit timestamp fields in the PEL will be updated - the log
ID to a unique value, and the timestamp to the current time.
Change-Id: I8dbaddf0f155bcb6d40b933294ada83feb75ce53
diff --git a/test/openpower-pels/pel_manager_test.cpp b/test/openpower-pels/pel_manager_test.cpp
new file mode 100644
index 0000000..a5b16f5
--- /dev/null
+++ b/test/openpower-pels/pel_manager_test.cpp
@@ -0,0 +1,66 @@
+#include "extensions/openpower-pels/manager.hpp"
+#include "log_manager.hpp"
+#include "pel_utils.hpp"
+
+#include <fstream>
+#include <regex>
+
+#include <gtest/gtest.h>
+
+using namespace openpower::pels;
+namespace fs = std::filesystem;
+
+class ManagerTest : public CleanPELFiles
+{
+};
+
+fs::path makeTempDir()
+{
+ char path[] = "/tmp/tempnameXXXXXX";
+ std::filesystem::path dir = mkdtemp(path);
+ return dir;
+}
+
+// Test that using the RAWPEL=<file> with the Manager::create() call gets
+// a PEL saved in the repository.
+TEST_F(ManagerTest, TestCreateWithPEL)
+{
+ auto bus = sdbusplus::bus::new_default();
+ phosphor::logging::internal::Manager logManager(bus, "logging_path");
+
+ openpower::pels::Manager manager{logManager};
+
+ // Create a PEL, write it to a file, and pass that filename into
+ // the create function.
+ auto data = pelDataFactory(TestPelType::pelSimple);
+
+ fs::path pelFilename = makeTempDir() / "rawpel";
+ std::ofstream pelFile{pelFilename};
+ pelFile.write(reinterpret_cast<const char*>(data->data()), data->size());
+ pelFile.close();
+
+ std::string adItem = "RAWPEL=" + pelFilename.string();
+ std::vector<std::string> additionalData{adItem};
+ std::vector<std::string> associations;
+
+ manager.create("error message", 42, 0, Entry::Level::Error, additionalData,
+ associations);
+
+ // We don't know the exact name, but a file should have been added to the
+ // repo of the form <timestamp>_<ID>
+ std::regex expr{"\\d+_\\d+"};
+
+ bool found = false;
+ for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs"))
+ {
+ if (std::regex_search(f.path().string(), expr))
+ {
+ found = true;
+ break;
+ }
+ }
+
+ EXPECT_TRUE(found);
+
+ fs::remove_all(pelFilename.parent_path());
+}