diff options
author | Andrew Geissler <andrewg@us.ibm.com> | 2016-10-10 12:43:48 -0500 |
---|---|---|
committer | Andrew Geissler <andrewg@us.ibm.com> | 2016-10-31 13:20:30 -0500 |
commit | 328889d5a6d0926f4876f3692713a759185812e1 (patch) | |
tree | 57b0a6b443e5c2809fbac3094494e6573a85919b /logging_test.cpp | |
parent | f855c3e54f2c06da0649a7dfc31f60d7a36bcd6f (diff) | |
download | phosphor-logging-328889d5a6d0926f4876f3692713a759185812e1.tar.gz phosphor-logging-328889d5a6d0926f4876f3692713a759185812e1.zip |
Add in test case which validates elog client interface
Change-Id: I5c5f7a550a8f272251893ff616408c41d32281f9
Signed-off-by: Andrew Geissler <andrewg@us.ibm.com>
Diffstat (limited to 'logging_test.cpp')
-rw-r--r-- | logging_test.cpp | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/logging_test.cpp b/logging_test.cpp new file mode 100644 index 0000000..5f153c1 --- /dev/null +++ b/logging_test.cpp @@ -0,0 +1,130 @@ +// A basic unit test that runs on a BMC (qemu or hardware) + +#include <iostream> +#include <elog.hpp> +#include <log.hpp> +#include <systemd/sd-journal.h> + +using namespace phosphor; +using namespace logging; + +// validate the journal metadata equals the input value +int validate_journal(const char *i_entry, const char *i_value) +{ + sd_journal *journal; + const void *data; + size_t l; + int rc; + bool validated = false; + + rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY); + if (rc < 0) { + std::cerr << "Failed to open journal: " << strerror(-rc) << "\n"; + return 1; + } + rc = sd_journal_query_unique(journal, i_entry); + if (rc < 0) { + std::cerr << "Failed to query journal: " << strerror(-rc) << "\n"; + return 1; + } + SD_JOURNAL_FOREACH_UNIQUE(journal, data, l) + { + std::string journ_entry((const char*)data); + std::cout << journ_entry << "\n"; + if(journ_entry.find(i_value) != std::string::npos) + { + std::cout << "We found it!\n"; + validated = true; + break; + } + } + + sd_journal_close(journal); + + rc = (validated) ? 0 : 1; + if(rc) + { + std::cerr << "Failed to find " << i_entry << " in journal!" << "\n"; + } + + return rc; +} + +int main() +{ + // TEST 1 - Basic log + log<level::DEBUG>("Basic phosphor logging test"); + + // TEST 2 - Log with metadata field + const char *file_name = "phosphor_logging_test.txt"; + int number = 0xFEFE; + log<level::DEBUG>("phosphor logging test with attribute", + entry("FILE_NAME_WITH_NUM_TEST=%s_%x", file_name, number)); + + // Now read back and verify our data made it into the journal + int rc = validate_journal("FILE_NAME_WITH_NUM_TEST", + "phosphor_logging_test.txt_fefe"); + if(rc) + return(rc); + + // TEST 3 - Create error log with 2 meta data fields (rvalue and lvalue) + number = 0x1234; + const char *test_string = "/tmp/test_string/"; + elog<file_not_found>(file_not_found::errnum(number), + file_not_found::file_path(test_string), + file_not_found::file_name("elog_test_3.txt")); + + // Now read back and verify our data made it into the journal + rc = validate_journal(file_not_found::errnum::str_short, + std::to_string(number).c_str()); + if(rc) + return(rc); + + rc = validate_journal(file_not_found::file_path::str_short, + test_string); + if(rc) + return(rc); + + rc = validate_journal(file_not_found::file_name::str_short, + "elog_test_3.txt"); + if(rc) + return(rc); + + // TEST 4 - Create error log with previous entry use + number = 0xFEDC; + elog<file_not_found>(file_not_found::errnum(number), + prev_entry<file_not_found::file_path>(), + file_not_found::file_name("elog_test_4.txt")); + + // Now read back and verify our data made it into the journal + rc = validate_journal(file_not_found::errnum::str_short, + std::to_string(number).c_str()); + if(rc) + return(rc); + + // This should just be equal to what we put in test 3 + rc = validate_journal(file_not_found::file_path::str_short, + test_string); + if(rc) + return(rc); + + rc = validate_journal(file_not_found::file_name::str_short, + "elog_test_4.txt"); + if(rc) + return(rc); + + // Compile fail tests + + // Simple test to prove we fail to compile due to missing param + //elog<file_not_found>(file_not_found::errnum(1), + // file_not_found::file_path("test")); + + // Simple test to prove we fail to compile due to invalid param + //elog<file_not_found>(file_not_found::errnum(1), + // file_not_found::file_path("test"), + // file_not_found::file_name(1)); + + return 0; +} + + |