From 32879e0210844d5ae127fa2d33a5ae02f088818b Mon Sep 17 00:00:00 2001 From: sgoral Date: Tue, 4 Mar 2025 14:16:28 +0100 Subject: [PATCH 1/3] refactor: improve information in the user-agent param --- solnlib/splunk_rest_client.py | 6 ++- tests/integration/test_splunk_rest_client.py | 45 ++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 tests/integration/test_splunk_rest_client.py diff --git a/solnlib/splunk_rest_client.py b/solnlib/splunk_rest_client.py index f06eb3c5..7e858248 100644 --- a/solnlib/splunk_rest_client.py +++ b/solnlib/splunk_rest_client.py @@ -23,11 +23,13 @@ import logging import os +import sys import traceback +import solnlib + from io import BytesIO from urllib.parse import quote from urllib3.util.retry import Retry - from splunklib import binding, client from .net_utils import validate_scheme_host_port @@ -134,7 +136,7 @@ def request(url, message, **kwargs): body = message.get("body") headers = { - "User-Agent": "curl", + "User-Agent": f"solnlib/{solnlib.__version__} rest-client {sys.platform}", "Accept": "*/*", "Connection": "Keep-Alive", } diff --git a/tests/integration/test_splunk_rest_client.py b/tests/integration/test_splunk_rest_client.py new file mode 100644 index 00000000..29c9e4e7 --- /dev/null +++ b/tests/integration/test_splunk_rest_client.py @@ -0,0 +1,45 @@ +# +# Copyright 2025 Splunk Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import context +import pytest +import solnlib +import sys +from splunklib import binding +from solnlib import splunk_rest_client as rest_client + +from _search import search + + +def test_rest_client_user_agent(): + test_url = r'search index = _internal uri_path="*/servicesNS/nobody/test_app/some/unexisting/url"' + user_agent = f"solnlib/{solnlib.__version__} rest-client {sys.platform}" + session_key = context.get_session_key() + wrong_url = r"some/unexisting/url" + rc = rest_client.SplunkRestClient( + session_key, + app="test_app", + owner=context.owner, + scheme=context.scheme, + host=context.host, + port=context.port + ) + with pytest.raises(binding.HTTPError): + rc.get(wrong_url) + + search_results = search(session_key, test_url) + assert len(search_results) == 1 + assert user_agent in search_results[0]["_raw"] From 2a6d95a7139b994df598de602ee4b09d354b52aa Mon Sep 17 00:00:00 2001 From: sgoral Date: Tue, 4 Mar 2025 14:23:56 +0100 Subject: [PATCH 2/3] chore: fix pre-commit --- tests/integration/test_splunk_rest_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_splunk_rest_client.py b/tests/integration/test_splunk_rest_client.py index 29c9e4e7..6bd4a20e 100644 --- a/tests/integration/test_splunk_rest_client.py +++ b/tests/integration/test_splunk_rest_client.py @@ -35,7 +35,7 @@ def test_rest_client_user_agent(): owner=context.owner, scheme=context.scheme, host=context.host, - port=context.port + port=context.port, ) with pytest.raises(binding.HTTPError): rc.get(wrong_url) From 8260190aa7a4c7ac77888f8e0c2e2d7ccdaecc7e Mon Sep 17 00:00:00 2001 From: sgoral Date: Tue, 4 Mar 2025 15:56:20 +0100 Subject: [PATCH 3/3] test: hardcode sys.platform, add sleep --- tests/integration/test_splunk_rest_client.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/integration/test_splunk_rest_client.py b/tests/integration/test_splunk_rest_client.py index 6bd4a20e..557f7145 100644 --- a/tests/integration/test_splunk_rest_client.py +++ b/tests/integration/test_splunk_rest_client.py @@ -17,7 +17,7 @@ import context import pytest import solnlib -import sys +from time import sleep from splunklib import binding from solnlib import splunk_rest_client as rest_client @@ -26,7 +26,7 @@ def test_rest_client_user_agent(): test_url = r'search index = _internal uri_path="*/servicesNS/nobody/test_app/some/unexisting/url"' - user_agent = f"solnlib/{solnlib.__version__} rest-client {sys.platform}" + user_agent = f"solnlib/{solnlib.__version__} rest-client linux" session_key = context.get_session_key() wrong_url = r"some/unexisting/url" rc = rest_client.SplunkRestClient( @@ -40,6 +40,10 @@ def test_rest_client_user_agent(): with pytest.raises(binding.HTTPError): rc.get(wrong_url) - search_results = search(session_key, test_url) - assert len(search_results) == 1 + for i in range(50): + search_results = search(session_key, test_url) + if len(search_results) > 0: + break + sleep(0.5) + assert user_agent in search_results[0]["_raw"]