Skip to content

Commit b947538

Browse files
Merge pull request #263 from splunk/Update-file-creation-uf-file-monitor
Updated file creation mechanism in file monitor
2 parents c734e4f + 1bbbccb commit b947538

File tree

6 files changed

+45
-20
lines changed

6 files changed

+45
-20
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ orbs:
7171
jobs:
7272
release:
7373
docker:
74-
- image: circleci/node:11
74+
- image: circleci/node:12
7575
steps:
7676
- checkout
7777
- run: npx semantic-release

docs/how_to_use.rst

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ There are three ways to execute the tests:
4242
pip install poetry
4343
poetry install
4444
45-
Create a Dockerfile-splunk file
45+
Create a Dockerfile.splunk file
4646

4747
.. dropdown:: Example Dockerfile
4848

@@ -57,6 +57,19 @@ There are three ways to execute the tests:
5757
COPY deps/apps /opt/splunk/etc/apps/
5858
COPY $SPLUNK_APP_PACKAGE /opt/splunk/etc/apps/$SPLUNK_APP_ID
5959
60+
Create a Dockerfile.uf file
61+
62+
.. dropdown:: Example Dockerfile
63+
64+
.. code:: Dockerfile
65+
66+
ARG SPLUNK_VERSION=latest
67+
FROM splunk/universalforwarder:$SPLUNK_VERSION
68+
ARG SPLUNK_VERSION=latest
69+
ARG SPLUNK_APP_ID=TA_UNKNOWN
70+
ARG SPLUNK_APP_PACKAGE=$SPLUNK_APP_PACKAGE
71+
COPY $SPLUNK_APP_PACKAGE /opt/splunkforwarder/etc/apps/$SPLUNK_APP_ID
72+
6073
Create docker-compose.yml
6174

6275
.. dropdown:: Example docker-compose file
@@ -287,4 +300,4 @@ Extending pytest-splunk-addon
287300

288301
<hr width=100%>
289302

290-
.. [#] xfail indicates that you expect a test to fail for some reason. A common example is a test for a feature not yet implemented, or a bug not yet fixed. When a test passes despite being expected to fail, it's an xpass and will be reported in the test summary.
303+
.. [#] xfail indicates that you expect a test to fail for some reason. A common example is a test for a feature not yet implemented, or a bug not yet fixed. When a test passes despite being expected to fail, it's an xpass and will be reported in the test summary.

pytest_splunk_addon/splunk.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -426,16 +426,10 @@ def uf(request):
426426
uf = request.getfixturevalue("uf_docker")
427427
else:
428428
raise Exception
429-
uf["uf_username"] = request.config.getoption("splunk_uf_user")
430-
uf["uf_password"] = request.config.getoption("splunk_uf_password")
431-
for _ in range(RESPONSIVE_SPLUNK_TIMEOUT):
432-
if is_responsive_uf(uf):
433-
break
434-
sleep(1)
435429
yield uf
436430

437431
@pytest.fixture(scope="session")
438-
def uf_docker(docker_services, tmp_path_factory, worker_id):
432+
def uf_docker(docker_services, tmp_path_factory, worker_id, request):
439433
"""
440434
Provides IP of the uf server and management port based on pytest-args(splunk_type)
441435
"""
@@ -450,7 +444,13 @@ def uf_docker(docker_services, tmp_path_factory, worker_id):
450444
uf_info = {
451445
"uf_host": docker_services.docker_ip,
452446
"uf_port": docker_services.port_for("uf", 8089),
447+
"uf_username": request.config.getoption("splunk_uf_user"),
448+
"uf_password": request.config.getoption("splunk_uf_password"),
453449
}
450+
for _ in range(RESPONSIVE_SPLUNK_TIMEOUT):
451+
if is_responsive_uf(uf_info):
452+
break
453+
sleep(1)
454454
return uf_info
455455

456456
@pytest.fixture(scope="session")
@@ -461,6 +461,8 @@ def uf_external(request):
461461
uf_info = {
462462
"uf_host": request.config.getoption("splunk_uf_host"),
463463
"uf_port": request.config.getoption("splunk_uf_port"),
464+
"uf_username": request.config.getoption("splunk_uf_user"),
465+
"uf_password": request.config.getoption("splunk_uf_password"),
464466
}
465467
return uf_info
466468

@@ -704,9 +706,13 @@ def file_system_prerequisite():
704706
"""
705707
UF_FILE_MONTOR_DIR = "uf_files"
706708
monitor_dir = os.path.join(os.getcwd(), UF_FILE_MONTOR_DIR)
707-
if os.path.exists(monitor_dir):
708-
shutil.rmtree(UF_FILE_MONTOR_DIR, ignore_errors=True)
709-
os.mkdir(monitor_dir)
709+
if (
710+
"PYTEST_XDIST_WORKER" not in os.environ
711+
or os.environ.get("PYTEST_XDIST_WORKER") == "gw0"
712+
):
713+
if os.path.exists(monitor_dir):
714+
shutil.rmtree(monitor_dir, ignore_errors=True)
715+
os.mkdir(monitor_dir)
710716

711717
def is_responsive_uf(uf):
712718
"""

pytest_splunk_addon/standard_lib/event_ingestors/file_monitor_ingestor.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ def create_inputs_stanza(self, event):
8989
event (SampleEvent): Instance containing event info
9090
"""
9191
file_path = self.get_file_path(event)
92+
host_segment = len(file_path.split(os.sep)) - 2
93+
# If file path is /home/uf_files/host_name/sample_name
94+
# Then split will return ['', home, 'uf_files', 'host_name', 'sample_name']
9295
sourcetype = event.metadata.get("sourcetype")
9396
if not sourcetype:
9497
sourcetype = event.metadata.get("sourcetype_to_search", "pytest_splunk_addon")
@@ -100,15 +103,13 @@ def create_inputs_stanza(self, event):
100103
"crc-salt": "<SOURCE>"
101104
}
102105
if event.metadata.get("host_type") in ("plugin"):
103-
stanza["host"] = event.metadata.get("host")
104-
if event.metadata.get("source"):
105-
stanza["rename-source"] = event.metadata.get("source")
106+
stanza["host_segment"] = host_segment
106107
LOGGER.info("Making rest call to create stanza in inputs.conf file with following endpoint : {}".format(self.inputs_endpoint))
107108
LOGGER.debug("Creating following stanza in inputs.conf : {}".format(stanza))
108109
try:
109110
response = requests.post(self.inputs_endpoint, stanza, auth=(self.uf_username, self.uf_password), verify=False)
110111
if response.status_code not in (200, 201):
111-
LOGGER.warning("Unable to add stanza in inputs.conf\nStatus code: {} \nReason: {} \ntext:{}".format(response.status_code, response.reason, response.text))
112+
LOGGER.warning("Unable to add stanza in inputs.conf for Path : {} \nStatus code: {} \nReason: {} \ntext:{}".format(file_path, response.status_code, response.reason, response.text))
112113
except ConnectionError as e:
113114
LOGGER.error("Unable to connect to Universal forwarder, {}".format(e))
114115

@@ -119,4 +120,7 @@ def get_file_path(self, event):
119120
Args:
120121
event (SampleEvent): Instance containing event info
121122
"""
122-
return "{}/{}/{}".format(os.getcwd(), MONITOR_DIR, event.metadata.get("host"))
123+
host_dir = os.path.join(os.getcwd(), MONITOR_DIR, event.metadata.get("host"))
124+
if not os.path.exists(host_dir):
125+
os.mkdir(host_dir)
126+
return os.path.join(host_dir, event.metadata.get("source"))

pytest_splunk_addon/standard_lib/sample_generation/sample_stanza.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ def _parse_meta(self, eventgen_params):
160160
metadata.update(count="100")
161161
if metadata.get("index") is not None and metadata.get("input_type") in ["syslog_tcp", "tcp", "udp"]:
162162
raise_warning("For input_type '{}', there should be no index set".format(metadata.get("input_type")))
163+
if metadata.get("input_type") == "uf_file_monitor":
164+
metadata["host"] = metadata.get("host").replace("_", "-").replace(".", "-")
163165
return metadata
164166

165167
def get_eventmetadata(self):

tests/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,8 @@
616616
'*test_splunk_fiction_indextime.py::Test_App::test_indextime_line_breaker*test:indextime:uf_file_monitor_host_event::uf_file_monitor_host_event.sample* PASSED*',
617617
'*test_splunk_fiction_indextime.py::Test_App::test_indextime_line_breaker*test:indextime:uf_file_monitor_host_plugin::uf_file_monitor_host_plugin.sample* PASSED*',
618618
'*test_splunk_fiction_indextime.py::Test_App::test_indextime_line_breaker*test:indextime:uf_file_monitor_rename_sourcetype:source::uf_file_monitor_rename_sourcetype.sample* PASSED*',
619-
'*test_splunk_fiction_indextime.py::Test_App::test_indextime_key_fields*test:indextime:uf_file_monitor_indexed_extraction::uf_file_monitor_indexed_extraction.sample* PASSED*',
620-
'*test_splunk_fiction_indextime.py::Test_App::test_indextime_time*test:indextime:uf_file_monitor_indexed_extraction::uf_file_monitor_indexed_extraction.sample* PASSED*',
619+
'*test_splunk_fiction_indextime.py::Test_App::test_indextime_key_fields*test:indextime:uf_file_monitor_indexed_extraction::uf-file-monitor-indexed-extraction-sample* PASSED*',
620+
'*test_splunk_fiction_indextime.py::Test_App::test_indextime_time*test:indextime:uf_file_monitor_indexed_extraction::uf-file-monitor-indexed-extraction-sample* PASSED*',
621621
'*test_splunk_fiction_indextime.py::Test_App::test_indextime_line_breaker*test:indextime:uf_file_monitor_indexed_extraction::uf_file_monitor_indexed_extraction.sample* PASSED*',
622622
]
623623

0 commit comments

Comments
 (0)