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