PEL: Guard against hostboot sending down duplicate PEL Ids

This commit is to guard against hostboot sending down PEL Id
that we already have in our repository. This caused PEL to
get orphaned in the filesystem without a corresponding valid
openbmc event log Id.
The action is to move such PELs to archive folder.

Signed-off-by: Sumit Kumar <sumit_kumar@in.ibm.com>
Change-Id: I46bb865b4b87ec0b59f362e3f79a1b5a6710a45c
diff --git a/test/openpower-pels/pel_manager_test.cpp b/test/openpower-pels/pel_manager_test.cpp
index 7bf350f..c1cf5ec 100644
--- a/test/openpower-pels/pel_manager_test.cpp
+++ b/test/openpower-pels/pel_manager_test.cpp
@@ -973,3 +973,52 @@
                        associations);
     }
 }
+
+// Test for duplicate PELs moved to archive folder
+TEST_F(ManagerTest, TestDuplicatePEL)
+{
+    sdeventplus::Event e{sdEvent};
+    size_t count = 0;
+
+    std::unique_ptr<DataInterfaceBase> dataIface =
+        std::make_unique<MockDataInterface>();
+
+    openpower::pels::Manager manager{
+        logManager, std::move(dataIface),
+        std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
+                  std::placeholders::_2, std::placeholders::_3)};
+
+    for (int i = 0; i < 2; i++)
+    {
+        // This hostboot PEL has a single hardware callout in it.
+        auto data = pelFactory(1, 'B', 0x20, 0xA400, 500);
+
+        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,
+                       phosphor::logging::Entry::Level::Error, additionalData,
+                       associations);
+
+        e.run(std::chrono::milliseconds(1));
+    }
+
+    for (auto& f :
+         fs::directory_iterator(getPELRepoPath() / "logs" / "archive"))
+    {
+        if (fs::is_regular_file(f.path()))
+        {
+            count++;
+        }
+    }
+
+    // Get count of PELs in the repository & in archive directtory
+    EXPECT_EQ(countPELsInRepo(), 1);
+    EXPECT_EQ(count, 1);
+}