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::vector<std::string> testData{"additional", "data"};
52     phosphor::logging::AssociationList associations{};
53 
54     Entry elog{bus,
55                std::string(OBJ_ENTRY) + '/' + std::to_string(id),
56                id,
57                timestamp,
58                Entry::Level::Informational,
59                std::move(message),
60                std::move(testData),
61                std::move(associations),
62                fwLevel,
63                manager};
64 
65     EXPECT_EQ(elog.timestamp(), elog.updateTimestamp());
66 
67     std::this_thread::sleep_for(1ms);
68 
69     elog.resolved(true);
70     auto updateTS = elog.updateTimestamp();
71     EXPECT_NE(updateTS, elog.timestamp());
72 
73     std::this_thread::sleep_for(1ms);
74 
75     elog.resolved(false);
76     EXPECT_NE(updateTS, elog.updateTimestamp());
77     updateTS = elog.updateTimestamp();
78 
79     std::this_thread::sleep_for(1ms);
80 
81     // No change
82     elog.resolved(false);
83     EXPECT_EQ(updateTS, elog.updateTimestamp());
84 
85     // Leave the directory in case other CI instances are running
86     fs::remove(fs::path{ERRLOG_PERSIST_PATH} / std::to_string(id));
87 }
88 
89 } // namespace test
90 } // namespace logging
91 } // namespace phosphor
92