@@ -243,8 +243,7 @@ def extract_json(self, container_name):
243243 self .logger .error (e )
244244 return None
245245
246- def search_internal_distinct_hosts (self , container_id , username = "admin" , password = "password" ):
247- query = "search index=_internal earliest=-1m | stats dc(host) as distinct_hosts"
246+ def _run_splunk_query (self , container_id , query , username = "admin" , password = "password" ):
248247 splunkd_port = self .client .port (container_id , 8089 )[0 ]["HostPort" ]
249248 url = "https://localhost:{}/services/search/jobs?output_mode=json" .format (splunkd_port )
250249 kwargs = {
@@ -258,30 +257,43 @@ def search_internal_distinct_hosts(self, container_id, username="admin", passwor
258257 assert sid
259258 self .logger .info ("Search job {} created against on {}" .format (sid , container_id ))
260259 # Wait for search to finish
261- # TODO: implement polling mechanism here
262260 job_status = None
261+ url = "https://localhost:{}/services/search/jobs/{}?output_mode=json" .format (splunkd_port , sid )
262+ kwargs = {
263+ "auth" : (username , password ),
264+ "verify" : False
265+ }
263266 for _ in range (10 ):
264- url = "https://localhost:{}/services/search/jobs/{}?output_mode=json" .format (splunkd_port , sid )
265- kwargs = {"auth" : (username , password ), "verify" : False }
266267 job_status = requests .get (url , ** kwargs )
267268 done = json .loads (job_status .content )["entry" ][0 ]["content" ]["isDone" ]
268269 self .logger .info ("Search job {} done status is {}" .format (sid , done ))
269270 if done :
270271 break
271272 time .sleep (3 )
272- # Check searchProviders - use the latest job_status check from the polling
273- assert job_status .status_code == 200
274- search_providers = json .loads (job_status .content )["entry" ][0 ]["content" ]["searchProviders" ]
275- assert search_providers
273+ assert job_status and job_status .status_code == 200
274+ # Get job metadata
275+ job_metadata = json .loads (job_status .content )
276276 # Check search results
277277 url = "https://localhost:{}/services/search/jobs/{}/results?output_mode=json" .format (splunkd_port , sid )
278- kwargs = {"auth" : (username , password ), "verify" : False }
279- resp = requests .get (url , ** kwargs )
280- assert resp .status_code == 200
281- distinct_hosts = int (json .loads (resp .content )["results" ][0 ]["distinct_hosts" ])
282- assert distinct_hosts
278+ job_results = requests .get (url , ** kwargs )
279+ assert job_results .status_code == 200
280+ job_results = json .loads (job_results .content )
281+ return job_metadata , job_results
282+
283+
284+
285+ #search_providers = json.loads(job_status.content)["entry"][0]["content"]["searchProviders"]
286+ #assert search_providers
287+
288+
289+
290+ def search_internal_distinct_hosts (self , container_id , username = "admin" , password = "password" ):
291+ query = "search index=_internal earliest=-1m | stats dc(host) as distinct_hosts"
292+ meta , results = self ._run_splunk_query (container_id , query , username , password )
293+ search_providers = meta ["entry" ][0 ]["content" ]["searchProviders" ]
294+ distinct_hosts = int (results ["results" ][0 ]["distinct_hosts" ])
283295 return search_providers , distinct_hosts
284-
296+
285297 def check_common_keys (self , log_output , role ):
286298 try :
287299 assert log_output ["all" ]["vars" ]["ansible_ssh_user" ] == "splunk"
@@ -1215,7 +1227,7 @@ def test_adhoc_1so_upgrade(self):
12151227 try :
12161228 cid = None
12171229 splunk_container_name = generate_random_string ()
1218- password = generate_random_string ()
1230+ user , password = "admin" , generate_random_string ()
12191231 cid = self .client .create_container ("splunk/splunk:{}" .format (OLD_SPLUNK_VERSION ), tty = True , ports = [8089 , 8088 ], hostname = "splunk" ,
12201232 name = splunk_container_name , environment = {"DEBUG" : "true" , "SPLUNK_HEC_TOKEN" : "qwerty" , "SPLUNK_PASSWORD" : password , "SPLUNK_START_ARGS" : "--accept-license" },
12211233 host_config = self .client .create_host_config (mounts = [Mount ("/opt/splunk/etc" , "opt-splunk-etc" ), Mount ("/opt/splunk/var" , "opt-splunk-var" )],
@@ -1226,13 +1238,15 @@ def test_adhoc_1so_upgrade(self):
12261238 # Poll for the container to be ready
12271239 assert self .wait_for_containers (1 , name = splunk_container_name )
12281240 # Check splunkd
1229- assert self .check_splunkd ("admin" , password )
1241+ assert self .check_splunkd (user , password )
12301242 # Add some data via HEC
12311243 splunk_hec_port = self .client .port (cid , 8088 )[0 ]["HostPort" ]
12321244 url = "https://localhost:{}/services/collector/event" .format (splunk_hec_port )
12331245 kwargs = {"json" : {"event" : "world never says hello back" }, "verify" : False , "headers" : {"Authorization" : "Splunk qwerty" }}
12341246 status , content = self .handle_request_retry ("POST" , url , kwargs )
12351247 assert status == 200
1248+ # Sleep to let the data index
1249+ time .sleep (3 )
12361250 # Remove the "splunk-old" container
12371251 self .client .remove_container (cid , v = False , force = True )
12381252 # Create the "splunk-new" container re-using volumes
@@ -1247,41 +1261,12 @@ def test_adhoc_1so_upgrade(self):
12471261 # Poll for the container to be ready
12481262 assert self .wait_for_containers (1 , name = splunk_container_name )
12491263 # Check splunkd
1250- assert self .check_splunkd ("admin" , password )
1251- # Run a search - we should be getting 2 hosts because the hostnames were different in the two containers created above
1252- query = "search index=main earliest=-3m"
1253- splunkd_port = self .client .port (cid , 8089 )[0 ]["HostPort" ]
1254- url = "https://localhost:{}/services/search/jobs?output_mode=json" .format (splunkd_port )
1255- kwargs = {
1256- "auth" : ("admin" , password ),
1257- "data" : "search={}" .format (urllib .quote_plus (query )),
1258- "verify" : False
1259- }
1260- resp = requests .post (url , ** kwargs )
1261- assert resp .status_code == 201
1262- sid = json .loads (resp .content )["sid" ]
1263- assert sid
1264- self .logger .info ("Search job {} created against on {}" .format (sid , cid ))
1265- # Wait for search to finish
1266- # TODO: implement polling mechanism here
1267- job_status = None
1268- for _ in range (10 ):
1269- url = "https://localhost:{}/services/search/jobs/{}?output_mode=json" .format (splunkd_port , sid )
1270- kwargs = {"auth" : ("admin" , password ), "verify" : False }
1271- job_status = requests .get (url , ** kwargs )
1272- done = json .loads (job_status .content )["entry" ][0 ]["content" ]["isDone" ]
1273- self .logger .info ("Search job {} done status is {}" .format (sid , done ))
1274- if done :
1275- break
1276- time .sleep (3 )
1277- # Check searchProviders - use the latest job_status check from the polling
1278- assert job_status .status_code == 200
1279- # Check search results
1280- url = "https://localhost:{}/services/search/jobs/{}/results?output_mode=json" .format (splunkd_port , sid )
1281- kwargs = {"auth" : ("admin" , password ), "verify" : False }
1282- resp = requests .get (url , ** kwargs )
1283- assert resp .status_code == 200
1284- results = json .loads (resp .content )["results" ]
1264+ assert self .check_splunkd (user , password )
1265+ # Run a search
1266+ time .sleep (3 )
1267+ query = "search index=main earliest=-10m"
1268+ meta , results = self ._run_splunk_query (cid , query , user , password )
1269+ results = results ["results" ]
12851270 assert len (results ) == 1
12861271 assert results [0 ]["_raw" ] == "world never says hello back"
12871272 except Exception as e :
0 commit comments