Skip to content

Commit 3f4db26

Browse files
authored
Updated testing(unit and integrated) (#372)
1 parent 92b34f1 commit 3f4db26

File tree

5 files changed

+71
-41
lines changed

5 files changed

+71
-41
lines changed

src/main/java/com/splunk/kafka/connect/SplunkSinkConnector.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,17 @@ private void validateSplunkConfigurations(final Map<String, String> configs) thr
158158
}
159159

160160
private void preparePayloadAndExecuteRequest(SplunkSinkConnectorConfig connectorConfig, String index) throws ConfigException {
161-
Header[] headers = new Header[]{new BasicHeader("Authorization", String.format("Splunk %s", connectorConfig.splunkToken))};
161+
Header[] headers;
162+
if (connectorConfig.ack) {
163+
headers = new Header[]{
164+
new BasicHeader("Authorization", String.format("Splunk %s", connectorConfig.splunkToken)),
165+
new BasicHeader("X-Splunk-Request-Channel", java.util.UUID.randomUUID().toString())
166+
};
167+
} else {
168+
headers = new Header[]{
169+
new BasicHeader("Authorization", String.format("Splunk %s", connectorConfig.splunkToken)),
170+
};
171+
}
162172
String endpoint = "/services/collector";
163173
String url = connectorConfig.splunkURI + endpoint;
164174
final HttpPost httpPost = new HttpPost(url);

src/test/java/com/splunk/kafka/connect/SplunkSinkConnecterTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,44 @@ public void testInvalidKerberosOnlyKeytabSet() {
131131
assertHasErrorMessage(result, KERBEROS_KEYTAB_PATH_CONF, "must be set");
132132
}
133133

134+
@Test
135+
public void testInvalidJsonEventEnrichmentConfig1() {
136+
final Map<String, String> configs = new HashMap<>();
137+
addNecessaryConfigs(configs);
138+
SinkConnector connector = new SplunkSinkConnector();
139+
configs.put("topics", "b");
140+
configs.put("tasks_max", "3");
141+
configs.put("splunk.hec.json.event.enrichment", "k1=v1 k2=v2");
142+
MockHecClientWrapper clientInstance = new MockHecClientWrapper();
143+
((SplunkSinkConnector) connector).setHecInstance(clientInstance);
144+
Assertions.assertThrows(ConfigException.class, ()->connector.validate(configs));
145+
}
146+
147+
@Test
148+
public void testInvalidJsonEventEnrichmentConfig2() {
149+
final Map<String, String> configs = new HashMap<>();
150+
addNecessaryConfigs(configs);
151+
SinkConnector connector = new SplunkSinkConnector();
152+
configs.put("topics", "b");
153+
configs.put("splunk.hec.json.event.enrichment", "testing-testing non KV");
154+
MockHecClientWrapper clientInstance = new MockHecClientWrapper();
155+
((SplunkSinkConnector) connector).setHecInstance(clientInstance);
156+
Assertions.assertThrows(ConfigException.class, ()->connector.validate(configs));
157+
}
158+
159+
@Test
160+
public void testInvalidJsonEventEnrichmentConfig3() {
161+
final Map<String, String> configs = new HashMap<>();
162+
addNecessaryConfigs(configs);
163+
SinkConnector connector = new SplunkSinkConnector();
164+
configs.put("topics", "b");
165+
configs.put("tasks_max", "3");
166+
configs.put("splunk.hec.json.event.enrichment", "k1=v1 k2=v2");
167+
MockHecClientWrapper clientInstance = new MockHecClientWrapper();
168+
((SplunkSinkConnector) connector).setHecInstance(clientInstance);
169+
Assertions.assertThrows(ConfigException.class, ()->connector.validate(configs));
170+
}
171+
134172
@Test
135173
public void testInvalidToken() {
136174
final Map<String, String> configs = new HashMap<>();
@@ -144,6 +182,18 @@ public void testInvalidToken() {
144182
Assertions.assertThrows(ConfigException.class, ()->connector.validate(configs));
145183
}
146184

185+
@Test
186+
public void testNullHecToken() {
187+
final Map<String, String> configs = new HashMap<>();
188+
addNecessaryConfigs(configs);
189+
SinkConnector connector = new SplunkSinkConnector();
190+
configs.put("topics", "b");
191+
configs.put("splunk.hec.token", null);
192+
MockHecClientWrapper clientInstance = new MockHecClientWrapper();
193+
((SplunkSinkConnector) connector).setHecInstance(clientInstance);
194+
Assertions.assertThrows(java.lang.NullPointerException.class, ()->connector.validate(configs));
195+
}
196+
147197
@Test
148198
public void testInvalidIndex() {
149199
final Map<String, String> configs = new HashMap<>();

test/lib/connect_params.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@
8383
"tasks_max": "3",
8484
"splunk_hec_raw": False,
8585
"splunk_hec_json_event_enrichment": "chars=test_tasks_max_3_hec_raw_false"},
86-
{"name": "test_tasks_max_null",
87-
"tasks_max": "null",
88-
"splunk_hec_raw": False,
89-
"splunk_hec_json_event_enrichment": "chars=test_tasks_max_null"},
9086
{"name": "test_1_source_hec_raw_true",
9187
"splunk_hec_raw": True,
9288
"splunk_sources": "test_1_source_hec_raw_true"},
@@ -179,12 +175,6 @@
179175
"splunk_hec_raw": True,
180176
"splunk_hec_json_event_formatted": "false",
181177
"splunk_sourcetypes": "test_splunk_hec_json_event_formatted_false_raw_data"},
182-
{"name": "test_empty_hec_token",
183-
"splunk_hec_token": None,
184-
"splunk_hec_json_event_enrichment": "chars=test_empty_hec_token"},
185-
{"name": "test_incorrect_hec_token",
186-
"splunk_hec_token": "dummy-tocken",
187-
"splunk_hec_json_event_enrichment": "chars=test_incorrect_hec_token"},
188178
{"name": "test_splunk_hec_empty_event",
189179
"topics": "test_splunk_hec_malformed_events",
190180
"splunk_hec_raw": False,
@@ -215,4 +205,4 @@
215205
"enable_timestamp_extraction" : "true",
216206
"timestamp_regex": r"\\\"time\\\":\\s*\\\"(?<time>.*?)\"",
217207
"timestamp_format": "epoch"}
218-
]
208+
]

test/testcases/test_configurations.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def setup_class(self, setup):
2525
("test_tasks_max_1_hec_raw_false", "chars::test_tasks_max_1_hec_raw_false", 3),
2626
# ("test_tasks_max_3_hec_raw_true", "sourcetype::raw_data-tasks_max_3", 1),
2727
# ("test_tasks_max_3_hec_raw_false", "chars::test_tasks_max_3_hec_raw_false", 3),
28-
("test_tasks_max_null", "chars::test_tasks_max_null", 0)
28+
# ("test_tasks_max_null", "chars::test_tasks_max_null", 0)
2929
])
3030
def test_tasks_max(self, setup, test_scenario, test_input, expected):
3131
logger.info(f"testing {test_scenario} input={test_input} expected={expected} event(s)")
@@ -141,21 +141,6 @@ def test_header_support_true_event_data(self, setup, test_scenario, test_input,
141141
logger.info("Splunk received %s events in the last 15m", len(events))
142142
assert len(events) == expected
143143

144-
@pytest.mark.parametrize("test_case, test_input, expected", [
145-
("test_incorrect_hec_token", "chars::test_incorrect_hec_token", 0),
146-
("test_empty_hec_token", "chars::test_empty_hec_token", 0)
147-
])
148-
def test_create_connector_with_incorrect_hec_token(self, setup, test_case, test_input, expected):
149-
search_query = f"index={setup['kafka_header_index']} | search timestamp=\"{setup['timestamp']}\" {test_input}"
150-
logger.info(search_query)
151-
events = check_events_from_splunk(start_time="-15m@m",
152-
url=setup["splunkd_url"],
153-
user=setup["splunk_user"],
154-
query=[f"search {search_query}"],
155-
password=setup["splunk_password"])
156-
logger.info("Splunk received %s events in the last 15m", len(events))
157-
assert len(events) == expected
158-
159144
@pytest.mark.parametrize("test_scenario, test_input, expected", [
160145
("test_splunk_hec_json_event_formatted_true_event_data",
161146
"chars::test_splunk_hec_json_event_formatted_true_event_data", 3),

test/testcases/test_crud.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_valid_crud_tasks(self, setup, test_input, expected):
3636
"tasks.max": "3",
3737
"topics": setup["kafka_topic"],
3838
"splunk.indexes": setup["splunk_index"],
39-
"splunk.hec.uri": setup["splunkd_url"],
39+
"splunk.hec.uri": setup["splunk_hec_url"],
4040
"splunk.hec.token": setup["splunk_token"],
4141
"splunk.hec.raw": "false",
4242
"splunk.hec.ack.enabled": "false",
@@ -56,7 +56,7 @@ def test_valid_crud_tasks(self, setup, test_input, expected):
5656
"tasks.max": "5",
5757
"topics": setup["kafka_topic"],
5858
"splunk.indexes": setup["splunk_index"],
59-
"splunk.hec.uri": setup["splunkd_url"],
59+
"splunk.hec.uri": setup["splunk_hec_url"],
6060
"splunk.hec.token": setup["splunk_token"],
6161
"splunk.hec.raw": "false",
6262
"splunk.hec.ack.enabled": "false",
@@ -101,30 +101,25 @@ def test_invalid_crud_tasks(self, setup, test_case, config_input, expected):
101101

102102
@pytest.mark.parametrize("test_case, config_input, expected", [
103103
("event_enrichment_non_key_value", {"name": "event_enrichment_non_key_value",
104-
"splunk_hec_json_event_enrichment": "testing-testing non KV"},
105-
["FAILED"]),
104+
"splunk_hec_json_event_enrichment": "testing-testing non KV"}, False),
106105
("event_enrichment_non_key_value_3_tasks", {"name": "event_enrichment_non_key_value_3_tasks",
107106
"tasks_max": "3",
108-
"splunk_hec_json_event_enrichment": "testing-testing non KV"},
109-
["FAILED", "FAILED", "FAILED"]),
107+
"splunk_hec_json_event_enrichment": "testing-testing non KV"}, False),
110108
("event_enrichment_not_separated_by_commas", {"name": "event_enrichment_not_separated_by_commas",
111-
"splunk_hec_json_event_enrichment": "key1=value1 key2=value2"},
112-
["FAILED"]),
109+
"splunk_hec_json_event_enrichment": "key1=value1 key2=value2"}, False),
113110
("event_enrichment_not_separated_by_commas_3_tasks", {"name": "event_enrichment_not_separated_by_commas_3_tasks",
114111
"tasks_max": "3",
115-
"splunk_hec_json_event_enrichment": "key1=value1 key2=value2"},
116-
["FAILED", "FAILED", "FAILED"])
112+
"splunk_hec_json_event_enrichment": "key1=value1 key2=value2"}, False)
117113
118114
])
119115
def test_invalid_crud_event_enrichment_tasks(self, setup, test_case, config_input, expected):
120116
'''
121117
Test that invalid event_enrichment kafka connect task can be created but task status should be FAILED
122118
and no data should enter splunk
123119
'''
124-
logger.info(f"testing {test_case} input={config_input} expected={expected} ")
120+
logger.info(f"testing {test_case} input={config_input}")
125121

126122
connector_definition_invalid_tasks = generate_connector_content(config_input)
127123
setup['connectors'].append(test_case)
128124

129-
assert create_kafka_connector(setup, connector_definition_invalid_tasks) is True
130-
assert get_running_kafka_connector_task_status(setup, connector_definition_invalid_tasks) == expected
125+
assert create_kafka_connector(setup, connector_definition_invalid_tasks) == expected

0 commit comments

Comments
 (0)