blob: 0ad10b75a7de2216e86e3da6ede2113d50ba1ced [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 <iostream>
Andrew Geissler328889d2016-10-10 12:43:48 -05005#include <systemd/sd-journal.h>
Andrew Geisslerc830e0f2016-10-18 12:51:29 -05006#include <sstream>
Deepak Kodihalli15331102017-03-09 23:50:43 -06007#include <sdbusplus/exception.hpp>
Saqib Khan2bb15192017-02-13 13:19:55 -06008#include <phosphor-logging/elog.hpp>
9#include <phosphor-logging/log.hpp>
Deepak Kodihalli15331102017-03-09 23:50:43 -060010#include <phosphor-logging/elog-errors.hpp>
Andrew Geissler328889d2016-10-10 12:43:48 -050011
12using namespace phosphor;
13using namespace logging;
14
Michael Tritze0eb1dd2017-02-19 20:57:46 -060015const char *usage = "Usage: logging-test [OPTION] \n\n\
16Options: \n\
17[NONE] Default test case. \n\
18-h, --help Display this usage text. \n\
19-c, --commit <string> Commit desired error. \n\n\
20Valid errors to commit: \n\
Marri Devender Rao54932102017-04-04 04:16:35 -050021AutoTestSimple, AutoTestCreateAndCommit\n";
Michael Tritze0eb1dd2017-02-19 20:57:46 -060022
Andrew Geissler328889d2016-10-10 12:43:48 -050023// validate the journal metadata equals the input value
24int validate_journal(const char *i_entry, const char *i_value)
25{
26 sd_journal *journal;
27 const void *data;
28 size_t l;
29 int rc;
30 bool validated = false;
31
32 rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY);
33 if (rc < 0) {
34 std::cerr << "Failed to open journal: " << strerror(-rc) << "\n";
35 return 1;
36 }
37 rc = sd_journal_query_unique(journal, i_entry);
38 if (rc < 0) {
39 std::cerr << "Failed to query journal: " << strerror(-rc) << "\n";
40 return 1;
41 }
42 SD_JOURNAL_FOREACH_UNIQUE(journal, data, l)
43 {
44 std::string journ_entry((const char*)data);
45 std::cout << journ_entry << "\n";
46 if(journ_entry.find(i_value) != std::string::npos)
47 {
48 std::cout << "We found it!\n";
49 validated = true;
50 break;
51 }
52 }
53
54 sd_journal_close(journal);
55
56 rc = (validated) ? 0 : 1;
57 if(rc)
58 {
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050059 std::cerr << "Failed to find " << i_entry << " with value " << i_value
60 <<" in journal!" << "\n";
Andrew Geissler328889d2016-10-10 12:43:48 -050061 }
62
63 return rc;
64}
65
Michael Tritze0eb1dd2017-02-19 20:57:46 -060066int elog_test()
Andrew Geissler328889d2016-10-10 12:43:48 -050067{
68 // TEST 1 - Basic log
69 log<level::DEBUG>("Basic phosphor logging test");
70
71 // TEST 2 - Log with metadata field
72 const char *file_name = "phosphor_logging_test.txt";
73 int number = 0xFEFE;
74 log<level::DEBUG>("phosphor logging test with attribute",
75 entry("FILE_NAME_WITH_NUM_TEST=%s_%x", file_name, number));
76
77 // Now read back and verify our data made it into the journal
78 int rc = validate_journal("FILE_NAME_WITH_NUM_TEST",
79 "phosphor_logging_test.txt_fefe");
80 if(rc)
81 return(rc);
82
83 // TEST 3 - Create error log with 2 meta data fields (rvalue and lvalue)
84 number = 0x1234;
85 const char *test_string = "/tmp/test_string/";
Andrew Geissler6d910ad2016-10-16 20:49:14 -050086 try
87 {
Deepak Kodihalli5221e1b2017-03-03 00:02:09 -060088 elog<example::xyz::openbmc_project::Example::Elog::TestErrorOne>(
89 example::xyz::openbmc_project::Example::Elog::
Adriana Kobylak465aaec2017-02-20 11:58:03 -060090 TestErrorOne::ERRNUM(number),
Deepak Kodihalli5221e1b2017-03-03 00:02:09 -060091 example::xyz::openbmc_project::Example::Elog::
Adriana Kobylak465aaec2017-02-20 11:58:03 -060092 TestErrorOne::FILE_PATH(test_string),
Deepak Kodihalli5221e1b2017-03-03 00:02:09 -060093 example::xyz::openbmc_project::Example::Elog::
Adriana Kobylak465aaec2017-02-20 11:58:03 -060094 TestErrorOne::FILE_NAME("elog_test_3.txt"),
Deepak Kodihalli5221e1b2017-03-03 00:02:09 -060095 example::xyz::openbmc_project::Example::Elog::
Adriana Kobylak465aaec2017-02-20 11:58:03 -060096 TestErrorTwo::DEV_ADDR(0xDEADDEAD),
Deepak Kodihalli5221e1b2017-03-03 00:02:09 -060097 example::xyz::openbmc_project::Example::Elog::
Adriana Kobylak465aaec2017-02-20 11:58:03 -060098 TestErrorTwo::DEV_ID(100),
Deepak Kodihalli5221e1b2017-03-03 00:02:09 -060099 example::xyz::openbmc_project::Example::Elog::
Adriana Kobylak465aaec2017-02-20 11:58:03 -0600100 TestErrorTwo::DEV_NAME("test case 3"));
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500101 }
Deepak Kodihalli15331102017-03-09 23:50:43 -0600102 catch (example::xyz::openbmc_project::Example::Elog::TestErrorOne& e)
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500103 {
104 std::cout << "elog exception caught: " << e.what() << std::endl;
105 }
Andrew Geissler328889d2016-10-10 12:43:48 -0500106
Andrew Geisslerdf048c12016-11-10 16:50:35 -0600107 // Reduce our error namespaces
Deepak Kodihalli5221e1b2017-03-03 00:02:09 -0600108 using namespace example::xyz::openbmc_project::Example::Elog;
Andrew Geisslerdf048c12016-11-10 16:50:35 -0600109
Andrew Geissler328889d2016-10-10 12:43:48 -0500110 // Now read back and verify our data made it into the journal
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500111 std::stringstream stream;
112 stream << std::hex << number;
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600113 rc = validate_journal(TestErrorOne::ERRNUM::str_short,
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500114 std::string(stream.str()).c_str());
Andrew Geissler328889d2016-10-10 12:43:48 -0500115 if(rc)
116 return(rc);
117
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600118 rc = validate_journal(TestErrorOne::FILE_PATH::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500119 test_string);
120 if(rc)
121 return(rc);
122
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600123 rc = validate_journal(TestErrorOne::FILE_NAME::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500124 "elog_test_3.txt");
125 if(rc)
126 return(rc);
127
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600128 rc = validate_journal(TestErrorTwo::DEV_ADDR::str_short,
129 "0xDEADDEAD");
130 if(rc)
131 return(rc);
132
133 rc = validate_journal(TestErrorTwo::DEV_ID::str_short,
134 "100");
135 if(rc)
136 return(rc);
137
138 rc = validate_journal(TestErrorTwo::DEV_NAME::str_short,
139 "test case 3");
140 if(rc)
141 return(rc);
142
Andrew Geissler328889d2016-10-10 12:43:48 -0500143 // TEST 4 - Create error log with previous entry use
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500144 number = 0x9876;
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500145 try
146 {
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600147 elog<TestErrorOne>(TestErrorOne::ERRNUM(number),
148 prev_entry<TestErrorOne::FILE_PATH>(),
Deepak Kodihallif2462f02017-01-19 03:40:12 -0600149 TestErrorOne::FILE_NAME("elog_test_4.txt"),
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600150 TestErrorTwo::DEV_ADDR(0xDEADDEAD),
151 TestErrorTwo::DEV_ID(100),
Deepak Kodihallif2462f02017-01-19 03:40:12 -0600152 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());
Andrew Geissler328889d2016-10-10 12:43:48 -0500164 if(rc)
165 return(rc);
166
167 // This should just be equal to what we put in test 3
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600168 rc = validate_journal(TestErrorOne::FILE_PATH::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500169 test_string);
170 if(rc)
171 return(rc);
172
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600173 rc = validate_journal(TestErrorOne::FILE_NAME::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500174 "elog_test_4.txt");
175 if(rc)
176 return(rc);
177
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600178 rc = validate_journal(TestErrorTwo::DEV_ADDR::str_short,
179 "0xDEADDEAD");
180 if(rc)
181 return(rc);
182
183 rc = validate_journal(TestErrorTwo::DEV_ID::str_short,
184 "100");
185 if(rc)
186 return(rc);
187
188 rc = validate_journal(TestErrorTwo::DEV_NAME::str_short,
189 "test case 4");
190 if(rc)
191 return(rc);
192
Andrew Geissler328889d2016-10-10 12:43:48 -0500193 // Compile fail tests
194
195 // Simple test to prove we fail to compile due to missing param
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600196 //elog<TestErrorOne>(TestErrorOne::ERRNUM(1),
197 // TestErrorOne::FILE_PATH("test"));
Andrew Geissler328889d2016-10-10 12:43:48 -0500198
199 // Simple test to prove we fail to compile due to invalid param
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600200 //elog<TestErrorOne>(TestErrorOne::ERRNUM(1),
201 // TestErrorOne::FILE_PATH("test"),
202 // TestErrorOne::FILE_NAME(1));
Andrew Geissler328889d2016-10-10 12:43:48 -0500203
204 return 0;
205}
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600206
207void commitError(const char *text)
208{
209 if (strcmp(text, "AutoTestSimple") == 0)
210 {
211 try
212 {
213 elog<example::xyz::openbmc_project::Example::Elog::
Deepak Kodihalli98a18342017-03-15 09:19:34 -0500214 AutoTestSimple>(
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600215 example::xyz::openbmc_project::Example::Elog::
Deepak Kodihalli98a18342017-03-15 09:19:34 -0500216 AutoTestSimple::STRING("FOO"));
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600217 }
Deepak Kodihalli15331102017-03-09 23:50:43 -0600218 catch (example::xyz::openbmc_project::Example::Elog::
219 AutoTestSimple& e)
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600220 {
221 std::cout << "elog exception caught: " << e.what() << std::endl;
222 commit(e.name());
223 }
224 }
Marri Devender Rao54932102017-04-04 04:16:35 -0500225 else if (strcmp(text, "AutoTestCreateAndCommit") == 0)
226 {
227 report<example::xyz::openbmc_project::Example::Elog::
228 AutoTestSimple>(
229 example::xyz::openbmc_project::Example::Elog::
230 AutoTestSimple::STRING("FOO"));
231 }
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600232
233 return;
234}
235
236int main(int argc, char *argv[])
237{
238 char arg;
239
240 if (argc == 1)
241 return elog_test();
242
243 static struct option long_options[] =
244 {
245 {"help", no_argument, 0, 'h'},
246 {"commit", required_argument, 0, 'c'},
247 {0, 0, 0, 0}
248 };
249 int option_index = 0;
250
251 while((arg=getopt_long(argc,argv,"hc:",long_options,&option_index)) != -1)
252 {
253 switch (arg)
254 {
255 case 'c':
256 commitError(optarg);
257 return 0;
Michael Tritze0eb1dd2017-02-19 20:57:46 -0600258 case 'h':
259 case '?':
260 std::cerr << usage;
261 return 1;
262
263 }
264 }
265
266 return 0;
267
268}