blob: b781482f39584ce112f0addd1de7df62459e1e44 [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 {
Deepak Kodihalli160d3e02017-01-17 04:25:22 -060078 elog<example::xyz::openbmc_project::Example::TestErrorOne>(
79 example::xyz::openbmc_project::Example::TestErrorOne::
Andrew Geisslerdf048c12016-11-10 16:50:35 -060080 ERRNUM(number),
Deepak Kodihalli160d3e02017-01-17 04:25:22 -060081 example::xyz::openbmc_project::Example::TestErrorOne::
Andrew Geisslerdf048c12016-11-10 16:50:35 -060082 FILE_PATH(test_string),
Deepak Kodihalli160d3e02017-01-17 04:25:22 -060083 example::xyz::openbmc_project::Example::TestErrorOne::
Andrew Geisslerdf048c12016-11-10 16:50:35 -060084 FILE_NAME("elog_test_3.txt"));
Andrew Geissler6d910ad2016-10-16 20:49:14 -050085 }
Deepak Kodihalli160d3e02017-01-17 04:25:22 -060086 catch (elogException<example::xyz::openbmc_project::Example::TestErrorOne>& e)
Andrew Geissler6d910ad2016-10-16 20:49:14 -050087 {
88 std::cout << "elog exception caught: " << e.what() << std::endl;
89 }
Andrew Geissler328889d2016-10-10 12:43:48 -050090
Andrew Geisslerdf048c12016-11-10 16:50:35 -060091 // Reduce our error namespaces
Deepak Kodihalli160d3e02017-01-17 04:25:22 -060092 using namespace example::xyz::openbmc_project::Example;
Andrew Geisslerdf048c12016-11-10 16:50:35 -060093
Andrew Geissler328889d2016-10-10 12:43:48 -050094 // Now read back and verify our data made it into the journal
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050095 std::stringstream stream;
96 stream << std::hex << number;
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -060097 rc = validate_journal(TestErrorOne::ERRNUM::str_short,
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050098 std::string(stream.str()).c_str());
Andrew Geissler328889d2016-10-10 12:43:48 -050099 if(rc)
100 return(rc);
101
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600102 rc = validate_journal(TestErrorOne::FILE_PATH::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500103 test_string);
104 if(rc)
105 return(rc);
106
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600107 rc = validate_journal(TestErrorOne::FILE_NAME::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500108 "elog_test_3.txt");
109 if(rc)
110 return(rc);
111
112 // TEST 4 - Create error log with previous entry use
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500113 number = 0x9876;
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500114 try
115 {
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600116 elog<TestErrorOne>(TestErrorOne::ERRNUM(number),
117 prev_entry<TestErrorOne::FILE_PATH>(),
118 TestErrorOne::FILE_NAME("elog_test_4.txt"));
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500119 }
120 catch (elogExceptionBase& e)
121 {
122 std::cout << "elog exception caught: " << e.what() << std::endl;
123 }
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500124
Andrew Geissler328889d2016-10-10 12:43:48 -0500125 // Now read back and verify our data made it into the journal
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500126 stream.str("");
127 stream << std::hex << number;
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600128 rc = validate_journal(TestErrorOne::ERRNUM::str_short,
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500129 std::string(stream.str()).c_str());
Andrew Geissler328889d2016-10-10 12:43:48 -0500130 if(rc)
131 return(rc);
132
133 // This should just be equal to what we put in test 3
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600134 rc = validate_journal(TestErrorOne::FILE_PATH::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500135 test_string);
136 if(rc)
137 return(rc);
138
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600139 rc = validate_journal(TestErrorOne::FILE_NAME::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500140 "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 Geisslerf1f2cfa2016-11-21 15:16:45 -0600147 //elog<TestErrorOne>(TestErrorOne::ERRNUM(1),
148 // TestErrorOne::FILE_PATH("test"));
Andrew Geissler328889d2016-10-10 12:43:48 -0500149
150 // Simple test to prove we fail to compile due to invalid param
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600151 //elog<TestErrorOne>(TestErrorOne::ERRNUM(1),
152 // TestErrorOne::FILE_PATH("test"),
153 // TestErrorOne::FILE_NAME(1));
Andrew Geissler328889d2016-10-10 12:43:48 -0500154
155 return 0;
156}
157
158