@@ -19,17 +19,30 @@ using namespace std;
1919using namespace swss ;
2020using json = nlohmann::json;
2121
22+ vector<EventParam> createEventParams (vector<string> params, vector<string> luaCodes) {
23+ vector<EventParam> eventParams;
24+ for (long unsigned int i = 0 ; i < params.size (); i++) {
25+ EventParam ep = EventParam ();
26+ ep.paramName = params[i];
27+ ep.luaCode = luaCodes[i];
28+ eventParams.push_back (ep);
29+ }
30+ return eventParams;
31+ }
32+
2233TEST (syslog_parser, matching_regex) {
2334 json jList = json::array ();
24- vector<regex> testExpressions ;
35+ vector<RegexStruct> regexList ;
2536 string regexString = " ^([a-zA-Z]{3})?\\ s*([0-9]{1,2})?\\ s*([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{0,6})?\\ s*message (.*) other_data (.*) even_more_data (.*)" ;
26- json jTest;
27- jTest[" tag" ] = " test_tag" ;
28- jTest[" regex" ] = regexString;
29- jTest[" params" ] = { " month" , " day" , " time" , " message" , " other_data" , " even_more_data" };
30- jList.push_back (jTest);
37+ vector<string> params = { " month" , " day" , " time" , " message" , " other_data" , " even_more_data" };
38+ vector<string> luaCodes = { " " , " " , " " , " " , " " , " " };
3139 regex expression (regexString);
32- testExpressions.push_back (expression);
40+
41+ RegexStruct rs = RegexStruct ();
42+ rs.tag = " test_tag" ;
43+ rs.regexExpression = expression;
44+ rs.params = createEventParams (params, luaCodes);
45+ regexList.push_back (rs);
3346
3447 string tag;
3548 event_params_t paramDict;
@@ -40,8 +53,7 @@ TEST(syslog_parser, matching_regex) {
4053 expectedDict[" even_more_data" ] = " test_data" ;
4154
4255 unique_ptr<SyslogParser> parser (new SyslogParser ());
43- parser->m_expressions = testExpressions;
44- parser->m_regexList = jList;
56+ parser->m_regexList = regexList;
4557 lua_State* luaState = luaL_newstate ();
4658 luaL_openlibs (luaState);
4759
@@ -55,15 +67,17 @@ TEST(syslog_parser, matching_regex) {
5567
5668TEST (syslog_parser, matching_regex_timestamp) {
5769 json jList = json::array ();
58- vector<regex> testExpressions ;
70+ vector<RegexStruct> regexList ;
5971 string regexString = " ^([a-zA-Z]{3})?\\ s*([0-9]{1,2})?\\ s*([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{0,6})?\\ s*message (.*) other_data (.*)" ;
60- json jTest;
61- jTest[" tag" ] = " test_tag" ;
62- jTest[" regex" ] = regexString;
63- jTest[" params" ] = { " month" , " day" , " time" , " message" , " other_data" };
64- jList.push_back (jTest);
72+ vector<string> params = { " month" , " day" , " time" , " message" , " other_data" };
73+ vector<string> luaCodes = { " " , " " , " " , " " , " " };
6574 regex expression (regexString);
66- testExpressions.push_back (expression);
75+
76+ RegexStruct rs = RegexStruct ();
77+ rs.tag = " test_tag" ;
78+ rs.regexExpression = expression;
79+ rs.params = createEventParams (params, luaCodes);
80+ regexList.push_back (rs);
6781
6882 string tag;
6983 event_params_t paramDict;
@@ -74,8 +88,7 @@ TEST(syslog_parser, matching_regex_timestamp) {
7488 expectedDict[" timestamp" ] = " 2022-07-21T02:10:00.000000Z" ;
7589
7690 unique_ptr<SyslogParser> parser (new SyslogParser ());
77- parser->m_expressions = testExpressions;
78- parser->m_regexList = jList;
91+ parser->m_regexList = regexList;
7992 lua_State* luaState = luaL_newstate ();
8093 luaL_openlibs (luaState);
8194
@@ -89,22 +102,23 @@ TEST(syslog_parser, matching_regex_timestamp) {
89102
90103TEST (syslog_parser, no_matching_regex) {
91104 json jList = json::array ();
92- vector<regex> testExpressions;
93- string regexString = " ^([a-zA-Z]{3})?\\ s*([0-9]{1,2})?\\ s*([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{0,6})?\s*no match" ;
94- json jTest;
95- jTest[" tag" ] = " test_tag" ;
96- jTest[" regex" ] = regexString;
97- jTest[" params" ] = { " month" , " day" , " time" };
98- jList.push_back (jTest);
105+ vector<RegexStruct> regexList;
106+ string regexString = " ^([a-zA-Z]{3})?\\ s*([0-9]{1,2})?\\ s*([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{0,6})?\\ s*no match" ;
107+ vector<string> params = { " month" , " day" , " time" };
108+ vector<string> luaCodes = { " " , " " , " " };
99109 regex expression (regexString);
100- testExpressions.push_back (expression);
110+
111+ RegexStruct rs = RegexStruct ();
112+ rs.tag = " test_tag" ;
113+ rs.regexExpression = expression;
114+ rs.params = createEventParams (params, luaCodes);
115+ regexList.push_back (rs);
101116
102117 string tag;
103118 event_params_t paramDict;
104119
105120 unique_ptr<SyslogParser> parser (new SyslogParser ());
106- parser->m_expressions = testExpressions;
107- parser->m_regexList = jList;
121+ parser->m_regexList = regexList;
108122 lua_State* luaState = luaL_newstate ();
109123 luaL_openlibs (luaState);
110124
@@ -116,15 +130,17 @@ TEST(syslog_parser, no_matching_regex) {
116130
117131TEST (syslog_parser, lua_code_valid_1) {
118132 json jList = json::array ();
119- vector<regex> testExpressions ;
133+ vector<RegexStruct> regexList ;
120134 string regexString = " ^([a-zA-Z]{3})?\\ s*([0-9]{1,2})?\\ s*([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{0,6})?\\ s*.* (sent|received) (?:to|from) .* ([0-9]{2,3}.[0-9]{2,3}.[0-9]{2,3}.[0-9]{2,3}) active ([1-9]{1,3})/([1-9]{1,3}) .*" ;
121- json jTest;
122- jTest[" tag" ] = " test_tag" ;
123- jTest[" regex" ] = regexString;
124- jTest[" params" ] = { " month" , " day" , " time" , " is-sent:ret=tostring(arg==\" sent\" )" , " ip" , " major-code" , " minor-code" };
125- jList.push_back (jTest);
135+ vector<string> params = { " month" , " day" , " time" , " is-sent" , " ip" , " major-code" , " minor-code" };
136+ vector<string> luaCodes = { " " , " " , " " , " ret=tostring(arg==\" sent\" )" , " " , " " , " " };
126137 regex expression (regexString);
127- testExpressions.push_back (expression);
138+
139+ RegexStruct rs = RegexStruct ();
140+ rs.tag = " test_tag" ;
141+ rs.regexExpression = expression;
142+ rs.params = createEventParams (params, luaCodes);
143+ regexList.push_back (rs);
128144
129145 string tag;
130146 event_params_t paramDict;
@@ -136,8 +152,7 @@ TEST(syslog_parser, lua_code_valid_1) {
136152 expectedDict[" minor-code" ] = " 2" ;
137153
138154 unique_ptr<SyslogParser> parser (new SyslogParser ());
139- parser->m_expressions = testExpressions;
140- parser->m_regexList = jList;
155+ parser->m_regexList = regexList;
141156 lua_State* luaState = luaL_newstate ();
142157 luaL_openlibs (luaState);
143158
@@ -151,15 +166,17 @@ TEST(syslog_parser, lua_code_valid_1) {
151166
152167TEST (syslog_parser, lua_code_valid_2) {
153168 json jList = json::array ();
154- vector<regex> testExpressions ;
169+ vector<RegexStruct> regexList ;
155170 string regexString = " ([a-zA-Z]{3})?\\ s*([0-9]{1,2})?\\ s*([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{0,6})?\\ s*.* (sent|received) (?:to|from) .* ([0-9]{2,3}.[0-9]{2,3}.[0-9]{2,3}.[0-9]{2,3}) active ([1-9]{1,3})/([1-9]{1,3}) .*" ;
156- json jTest;
157- jTest[" tag" ] = " test_tag" ;
158- jTest[" regex" ] = regexString;
159- jTest[" params" ] = { " month" , " day" , " time" , " is-sent:ret=tostring(arg==\" sent\" )" , " ip" , " major-code" , " minor-code" };
160- jList.push_back (jTest);
171+ vector<string> params = { " month" , " day" , " time" , " is-sent" , " ip" , " major-code" , " minor-code" };
172+ vector<string> luaCodes = { " " , " " , " " , " ret=tostring(arg==\" sent\" )" , " " , " " , " " };
161173 regex expression (regexString);
162- testExpressions.push_back (expression);
174+
175+ RegexStruct rs = RegexStruct ();
176+ rs.tag = " test_tag" ;
177+ rs.regexExpression = expression;
178+ rs.params = createEventParams (params, luaCodes);
179+ regexList.push_back (rs);
163180
164181 string tag;
165182 event_params_t paramDict;
@@ -172,8 +189,7 @@ TEST(syslog_parser, lua_code_valid_2) {
172189 expectedDict[" timestamp" ] = " 2022-12-03T12:36:24.503424Z" ;
173190
174191 unique_ptr<SyslogParser> parser (new SyslogParser ());
175- parser->m_expressions = testExpressions;
176- parser->m_regexList = jList;
192+ parser->m_regexList = regexList;
177193 lua_State* luaState = luaL_newstate ();
178194 luaL_openlibs (luaState);
179195
0 commit comments