88from ..addon_parser import Field
99import json
1010
11+ TOP_FIVE_STRUCTURALLY_UNIQUE_EVENTS_QUERY_PART = " | dedup punct | head 5"
12+ COUNT_BY_SOURCE_TYPE_SEARCH_QUERY_PART = " | stats count by sourcetype"
13+
14+
1115class FieldTestTemplates (object ):
1216 """
1317 Test templates to test the knowledge objects of an App
1418 """
1519
16- logger = logging .getLogger ("pytest-splunk-addon-tests " )
20+ logger = logging .getLogger ("pytest-splunk-addon" )
1721
1822 @pytest .mark .splunk_searchtime_fields
1923 @pytest .mark .splunk_searchtime_internal_errors
@@ -80,7 +84,7 @@ def test_props_fields(
8084 search + f" AND ({ field } IN ({ expected_values } )"
8185 f" AND NOT { field } IN ({ negative_values } ))"
8286 )
83- search += " | stats count by sourcetype"
87+ search += COUNT_BY_SOURCE_TYPE_SEARCH_QUERY_PART
8488
8589 self .logger .info (f"Executing the search query: { search } " )
8690
@@ -125,7 +129,7 @@ def test_props_fields_no_dash_not_empty(
125129 record_property ("fields" , splunk_searchtime_fields_negative ["fields" ])
126130
127131 index_list = "(index=" + " OR index=" .join (splunk_search_util .search_index .split (',' )) + ")"
128- search = (
132+ base_search = (
129133 f"search { index_list } "
130134 f" { splunk_searchtime_fields_negative ['stanza_type' ]} =\" "
131135 f"{ splunk_searchtime_fields_negative ['stanza' ]} \" "
@@ -137,8 +141,8 @@ def test_props_fields_no_dash_not_empty(
137141 negative_values = ", " .join ([f'"{ each } "' for each in field .negative_values ])
138142
139143 fields_search .append (f"({ field } IN ({ negative_values } ))" )
140- search += " AND ({})" .format (" OR " .join (fields_search ))
141- search += " | stats count by sourcetype"
144+ base_search += " AND ({})" .format (" OR " .join (fields_search ))
145+ search = base_search + COUNT_BY_SOURCE_TYPE_SEARCH_QUERY_PART
142146
143147 self .logger .info (f"Executing the search query: { search } " )
144148
@@ -149,9 +153,16 @@ def test_props_fields_no_dash_not_empty(
149153 record_property ("results" , results .as_list )
150154 pp = pprint .PrettyPrinter (indent = 4 )
151155 result_str = pp .pformat (results .as_list [:10 ])
156+
157+ query_for_unique_events = base_search + TOP_FIVE_STRUCTURALLY_UNIQUE_EVENTS_QUERY_PART
158+ query_results = splunk_search_util .get_search_results (query_for_unique_events )
159+ results_formatted_str = pp .pformat (query_results .as_list )
152160 assert result , (
153161 f"Query result greater than 0.\n search={ search } \n "
154- f"found result={ result_str } "
162+ f"found result={ result_str } \n "
163+ " === STRUCTURALLY UNIQUE EVENTS:\n "
164+ f"query={ query_for_unique_events } \n "
165+ f"events= { results_formatted_str } "
155166 )
156167
157168 @pytest .mark .splunk_searchtime_fields
@@ -190,7 +201,7 @@ def test_tags(
190201
191202 index_list = "(index=" + " OR index=" .join (splunk_search_util .search_index .split (',' )) + ")"
192203 search = f"search { index_list } { tag_query } AND tag={ tag } "
193- search += " | stats count by sourcetype"
204+ search += COUNT_BY_SOURCE_TYPE_SEARCH_QUERY_PART
194205
195206 self .logger .info (f"Search: { search } " )
196207
@@ -247,7 +258,7 @@ def test_eventtype(
247258 search = (f"search { index_list } AND "
248259 f"eventtype="
249260 f"\" { splunk_searchtime_fields_eventtypes ['stanza' ]} \" " )
250- search += " | stats count by sourcetype"
261+ search += COUNT_BY_SOURCE_TYPE_SEARCH_QUERY_PART
251262
252263 self .logger .info (
253264 "Testing eventtype =%s" , splunk_searchtime_fields_eventtypes ["stanza" ]
0 commit comments