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 | |
| 3 | #include <iostream> |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 4 | #include <systemd/sd-journal.h> |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 5 | #include <sstream> |
Saqib Khan | 2bb1519 | 2017-02-13 13:19:55 -0600 | [diff] [blame] | 6 | #include <phosphor-logging/elog.hpp> |
| 7 | #include <phosphor-logging/log.hpp> |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 8 | |
| 9 | using namespace phosphor; |
| 10 | using namespace logging; |
| 11 | |
| 12 | // validate the journal metadata equals the input value |
| 13 | int validate_journal(const char *i_entry, const char *i_value) |
| 14 | { |
| 15 | sd_journal *journal; |
| 16 | const void *data; |
| 17 | size_t l; |
| 18 | int rc; |
| 19 | bool validated = false; |
| 20 | |
| 21 | rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY); |
| 22 | if (rc < 0) { |
| 23 | std::cerr << "Failed to open journal: " << strerror(-rc) << "\n"; |
| 24 | return 1; |
| 25 | } |
| 26 | rc = sd_journal_query_unique(journal, i_entry); |
| 27 | if (rc < 0) { |
| 28 | std::cerr << "Failed to query journal: " << strerror(-rc) << "\n"; |
| 29 | return 1; |
| 30 | } |
| 31 | SD_JOURNAL_FOREACH_UNIQUE(journal, data, l) |
| 32 | { |
| 33 | std::string journ_entry((const char*)data); |
| 34 | std::cout << journ_entry << "\n"; |
| 35 | if(journ_entry.find(i_value) != std::string::npos) |
| 36 | { |
| 37 | std::cout << "We found it!\n"; |
| 38 | validated = true; |
| 39 | break; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | sd_journal_close(journal); |
| 44 | |
| 45 | rc = (validated) ? 0 : 1; |
| 46 | if(rc) |
| 47 | { |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 48 | std::cerr << "Failed to find " << i_entry << " with value " << i_value |
| 49 | <<" in journal!" << "\n"; |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | return rc; |
| 53 | } |
| 54 | |
| 55 | int main() |
| 56 | { |
| 57 | // TEST 1 - Basic log |
| 58 | log<level::DEBUG>("Basic phosphor logging test"); |
| 59 | |
| 60 | // TEST 2 - Log with metadata field |
| 61 | const char *file_name = "phosphor_logging_test.txt"; |
| 62 | int number = 0xFEFE; |
| 63 | log<level::DEBUG>("phosphor logging test with attribute", |
| 64 | entry("FILE_NAME_WITH_NUM_TEST=%s_%x", file_name, number)); |
| 65 | |
| 66 | // Now read back and verify our data made it into the journal |
| 67 | int rc = validate_journal("FILE_NAME_WITH_NUM_TEST", |
| 68 | "phosphor_logging_test.txt_fefe"); |
| 69 | if(rc) |
| 70 | return(rc); |
| 71 | |
| 72 | // TEST 3 - Create error log with 2 meta data fields (rvalue and lvalue) |
| 73 | number = 0x1234; |
| 74 | const char *test_string = "/tmp/test_string/"; |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 75 | try |
| 76 | { |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 77 | elog<example::xyz::openbmc_project::Example::TestErrorOne>( |
| 78 | example::xyz::openbmc_project::Example::TestErrorOne:: |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 79 | ERRNUM(number), |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 80 | example::xyz::openbmc_project::Example::TestErrorOne:: |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 81 | FILE_PATH(test_string), |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 82 | example::xyz::openbmc_project::Example::TestErrorOne:: |
Deepak Kodihalli | f2462f0 | 2017-01-19 03:40:12 -0600 | [diff] [blame] | 83 | FILE_NAME("elog_test_3.txt"), |
| 84 | example::xyz::openbmc_project::Example::TestErrorTwo:: |
Deepak Kodihalli | ac784cc | 2017-01-20 02:59:22 -0600 | [diff] [blame] | 85 | DEV_ADDR(0xDEADDEAD), |
Deepak Kodihalli | f2462f0 | 2017-01-19 03:40:12 -0600 | [diff] [blame] | 86 | example::xyz::openbmc_project::Example::TestErrorTwo:: |
Deepak Kodihalli | ac784cc | 2017-01-20 02:59:22 -0600 | [diff] [blame] | 87 | DEV_ID(100), |
Deepak Kodihalli | f2462f0 | 2017-01-19 03:40:12 -0600 | [diff] [blame] | 88 | example::xyz::openbmc_project::Example::TestErrorTwo:: |
| 89 | DEV_NAME("test case 3")); |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 90 | } |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 91 | catch (elogException<example::xyz::openbmc_project::Example::TestErrorOne>& e) |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 92 | { |
| 93 | std::cout << "elog exception caught: " << e.what() << std::endl; |
| 94 | } |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 95 | |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 96 | // Reduce our error namespaces |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 97 | using namespace example::xyz::openbmc_project::Example; |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 98 | |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 99 | // Now read back and verify our data made it into the journal |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 100 | std::stringstream stream; |
| 101 | stream << std::hex << number; |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 102 | rc = validate_journal(TestErrorOne::ERRNUM::str_short, |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 103 | std::string(stream.str()).c_str()); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 104 | if(rc) |
| 105 | return(rc); |
| 106 | |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 107 | rc = validate_journal(TestErrorOne::FILE_PATH::str_short, |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 108 | test_string); |
| 109 | if(rc) |
| 110 | return(rc); |
| 111 | |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 112 | rc = validate_journal(TestErrorOne::FILE_NAME::str_short, |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 113 | "elog_test_3.txt"); |
| 114 | if(rc) |
| 115 | return(rc); |
| 116 | |
Deepak Kodihalli | ac784cc | 2017-01-20 02:59:22 -0600 | [diff] [blame] | 117 | rc = validate_journal(TestErrorTwo::DEV_ADDR::str_short, |
| 118 | "0xDEADDEAD"); |
| 119 | if(rc) |
| 120 | return(rc); |
| 121 | |
| 122 | rc = validate_journal(TestErrorTwo::DEV_ID::str_short, |
| 123 | "100"); |
| 124 | if(rc) |
| 125 | return(rc); |
| 126 | |
| 127 | rc = validate_journal(TestErrorTwo::DEV_NAME::str_short, |
| 128 | "test case 3"); |
| 129 | if(rc) |
| 130 | return(rc); |
| 131 | |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 132 | // TEST 4 - Create error log with previous entry use |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 133 | number = 0x9876; |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 134 | try |
| 135 | { |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 136 | elog<TestErrorOne>(TestErrorOne::ERRNUM(number), |
| 137 | prev_entry<TestErrorOne::FILE_PATH>(), |
Deepak Kodihalli | f2462f0 | 2017-01-19 03:40:12 -0600 | [diff] [blame] | 138 | TestErrorOne::FILE_NAME("elog_test_4.txt"), |
Deepak Kodihalli | ac784cc | 2017-01-20 02:59:22 -0600 | [diff] [blame] | 139 | TestErrorTwo::DEV_ADDR(0xDEADDEAD), |
| 140 | TestErrorTwo::DEV_ID(100), |
Deepak Kodihalli | f2462f0 | 2017-01-19 03:40:12 -0600 | [diff] [blame] | 141 | TestErrorTwo::DEV_NAME("test case 4")); |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 142 | } |
| 143 | catch (elogExceptionBase& e) |
| 144 | { |
| 145 | std::cout << "elog exception caught: " << e.what() << std::endl; |
| 146 | } |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 147 | |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 148 | // Now read back and verify our data made it into the journal |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 149 | stream.str(""); |
| 150 | stream << std::hex << number; |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 151 | rc = validate_journal(TestErrorOne::ERRNUM::str_short, |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 152 | std::string(stream.str()).c_str()); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 153 | if(rc) |
| 154 | return(rc); |
| 155 | |
| 156 | // This should just be equal to what we put in test 3 |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 157 | rc = validate_journal(TestErrorOne::FILE_PATH::str_short, |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 158 | test_string); |
| 159 | if(rc) |
| 160 | return(rc); |
| 161 | |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 162 | rc = validate_journal(TestErrorOne::FILE_NAME::str_short, |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 163 | "elog_test_4.txt"); |
| 164 | if(rc) |
| 165 | return(rc); |
| 166 | |
Deepak Kodihalli | ac784cc | 2017-01-20 02:59:22 -0600 | [diff] [blame] | 167 | rc = validate_journal(TestErrorTwo::DEV_ADDR::str_short, |
| 168 | "0xDEADDEAD"); |
| 169 | if(rc) |
| 170 | return(rc); |
| 171 | |
| 172 | rc = validate_journal(TestErrorTwo::DEV_ID::str_short, |
| 173 | "100"); |
| 174 | if(rc) |
| 175 | return(rc); |
| 176 | |
| 177 | rc = validate_journal(TestErrorTwo::DEV_NAME::str_short, |
| 178 | "test case 4"); |
| 179 | if(rc) |
| 180 | return(rc); |
| 181 | |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 182 | // Compile fail tests |
| 183 | |
| 184 | // Simple test to prove we fail to compile due to missing param |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 185 | //elog<TestErrorOne>(TestErrorOne::ERRNUM(1), |
| 186 | // TestErrorOne::FILE_PATH("test")); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 187 | |
| 188 | // Simple test to prove we fail to compile due to invalid param |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 189 | //elog<TestErrorOne>(TestErrorOne::ERRNUM(1), |
| 190 | // TestErrorOne::FILE_PATH("test"), |
| 191 | // TestErrorOne::FILE_NAME(1)); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | |