blob: c5f622c9a578379a331af619633cf0f9a2720eba [file] [log] [blame]
Andrew Geissler328889d2016-10-10 12:43:48 -05001// A basic unit test that runs on a BMC (qemu or hardware)
2
3#include <iostream>
Andrew Geissler328889d2016-10-10 12:43:48 -05004#include <systemd/sd-journal.h>
Andrew Geisslerc830e0f2016-10-18 12:51:29 -05005#include <sstream>
6#include "elog.hpp"
7#include "log.hpp"
8#include "elog-gen.hpp"
Andrew Geissler328889d2016-10-10 12:43:48 -05009
10using namespace phosphor;
11using namespace logging;
12
13// validate the journal metadata equals the input value
14int 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 Geisslerc830e0f2016-10-18 12:51:29 -050049 std::cerr << "Failed to find " << i_entry << " with value " << i_value
50 <<" in journal!" << "\n";
Andrew Geissler328889d2016-10-10 12:43:48 -050051 }
52
53 return rc;
54}
55
56int 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 Geissler6d910ad2016-10-16 20:49:14 -050076 try
77 {
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050078 elog<FILE_NOT_FOUND>(FILE_NOT_FOUND::ERRNUM(number),
79 FILE_NOT_FOUND::FILE_PATH(test_string),
80 FILE_NOT_FOUND::FILE_NAME("elog_test_3.txt"));
Andrew Geissler6d910ad2016-10-16 20:49:14 -050081 }
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050082 catch (elogException<FILE_NOT_FOUND>& e)
Andrew Geissler6d910ad2016-10-16 20:49:14 -050083 {
84 std::cout << "elog exception caught: " << e.what() << std::endl;
85 }
Andrew Geissler328889d2016-10-10 12:43:48 -050086
87 // Now read back and verify our data made it into the journal
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050088 std::stringstream stream;
89 stream << std::hex << number;
90 rc = validate_journal(FILE_NOT_FOUND::ERRNUM::str_short,
91 std::string(stream.str()).c_str());
Andrew Geissler328889d2016-10-10 12:43:48 -050092 if(rc)
93 return(rc);
94
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050095 rc = validate_journal(FILE_NOT_FOUND::FILE_PATH::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -050096 test_string);
97 if(rc)
98 return(rc);
99
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500100 rc = validate_journal(FILE_NOT_FOUND::FILE_NAME::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500101 "elog_test_3.txt");
102 if(rc)
103 return(rc);
104
105 // TEST 4 - Create error log with previous entry use
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500106 number = 0x9876;
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500107 try
108 {
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500109 elog<FILE_NOT_FOUND>(FILE_NOT_FOUND::ERRNUM(number),
110 prev_entry<FILE_NOT_FOUND::FILE_PATH>(),
111 FILE_NOT_FOUND::FILE_NAME("elog_test_4.txt"));
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500112 }
113 catch (elogExceptionBase& e)
114 {
115 std::cout << "elog exception caught: " << e.what() << std::endl;
116 }
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500117
Andrew Geissler328889d2016-10-10 12:43:48 -0500118 // Now read back and verify our data made it into the journal
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500119 stream.str("");
120 stream << std::hex << number;
121 rc = validate_journal(FILE_NOT_FOUND::ERRNUM::str_short,
122 std::string(stream.str()).c_str());
Andrew Geissler328889d2016-10-10 12:43:48 -0500123 if(rc)
124 return(rc);
125
126 // This should just be equal to what we put in test 3
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500127 rc = validate_journal(FILE_NOT_FOUND::FILE_PATH::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500128 test_string);
129 if(rc)
130 return(rc);
131
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500132 rc = validate_journal(FILE_NOT_FOUND::FILE_NAME::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500133 "elog_test_4.txt");
134 if(rc)
135 return(rc);
136
137 // Compile fail tests
138
139 // Simple test to prove we fail to compile due to missing param
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500140 //elog<FILE_NOT_FOUND>(FILE_NOT_FOUND::ERRNUM(1),
141 // FILE_NOT_FOUND::FILE_PATH("test"));
Andrew Geissler328889d2016-10-10 12:43:48 -0500142
143 // Simple test to prove we fail to compile due to invalid param
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500144 //elog<FILE_NOT_FOUND>(FILE_NOT_FOUND::ERRNUM(1),
145 // FILE_NOT_FOUND::FILE_PATH("test"),
146 // FILE_NOT_FOUND::FILE_NAME(1));
Andrew Geissler328889d2016-10-10 12:43:48 -0500147
148 return 0;
149}
150
151