Skip to content

Commit 7a121e6

Browse files
Merge pull request #242 from splunk/INFRA-28546-develop
feat: updating test output error message in case of failure for no_dash_not_empty tests
2 parents bad9fc9 + a40e6ec commit 7a121e6

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,6 @@ venv
7979
# Vs Code Settings
8080
.vscode
8181
test_report.md
82+
83+
# Pycharm
84+
.idea/

pytest_splunk_addon/helmut_lib/SearchUtil.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,25 @@ def checkQueryCountIsZero(self, query, max_time=120):
162162
self.logger.debug("Count of results is > 0, it is:%d", result_count)
163163
return False, job.get_results()
164164

165+
def get_search_results(self, query, max_time=120):
166+
"""
167+
Execute a search query
168+
Args:
169+
query (str): query string for Splunk Search
170+
max_time: Amount of time job can wait to finish.
171+
Returns:
172+
events that match the query
173+
"""
174+
175+
self.logger.debug("query is %s", query)
176+
try:
177+
job = self.jobs.create(query, auto_finalize_ec=120, max_time=max_time)
178+
job.wait(max_time)
179+
return job.get_results()
180+
except Exception as e:
181+
self.logger.debug("Errors when executing search!!!")
182+
self.logger.debug(e)
183+
165184
def checkQueryFields(
166185
self,
167186
query,

pytest_splunk_addon/standard_lib/fields_tests/test_templates.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88
from ..addon_parser import Field
99
import 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+
1115
class 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.\nsearch={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

Comments
 (0)