PEL: Make elog_update_ts_test more CI friendly

This testcase was intermittently failing in Jenkins with an error
that implied it could not write the log serialization file.

Since all event logs serialize to the same directory, it's possible
other instances of this testcase, or elog_errorwrap_test, were running
at the same time and caused collisions.  To try to fix that, make the
following changes:
* Make the elog ID, and hence the filename, be random
* If there is a save file by that ID already, get a new ID.
* Don't delete the save directory at the end, only the file, in case
  another test is still using it.
* Also don't delete the save directory at the end of
  elog_errorwrap_test, in case another jenkins job is running this at
  the same time.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: Ib81015936d0cc673b330f49f1c703e8fa16853ae
diff --git a/test/elog_update_ts_test.cpp b/test/elog_update_ts_test.cpp
index ece0b80..65b1328 100644
--- a/test/elog_update_ts_test.cpp
+++ b/test/elog_update_ts_test.cpp
@@ -17,17 +17,34 @@
 {
 
 using namespace std::chrono_literals;
+namespace fs = std::filesystem;
 
 // Test that the update timestamp changes when the resolved property changes
 TEST(TestUpdateTS, testChangeResolved)
 {
     // Setting resolved will serialize, so need this directory.
-    std::filesystem::create_directory(ERRLOG_PERSIST_PATH);
+    fs::create_directory(ERRLOG_PERSIST_PATH);
+
+    if (!fs::exists(ERRLOG_PERSIST_PATH))
+    {
+        ADD_FAILURE() << "Could not create " << ERRLOG_PERSIST_PATH << "\n";
+        exit(1);
+    }
 
     auto bus = sdbusplus::bus::new_default();
     phosphor::logging::internal::Manager manager(bus, OBJ_INTERNAL);
 
-    uint32_t id = 99;
+    // Use a random number for the ID to avoid other CI
+    // testcases running in parallel.
+    std::srand(std::time(nullptr));
+    uint32_t id = std::rand();
+
+    if (fs::exists(fs::path{ERRLOG_PERSIST_PATH} / std::to_string(id)))
+    {
+        std::cerr << "Another testcase is using ID " << id << "\n";
+        id = std::rand();
+    }
+
     uint64_t timestamp{100};
     std::string message{"test error"};
     std::string fwLevel{"level42"};
@@ -65,7 +82,8 @@
     elog.resolved(false);
     EXPECT_EQ(updateTS, elog.updateTimestamp());
 
-    std::filesystem::remove_all(ERRLOG_PERSIST_PATH);
+    // Leave the directory in case other CI instances are running
+    fs::remove(fs::path{ERRLOG_PERSIST_PATH} / std::to_string(id));
 }
 
 } // namespace test