blob: 65b13287b6378deecdd30fc73b79d49186ded3d6 [file] [log] [blame]
Matt Spinler1e71a4d2020-03-04 13:40:22 -06001#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
12namespace phosphor
13{
14namespace logging
15{
16namespace test
17{
18
19using namespace std::chrono_literals;
Matt Spinler7ec2ab72020-04-03 11:54:29 -050020namespace fs = std::filesystem;
Matt Spinler1e71a4d2020-03-04 13:40:22 -060021
22// Test that the update timestamp changes when the resolved property changes
23TEST(TestUpdateTS, testChangeResolved)
24{
25 // Setting resolved will serialize, so need this directory.
Matt Spinler7ec2ab72020-04-03 11:54:29 -050026 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 }
Matt Spinler1e71a4d2020-03-04 13:40:22 -060033
34 auto bus = sdbusplus::bus::new_default();
35 phosphor::logging::internal::Manager manager(bus, OBJ_INTERNAL);
36
Matt Spinler7ec2ab72020-04-03 11:54:29 -050037 // 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
Matt Spinler1e71a4d2020-03-04 13:40:22 -060048 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
Matt Spinler7ec2ab72020-04-03 11:54:29 -050085 // Leave the directory in case other CI instances are running
86 fs::remove(fs::path{ERRLOG_PERSIST_PATH} / std::to_string(id));
Matt Spinler1e71a4d2020-03-04 13:40:22 -060087}
88
89} // namespace test
90} // namespace logging
91} // namespace phosphor