Skip to content

Commit e594aeb

Browse files
author
Nelson Wang
committed
Adding test for upgrades from older splunk images
1 parent 83b8759 commit e594aeb

File tree

1 file changed

+93
-2
lines changed

1 file changed

+93
-2
lines changed

tests/test_docker_splunk.py

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import shlex
99
import yaml
1010
import docker
11+
from docker.types import Mount
1112
import urllib
1213
import requests
1314
import subprocess
@@ -41,6 +42,8 @@
4142

4243
global platform
4344
platform = "debian-9"
45+
OLD_SPLUNK_VERSION = "7.2.7"
46+
4447

4548
def generate_random_string():
4649
return ''.join(choice(ascii_lowercase) for b in range(20))
@@ -239,7 +242,7 @@ def extract_json(self, container_name):
239242
except Exception as e:
240243
self.logger.error(e)
241244
return None
242-
245+
243246
def search_internal_distinct_hosts(self, container_id, username="admin", password="password"):
244247
query = "search index=_internal earliest=-1m | stats dc(host) as distinct_hosts"
245248
splunkd_port = self.client.port(container_id, 8089)[0]["HostPort"]
@@ -1134,6 +1137,94 @@ def test_adhoc_1so_web_ssl(self):
11341137
except OSError:
11351138
pass
11361139

1140+
def test_adhoc_1so_upgrade(self):
1141+
# Pull the old image
1142+
for line in self.client.pull("splunk/splunk:{}".format(OLD_SPLUNK_VERSION), stream=True, decode=True):
1143+
continue
1144+
# Create the "splunk-old" container
1145+
try:
1146+
cid = None
1147+
splunk_container_name = generate_random_string()
1148+
password = generate_random_string()
1149+
cid = self.client.create_container("splunk/splunk:{}".format(OLD_SPLUNK_VERSION), tty=True, ports=[8089, 8088], hostname="splunk",
1150+
name=splunk_container_name, environment={"DEBUG": "true", "SPLUNK_HEC_TOKEN": "qwerty", "SPLUNK_PASSWORD": password, "SPLUNK_START_ARGS": "--accept-license"},
1151+
host_config=self.client.create_host_config(mounts=[Mount("/opt/splunk/etc", "opt-splunk-etc"), Mount("/opt/splunk/var", "opt-splunk-var")],
1152+
port_bindings={8089: ("0.0.0.0",), 8088: ("0.0.0.0",)})
1153+
)
1154+
cid = cid.get("Id")
1155+
self.client.start(cid)
1156+
# Poll for the container to be ready
1157+
assert self.wait_for_containers(1, name=splunk_container_name)
1158+
# Check splunkd
1159+
assert self.check_splunkd("admin", password)
1160+
# Add some data via HEC
1161+
splunk_hec_port = self.client.port(cid, 8088)[0]["HostPort"]
1162+
url = "https://localhost:{}/services/collector/event".format(splunk_hec_port)
1163+
kwargs = {"json": {"event": "world never says hello back"}, "verify": False, "headers": {"Authorization": "Splunk qwerty"}}
1164+
status, content = self.handle_request_retry("POST", url, kwargs)
1165+
assert status == 200
1166+
# Remove the "splunk-old" container
1167+
self.client.remove_container(cid, v=False, force=True)
1168+
# Create the "splunk-new" container re-using volumes
1169+
splunk_container_name = generate_random_string()
1170+
cid = self.client.create_container(self.SPLUNK_IMAGE_NAME, tty=True, ports=[8089, 8000], hostname="splunk",
1171+
name=splunk_container_name, environment={"DEBUG": "true", "SPLUNK_HEC_TOKEN": "qwerty", "SPLUNK_PASSWORD": password, "SPLUNK_START_ARGS": "--accept-license"},
1172+
host_config=self.client.create_host_config(mounts=[Mount("/opt/splunk/etc", "opt-splunk-etc"), Mount("/opt/splunk/var", "opt-splunk-var")],
1173+
port_bindings={8089: ("0.0.0.0",), 8000: ("0.0.0.0",)})
1174+
)
1175+
cid = cid.get("Id")
1176+
self.client.start(cid)
1177+
# Poll for the container to be ready
1178+
assert self.wait_for_containers(1, name=splunk_container_name)
1179+
# Check splunkd
1180+
assert self.check_splunkd("admin", password)
1181+
# Run a search - we should be getting 2 hosts because the hostnames were different in the two containers created above
1182+
query = "search index=main earliest=-3m"
1183+
splunkd_port = self.client.port(cid, 8089)[0]["HostPort"]
1184+
url = "https://localhost:{}/services/search/jobs?output_mode=json".format(splunkd_port)
1185+
kwargs = {
1186+
"auth": ("admin", password),
1187+
"data": "search={}".format(urllib.quote_plus(query)),
1188+
"verify": False
1189+
}
1190+
resp = requests.post(url, **kwargs)
1191+
assert resp.status_code == 201
1192+
sid = json.loads(resp.content)["sid"]
1193+
assert sid
1194+
self.logger.info("Search job {} created against on {}".format(sid, cid))
1195+
# Wait for search to finish
1196+
# TODO: implement polling mechanism here
1197+
job_status = None
1198+
for _ in range(10):
1199+
url = "https://localhost:{}/services/search/jobs/{}?output_mode=json".format(splunkd_port, sid)
1200+
kwargs = {"auth": ("admin", password), "verify": False}
1201+
job_status = requests.get(url, **kwargs)
1202+
done = json.loads(job_status.content)["entry"][0]["content"]["isDone"]
1203+
self.logger.info("Search job {} done status is {}".format(sid, done))
1204+
if done:
1205+
break
1206+
time.sleep(3)
1207+
# Check searchProviders - use the latest job_status check from the polling
1208+
assert job_status.status_code == 200
1209+
# Check search results
1210+
url = "https://localhost:{}/services/search/jobs/{}/results?output_mode=json".format(splunkd_port, sid)
1211+
kwargs = {"auth": ("admin", password), "verify": False}
1212+
resp = requests.get(url, **kwargs)
1213+
assert resp.status_code == 200
1214+
results = json.loads(resp.content)["results"]
1215+
assert len(results) == 1
1216+
assert results[0]["_raw"] == "world never says hello back"
1217+
except Exception as e:
1218+
self.logger.error(e)
1219+
raise e
1220+
finally:
1221+
if cid:
1222+
self.client.remove_container(cid, v=True, force=True)
1223+
try:
1224+
os.remove(os.path.join(FIXTURES_DIR, "default.yml"))
1225+
except OSError:
1226+
pass
1227+
11371228
def test_compose_1so_trial(self):
11381229
# Standup deployment
11391230
self.compose_file_name = "1so_trial.yaml"
@@ -1616,7 +1707,7 @@ def test_compose_1so_hec(self):
16161707
url = "https://localhost:{}/services/collector/event".format(splunk_hec_port)
16171708
kwargs = {"json": {"event": "hello world"}, "verify": False, "headers": {"Authorization": "Splunk abcd1234"}}
16181709
status, content = self.handle_request_retry("POST", url, kwargs)
1619-
assert status == 200
1710+
assert status == 200
16201711

16211712
def test_compose_1uf_hec(self):
16221713
# Standup deployment

0 commit comments

Comments
 (0)