-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_splunk_rest_client.py
More file actions
177 lines (143 loc) · 5.51 KB
/
test_splunk_rest_client.py
File metadata and controls
177 lines (143 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#
# Copyright 2023 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 os
from unittest import mock
import pytest
from splunklib.binding import HTTPError
from solnlib.splunk_rest_client import MAX_REQUEST_RETRIES
from requests.exceptions import ConnectionError
from solnlib import splunk_rest_client
from solnlib.splunk_rest_client import SplunkRestClient
@mock.patch.dict(os.environ, {"SPLUNK_HOME": "/opt/splunk"}, clear=True)
@mock.patch("solnlib.splunk_rest_client.get_splunkd_access_info")
def test_init_with_only_required_fields_when_splunk_env(mock_get_splunkd_access_info):
mock_get_splunkd_access_info.return_value = "https", "localhost", 8089
splunk_rest_client.SplunkRestClient(
"session_key",
"app",
"owner",
)
@mock.patch.dict(os.environ, {"SPLUNK_HOME": "/opt/splunk"}, clear=True)
@mock.patch("solnlib.splunk_rest_client.get_splunkd_access_info")
def test_init_with_only_host_when_splunk_env(mock_get_splunkd_access_info):
mock_get_splunkd_access_info.return_value = "https", "localhost", 8089
splunk_rest_client.SplunkRestClient("session_key", "app", "owner", host="localhost")
def test_init_with_only_required_fields_when_not_in_splunk_env():
with pytest.raises(ValueError):
splunk_rest_client.SplunkRestClient(
"session_key",
"app",
"owner",
)
def test_init_with_only_host_and_port():
with pytest.raises(ValueError):
splunk_rest_client.SplunkRestClient(
"session_key",
"app",
"nobody",
host="localhost",
port=8089,
)
def test_init_with_all_fields():
splunk_rest_client.SplunkRestClient(
"session_key",
"app",
"nobody",
scheme="https",
host="localhost",
port=8089,
)
def test_init_with_invalid_port():
with pytest.raises(ValueError):
splunk_rest_client.SplunkRestClient(
"session_key",
"app",
"nobody",
scheme="https",
host="localhost",
port=99999,
)
@mock.patch.dict(os.environ, {"SPLUNK_HOME": "/opt/splunk"}, clear=True)
@mock.patch("solnlib.splunk_rest_client.get_splunkd_access_info")
@mock.patch("http.client.HTTPResponse")
@mock.patch("urllib3.HTTPConnectionPool._make_request")
def test_request_retry(http_conn_pool, http_resp, mock_get_splunkd_access_info):
mock_get_splunkd_access_info.return_value = "https", "localhost", 8089
session_key = "123"
context = {"pool_connections": 5}
rest_client = SplunkRestClient("msg_name_1", session_key, "_", **context)
mock_resp = http_resp()
mock_resp.status = 200
mock_resp.reason = "TEST OK"
side_effects = [ConnectionError(), ConnectionError(), ConnectionError(), mock_resp]
http_conn_pool.side_effect = side_effects
res = rest_client.get("test")
assert http_conn_pool.call_count == len(side_effects)
assert res.reason == mock_resp.reason
side_effects = [ConnectionError()] * (MAX_REQUEST_RETRIES + 1) + [mock_resp]
http_conn_pool.side_effect = side_effects
with pytest.raises(ConnectionError):
rest_client.get("test")
@pytest.mark.parametrize("error_code", [429, 500, 503])
def test_request_throttling(http_mock_server, error_code):
@http_mock_server.get
def throttling(request):
"""Mock endpoint to simulate request throttling.
The endpoint will return an error status code for the first 5
requests, and a 200 status code for subsequent requests.
"""
number = getattr(throttling, "call_count", 0)
throttling.call_count = number + 1
if number < 2:
request.send_response(error_code)
request.send_header("Retry-After", "1")
return {"error": f"Error {number}"}
return {"content": "Success"}
rest_client = SplunkRestClient(
"msg_name_1",
"session_key",
"_",
scheme="http",
host="localhost",
port=http_mock_server.port,
)
resp = rest_client.get("test")
assert resp.status == 200
assert resp.body.read().decode("utf-8") == '{"content": "Success"}'
@pytest.mark.parametrize("error_code", [429, 500, 503])
def test_request_throttling_exceeded(http_mock_server, error_code):
@http_mock_server.get
def throttling(request):
"""Mock endpoint to simulate request throttling.
The endpoint will always return an error status code.
"""
number = getattr(throttling, "call_count", 0)
throttling.call_count = number + 1
request.send_response(error_code)
request.send_header("Retry-After", "1")
return {"error": f"Error {number}"}
rest_client = SplunkRestClient(
"msg_name_1",
"session_key",
"_",
scheme="http",
host="localhost",
port=http_mock_server.port,
)
with pytest.raises(HTTPError) as ex:
rest_client.get("test")
assert ex.value.status == error_code
assert ex.value.body.decode("utf-8") == '{"error": "Error 5"}'