blob: cabcb600c6ef6634c53eb32a193e8a5c9bc75c48 [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"
Arya K Padman916bb972024-09-26 01:39:26 -05005#include "extensions.hpp"
Matt Spinler1e71a4d2020-03-04 13:40:22 -06006#include "log_manager.hpp"
Patrick Williamsfa2d9622024-09-30 16:25:43 -04007#include "paths.hpp"
Matt Spinler1e71a4d2020-03-04 13:40:22 -06008
9#include <filesystem>
10#include <thread>
11
12#include <gtest/gtest.h>
13
14namespace phosphor
15{
16namespace logging
17{
18namespace test
19{
20
21using namespace std::chrono_literals;
Matt Spinler7ec2ab72020-04-03 11:54:29 -050022namespace fs = std::filesystem;
Matt Spinler1e71a4d2020-03-04 13:40:22 -060023
Arya K Padman916bb972024-09-26 01:39:26 -050024void deleteIsProhibitedMock(uint32_t /*id*/, bool& prohibited)
25{
26 prohibited = true;
27}
28
29void deleteIsNotProhibitedMock(uint32_t /*id*/, bool& prohibited)
30{
31 prohibited = false;
32}
33
Matt Spinler1e71a4d2020-03-04 13:40:22 -060034// Test that the update timestamp changes when the resolved property changes
35TEST(TestUpdateTS, testChangeResolved)
36{
37 // Setting resolved will serialize, so need this directory.
Patrick Williamsfa2d9622024-09-30 16:25:43 -040038 fs::create_directories(paths::error());
Matt Spinler7ec2ab72020-04-03 11:54:29 -050039
Patrick Williamsfa2d9622024-09-30 16:25:43 -040040 if (!fs::exists(paths::error()))
Matt Spinler7ec2ab72020-04-03 11:54:29 -050041 {
Patrick Williamsfa2d9622024-09-30 16:25:43 -040042 ADD_FAILURE() << "Could not create " << paths::error() << "\n";
Matt Spinler7ec2ab72020-04-03 11:54:29 -050043 exit(1);
44 }
Matt Spinler1e71a4d2020-03-04 13:40:22 -060045
46 auto bus = sdbusplus::bus::new_default();
47 phosphor::logging::internal::Manager manager(bus, OBJ_INTERNAL);
48
Matt Spinler7ec2ab72020-04-03 11:54:29 -050049 // Use a random number for the ID to avoid other CI
50 // testcases running in parallel.
51 std::srand(std::time(nullptr));
52 uint32_t id = std::rand();
53
Patrick Williamsfa2d9622024-09-30 16:25:43 -040054 if (fs::exists(fs::path{paths::error()} / std::to_string(id)))
Matt Spinler7ec2ab72020-04-03 11:54:29 -050055 {
56 std::cerr << "Another testcase is using ID " << id << "\n";
57 id = std::rand();
58 }
59
Matt Spinler1e71a4d2020-03-04 13:40:22 -060060 uint64_t timestamp{100};
61 std::string message{"test error"};
62 std::string fwLevel{"level42"};
Matt Spinlerfb978da2022-01-21 08:42:24 -060063 std::string path{"/tmp/99"};
Matt Spinler1e71a4d2020-03-04 13:40:22 -060064 std::vector<std::string> testData{"additional", "data"};
65 phosphor::logging::AssociationList associations{};
66
67 Entry elog{bus,
68 std::string(OBJ_ENTRY) + '/' + std::to_string(id),
69 id,
70 timestamp,
71 Entry::Level::Informational,
72 std::move(message),
73 std::move(testData),
74 std::move(associations),
75 fwLevel,
Matt Spinlerfb978da2022-01-21 08:42:24 -060076 path,
Matt Spinler1e71a4d2020-03-04 13:40:22 -060077 manager};
78
79 EXPECT_EQ(elog.timestamp(), elog.updateTimestamp());
80
81 std::this_thread::sleep_for(1ms);
82
83 elog.resolved(true);
84 auto updateTS = elog.updateTimestamp();
85 EXPECT_NE(updateTS, elog.timestamp());
86
87 std::this_thread::sleep_for(1ms);
88
89 elog.resolved(false);
90 EXPECT_NE(updateTS, elog.updateTimestamp());
91 updateTS = elog.updateTimestamp();
92
93 std::this_thread::sleep_for(1ms);
94
95 // No change
96 elog.resolved(false);
97 EXPECT_EQ(updateTS, elog.updateTimestamp());
98
Matt Spinler7ec2ab72020-04-03 11:54:29 -050099 // Leave the directory in case other CI instances are running
Patrick Williamsfa2d9622024-09-30 16:25:43 -0400100 fs::remove(fs::path{paths::error()} / std::to_string(id));
Matt Spinler1e71a4d2020-03-04 13:40:22 -0600101}
102
Arya K Padman916bb972024-09-26 01:39:26 -0500103TEST(TestResolveProhibited, testResolveFlagChange)
104{
Patrick Williams730c40f2024-10-03 09:50:50 -0400105 auto persist_path = phosphor::logging::paths::error();
Arya K Padman916bb972024-09-26 01:39:26 -0500106
Patrick Williams730c40f2024-10-03 09:50:50 -0400107 // Setting resolved will serialize, so need this directory.
108 fs::create_directories(persist_path);
109
110 if (!fs::exists(persist_path))
Arya K Padman916bb972024-09-26 01:39:26 -0500111 {
Patrick Williams730c40f2024-10-03 09:50:50 -0400112 ADD_FAILURE() << "Could not create "
113 << phosphor::logging::paths::error() << "\n";
Arya K Padman916bb972024-09-26 01:39:26 -0500114 exit(1);
115 }
116
117 auto bus = sdbusplus::bus::new_default();
118 phosphor::logging::internal::Manager manager(bus, OBJ_INTERNAL);
119
120 // Use a random number for the ID to avoid other CI
121 // testcases running in parallel.
122 std::srand(std::time(nullptr));
123 uint32_t id = std::rand();
124
Patrick Williams730c40f2024-10-03 09:50:50 -0400125 if (fs::exists(persist_path / std::to_string(id)))
Arya K Padman916bb972024-09-26 01:39:26 -0500126 {
127 std::cerr << "Another testcase is using ID " << id << "\n";
128 id = std::rand();
129 }
130
131 uint64_t timestamp{100};
132 std::string message{"test error"};
133 std::string fwLevel{"level42"};
134 std::string path{"/tmp/99"};
135 std::vector<std::string> testData{"additional", "data"};
136 phosphor::logging::AssociationList associations{};
137
138 Entry elog{bus,
139 std::string(OBJ_ENTRY) + '/' + std::to_string(id),
140 id,
141 timestamp,
142 Entry::Level::Informational,
143 std::move(message),
144 std::move(testData),
145 std::move(associations),
146 fwLevel,
147 path,
148 manager};
149
150 Extensions ext{deleteIsProhibitedMock};
151
152 EXPECT_THROW(elog.resolved(true),
153 sdbusplus::xyz::openbmc_project::Common::Error::Unavailable);
154
155 Extensions::getDeleteProhibitedFunctions().clear();
156
157 Extensions e{deleteIsNotProhibitedMock};
158
159 EXPECT_NO_THROW(elog.resolved(true));
160 EXPECT_EQ(elog.resolved(), true);
161
162 // Leave the directory in case other CI instances are running
Patrick Williams730c40f2024-10-03 09:50:50 -0400163 fs::remove(persist_path / std::to_string(id));
Arya K Padman916bb972024-09-26 01:39:26 -0500164}
Matt Spinler1e71a4d2020-03-04 13:40:22 -0600165} // namespace test
166} // namespace logging
167} // namespace phosphor