PEL: Check if PEL pruning necessary on a new PEL

When a new PEL is added check if the size of all PELs is now more than
95% of the maximum capacity.  If it is, call the repository's prune()
function from the event loop to make more space.  Also delete the
OpenBMC event logs corresponding to the PELs that were just deleted.

It's called from the event loop so that the current D-Bus method
response that the code is in now can return to the caller first.

It tops out at 95% to ensure that we never go over.  It's possible this
can be tuned in the future, but at the current 20MB limit that still
allows 19MB before pruning will take it down to at most 18MB.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I962866eceee89cd605fcd36ec08b20ff762fe6cd
diff --git a/test/openpower-pels/repository_test.cpp b/test/openpower-pels/repository_test.cpp
index a4f8bef..3fa2290 100644
--- a/test/openpower-pels/repository_test.cpp
+++ b/test/openpower-pels/repository_test.cpp
@@ -784,3 +784,55 @@
     EXPECT_EQ(IDs[1], 500 + 2);
     EXPECT_EQ(IDs[2], 500 + 3);
 }
+
+// Test the sizeWarning function
+TEST_F(RepositoryTest, TestSizeWarning)
+{
+    uint32_t id = 1;
+    Repository repo{repoPath, 100 * 4096, 500};
+
+    EXPECT_FALSE(repo.sizeWarning());
+
+    // 95% is still OK (disk size for these is 4096)
+    for (uint32_t i = 1; i <= 95; i++)
+    {
+        auto data = pelFactory(i, 'O', 0x20, 0x8800, 500);
+        auto pel = std::make_unique<PEL>(data);
+        repo.add(pel);
+    }
+
+    EXPECT_FALSE(repo.sizeWarning());
+
+    // Now at 96%
+    auto data = pelFactory(id++, 'B', 0x20, 0x8800, 400);
+    auto pel = std::make_unique<PEL>(data);
+    repo.add(pel);
+
+    EXPECT_TRUE(repo.sizeWarning());
+}
+
+// Test sizeWarning when there are too many PEls
+TEST_F(RepositoryTest, TestSizeWarningNumPELs)
+{
+    Repository repo{repoPath, 4096 * 100, 5};
+
+    EXPECT_FALSE(repo.sizeWarning());
+
+    for (uint32_t i = 1; i <= 5; i++)
+    {
+        auto data = pelFactory(i, 'O', 0x20, 0x8800, 500);
+        auto pel = std::make_unique<PEL>(data);
+        repo.add(pel);
+    }
+
+    EXPECT_FALSE(repo.sizeWarning());
+
+    // Add 1 more for a total of 6, now over the limit
+    {
+        auto data = pelFactory(6, 'O', 0x20, 0x8800, 500);
+        auto pel = std::make_unique<PEL>(data);
+        repo.add(pel);
+    }
+
+    EXPECT_TRUE(repo.sizeWarning());
+}