Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 1 | // A basic unit test that runs on a BMC (qemu or hardware) |
| 2 | |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 3 | #include <getopt.h> |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 4 | #include <iostream> |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 5 | #include <systemd/sd-journal.h> |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 6 | #include <sstream> |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 7 | #include <sdbusplus/exception.hpp> |
Saqib Khan | 2bb1519 | 2017-02-13 13:19:55 -0600 | [diff] [blame] | 8 | #include <phosphor-logging/elog.hpp> |
| 9 | #include <phosphor-logging/log.hpp> |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 10 | #include <phosphor-logging/elog-errors.hpp> |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 11 | |
| 12 | using namespace phosphor; |
| 13 | using namespace logging; |
| 14 | |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 15 | const char *usage = "Usage: logging-test [OPTION] \n\n\ |
| 16 | Options: \n\ |
| 17 | [NONE] Default test case. \n\ |
| 18 | -h, --help Display this usage text. \n\ |
| 19 | -c, --commit <string> Commit desired error. \n\n\ |
| 20 | Valid errors to commit: \n\ |
Marri Devender Rao | 5493210 | 2017-04-04 04:16:35 -0500 | [diff] [blame] | 21 | AutoTestSimple, AutoTestCreateAndCommit\n"; |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 22 | |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 23 | // validate the journal metadata equals the input value |
| 24 | int 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 Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 59 | std::cerr << "Failed to find " << i_entry << " with value " << i_value |
| 60 | <<" in journal!" << "\n"; |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | return rc; |
| 64 | } |
| 65 | |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 66 | int elog_test() |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 67 | { |
| 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 Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 86 | try |
| 87 | { |
Deepak Kodihalli | 5221e1b | 2017-03-03 00:02:09 -0600 | [diff] [blame] | 88 | elog<example::xyz::openbmc_project::Example::Elog::TestErrorOne>( |
| 89 | example::xyz::openbmc_project::Example::Elog:: |
Adriana Kobylak | 465aaec | 2017-02-20 11:58:03 -0600 | [diff] [blame] | 90 | TestErrorOne::ERRNUM(number), |
Deepak Kodihalli | 5221e1b | 2017-03-03 00:02:09 -0600 | [diff] [blame] | 91 | example::xyz::openbmc_project::Example::Elog:: |
Adriana Kobylak | 465aaec | 2017-02-20 11:58:03 -0600 | [diff] [blame] | 92 | TestErrorOne::FILE_PATH(test_string), |
Deepak Kodihalli | 5221e1b | 2017-03-03 00:02:09 -0600 | [diff] [blame] | 93 | example::xyz::openbmc_project::Example::Elog:: |
Adriana Kobylak | 465aaec | 2017-02-20 11:58:03 -0600 | [diff] [blame] | 94 | TestErrorOne::FILE_NAME("elog_test_3.txt"), |
Deepak Kodihalli | 5221e1b | 2017-03-03 00:02:09 -0600 | [diff] [blame] | 95 | example::xyz::openbmc_project::Example::Elog:: |
Adriana Kobylak | 465aaec | 2017-02-20 11:58:03 -0600 | [diff] [blame] | 96 | TestErrorTwo::DEV_ADDR(0xDEADDEAD), |
Deepak Kodihalli | 5221e1b | 2017-03-03 00:02:09 -0600 | [diff] [blame] | 97 | example::xyz::openbmc_project::Example::Elog:: |
Adriana Kobylak | 465aaec | 2017-02-20 11:58:03 -0600 | [diff] [blame] | 98 | TestErrorTwo::DEV_ID(100), |
Deepak Kodihalli | 5221e1b | 2017-03-03 00:02:09 -0600 | [diff] [blame] | 99 | example::xyz::openbmc_project::Example::Elog:: |
Adriana Kobylak | 465aaec | 2017-02-20 11:58:03 -0600 | [diff] [blame] | 100 | TestErrorTwo::DEV_NAME("test case 3")); |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 101 | } |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 102 | catch (example::xyz::openbmc_project::Example::Elog::TestErrorOne& e) |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 103 | { |
| 104 | std::cout << "elog exception caught: " << e.what() << std::endl; |
| 105 | } |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 106 | |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 107 | // Reduce our error namespaces |
Deepak Kodihalli | 5221e1b | 2017-03-03 00:02:09 -0600 | [diff] [blame] | 108 | using namespace example::xyz::openbmc_project::Example::Elog; |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 109 | |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 110 | // Now read back and verify our data made it into the journal |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 111 | std::stringstream stream; |
| 112 | stream << std::hex << number; |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 113 | rc = validate_journal(TestErrorOne::ERRNUM::str_short, |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 114 | std::string(stream.str()).c_str()); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 115 | if(rc) |
| 116 | return(rc); |
| 117 | |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 118 | rc = validate_journal(TestErrorOne::FILE_PATH::str_short, |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 119 | test_string); |
| 120 | if(rc) |
| 121 | return(rc); |
| 122 | |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 123 | rc = validate_journal(TestErrorOne::FILE_NAME::str_short, |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 124 | "elog_test_3.txt"); |
| 125 | if(rc) |
| 126 | return(rc); |
| 127 | |
Deepak Kodihalli | ac784cc | 2017-01-20 02:59:22 -0600 | [diff] [blame] | 128 | 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 Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 143 | // TEST 4 - Create error log with previous entry use |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 144 | number = 0x9876; |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 145 | try |
| 146 | { |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 147 | elog<TestErrorOne>(TestErrorOne::ERRNUM(number), |
| 148 | prev_entry<TestErrorOne::FILE_PATH>(), |
Deepak Kodihalli | f2462f0 | 2017-01-19 03:40:12 -0600 | [diff] [blame] | 149 | TestErrorOne::FILE_NAME("elog_test_4.txt"), |
Deepak Kodihalli | ac784cc | 2017-01-20 02:59:22 -0600 | [diff] [blame] | 150 | TestErrorTwo::DEV_ADDR(0xDEADDEAD), |
| 151 | TestErrorTwo::DEV_ID(100), |
Deepak Kodihalli | f2462f0 | 2017-01-19 03:40:12 -0600 | [diff] [blame] | 152 | TestErrorTwo::DEV_NAME("test case 4")); |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 153 | } |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 154 | catch (sdbusplus::exception_t& e) |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 155 | { |
| 156 | std::cout << "elog exception caught: " << e.what() << std::endl; |
| 157 | } |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 158 | |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 159 | // Now read back and verify our data made it into the journal |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 160 | stream.str(""); |
| 161 | stream << std::hex << number; |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 162 | rc = validate_journal(TestErrorOne::ERRNUM::str_short, |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 163 | std::string(stream.str()).c_str()); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 164 | if(rc) |
| 165 | return(rc); |
| 166 | |
| 167 | // This should just be equal to what we put in test 3 |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 168 | rc = validate_journal(TestErrorOne::FILE_PATH::str_short, |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 169 | test_string); |
| 170 | if(rc) |
| 171 | return(rc); |
| 172 | |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 173 | rc = validate_journal(TestErrorOne::FILE_NAME::str_short, |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 174 | "elog_test_4.txt"); |
| 175 | if(rc) |
| 176 | return(rc); |
| 177 | |
Deepak Kodihalli | ac784cc | 2017-01-20 02:59:22 -0600 | [diff] [blame] | 178 | 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 Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 193 | // Compile fail tests |
| 194 | |
| 195 | // Simple test to prove we fail to compile due to missing param |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 196 | //elog<TestErrorOne>(TestErrorOne::ERRNUM(1), |
| 197 | // TestErrorOne::FILE_PATH("test")); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 198 | |
| 199 | // Simple test to prove we fail to compile due to invalid param |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 200 | //elog<TestErrorOne>(TestErrorOne::ERRNUM(1), |
| 201 | // TestErrorOne::FILE_PATH("test"), |
| 202 | // TestErrorOne::FILE_NAME(1)); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 203 | |
| 204 | return 0; |
| 205 | } |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 206 | |
| 207 | void commitError(const char *text) |
| 208 | { |
| 209 | if (strcmp(text, "AutoTestSimple") == 0) |
| 210 | { |
| 211 | try |
| 212 | { |
| 213 | elog<example::xyz::openbmc_project::Example::Elog:: |
Deepak Kodihalli | 98a1834 | 2017-03-15 09:19:34 -0500 | [diff] [blame] | 214 | AutoTestSimple>( |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 215 | example::xyz::openbmc_project::Example::Elog:: |
Deepak Kodihalli | 98a1834 | 2017-03-15 09:19:34 -0500 | [diff] [blame] | 216 | AutoTestSimple::STRING("FOO")); |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 217 | } |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 218 | catch (example::xyz::openbmc_project::Example::Elog:: |
| 219 | AutoTestSimple& e) |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 220 | { |
| 221 | std::cout << "elog exception caught: " << e.what() << std::endl; |
| 222 | commit(e.name()); |
| 223 | } |
| 224 | } |
Marri Devender Rao | 5493210 | 2017-04-04 04:16:35 -0500 | [diff] [blame] | 225 | 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 Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 232 | |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | int 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 Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 258 | case 'h': |
| 259 | case '?': |
| 260 | std::cerr << usage; |
| 261 | return 1; |
| 262 | |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | return 0; |
| 267 | |
| 268 | } |