1 #include "config.h"
2 
3 #include "elog_entry.hpp"
4 #include "elog_serialize.hpp"
5 #include "log_manager.hpp"
6 
7 #include <filesystem>
8 #include <thread>
9 
10 #include <gtest/gtest.h>
11 
12 namespace phosphor
13 {
14 namespace logging
15 {
16 namespace test
17 {
18 
19 using namespace std::chrono_literals;
20 namespace fs = std::filesystem;
21 
22 // Test that the update timestamp changes when the resolved property changes
23 TEST(TestUpdateTS, testChangeResolved)
24 {
25     // Setting resolved will serialize, so need this directory.
26     fs::create_directory(ERRLOG_PERSIST_PATH);
27 
28     if (!fs::exists(ERRLOG_PERSIST_PATH))
29     {
30         ADD_FAILURE() << "Could not create " << ERRLOG_PERSIST_PATH << "\n";
31         exit(1);
32     }
33 
34     auto bus = sdbusplus::bus::new_default();
35     phosphor::logging::internal::Manager manager(bus, OBJ_INTERNAL);
36 
37     // Use a random number for the ID to avoid other CI
38     // testcases running in parallel.
39     std::srand(std::time(nullptr));
40     uint32_t id = std::rand();
41 
42     if (fs::exists(fs::path{ERRLOG_PERSIST_PATH} / std::to_string(id)))
43     {
44         std::cerr << "Another testcase is using ID " << id << "\n";
45         id = std::rand();
46     }
47 
48     uint64_t timestamp{100};
49     std::string message{"test error"};
50     std::string fwLevel{"level42"};
51     std::string path{"/tmp/99"};
52     std::vector<std::string> testData{"additional", "data"};
53     phosphor::logging::AssociationList associations{};
54 
55     Entry elog{bus,
56                std::string(OBJ_ENTRY) + '/' + std::to_string(id),
57                id,
58                timestamp,
59                Entry::Level::Informational,
60                std::move(message),
61                std::move(testData),
62                std::move(associations),
63                fwLevel,
64                path,
65                manager};
66 
67     EXPECT_EQ(elog.timestamp(), elog.updateTimestamp());
68 
69     std::this_thread::sleep_for(1ms);
70 
71     elog.resolved(true);
72     auto updateTS = elog.updateTimestamp();
73     EXPECT_NE(updateTS, elog.timestamp());
74 
75     std::this_thread::sleep_for(1ms);
76 
77     elog.resolved(false);
78     EXPECT_NE(updateTS, elog.updateTimestamp());
79     updateTS = elog.updateTimestamp();
80 
81     std::this_thread::sleep_for(1ms);
82 
83     // No change
84     elog.resolved(false);
85     EXPECT_EQ(updateTS, elog.updateTimestamp());
86 
87     // Leave the directory in case other CI instances are running
88     fs::remove(fs::path{ERRLOG_PERSIST_PATH} / std::to_string(id));
89 }
90 
91 } // namespace test
92 } // namespace logging
93 } // namespace phosphor
94