blob: 3a144f83299f0bebde2fdca3ddc7bdee2da18d9c [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
6#include <iostream>
7#include <phosphor-logging/elog-errors.hpp>
Saqib Khan2bb15192017-02-13 13:19:55 -06008#include <phosphor-logging/elog.hpp>
9#include <phosphor-logging/log.hpp>
Patrick Venturef18bf832018-10-26 18:14:00 -070010#include <sdbusplus/exception.hpp>
11#include <sstream>
Andrew Geissler328889d2016-10-10 12:43:48 -050012
13using namespace phosphor;
14using namespace logging;
15
Patrick Venturef18bf832018-10-26 18:14:00 -070016const char* usage = "Usage: logging-test [OPTION] \n\n\
Michael Tritze0eb1dd2017-02-19 20:57:46 -060017Options: \n\
18[NONE] Default test case. \n\
19-h, --help Display this usage text. \n\
20-c, --commit <string> Commit desired error. \n\n\
21Valid errors to commit: \n\
Marri Devender Rao54932102017-04-04 04:16:35 -050022AutoTestSimple, AutoTestCreateAndCommit\n";
Michael Tritze0eb1dd2017-02-19 20:57:46 -060023
Andrew Geissler328889d2016-10-10 12:43:48 -050024// validate the journal metadata equals the input value
Patrick Venturef18bf832018-10-26 18:14:00 -070025int validate_journal(const char* i_entry, const char* i_value)
Andrew Geissler328889d2016-10-10 12:43:48 -050026{
Patrick Venturef18bf832018-10-26 18:14:00 -070027 sd_journal* journal;
28 const void* data;
Andrew Geissler328889d2016-10-10 12:43:48 -050029 size_t l;
30 int rc;
31 bool validated = false;
32
33 rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY);
Patrick Venturef18bf832018-10-26 18:14:00 -070034 if (rc < 0)
35 {
36 std::cerr << "Failed to open journal: " << strerror(-rc) << "\n";
37 return 1;
Andrew Geissler328889d2016-10-10 12:43:48 -050038 }
39 rc = sd_journal_query_unique(journal, i_entry);
Patrick Venturef18bf832018-10-26 18:14:00 -070040 if (rc < 0)
41 {
42 std::cerr << "Failed to query journal: " << strerror(-rc) << "\n";
43 return 1;
Andrew Geissler328889d2016-10-10 12:43:48 -050044 }
45 SD_JOURNAL_FOREACH_UNIQUE(journal, data, l)
46 {
47 std::string journ_entry((const char*)data);
48 std::cout << journ_entry << "\n";
Patrick Venturef18bf832018-10-26 18:14:00 -070049 if (journ_entry.find(i_value) != std::string::npos)
Andrew Geissler328889d2016-10-10 12:43:48 -050050 {
51 std::cout << "We found it!\n";
52 validated = true;
53 break;
54 }
55 }
56
57 sd_journal_close(journal);
58
59 rc = (validated) ? 0 : 1;
Patrick Venturef18bf832018-10-26 18:14:00 -070060 if (rc)
Andrew Geissler328889d2016-10-10 12:43:48 -050061 {
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050062 std::cerr << "Failed to find " << i_entry << " with value " << i_value
Patrick Venturef18bf832018-10-26 18:14:00 -070063 << " in journal!"
64 << "\n";
Andrew Geissler328889d2016-10-10 12:43:48 -050065 }
66
67 return rc;
68}
69
Michael Tritze0eb1dd2017-02-19 20:57:46 -060070int elog_test()
Andrew Geissler328889d2016-10-10 12:43:48 -050071{
72 // TEST 1 - Basic log
73 log<level::DEBUG>("Basic phosphor logging test");
74
75 // TEST 2 - Log with metadata field
Patrick Venturef18bf832018-10-26 18:14:00 -070076 const char* file_name = "phosphor_logging_test.txt";
Andrew Geissler328889d2016-10-10 12:43:48 -050077 int number = 0xFEFE;
Patrick Venturef18bf832018-10-26 18:14:00 -070078 log<level::DEBUG>(
79 "phosphor logging test with attribute",
80 entry("FILE_NAME_WITH_NUM_TEST=%s_%x", file_name, number));
Andrew Geissler328889d2016-10-10 12:43:48 -050081
82 // Now read back and verify our data made it into the journal
83 int rc = validate_journal("FILE_NAME_WITH_NUM_TEST",
84 "phosphor_logging_test.txt_fefe");
Patrick Venturef18bf832018-10-26 18:14:00 -070085 if (rc)
86 return (rc);
Andrew Geissler328889d2016-10-10 12:43:48 -050087
88 // TEST 3 - Create error log with 2 meta data fields (rvalue and lvalue)
89 number = 0x1234;
Patrick Venturef18bf832018-10-26 18:14:00 -070090 const char* test_string = "/tmp/test_string/";
Andrew Geissler6d910ad2016-10-16 20:49:14 -050091 try
92 {
Deepak Kodihalli5221e1b2017-03-03 00:02:09 -060093 elog<example::xyz::openbmc_project::Example::Elog::TestErrorOne>(
Patrick Venturef18bf832018-10-26 18:14:00 -070094 example::xyz::openbmc_project::Example::Elog::TestErrorOne::ERRNUM(
95 number),
96 example::xyz::openbmc_project::Example::Elog::TestErrorOne::
97 FILE_PATH(test_string),
98 example::xyz::openbmc_project::Example::Elog::TestErrorOne::
99 FILE_NAME("elog_test_3.txt"),
100 example::xyz::openbmc_project::Example::Elog::TestErrorTwo::
101 DEV_ADDR(0xDEADDEAD),
102 example::xyz::openbmc_project::Example::Elog::TestErrorTwo::DEV_ID(
103 100),
104 example::xyz::openbmc_project::Example::Elog::TestErrorTwo::
105 DEV_NAME("test case 3"));
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500106 }
Deepak Kodihalli15331102017-03-09 23:50:43 -0600107 catch (example::xyz::openbmc_project::Example::Elog::TestErrorOne& e)
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500108 {
109 std::cout << "elog exception caught: " << e.what() << std::endl;
110 }
Andrew Geissler328889d2016-10-10 12:43:48 -0500111
Andrew Geisslerdf048c12016-11-10 16:50:35 -0600112 // Reduce our error namespaces
Deepak Kodihalli5221e1b2017-03-03 00:02:09 -0600113 using namespace example::xyz::openbmc_project::Example::Elog;
Andrew Geisslerdf048c12016-11-10 16:50:35 -0600114
Andrew Geissler328889d2016-10-10 12:43:48 -0500115 // Now read back and verify our data made it into the journal
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500116 std::stringstream stream;
117 stream << std::hex << number;
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600118 rc = validate_journal(TestErrorOne::ERRNUM::str_short,
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500119 std::string(stream.str()).c_str());
Patrick Venturef18bf832018-10-26 18:14:00 -0700120 if (rc)
121 return (rc);
Andrew Geissler328889d2016-10-10 12:43:48 -0500122
Patrick Venturef18bf832018-10-26 18:14:00 -0700123 rc = validate_journal(TestErrorOne::FILE_PATH::str_short, test_string);
124 if (rc)
125 return (rc);
Andrew Geissler328889d2016-10-10 12:43:48 -0500126
Patrick Venturef18bf832018-10-26 18:14:00 -0700127 rc =
128 validate_journal(TestErrorOne::FILE_NAME::str_short, "elog_test_3.txt");
129 if (rc)
130 return (rc);
Andrew Geissler328889d2016-10-10 12:43:48 -0500131
Patrick Venturef18bf832018-10-26 18:14:00 -0700132 rc = validate_journal(TestErrorTwo::DEV_ADDR::str_short, "0xDEADDEAD");
133 if (rc)
134 return (rc);
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600135
Patrick Venturef18bf832018-10-26 18:14:00 -0700136 rc = validate_journal(TestErrorTwo::DEV_ID::str_short, "100");
137 if (rc)
138 return (rc);
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600139
Patrick Venturef18bf832018-10-26 18:14:00 -0700140 rc = validate_journal(TestErrorTwo::DEV_NAME::str_short, "test case 3");
141 if (rc)
142 return (rc);
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600143
Andrew Geissler328889d2016-10-10 12:43:48 -0500144 // TEST 4 - Create error log with previous entry use
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500145 number = 0x9876;
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500146 try
147 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700148 elog<TestErrorOne>(
149 TestErrorOne::ERRNUM(number), prev_entry<TestErrorOne::FILE_PATH>(),
150 TestErrorOne::FILE_NAME("elog_test_4.txt"),
151 TestErrorTwo::DEV_ADDR(0xDEADDEAD), TestErrorTwo::DEV_ID(100),
152 TestErrorTwo::DEV_NAME("test case 4"));
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500153 }
Deepak Kodihalli15331102017-03-09 23:50:43 -0600154 catch (sdbusplus::exception_t& e)
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500155 {
156 std::cout << "elog exception caught: " << e.what() << std::endl;
157 }
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500158
Andrew Geissler328889d2016-10-10 12:43:48 -0500159 // Now read back and verify our data made it into the journal
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500160 stream.str("");
161 stream << std::hex << number;
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600162 rc = validate_journal(TestErrorOne::ERRNUM::str_short,
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500163 std::string(stream.str()).c_str());
Patrick Venturef18bf832018-10-26 18:14:00 -0700164 if (rc)
165 return (rc);
Andrew Geissler328889d2016-10-10 12:43:48 -0500166
167 // This should just be equal to what we put in test 3
Patrick Venturef18bf832018-10-26 18:14:00 -0700168 rc = validate_journal(TestErrorOne::FILE_PATH::str_short, test_string);
169 if (rc)
170 return (rc);
Andrew Geissler328889d2016-10-10 12:43:48 -0500171
Patrick Venturef18bf832018-10-26 18:14:00 -0700172 rc =
173 validate_journal(TestErrorOne::FILE_NAME::str_short, "elog_test_4.txt");
174 if (rc)
175 return (rc);
Andrew Geissler328889d2016-10-10 12:43:48 -0500176
Patrick Venturef18bf832018-10-26 18:14:00 -0700177 rc = validate_journal(TestErrorTwo::DEV_ADDR::str_short, "0xDEADDEAD");
178 if (rc)
179 return (rc);
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600180
Patrick Venturef18bf832018-10-26 18:14:00 -0700181 rc = validate_journal(TestErrorTwo::DEV_ID::str_short, "100");
182 if (rc)
183 return (rc);
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600184
Patrick Venturef18bf832018-10-26 18:14:00 -0700185 rc = validate_journal(TestErrorTwo::DEV_NAME::str_short, "test case 4");
186 if (rc)
187 return (rc);
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600188
Andrew Geissler328889d2016-10-10 12:43:48 -0500189 // Compile fail tests
190
191 // Simple test to prove we fail to compile due to missing param
Patrick Venturef18bf832018-10-26 18:14:00 -0700192 // elog<TestErrorOne>(TestErrorOne::ERRNUM(1),
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600193 // TestErrorOne::FILE_PATH("test"));
Andrew Geissler328889d2016-10-10 12:43:48 -0500194
195 // Simple test to prove we fail to compile due to invalid param
Patrick Venturef18bf832018-10-26 18:14:00 -0700196 // elog<TestErrorOne>(TestErrorOne::ERRNUM(1),
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600197 // TestErrorOne::FILE_PATH("test"),
198 // TestErrorOne::FILE_NAME(1));
Andrew Geissler328889d2016-10-10 12:43:48 -0500199
200 return 0;
201}
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600202
Patrick Venturef18bf832018-10-26 18:14:00 -0700203void commitError(const char* text)
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600204{
205 if (strcmp(text, "AutoTestSimple") == 0)
206 {
207 try
208 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700209 elog<example::xyz::openbmc_project::Example::Elog::AutoTestSimple>(
210 example::xyz::openbmc_project::Example::Elog::AutoTestSimple::
211 STRING("FOO"));
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600212 }
Patrick Venturef18bf832018-10-26 18:14:00 -0700213 catch (example::xyz::openbmc_project::Example::Elog::AutoTestSimple& e)
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600214 {
215 std::cout << "elog exception caught: " << e.what() << std::endl;
216 commit(e.name());
217 }
218 }
Marri Devender Rao54932102017-04-04 04:16:35 -0500219 else if (strcmp(text, "AutoTestCreateAndCommit") == 0)
220 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700221 report<example::xyz::openbmc_project::Example::Elog::AutoTestSimple>(
222 example::xyz::openbmc_project::Example::Elog::AutoTestSimple::
223 STRING("FOO"));
Marri Devender Rao54932102017-04-04 04:16:35 -0500224 }
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600225
226 return;
227}
228
Patrick Venturef18bf832018-10-26 18:14:00 -0700229int main(int argc, char* argv[])
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600230{
231 char arg;
232
233 if (argc == 1)
234 return elog_test();
235
Patrick Venturef18bf832018-10-26 18:14:00 -0700236 static struct option long_options[] = {
237 {"help", no_argument, 0, 'h'},
238 {"commit", required_argument, 0, 'c'},
239 {0, 0, 0, 0}};
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600240 int option_index = 0;
241
Patrick Venturef18bf832018-10-26 18:14:00 -0700242 while ((arg = getopt_long(argc, argv, "hc:", long_options,
243 &option_index)) != -1)
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600244 {
245 switch (arg)
246 {
247 case 'c':
248 commitError(optarg);
249 return 0;
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600250 case 'h':
251 case '?':
252 std::cerr << usage;
253 return 1;
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600254 }
255 }
256
257 return 0;
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600258}