Skip to content

Commit

Permalink
Throw specific errors instead of generic exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jefflester committed Nov 24, 2023
1 parent d17d9e8 commit f95cb5f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/test/src/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ def start_docker_daemon():
elif "linux" in sys.platform.lower():
return_code = subprocess.call("sudo service docker start", shell=True)
else:
raise Exception(f"Incompatible testing platform: {sys.platform}")
raise RuntimeError(f"Incompatible testing platform: {sys.platform}")
if return_code != 0:
raise Exception("Failed to start Docker daemon.")
raise RuntimeError("Failed to start Docker daemon.")

counter = 0
while counter < 61:
if counter == 61:
raise Exception("Docker daemon failed to start after one minute.")
raise TimeoutError("Docker daemon failed to start after one minute.")
try:
docker_client = docker.from_env()
docker_client.ping()
Expand All @@ -85,9 +85,9 @@ def stop_docker_daemon():
"sudo service docker stop; sudo systemctl stop docker.socket", shell=True
)
else:
raise Exception(f"Incompatible testing platform: {sys.platform}")
raise RuntimeError(f"Incompatible testing platform: {sys.platform}")
if return_code != 0:
raise Exception("Failed to stop Docker daemon.")
raise RuntimeError("Failed to stop Docker daemon.")

# Hard wait for daemon to stop
sleep(3)
25 changes: 14 additions & 11 deletions src/test/src/lib/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,25 @@ def test_query(self, json_data):
row_count = json_data.get("successCriteria", {}).get("rowCount", None)

if not contains and row_count is None:
raise Exception(
raise KeyError(
"'contains' and/or 'rowCount' must be supplied in query success criteria."
)

# Wait for server to become available
i = 0
cmd = "curl -X GET -H 'Accept: application/json' -H 'X-Trino-User: admin' 'localhost:8080/v1/info/'"
while i <= 30:
while i <= 60:
output = execute_command(cmd, "trino")
if '"starting":false' in output.get("output", ""):
time.sleep(5) # hard stop to ensure coordinator is ready
break
else:
elif i < 60:
time.sleep(1)
i += 1
else:
raise TimeoutError(
"Timed out waiting for coordinator to become available"
)

# Build command
sql = json_data.get("sql", "")
Expand Down Expand Up @@ -106,7 +110,7 @@ def test_shell(self, json_data):
exit_code = json_data.get("successCriteria", {}).get("exitCode", None)

if not contains and exit_code is None:
raise Exception(
raise KeyError(
"'contains' and/or 'exitCode' must be supplied in shell success criteria."
)

Expand All @@ -132,9 +136,7 @@ def test_shell(self, json_data):
time.sleep(1)
pass
else:
assert c in output.get(
"output", ""
), f"'{c}' not in healthcheck output"
raise TimeoutError(f"'{c}' not in healthcheck output")

# Run command
cmd = json_data.get("command", "")
Expand All @@ -143,9 +145,10 @@ def test_shell(self, json_data):

# Run assertions
if exit_code is not None:
assert exit_code == output.get(
"return_code"
), f"Unexpected return code: {output.get('return_code')} expected: {exit_code}"
cmd_exit_code = output.get("return_code")
assert (
exit_code == cmd_exit_code
), f"Unexpected return code: {cmd_exit_code} expected: {exit_code}"

for c in contains:
assert c in output.get("output"), f"'{c}' not in command output"
Expand Down Expand Up @@ -173,7 +176,7 @@ def test_logs(self, json_data):
time.sleep(1)
i += 1
else:
assert c in logs, f"'{c}' not found in container log output"
raise TimeoutError(f"'{c}' not found in container log output")

def validate(self, json_data={}):
"""Validates JSON input."""
Expand Down

0 comments on commit f95cb5f

Please sign in to comment.