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> |
| 6 | #include "elog.hpp" |
| 7 | #include "log.hpp" |
| 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:: |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 84 | FILE_NAME("elog_test_3.txt")); |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 85 | } |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 86 | catch (elogException<example::xyz::openbmc_project::Example::TestErrorOne>& e) |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 87 | { |
| 88 | std::cout << "elog exception caught: " << e.what() << std::endl; |
| 89 | } |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 90 | |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 91 | // Reduce our error namespaces |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 92 | using namespace example::xyz::openbmc_project::Example; |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 93 | |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 94 | // Now read back and verify our data made it into the journal |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 95 | std::stringstream stream; |
| 96 | stream << std::hex << number; |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 97 | rc = validate_journal(TestErrorOne::ERRNUM::str_short, |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 98 | std::string(stream.str()).c_str()); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 99 | if(rc) |
| 100 | return(rc); |
| 101 | |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 102 | rc = validate_journal(TestErrorOne::FILE_PATH::str_short, |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 103 | test_string); |
| 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_NAME::str_short, |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 108 | "elog_test_3.txt"); |
| 109 | if(rc) |
| 110 | return(rc); |
| 111 | |
| 112 | // TEST 4 - Create error log with previous entry use |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 113 | number = 0x9876; |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 114 | try |
| 115 | { |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 116 | elog<TestErrorOne>(TestErrorOne::ERRNUM(number), |
| 117 | prev_entry<TestErrorOne::FILE_PATH>(), |
| 118 | TestErrorOne::FILE_NAME("elog_test_4.txt")); |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 119 | } |
| 120 | catch (elogExceptionBase& e) |
| 121 | { |
| 122 | std::cout << "elog exception caught: " << e.what() << std::endl; |
| 123 | } |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 124 | |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 125 | // Now read back and verify our data made it into the journal |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 126 | stream.str(""); |
| 127 | stream << std::hex << number; |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 128 | rc = validate_journal(TestErrorOne::ERRNUM::str_short, |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 129 | std::string(stream.str()).c_str()); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 130 | if(rc) |
| 131 | return(rc); |
| 132 | |
| 133 | // This should just be equal to what we put in test 3 |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 134 | rc = validate_journal(TestErrorOne::FILE_PATH::str_short, |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 135 | test_string); |
| 136 | if(rc) |
| 137 | return(rc); |
| 138 | |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 139 | rc = validate_journal(TestErrorOne::FILE_NAME::str_short, |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 140 | "elog_test_4.txt"); |
| 141 | if(rc) |
| 142 | return(rc); |
| 143 | |
| 144 | // Compile fail tests |
| 145 | |
| 146 | // Simple test to prove we fail to compile due to missing param |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 147 | //elog<TestErrorOne>(TestErrorOne::ERRNUM(1), |
| 148 | // TestErrorOne::FILE_PATH("test")); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 149 | |
| 150 | // Simple test to prove we fail to compile due to invalid param |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 151 | //elog<TestErrorOne>(TestErrorOne::ERRNUM(1), |
| 152 | // TestErrorOne::FILE_PATH("test"), |
| 153 | // TestErrorOne::FILE_NAME(1)); |
Andrew Geissler | 328889d | 2016-10-10 12:43:48 -0500 | [diff] [blame] | 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | |