8
8
check_syslog_ng_health ,
9
9
subprocess ,
10
10
check_queue_size ,
11
+ get_list_of_destinations ,
11
12
)
12
13
13
14
# str_to_bool
@@ -52,9 +53,9 @@ def test_check_syslog_ng_health_exception(mock_run):
52
53
# check_queue_size
53
54
def test_check_queue_size_no_url ():
54
55
"""
55
- If sc4s_dest_splunk_hec_default is not set, check_queue_size should fail.
56
+ If sc4s_dest_splunk_hec_destinations is not set, check_queue_size should fail.
56
57
"""
57
- assert check_queue_size (sc4s_dest_splunk_hec_default = None , max_queue_size = 1000 ) is False
58
+ assert check_queue_size (sc4s_dest_splunk_hec_destinations = None , max_queue_size = 1000 ) is False
58
59
59
60
@patch ("subprocess.run" )
60
61
def test_check_queue_size_stats_fail (mock_run ):
@@ -63,7 +64,7 @@ def test_check_queue_size_stats_fail(mock_run):
63
64
"""
64
65
mock_run .return_value .returncode = 1
65
66
mock_run .return_value .stderr = "stats error"
66
- assert check_queue_size (sc4s_dest_splunk_hec_default = "http://example.com:8088" , max_queue_size = 1000 ) is False
67
+ assert check_queue_size (sc4s_dest_splunk_hec_destinations = { "http://example.com:8088" } , max_queue_size = 1000 ) is False
67
68
68
69
@patch ("subprocess.run" )
69
70
def test_check_queue_size_no_matching_stats (mock_run ):
@@ -72,7 +73,7 @@ def test_check_queue_size_no_matching_stats(mock_run):
72
73
"""
73
74
mock_run .return_value .returncode = 0
74
75
mock_run .return_value .stdout = "some;other;stat;line\n another;stat"
75
- assert check_queue_size (sc4s_dest_splunk_hec_default = "http://example.com:8088" , max_queue_size = 1000 ) is False
76
+ assert check_queue_size (sc4s_dest_splunk_hec_destinations = { "http://example.com:8088" } , max_queue_size = 1000 ) is False
76
77
77
78
@patch ("subprocess.run" )
78
79
def test_check_queue_size_exceeds_limit (mock_run ):
@@ -84,7 +85,7 @@ def test_check_queue_size_exceeds_limit(mock_run):
84
85
"destination;queued;http://example.com:8088;2000\n "
85
86
"another;queued;http://other-url.com;1234"
86
87
)
87
- assert check_queue_size (sc4s_dest_splunk_hec_default = "http://example.com:8088" , max_queue_size = 1000 ) is False
88
+ assert check_queue_size (sc4s_dest_splunk_hec_destinations = { "http://example.com:8088" } , max_queue_size = 1000 ) is False
88
89
89
90
@patch ("subprocess.run" )
90
91
def test_check_queue_size_under_limit (mock_run ):
@@ -96,7 +97,7 @@ def test_check_queue_size_under_limit(mock_run):
96
97
"destination;queued;http://example.com:8088;500\n "
97
98
"another;queued;http://other-url.com;1234"
98
99
)
99
- assert check_queue_size (sc4s_dest_splunk_hec_default = "http://example.com:8088" , max_queue_size = 1000 ) is True
100
+ assert check_queue_size (sc4s_dest_splunk_hec_destinations = { "http://example.com:8088" } , max_queue_size = 1000 ) is True
100
101
101
102
@patch ("subprocess.run" )
102
103
def test_check_queue_size_equals_limit (mock_run ):
@@ -108,7 +109,62 @@ def test_check_queue_size_equals_limit(mock_run):
108
109
"destination;queued;http://example.com:8088;1000\n "
109
110
"another;queued;http://other-url.com;1234"
110
111
)
111
- assert check_queue_size (sc4s_dest_splunk_hec_default = "http://example.com:8088" , max_queue_size = 1000 ) is True
112
+ assert check_queue_size (sc4s_dest_splunk_hec_destinations = {"http://example.com:8088" }, max_queue_size = 1000 ) is True
113
+
114
+ @patch ("subprocess.run" )
115
+ def test_check_queue_size_multiple_destinations (mock_run ):
116
+ """
117
+ If queue size for all destinations is <= HEALTHCHECK_MAX_QUEUE_SIZE, check_queue_size should pass.
118
+ """
119
+ mock_run .return_value .returncode = 0
120
+ mock_run .return_value .stdout = (
121
+ "destination;queued;http://example.com:8088;300\n "
122
+ "destination;queued;http://another.com:8088;500\n "
123
+ "another;queued;http://other-url.com;1234"
124
+ )
125
+ assert check_queue_size (sc4s_dest_splunk_hec_destinations = {"http://example.com:8088" , "http://another.com:8088" },
126
+ max_queue_size = 1000 ) is True
127
+
128
+ @patch ("subprocess.run" )
129
+ def test_check_queue_size_multiple_destinations_over_limit (mock_run ):
130
+ """
131
+ If queue size for at least one destination is > HEALTHCHECK_MAX_QUEUE_SIZE, check_queue_size should fail.
132
+ """
133
+ mock_run .return_value .returncode = 0
134
+ mock_run .return_value .stdout = (
135
+ "destination;queued;http://example.com:8088;1300\n "
136
+ "destination;queued;http://another.com:8088;500\n "
137
+ "another;queued;http://other-url.com;1234"
138
+ )
139
+ assert check_queue_size (sc4s_dest_splunk_hec_destinations = {"http://example.com:8088" , "http://another.com:8088" },
140
+ max_queue_size = 1000 ) is False
141
+
142
+ @patch ("subprocess.run" )
143
+ def test_check_queue_size_multiple_destinations_all_over_limit (mock_run ):
144
+ """
145
+ If queue size for all destinations is > HEALTHCHECK_MAX_QUEUE_SIZE, check_queue_size should fail.
146
+ """
147
+ mock_run .return_value .returncode = 0
148
+ mock_run .return_value .stdout = (
149
+ "destination;queued;http://example.com:8088;1300\n "
150
+ "destination;queued;http://another.com:8088;1500\n "
151
+ "another;queued;http://other-url.com;1234"
152
+ )
153
+ assert check_queue_size (sc4s_dest_splunk_hec_destinations = {"http://example.com:8088" , "http://another.com:8088" },
154
+ max_queue_size = 1000 ) is False
155
+
156
+ @patch ("subprocess.run" )
157
+ def test_check_queue_size_multiple_incomplete_info (mock_run ):
158
+ """
159
+ If stats run successfully but do not contain stats for one of the desired destinations, it should fail.
160
+ """
161
+ mock_run .return_value .returncode = 0
162
+ mock_run .return_value .stdout = (
163
+ "destination;queued;http://example.com:8088;300\n "
164
+ "another;queued;http://other-url.com;1234"
165
+ )
166
+ assert check_queue_size (sc4s_dest_splunk_hec_destinations = {"http://example.com:8088" , "http://another.com:8088" },
167
+ max_queue_size = 1000 ) is False
112
168
113
169
@patch ("subprocess.run" , side_effect = Exception ("some exception" ))
114
170
def test_check_queue_size_exception (mock_run ):
@@ -139,4 +195,26 @@ def test_health_endpoint_no_queue_check(mock_run, client):
139
195
140
196
response = client .get ("/health" )
141
197
assert response .status_code == 200
142
- assert response .json ["status" ] == "healthy"
198
+ assert response .json ["status" ] == "healthy"
199
+
200
+ @patch .dict (
201
+ os .environ ,
202
+ {
203
+ "SC4S_DEST_SPLUNK_HEC_DEFAULT_URL" : "http://my_test_url:1234" ,
204
+ "SC4S_DEST_SPLUNK_HEC_OTHER_URL" : "http://my_hec:1234" ,
205
+ "SOME_OTHER_URL" : "http://my_url/test_url" ,
206
+ "SOME_OTHER_ENV_VARIABLE" : "my_variable" ,
207
+ "SC4S_LISTEN_STATUS_PORT" : "1234" ,
208
+ },
209
+ clear = True
210
+ )
211
+ def test_get_destinations ():
212
+ """
213
+ Check if get_list_of_destinations method parses and returns the expected
214
+ destinations from environment variables.
215
+ """
216
+ destinations = get_list_of_destinations ()
217
+
218
+ assert len (destinations ) == 2
219
+ assert "http://my_test_url:1234" in destinations
220
+ assert "http://my_hec:1234" in destinations
0 commit comments