Skip to content

Commit b2f6dc5

Browse files
feat: adding CLEAN_KEYS support (#869)
Added support for CLEAN_KEYS in Transforms.conf. Added respective e2e test case for the same Impact on Addons: Now if Addons have have field names in transforms report which have non-alphanumeric chars then those chars would be first converted to _ and then tests would be generated as by default in splunk CLEAN_KEYS is set to true, so splunk also does the same thing. Moreover if any report stanza has explicitly set CLEAN_KEYS=false then for those reports field conversion would not happen and tests would be generated as it is.
1 parent b54ce6a commit b2f6dc5

File tree

6 files changed

+48
-4
lines changed

6 files changed

+48
-4
lines changed

pytest_splunk_addon/addon_parser/props_parser.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"""
1717
Provides props.conf parsing mechanism
1818
"""
19-
from typing import Dict
19+
from typing import Dict, List
2020
from typing import Generator
2121
from typing import Optional
2222
import logging
@@ -57,6 +57,14 @@ def props(self) -> Optional[Dict]:
5757
self._props = self._conf_parser.item_dict()
5858
return self._props if self._props else None
5959

60+
def update_field_names(self, field_list: List[str]) -> List[str]:
61+
"""
62+
update field names to remove all the non-alphanumeric chars and replace them with _
63+
"""
64+
for field in field_list:
65+
field.name = re.sub(r"\W+", "_", field.name)
66+
return field_list
67+
6068
def get_props_fields(self):
6169
"""
6270
Parse the props.conf and yield all supported fields
@@ -82,6 +90,13 @@ def get_props_fields(self):
8290
else:
8391
for transform_stanza, fields in self._get_report_fields(key, value):
8492
field_list = list(fields)
93+
if (
94+
self.transforms_parser.transforms.get(
95+
transform_stanza, {}
96+
).get("CLEAN_KEYS")
97+
!= "false"
98+
):
99+
field_list = self.update_field_names(field_list)
85100
if field_list:
86101
yield {
87102
"stanza": stanza_name,

pytest_splunk_addon/fields_tests/test_templates.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020
import pprint
2121
import logging
2222
import pytest
23-
from ..addon_parser import Field
2423
import json
2524
from itertools import chain
25+
from ..addon_parser import Field
2626
from ..utilities.log_helper import get_table_output
2727
from ..utilities.log_helper import format_search_query_log
28-
2928
from .requirement_test_datamodel_tag_constants import dict_datamodel_tag
3029

3130
TOP_FIVE_STRUCTURALLY_UNIQUE_EVENTS_QUERY_PART = " | dedup punct | head 5"

tests/e2e/addons/TA_broken/default/transforms.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ case_sensitive_match = false
4848
# Expected result: FAIL
4949
[broken-NaN_lookup]
5050
filename = NaN.csv
51-
case_sensitive_match = false
51+
case_sensitive_match = false

tests/e2e/addons/TA_fiction/default/props.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ EXTRACT-fiction-fourteen = (?<date_only_in_hotwarmpath_event>\d+-\d+-\d+).*in ho
8181
REPORT-fiction-tsc-delim-fields = fiction-tsc-delim-fields
8282
REPORT-fiction-tsc-sk-regex-format = fiction-tsc-sk-regex-format
8383
REPORT-fiction-tsc-sk-delim-format = fiction-tsc-sk-delim-format
84+
REPORT-fiction-tsc-sk-delim-format-with-clean-keys = fiction-tsc-sk-delim-format-with-clean-keys
85+
REPORT-fiction-tsc-non-alphanumeric = fiction-tsc-non-alphanumeric
86+
8487

8588
## multiple transforms stanza associated with REPORT
8689
REPORT-fiction-tsc-regex-format = fiction-tsc-regex, fiction-tsc-regex-format

tests/e2e/addons/TA_fiction/default/transforms.conf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ SOURCE_KEY = event_id
2222
DELIMS = "="
2323
FIELDS = server_contact_mode, dest
2424

25+
# Component tested: REPORT, DELIM-FIELDS-SOURCE_KEY
26+
# Scenario:# Similar to the above scenario
27+
## Here as CLEAN_KEYS = false server-contact-mode will be searched as is instead of converting it.
28+
[fiction-tsc-sk-delim-format-with-clean-keys]
29+
CLEAN_KEYS = false
30+
SOURCE_KEY = event_id
31+
DELIMS = "="
32+
FIELDS = server-contact-mode, dest
33+
34+
# Component tested: REPORT, DELIM
35+
# Scenario:
36+
## server-contact-mode should be searched as server_contact_mode as CLEAN_KEYS = true by default[fiction-tsc-non-alphanumeric]
37+
[fiction-tsc-non-alphanumeric]
38+
DELIMS = " "
39+
FIELDS = server-contact, dest_1
40+
41+
2542
# Component tested: REPORT, REGEX-FORMAT-SOURCE_KEY
2643
# Scenario: Source-key with regex and format
2744
## An individual search for SOURCE_KEY and each field extracted in FORMAT and a single search of all the fields with SOURCE_KEY.

tests/e2e/constants.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464
"*test_splunk_app_fiction.py::Test_App::test_props_fields*splunkd::field::dest* PASSED*",
6565
"*test_splunk_app_fiction.py::Test_App::test_props_fields*splunkd::REPORT-fiction-tsc-regex-format::fiction-tsc-regex* PASSED*",
6666
"*test_splunk_app_fiction.py::Test_App::test_props_fields*splunkd::REPORT-fiction-tsc-regex-format::fiction-tsc-regex-format* PASSED*",
67+
"*test_splunk_app_fiction.py::Test_App::test_props_fields*splunkd::REPORT-fiction-tsc-sk-delim-format-with-clean-keys::fiction-tsc-sk-delim-format-with-clean-keys* PASSED*",
68+
"*test_splunk_app_fiction.py::Test_App::test_props_fields*splunkd::field::server-contact-mode* PASSED*",
69+
"*test_splunk_app_fiction.py::Test_App::test_props_fields*splunkd::field::server_contact* PASSED*",
70+
"*test_splunk_app_fiction.py::Test_App::test_props_fields*splunkd::field::dest* PASSED*",
71+
"*test_splunk_app_fiction.py::Test_App::test_props_fields*splunkd::field::dest_1* PASSED*",
6772
"*test_splunk_app_fiction.py::Test_App::test_props_fields*splunkd::field::size1* PASSED*",
6873
"*test_splunk_app_fiction.py::Test_App::test_props_fields*splunkd::field::size2* PASSED*",
6974
"*test_splunk_app_fiction.py::Test_App::test_props_fields*splunkd::field::myeval* PASSED*",
@@ -119,6 +124,11 @@
119124
"*test_splunk_app_fiction.py::Test_App::test_props_fields_no_dash_not_empty**/opt/splunk/var/log/splunk/splunkd.log*::field::splunk_server* PASSED*",
120125
"*test_splunk_app_fiction.py::Test_App::test_props_fields_no_dash_not_empty**/opt/splunk/var/log/splunk/splunkd_access.log*::field::splunk_server* PASSED*",
121126
"*test_splunk_app_fiction.py::Test_App::test_props_fields_no_dash_not_empty*splunkd::EXTRACT-fiction-one* PASSED*",
127+
"*test_splunk_app_fiction.py::Test_App::test_props_fields_no_dash_not_empty*splunkd::REPORT-fiction-tsc-sk-delim-format-with-clean-keys::fiction-tsc-sk-delim-format-with-clean-keys* PASSED*",
128+
"*test_splunk_app_fiction.py::Test_App::test_props_fields_no_dash_not_empty*splunkd::field::server-contact-mode* PASSED*",
129+
"*test_splunk_app_fiction.py::Test_App::test_props_fields_no_dash_not_empty*splunkd::field::dest* PASSED*",
130+
"*test_splunk_app_fiction.py::Test_App::test_props_fields_no_dash_not_empty*splunkd::field::dest_1* PASSED*",
131+
"*test_splunk_app_fiction.py::Test_App::test_props_fields_no_dash_not_empty*splunkd::field::server_contact* PASSED*",
122132
"*test_splunk_app_fiction.py::Test_App::test_props_fields_no_dash_not_empty*splunkd::field::extractone* PASSED*",
123133
"*test_splunk_app_fiction.py::Test_App::test_props_fields_no_dash_not_empty*splunkd::EXTRACT-fiction-two* PASSED*",
124134
"*test_splunk_app_fiction.py::Test_App::test_props_fields_no_dash_not_empty*splunkd::field::extracttwoA* PASSED*",

0 commit comments

Comments
 (0)