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 <systemd/sd-journal.h> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 5 | |
| 6 | #include <iostream> |
| 7 | #include <phosphor-logging/elog-errors.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> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 10 | #include <sdbusplus/exception.hpp> |
| 11 | #include <sstream> |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 12 | |
| 13 | using namespace phosphor; |
| 14 | using namespace logging; |
| 15 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 16 | const char* usage = "Usage: logging-test [OPTION] \n\n\ |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 17 | Options: \n\ |
| 18 | [NONE] Default test case. \n\ |
| 19 | -h, --help Display this usage text. \n\ |
| 20 | -c, --commit <string> Commit desired error. \n\n\ |
| 21 | Valid errors to commit: \n\ |
Marri Devender Rao | 5493210 | 2017-04-04 04:16:35 -0500 | [diff] [blame] | 22 | AutoTestSimple, AutoTestCreateAndCommit\n"; |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 23 | |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 24 | // validate the journal metadata equals the input value |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 25 | int validate_journal(const char* i_entry, const char* i_value) |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 26 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 27 | sd_journal* journal; |
| 28 | const void* data; |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 29 | size_t l; |
| 30 | int rc; |
| 31 | bool validated = false; |
| 32 | |
| 33 | rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY); |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 34 | if (rc < 0) |
| 35 | { |
| 36 | std::cerr << "Failed to open journal: " << strerror(-rc) << "\n"; |
| 37 | return 1; |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 38 | } |
| 39 | rc = sd_journal_query_unique(journal, i_entry); |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 40 | if (rc < 0) |
| 41 | { |
| 42 | std::cerr << "Failed to query journal: " << strerror(-rc) << "\n"; |
| 43 | return 1; |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 44 | } |
| 45 | SD_JOURNAL_FOREACH_UNIQUE(journal, data, l) |
| 46 | { |
| 47 | std::string journ_entry((const char*)data); |
| 48 | std::cout << journ_entry << "\n"; |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 49 | if (journ_entry.find(i_value) != std::string::npos) |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 50 | { |
| 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 Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 60 | if (rc) |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 61 | { |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 62 | std::cerr << "Failed to find " << i_entry << " with value " << i_value |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 63 | << " in journal!" |
| 64 | << "\n"; |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | return rc; |
| 68 | } |
| 69 | |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 70 | int elog_test() |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 71 | { |
| 72 | // TEST 1 - Basic log |
| 73 | log<level::DEBUG>("Basic phosphor logging test"); |
| 74 | |
| 75 | // TEST 2 - Log with metadata field |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 76 | const char* file_name = "phosphor_logging_test.txt"; |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 77 | int number = 0xFEFE; |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 78 | log<level::DEBUG>( |
| 79 | "phosphor logging test with attribute", |
| 80 | entry("FILE_NAME_WITH_NUM_TEST=%s_%x", file_name, number)); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 81 | |
| 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 Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 85 | if (rc) |
| 86 | return (rc); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 87 | |
| 88 | // TEST 3 - Create error log with 2 meta data fields (rvalue and lvalue) |
| 89 | number = 0x1234; |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 90 | const char* test_string = "/tmp/test_string/"; |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 91 | try |
| 92 | { |
Deepak Kodihalli | 5221e1b | 2017-03-03 00:02:09 -0600 | [diff] [blame] | 93 | elog<example::xyz::openbmc_project::Example::Elog::TestErrorOne>( |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 94 | 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 Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 106 | } |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 107 | catch (example::xyz::openbmc_project::Example::Elog::TestErrorOne& e) |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 108 | { |
| 109 | std::cout << "elog exception caught: " << e.what() << std::endl; |
| 110 | } |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 111 | |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 112 | // Reduce our error namespaces |
Deepak Kodihalli | 5221e1b | 2017-03-03 00:02:09 -0600 | [diff] [blame] | 113 | using namespace example::xyz::openbmc_project::Example::Elog; |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 114 | |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 115 | // Now read back and verify our data made it into the journal |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 116 | std::stringstream stream; |
| 117 | stream << std::hex << number; |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 118 | rc = validate_journal(TestErrorOne::ERRNUM::str_short, |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 119 | std::string(stream.str()).c_str()); |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 120 | if (rc) |
| 121 | return (rc); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 122 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 123 | rc = validate_journal(TestErrorOne::FILE_PATH::str_short, test_string); |
| 124 | if (rc) |
| 125 | return (rc); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 126 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 127 | rc = |
| 128 | validate_journal(TestErrorOne::FILE_NAME::str_short, "elog_test_3.txt"); |
| 129 | if (rc) |
| 130 | return (rc); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 131 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 132 | rc = validate_journal(TestErrorTwo::DEV_ADDR::str_short, "0xDEADDEAD"); |
| 133 | if (rc) |
| 134 | return (rc); |
Deepak Kodihalli | ac784cc | 2017-01-20 02:59:22 -0600 | [diff] [blame] | 135 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 136 | rc = validate_journal(TestErrorTwo::DEV_ID::str_short, "100"); |
| 137 | if (rc) |
| 138 | return (rc); |
Deepak Kodihalli | ac784cc | 2017-01-20 02:59:22 -0600 | [diff] [blame] | 139 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 140 | rc = validate_journal(TestErrorTwo::DEV_NAME::str_short, "test case 3"); |
| 141 | if (rc) |
| 142 | return (rc); |
Deepak Kodihalli | ac784cc | 2017-01-20 02:59:22 -0600 | [diff] [blame] | 143 | |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 144 | // TEST 4 - Create error log with previous entry use |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 145 | number = 0x9876; |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 146 | try |
| 147 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 148 | 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 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()); |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 164 | if (rc) |
| 165 | return (rc); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 166 | |
| 167 | // This should just be equal to what we put in test 3 |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 168 | rc = validate_journal(TestErrorOne::FILE_PATH::str_short, test_string); |
| 169 | if (rc) |
| 170 | return (rc); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 171 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 172 | rc = |
| 173 | validate_journal(TestErrorOne::FILE_NAME::str_short, "elog_test_4.txt"); |
| 174 | if (rc) |
| 175 | return (rc); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 176 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 177 | rc = validate_journal(TestErrorTwo::DEV_ADDR::str_short, "0xDEADDEAD"); |
| 178 | if (rc) |
| 179 | return (rc); |
Deepak Kodihalli | ac784cc | 2017-01-20 02:59:22 -0600 | [diff] [blame] | 180 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 181 | rc = validate_journal(TestErrorTwo::DEV_ID::str_short, "100"); |
| 182 | if (rc) |
| 183 | return (rc); |
Deepak Kodihalli | ac784cc | 2017-01-20 02:59:22 -0600 | [diff] [blame] | 184 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 185 | rc = validate_journal(TestErrorTwo::DEV_NAME::str_short, "test case 4"); |
| 186 | if (rc) |
| 187 | return (rc); |
Deepak Kodihalli | ac784cc | 2017-01-20 02:59:22 -0600 | [diff] [blame] | 188 | |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 189 | // Compile fail tests |
| 190 | |
| 191 | // Simple test to prove we fail to compile due to missing param |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 192 | // elog<TestErrorOne>(TestErrorOne::ERRNUM(1), |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 193 | // TestErrorOne::FILE_PATH("test")); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 194 | |
| 195 | // Simple test to prove we fail to compile due to invalid param |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 196 | // elog<TestErrorOne>(TestErrorOne::ERRNUM(1), |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 197 | // TestErrorOne::FILE_PATH("test"), |
| 198 | // TestErrorOne::FILE_NAME(1)); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 199 | |
| 200 | return 0; |
| 201 | } |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 202 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 203 | void commitError(const char* text) |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 204 | { |
| 205 | if (strcmp(text, "AutoTestSimple") == 0) |
| 206 | { |
| 207 | try |
| 208 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 209 | elog<example::xyz::openbmc_project::Example::Elog::AutoTestSimple>( |
| 210 | example::xyz::openbmc_project::Example::Elog::AutoTestSimple:: |
| 211 | STRING("FOO")); |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 212 | } |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 213 | catch (example::xyz::openbmc_project::Example::Elog::AutoTestSimple& e) |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 214 | { |
| 215 | std::cout << "elog exception caught: " << e.what() << std::endl; |
| 216 | commit(e.name()); |
| 217 | } |
| 218 | } |
Marri Devender Rao | 5493210 | 2017-04-04 04:16:35 -0500 | [diff] [blame] | 219 | else if (strcmp(text, "AutoTestCreateAndCommit") == 0) |
| 220 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 221 | report<example::xyz::openbmc_project::Example::Elog::AutoTestSimple>( |
| 222 | example::xyz::openbmc_project::Example::Elog::AutoTestSimple:: |
| 223 | STRING("FOO")); |
Marri Devender Rao | 5493210 | 2017-04-04 04:16:35 -0500 | [diff] [blame] | 224 | } |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 225 | |
| 226 | return; |
| 227 | } |
| 228 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 229 | int main(int argc, char* argv[]) |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 230 | { |
| 231 | char arg; |
| 232 | |
| 233 | if (argc == 1) |
| 234 | return elog_test(); |
| 235 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 236 | static struct option long_options[] = { |
| 237 | {"help", no_argument, 0, 'h'}, |
| 238 | {"commit", required_argument, 0, 'c'}, |
| 239 | {0, 0, 0, 0}}; |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 240 | int option_index = 0; |
| 241 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 242 | while ((arg = getopt_long(argc, argv, "hc:", long_options, |
| 243 | &option_index)) != -1) |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 244 | { |
| 245 | switch (arg) |
| 246 | { |
| 247 | case 'c': |
| 248 | commitError(optarg); |
| 249 | return 0; |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 250 | case 'h': |
| 251 | case '?': |
| 252 | std::cerr << usage; |
| 253 | return 1; |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | return 0; |
Michael Tritz | e0eb1dd | 2017-02-19 20:57:46 -0600 | [diff] [blame] | 258 | } |