|
8 | 8 | import shlex |
9 | 9 | import yaml |
10 | 10 | import docker |
| 11 | +from docker.types import Mount |
11 | 12 | import urllib |
12 | 13 | import requests |
13 | 14 | import subprocess |
|
41 | 42 |
|
42 | 43 | global platform |
43 | 44 | platform = "debian-9" |
| 45 | +OLD_SPLUNK_VERSION = "7.2.7" |
| 46 | + |
44 | 47 |
|
45 | 48 | def generate_random_string(): |
46 | 49 | return ''.join(choice(ascii_lowercase) for b in range(20)) |
@@ -239,7 +242,7 @@ def extract_json(self, container_name): |
239 | 242 | except Exception as e: |
240 | 243 | self.logger.error(e) |
241 | 244 | return None |
242 | | - |
| 245 | + |
243 | 246 | def search_internal_distinct_hosts(self, container_id, username="admin", password="password"): |
244 | 247 | query = "search index=_internal earliest=-1m | stats dc(host) as distinct_hosts" |
245 | 248 | splunkd_port = self.client.port(container_id, 8089)[0]["HostPort"] |
@@ -1134,6 +1137,94 @@ def test_adhoc_1so_web_ssl(self): |
1134 | 1137 | except OSError: |
1135 | 1138 | pass |
1136 | 1139 |
|
| 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 | + |
1137 | 1228 | def test_compose_1so_trial(self): |
1138 | 1229 | # Standup deployment |
1139 | 1230 | self.compose_file_name = "1so_trial.yaml" |
@@ -1616,7 +1707,7 @@ def test_compose_1so_hec(self): |
1616 | 1707 | url = "https://localhost:{}/services/collector/event".format(splunk_hec_port) |
1617 | 1708 | kwargs = {"json": {"event": "hello world"}, "verify": False, "headers": {"Authorization": "Splunk abcd1234"}} |
1618 | 1709 | status, content = self.handle_request_retry("POST", url, kwargs) |
1619 | | - assert status == 200 |
| 1710 | + assert status == 200 |
1620 | 1711 |
|
1621 | 1712 | def test_compose_1uf_hec(self): |
1622 | 1713 | # Standup deployment |
|
0 commit comments