blob: eb0dd21483733f5670b428803d0c34880a5320ab [file] [log] [blame]
Andrew Geissler328889d2016-10-10 12:43:48 -05001// A basic unit test that runs on a BMC (qemu or hardware)
2
Michael Tritze0eb1dd2017-02-19 20:57:46 -06003#include <getopt.h>
Andrew Geissler328889d2016-10-10 12:43:48 -05004#include <systemd/sd-journal.h>
Patrick Venturef18bf832018-10-26 18:14:00 -07005
Patrick Venture30047bf2018-11-01 18:52:15 -07006#include <cstring>
Patrick Venturef18bf832018-10-26 18:14:00 -07007#include <iostream>
8#include <phosphor-logging/elog-errors.hpp>
Saqib Khan2bb15192017-02-13 13:19:55 -06009#include <phosphor-logging/elog.hpp>
10#include <phosphor-logging/log.hpp>
Patrick Venturef18bf832018-10-26 18:14:00 -070011#include <sdbusplus/exception.hpp>
12#include <sstream>
Andrew Geissler328889d2016-10-10 12:43:48 -050013
14using namespace phosphor;
15using namespace logging;
16
Patrick Venturef18bf832018-10-26 18:14:00 -070017const char* usage = "Usage: logging-test [OPTION] \n\n\
Michael Tritze0eb1dd2017-02-19 20:57:46 -060018Options: \n\
19[NONE] Default test case. \n\
20-h, --help Display this usage text. \n\
21-c, --commit <string> Commit desired error. \n\n\
22Valid errors to commit: \n\
Marri Devender Rao54932102017-04-04 04:16:35 -050023AutoTestSimple, AutoTestCreateAndCommit\n";
Michael Tritze0eb1dd2017-02-19 20:57:46 -060024
Andrew Geissler328889d2016-10-10 12:43:48 -050025// validate the journal metadata equals the input value
Patrick Venturef18bf832018-10-26 18:14:00 -070026int validate_journal(const char* i_entry, const char* i_value)
Andrew Geissler328889d2016-10-10 12:43:48 -050027{
Patrick Venturef18bf832018-10-26 18:14:00 -070028 sd_journal* journal;
29 const void* data;
Andrew Geissler328889d2016-10-10 12:43:48 -050030 size_t l;
31 int rc;
32 bool validated = false;
33
34 rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY);
Patrick Venturef18bf832018-10-26 18:14:00 -070035 if (rc < 0)
36 {
37 std::cerr << "Failed to open journal: " << strerror(-rc) << "\n";
38 return 1;
Andrew Geissler328889d2016-10-10 12:43:48 -050039 }
40 rc = sd_journal_query_unique(journal, i_entry);
Patrick Venturef18bf832018-10-26 18:14:00 -070041 if (rc < 0)
42 {
43 std::cerr << "Failed to query journal: " << strerror(-rc) << "\n";
44 return 1;
Andrew Geissler328889d2016-10-10 12:43:48 -050045 }
46 SD_JOURNAL_FOREACH_UNIQUE(journal, data, l)
47 {
48 std::string journ_entry((const char*)data);
49 std::cout << journ_entry << "\n";
Patrick Venturef18bf832018-10-26 18:14:00 -070050 if (journ_entry.find(i_value) != std::string::npos)
Andrew Geissler328889d2016-10-10 12:43:48 -050051 {
52 std::cout << "We found it!\n";
53 validated = true;
54 break;
55 }
56 }
57
58 sd_journal_close(journal);
59
60 rc = (validated) ? 0 : 1;
Patrick Venturef18bf832018-10-26 18:14:00 -070061 if (rc)
Andrew Geissler328889d2016-10-10 12:43:48 -050062 {
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050063 std::cerr << "Failed to find " << i_entry << " with value " << i_value
Patrick Venturef18bf832018-10-26 18:14:00 -070064 << " in journal!"
65 << "\n";
Andrew Geissler328889d2016-10-10 12:43:48 -050066 }
67
68 return rc;
69}
70
Michael Tritze0eb1dd2017-02-19 20:57:46 -060071int elog_test()
Andrew Geissler328889d2016-10-10 12:43:48 -050072{
73 // TEST 1 - Basic log
74 log<level::DEBUG>("Basic phosphor logging test");
75
76 // TEST 2 - Log with metadata field
Patrick Venturef18bf832018-10-26 18:14:00 -070077 const char* file_name = "phosphor_logging_test.txt";
Andrew Geissler328889d2016-10-10 12:43:48 -050078 int number = 0xFEFE;
Patrick Venturef18bf832018-10-26 18:14:00 -070079 log<level::DEBUG>(
80 "phosphor logging test with attribute",
81 entry("FILE_NAME_WITH_NUM_TEST=%s_%x", file_name, number));
Andrew Geissler328889d2016-10-10 12:43:48 -050082
83 // Now read back and verify our data made it into the journal
84 int rc = validate_journal("FILE_NAME_WITH_NUM_TEST",
85 "phosphor_logging_test.txt_fefe");
Patrick Venturef18bf832018-10-26 18:14:00 -070086 if (rc)
87 return (rc);
Andrew Geissler328889d2016-10-10 12:43:48 -050088
89 // TEST 3 - Create error log with 2 meta data fields (rvalue and lvalue)
90 number = 0x1234;
Patrick Venturef18bf832018-10-26 18:14:00 -070091 const char* test_string = "/tmp/test_string/";
Andrew Geissler6d910ad2016-10-16 20:49:14 -050092 try
93 {
Deepak Kodihalli5221e1b2017-03-03 00:02:09 -060094 elog<example::xyz::openbmc_project::Example::Elog::TestErrorOne>(
Patrick Venturef18bf832018-10-26 18:14:00 -070095 example::xyz::openbmc_project::Example::Elog::TestErrorOne::ERRNUM(
96 number),
97 example::xyz::openbmc_project::Example::Elog::TestErrorOne::
98 FILE_PATH(test_string),
99 example::xyz::openbmc_project::Example::Elog::TestErrorOne::
100 FILE_NAME("elog_test_3.txt"),
101 example::xyz::openbmc_project::Example::Elog::TestErrorTwo::
102 DEV_ADDR(0xDEADDEAD),
103 example::xyz::openbmc_project::Example::Elog::TestErrorTwo::DEV_ID(
104 100),
105 example::xyz::openbmc_project::Example::Elog::TestErrorTwo::
106 DEV_NAME("test case 3"));
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500107 }
Patrick Williams66491c62021-10-06 12:23:37 -0500108 catch (const example::xyz::openbmc_project::Example::Elog::TestErrorOne& e)
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500109 {
110 std::cout << "elog exception caught: " << e.what() << std::endl;
111 }
Andrew Geissler328889d2016-10-10 12:43:48 -0500112
Andrew Geisslerdf048c12016-11-10 16:50:35 -0600113 // Reduce our error namespaces
Deepak Kodihalli5221e1b2017-03-03 00:02:09 -0600114 using namespace example::xyz::openbmc_project::Example::Elog;
Andrew Geisslerdf048c12016-11-10 16:50:35 -0600115
Andrew Geissler328889d2016-10-10 12:43:48 -0500116 // Now read back and verify our data made it into the journal
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500117 std::stringstream stream;
118 stream << std::hex << number;
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600119 rc = validate_journal(TestErrorOne::ERRNUM::str_short,
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500120 std::string(stream.str()).c_str());
Patrick Venturef18bf832018-10-26 18:14:00 -0700121 if (rc)
122 return (rc);
Andrew Geissler328889d2016-10-10 12:43:48 -0500123
Patrick Venturef18bf832018-10-26 18:14:00 -0700124 rc = validate_journal(TestErrorOne::FILE_PATH::str_short, test_string);
125 if (rc)
126 return (rc);
Andrew Geissler328889d2016-10-10 12:43:48 -0500127
Patrick Venturef18bf832018-10-26 18:14:00 -0700128 rc =
129 validate_journal(TestErrorOne::FILE_NAME::str_short, "elog_test_3.txt");
130 if (rc)
131 return (rc);
Andrew Geissler328889d2016-10-10 12:43:48 -0500132
Patrick Venturef18bf832018-10-26 18:14:00 -0700133 rc = validate_journal(TestErrorTwo::DEV_ADDR::str_short, "0xDEADDEAD");
134 if (rc)
135 return (rc);
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600136
Patrick Venturef18bf832018-10-26 18:14:00 -0700137 rc = validate_journal(TestErrorTwo::DEV_ID::str_short, "100");
138 if (rc)
139 return (rc);
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600140
Patrick Venturef18bf832018-10-26 18:14:00 -0700141 rc = validate_journal(TestErrorTwo::DEV_NAME::str_short, "test case 3");
142 if (rc)
143 return (rc);
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600144
Andrew Geissler328889d2016-10-10 12:43:48 -0500145 // TEST 4 - Create error log with previous entry use
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500146 number = 0x9876;
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500147 try
148 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700149 elog<TestErrorOne>(
150 TestErrorOne::ERRNUM(number), prev_entry<TestErrorOne::FILE_PATH>(),
151 TestErrorOne::FILE_NAME("elog_test_4.txt"),
152 TestErrorTwo::DEV_ADDR(0xDEADDEAD), TestErrorTwo::DEV_ID(100),
153 TestErrorTwo::DEV_NAME("test case 4"));
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500154 }
Patrick Williams66491c62021-10-06 12:23:37 -0500155 catch (const sdbusplus::exception_t& e)
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500156 {
157 std::cout << "elog exception caught: " << e.what() << std::endl;
158 }
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500159
Andrew Geissler328889d2016-10-10 12:43:48 -0500160 // Now read back and verify our data made it into the journal
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500161 stream.str("");
162 stream << std::hex << number;
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600163 rc = validate_journal(TestErrorOne::ERRNUM::str_short,
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500164 std::string(stream.str()).c_str());
Patrick Venturef18bf832018-10-26 18:14:00 -0700165 if (rc)
166 return (rc);
Andrew Geissler328889d2016-10-10 12:43:48 -0500167
168 // This should just be equal to what we put in test 3
Patrick Venturef18bf832018-10-26 18:14:00 -0700169 rc = validate_journal(TestErrorOne::FILE_PATH::str_short, test_string);
170 if (rc)
171 return (rc);
Andrew Geissler328889d2016-10-10 12:43:48 -0500172
Patrick Venturef18bf832018-10-26 18:14:00 -0700173 rc =
174 validate_journal(TestErrorOne::FILE_NAME::str_short, "elog_test_4.txt");
175 if (rc)
176 return (rc);
Andrew Geissler328889d2016-10-10 12:43:48 -0500177
Patrick Venturef18bf832018-10-26 18:14:00 -0700178 rc = validate_journal(TestErrorTwo::DEV_ADDR::str_short, "0xDEADDEAD");
179 if (rc)
180 return (rc);
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600181
Patrick Venturef18bf832018-10-26 18:14:00 -0700182 rc = validate_journal(TestErrorTwo::DEV_ID::str_short, "100");
183 if (rc)
184 return (rc);
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600185
Patrick Venturef18bf832018-10-26 18:14:00 -0700186 rc = validate_journal(TestErrorTwo::DEV_NAME::str_short, "test case 4");
187 if (rc)
188 return (rc);
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600189
Andrew Geissler328889d2016-10-10 12:43:48 -0500190 // Compile fail tests
191
192 // Simple test to prove we fail to compile due to missing param
Patrick Venturef18bf832018-10-26 18:14:00 -0700193 // elog<TestErrorOne>(TestErrorOne::ERRNUM(1),
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600194 // TestErrorOne::FILE_PATH("test"));
Andrew Geissler328889d2016-10-10 12:43:48 -0500195
196 // Simple test to prove we fail to compile due to invalid param
Patrick Venturef18bf832018-10-26 18:14:00 -0700197 // elog<TestErrorOne>(TestErrorOne::ERRNUM(1),
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600198 // TestErrorOne::FILE_PATH("test"),
199 // TestErrorOne::FILE_NAME(1));
Andrew Geissler328889d2016-10-10 12:43:48 -0500200
201 return 0;
202}
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600203
Patrick Venturef18bf832018-10-26 18:14:00 -0700204void commitError(const char* text)
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600205{
Patrick Venture30047bf2018-11-01 18:52:15 -0700206 if (std::strcmp(text, "AutoTestSimple") == 0)
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600207 {
208 try
209 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700210 elog<example::xyz::openbmc_project::Example::Elog::AutoTestSimple>(
211 example::xyz::openbmc_project::Example::Elog::AutoTestSimple::
212 STRING("FOO"));
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600213 }
Patrick Williams66491c62021-10-06 12:23:37 -0500214 catch (
215 const example::xyz::openbmc_project::Example::Elog::AutoTestSimple&
216 e)
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600217 {
218 std::cout << "elog exception caught: " << e.what() << std::endl;
219 commit(e.name());
220 }
221 }
Patrick Venture30047bf2018-11-01 18:52:15 -0700222 else if (std::strcmp(text, "AutoTestCreateAndCommit") == 0)
Marri Devender Rao54932102017-04-04 04:16:35 -0500223 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700224 report<example::xyz::openbmc_project::Example::Elog::AutoTestSimple>(
225 example::xyz::openbmc_project::Example::Elog::AutoTestSimple::
226 STRING("FOO"));
Marri Devender Rao54932102017-04-04 04:16:35 -0500227 }
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600228
229 return;
230}
231
Patrick Venturef18bf832018-10-26 18:14:00 -0700232int main(int argc, char* argv[])
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600233{
Patrick Williams9936b8a2021-04-21 21:45:19 -0500234 int arg;
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600235
236 if (argc == 1)
237 return elog_test();
238
Patrick Venturef18bf832018-10-26 18:14:00 -0700239 static struct option long_options[] = {
240 {"help", no_argument, 0, 'h'},
241 {"commit", required_argument, 0, 'c'},
242 {0, 0, 0, 0}};
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600243 int option_index = 0;
244
Patrick Venturef18bf832018-10-26 18:14:00 -0700245 while ((arg = getopt_long(argc, argv, "hc:", long_options,
246 &option_index)) != -1)
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600247 {
248 switch (arg)
249 {
250 case 'c':
251 commitError(optarg);
252 return 0;
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600253 case 'h':
254 case '?':
255 std::cerr << usage;
256 return 1;
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600257 }
258 }
259
260 return 0;
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600261}