Skip to content

Commit 46682b9

Browse files
chore: minor refactor
1 parent f43bfb6 commit 46682b9

File tree

17 files changed

+139
-177
lines changed

17 files changed

+139
-177
lines changed

pytest_splunk_addon/app_test_generator.py

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,16 @@ class AppTestGenerator(object):
4545
def __init__(self, pytest_config):
4646
self.pytest_config = pytest_config
4747
self.seen_tests = set()
48+
self.ingest_with_uuid = self.pytest_config.getoption("ingest_with_uuid")
49+
self.config_path = self.pytest_config.getoption("splunk_data_generator")
50+
self.store_events = self.pytest_config.getoption("store_events")
4851

49-
store_events = self.pytest_config.getoption("store_events")
50-
config_path = self.pytest_config.getoption("splunk_data_generator")
51-
ingest_with_uuid = self.pytest_config.getoption("ingest_with_uuid")
5252
sample_generator = SampleXdistGenerator(
53-
self.pytest_config.getoption("splunk_app"), ingest_with_uuid, config_path
53+
self.pytest_config.getoption("splunk_app"),
54+
self.ingest_with_uuid,
55+
self.config_path,
5456
)
55-
store_sample = sample_generator.get_samples(store_events)
57+
store_sample = sample_generator.get_samples(self.store_events)
5658
self.tokenized_events = store_sample.get("tokenized_events")
5759
LOGGER.debug("Initializing FieldTestGenerator to generate the test cases")
5860
self.fieldtest_generator = FieldTestGenerator(
@@ -72,6 +74,38 @@ def __init__(self, pytest_config):
7274
)
7375
self.indextime_test_generator = IndexTimeTestGenerator()
7476

77+
def _generate_indextime_tests(self, fixture):
78+
"""
79+
Generate index time tests based on the fixture type.
80+
81+
Args:
82+
fixture (str): The fixture name containing the test type
83+
84+
Returns:
85+
list: List of pytest parameters for the specified test type
86+
"""
87+
app_path = self.pytest_config.getoption("splunk_app")
88+
config_path = self.pytest_config.getoption("splunk_data_generator")
89+
90+
if "key_fields" in fixture:
91+
test_type = "key_fields"
92+
elif "_time" in fixture:
93+
test_type = "_time"
94+
elif "line_breaker" in fixture:
95+
test_type = "line_breaker"
96+
else:
97+
return []
98+
99+
return list(
100+
self.indextime_test_generator.generate_tests(
101+
self.store_events,
102+
app_path=app_path,
103+
config_path=config_path,
104+
test_type=test_type,
105+
ingest_with_uuid=self.ingest_with_uuid,
106+
)
107+
)
108+
75109
def generate_tests(self, fixture):
76110
"""
77111
Generate the test cases based on the fixture provided
@@ -101,51 +135,8 @@ def generate_tests(self, fixture):
101135
self.cim_test_generator.generate_tests(fixture),
102136
fixture,
103137
)
104-
105138
elif fixture.startswith("splunk_indextime"):
106-
# TODO: What should be the id of the test case?
107-
# Sourcetype + Host + Key field + _count
108-
109-
pytest_params = None
110-
111-
store_events = self.pytest_config.getoption("store_events")
112-
app_path = self.pytest_config.getoption("splunk_app")
113-
config_path = self.pytest_config.getoption("splunk_data_generator")
114-
ingest_with_uuid = self.pytest_config.getoption("ingest_with_uuid")
115-
116-
if "key_fields" in fixture:
117-
pytest_params = list(
118-
self.indextime_test_generator.generate_tests(
119-
store_events,
120-
app_path=app_path,
121-
config_path=config_path,
122-
test_type="key_fields",
123-
ingest_with_uuid=ingest_with_uuid,
124-
)
125-
)
126-
127-
elif "_time" in fixture:
128-
pytest_params = list(
129-
self.indextime_test_generator.generate_tests(
130-
store_events,
131-
app_path=app_path,
132-
config_path=config_path,
133-
test_type="_time",
134-
ingest_with_uuid=ingest_with_uuid,
135-
)
136-
)
137-
138-
elif "line_breaker" in fixture:
139-
pytest_params = list(
140-
self.indextime_test_generator.generate_tests(
141-
store_events,
142-
app_path=app_path,
143-
config_path=config_path,
144-
test_type="line_breaker",
145-
ingest_with_uuid=ingest_with_uuid,
146-
)
147-
)
148-
139+
pytest_params = self._generate_indextime_tests(fixture)
149140
yield from sorted(pytest_params, key=lambda param: param.id)
150141

151142
def dedup_tests(self, test_list, fixture):

pytest_splunk_addon/event_ingestors/hec_event_ingestor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def ingest(self, events, thread_count):
9191
"event": event.event,
9292
"index": event.metadata.get("index", "main"),
9393
}
94-
if event.metadata.get("ingest_with_uuid") == "true":
94+
if event.metadata.get("ingest_with_uuid"):
9595
event_dict["fields"] = {"unique_identifier": event.unique_identifier}
9696

9797
if event.metadata.get("host_type") in ("plugin", None):

pytest_splunk_addon/fields_tests/test_generator.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def generate_requirements_datamodels_tests(self):
194194
"datamodels": datamodels,
195195
"stanza": escaped_event,
196196
}
197-
if event.metadata.get("ingest_with_uuid") == "true":
197+
if event.metadata.get("ingest_with_uuid"):
198198
sample_event["unique_identifier"] = event.unique_identifier
199199
yield pytest.param(
200200
sample_event,
@@ -270,8 +270,7 @@ def generate_requirements_tests(self):
270270
"modinput_params": modinput_params,
271271
}
272272

273-
# Add unique_identifier if UUID ingestion is enabled and event has UUID
274-
if metadata.get("ingest_with_uuid") == "true":
273+
if metadata.get("ingest_with_uuid"):
275274
unique_identifier = getattr(event, "unique_identifier", None)
276275
if unique_identifier is not None:
277276
sample_event["unique_identifier"] = unique_identifier

pytest_splunk_addon/index_tests/test_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class IndexTimeTestGenerator(object):
3333
"""
3434

3535
def generate_tests(
36-
self, store_events, app_path, config_path, test_type, ingest_with_uuid="false"
36+
self, store_events, app_path, config_path, test_type, ingest_with_uuid=False
3737
):
3838
"""
3939
Generates the test cases based on test_type
@@ -43,7 +43,7 @@ def generate_tests(
4343
app_path (str): Path of the app package
4444
config_path (str): Path of package which contains pytest-splunk-addon-data.conf
4545
test_type (str): Type of test case
46-
ingest_with_uuid (str): Whether to ingest events with UUID
46+
ingest_with_uuid (bool): Whether to ingest events with UUID
4747
4848
Yields:
4949
pytest.params for the test templates

pytest_splunk_addon/sample_generation/sample_event.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
import uuid
1716
import re
1817
import logging
1918
from ..index_tests import key_fields
@@ -68,7 +67,6 @@ def __init__(self, event_string, metadata, sample_name, requirement_test_data=No
6867
self.time_values = list()
6968
self.metadata = metadata
7069
self.sample_name = sample_name
71-
# UUID will be assigned post-tokenization per final event to avoid duplicates
7270
self.host_count = 0
7371
self.requirement_test_data = requirement_test_data
7472

pytest_splunk_addon/sample_generation/sample_stanza.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class SampleStanza(object):
4848
psa_data_params (dict): Dictionary representing pytest-splunk-addon-data.conf
4949
"""
5050

51-
def __init__(self, sample_path, psa_data_params, ingest_with_uuid):
51+
def __init__(self, sample_path, psa_data_params, ingest_with_uuid: bool):
5252
self.sample_path = sample_path
5353
self.sample_name = os.path.basename(sample_path)
5454
self.metadata = self._parse_meta(psa_data_params)
@@ -105,7 +105,7 @@ def tokenize(self, conf_name):
105105
raw_event[event_counter] = each_rule.apply(raw_event[event_counter])
106106
for event in raw_event[event_counter]:
107107
# Assign UUID per finalized event to ensure uniqueness across variants
108-
if event.metadata.get("ingest_with_uuid") == "true":
108+
if event.metadata.get("ingest_with_uuid"):
109109
event.unique_identifier = str(uuid.uuid4())
110110

111111
host_value = event.metadata.get("host")

pytest_splunk_addon/sample_generation/sample_xdist_generator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SampleXdistGenerator:
3333
process_count (num): generate {no} process for execution
3434
"""
3535

36-
def __init__(self, addon_path, ingest_with_uuid, config_path=None, process_count=4):
36+
def __init__(self, addon_path, ingest_with_uuid: bool, config_path=None, process_count=4):
3737
self.addon_path = addon_path
3838
self.process_count = process_count
3939
self.config_path = config_path
@@ -141,7 +141,7 @@ def store_events(self, tokenized_events):
141141
}
142142
],
143143
}
144-
if self.ingest_with_uuid == "true":
144+
if self.ingest_with_uuid:
145145
tokenized_samples_dict[each_event.sample_name]["events"][0][
146146
"unique_identifier"
147147
] = each_event.unique_identifier
@@ -152,7 +152,7 @@ def store_events(self, tokenized_events):
152152
"time_values": each_event.time_values,
153153
"requirement_test_data": each_event.requirement_test_data,
154154
}
155-
if self.ingest_with_uuid == "true":
155+
if self.ingest_with_uuid:
156156
sample_event["unique_identifier"] = each_event.unique_identifier
157157
tokenized_samples_dict[each_event.sample_name]["events"].append(
158158
sample_event

pytest_splunk_addon/splunk.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,11 @@ def pytest_addoption(parser):
4949
"""
5050
group = parser.getgroup("splunk-addon")
5151
group.addoption(
52-
"--ingest-with-uuid",
53-
action="store",
52+
"--use-uuid",
53+
action="store_true",
5454
dest="ingest_with_uuid",
55-
default="false",
56-
help=(
57-
'Use generated UUID for ingesting and searching events. Setting this parameter to "true" will lead to matching events in search by the ID and not by escaped _raw. Default is "false".'
58-
),
55+
default=False,
56+
help="Use generated UUID for ingesting and searching events. Setting this flag will lead to matching events in search by the ID and not by escaped _raw.",
5957
)
6058

6159
group.addoption(

tests/e2e/test_splunk_addon.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ def empty_method():
845845
"--search-interval=4",
846846
"--search-retry=4",
847847
"--search-index=*,_internal",
848-
"--ingest-with-uuid=true",
848+
"--use-uuid=true",
849849
)
850850

851851
logger.info(result.outlines)
@@ -899,7 +899,7 @@ def empty_method():
899899
"--search-retry=4",
900900
"--search-index=*",
901901
"--splunk-data-generator=tests/addons/TA_transition_from_req/default",
902-
"--ingest-with-uuid=true",
902+
"--use-uuid=true",
903903
)
904904
logger.info(result.outlines)
905905

tests/unit/tests_standard_lib/test_app_test_generator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"store_events": True,
1212
"splunk_data_generator": "psa.conf",
1313
"requirement_test": "fake_requirement_path",
14-
"ingest_with_uuid": "false",
14+
"ingest_with_uuid": False,
1515
}
1616
pytest_config = namedtuple("Config", ["getoption"])
1717
test_config = pytest_config(getoption=lambda x, *y: config[x])
@@ -97,7 +97,7 @@ def test_app_test_generator_instantiation(
9797
"app_path": "fake_app",
9898
"config_path": "psa.conf",
9999
"test_type": "key_fields",
100-
"ingest_with_uuid": "false",
100+
"ingest_with_uuid": False,
101101
},
102102
[
103103
params(values=f"splunk_indextime_key_fields_test_1", id=1),
@@ -118,7 +118,7 @@ def test_app_test_generator_instantiation(
118118
"app_path": "fake_app",
119119
"config_path": "psa.conf",
120120
"test_type": "_time",
121-
"ingest_with_uuid": "false",
121+
"ingest_with_uuid": False,
122122
},
123123
[
124124
params(values=f"splunk_indextime__time_test_1", id=1),
@@ -139,7 +139,7 @@ def test_app_test_generator_instantiation(
139139
"app_path": "fake_app",
140140
"config_path": "psa.conf",
141141
"test_type": "line_breaker",
142-
"ingest_with_uuid": "false",
142+
"ingest_with_uuid": False,
143143
},
144144
[
145145
params(values=f"splunk_indextime_line_breaker_test_1", id=1),

0 commit comments

Comments
 (0)