blob: d04da04380bcd60f68e149769cc8196ce1e6b193 [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"};
Matt Spinlerfb978da2022-01-21 08:42:24 -060051 std::string path{"/tmp/99"};
Matt Spinler1e71a4d2020-03-04 13:40:22 -060052 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,
Matt Spinlerfb978da2022-01-21 08:42:24 -060064 path,
Matt Spinler1e71a4d2020-03-04 13:40:22 -060065 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
Matt Spinler7ec2ab72020-04-03 11:54:29 -050087 // Leave the directory in case other CI instances are running
88 fs::remove(fs::path{ERRLOG_PERSIST_PATH} / std::to_string(id));
Matt Spinler1e71a4d2020-03-04 13:40:22 -060089}
90
91} // namespace test
92} // namespace logging
93} // namespace phosphor