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