blob: ad51823be001fe47ada8e395b4ca2ee24951a81d [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>
Saqib Khan2bb15192017-02-13 13:19:55 -06006#include <phosphor-logging/elog.hpp>
7#include <phosphor-logging/log.hpp>
Andrew Geissler328889d2016-10-10 12:43:48 -05008
9using namespace phosphor;
10using namespace logging;
11
12// validate the journal metadata equals the input value
13int 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 Geisslerc830e0f2016-10-18 12:51:29 -050048 std::cerr << "Failed to find " << i_entry << " with value " << i_value
49 <<" in journal!" << "\n";
Andrew Geissler328889d2016-10-10 12:43:48 -050050 }
51
52 return rc;
53}
54
55int 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 Geissler6d910ad2016-10-16 20:49:14 -050075 try
76 {
Adriana Kobylak465aaec2017-02-20 11:58:03 -060077 elog<example::xyz::openbmc_project::Example::Elog::Error::TestErrorOne>(
78 example::xyz::openbmc_project::Example::Elog::Error::
79 TestErrorOne::ERRNUM(number),
80 example::xyz::openbmc_project::Example::Elog::Error::
81 TestErrorOne::FILE_PATH(test_string),
82 example::xyz::openbmc_project::Example::Elog::Error::
83 TestErrorOne::FILE_NAME("elog_test_3.txt"),
84 example::xyz::openbmc_project::Example::Elog::Error::
85 TestErrorTwo::DEV_ADDR(0xDEADDEAD),
86 example::xyz::openbmc_project::Example::Elog::Error::
87 TestErrorTwo::DEV_ID(100),
88 example::xyz::openbmc_project::Example::Elog::Error::
89 TestErrorTwo::DEV_NAME("test case 3"));
Andrew Geissler6d910ad2016-10-16 20:49:14 -050090 }
Adriana Kobylak465aaec2017-02-20 11:58:03 -060091 catch (elogException<example::xyz::openbmc_project::Example::Elog::Error::
92 TestErrorOne>& e)
Andrew Geissler6d910ad2016-10-16 20:49:14 -050093 {
94 std::cout << "elog exception caught: " << e.what() << std::endl;
95 }
Andrew Geissler328889d2016-10-10 12:43:48 -050096
Andrew Geisslerdf048c12016-11-10 16:50:35 -060097 // Reduce our error namespaces
Adriana Kobylak465aaec2017-02-20 11:58:03 -060098 using namespace example::xyz::openbmc_project::Example::Elog::Error;
Andrew Geisslerdf048c12016-11-10 16:50:35 -060099
Andrew Geissler328889d2016-10-10 12:43:48 -0500100 // Now read back and verify our data made it into the journal
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500101 std::stringstream stream;
102 stream << std::hex << number;
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600103 rc = validate_journal(TestErrorOne::ERRNUM::str_short,
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500104 std::string(stream.str()).c_str());
Andrew Geissler328889d2016-10-10 12:43:48 -0500105 if(rc)
106 return(rc);
107
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600108 rc = validate_journal(TestErrorOne::FILE_PATH::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500109 test_string);
110 if(rc)
111 return(rc);
112
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600113 rc = validate_journal(TestErrorOne::FILE_NAME::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500114 "elog_test_3.txt");
115 if(rc)
116 return(rc);
117
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600118 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 Geissler328889d2016-10-10 12:43:48 -0500133 // TEST 4 - Create error log with previous entry use
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500134 number = 0x9876;
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500135 try
136 {
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600137 elog<TestErrorOne>(TestErrorOne::ERRNUM(number),
138 prev_entry<TestErrorOne::FILE_PATH>(),
Deepak Kodihallif2462f02017-01-19 03:40:12 -0600139 TestErrorOne::FILE_NAME("elog_test_4.txt"),
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600140 TestErrorTwo::DEV_ADDR(0xDEADDEAD),
141 TestErrorTwo::DEV_ID(100),
Deepak Kodihallif2462f02017-01-19 03:40:12 -0600142 TestErrorTwo::DEV_NAME("test case 4"));
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500143 }
144 catch (elogExceptionBase& e)
145 {
146 std::cout << "elog exception caught: " << e.what() << std::endl;
147 }
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500148
Andrew Geissler328889d2016-10-10 12:43:48 -0500149 // Now read back and verify our data made it into the journal
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500150 stream.str("");
151 stream << std::hex << number;
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600152 rc = validate_journal(TestErrorOne::ERRNUM::str_short,
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500153 std::string(stream.str()).c_str());
Andrew Geissler328889d2016-10-10 12:43:48 -0500154 if(rc)
155 return(rc);
156
157 // This should just be equal to what we put in test 3
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600158 rc = validate_journal(TestErrorOne::FILE_PATH::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500159 test_string);
160 if(rc)
161 return(rc);
162
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600163 rc = validate_journal(TestErrorOne::FILE_NAME::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500164 "elog_test_4.txt");
165 if(rc)
166 return(rc);
167
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600168 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 Geissler328889d2016-10-10 12:43:48 -0500183 // Compile fail tests
184
185 // Simple test to prove we fail to compile due to missing param
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600186 //elog<TestErrorOne>(TestErrorOne::ERRNUM(1),
187 // TestErrorOne::FILE_PATH("test"));
Andrew Geissler328889d2016-10-10 12:43:48 -0500188
189 // Simple test to prove we fail to compile due to invalid param
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600190 //elog<TestErrorOne>(TestErrorOne::ERRNUM(1),
191 // TestErrorOne::FILE_PATH("test"),
192 // TestErrorOne::FILE_NAME(1));
Andrew Geissler328889d2016-10-10 12:43:48 -0500193
194 return 0;
195}