mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-06-04 05:10:40 +00:00
improve api tests and error output
show only relevant aptly logs if a test fails. for async tasks, show task output, as it contains the error message.
This commit is contained in:
+46
-1
@@ -5,6 +5,7 @@ import random
|
||||
import shutil
|
||||
import string
|
||||
import time
|
||||
import tempfile
|
||||
|
||||
from lib import BaseTest
|
||||
|
||||
@@ -18,11 +19,32 @@ TASK_SUCCEEDED = 2
|
||||
TASK_FAILED = 3
|
||||
|
||||
|
||||
class AptlyStream:
|
||||
def __init__(self):
|
||||
self.tmp_file = tempfile.NamedTemporaryFile(delete=False)
|
||||
self.read_pos = 0
|
||||
|
||||
def fileno(self):
|
||||
return self.tmp_file.fileno()
|
||||
|
||||
def get_contents(self):
|
||||
self.tmp_file.seek(self.read_pos, 0)
|
||||
return self.tmp_file.read().decode("utf-8")
|
||||
|
||||
def close(self):
|
||||
self.tmp_file.close()
|
||||
|
||||
def clear(self):
|
||||
self.read_pos = self.tmp_file.tell()
|
||||
|
||||
|
||||
class APITest(BaseTest):
|
||||
"""
|
||||
BaseTest + testing aptly API
|
||||
"""
|
||||
aptly_server = None
|
||||
aptly_out = None
|
||||
debugOutput = True
|
||||
base_url = "127.0.0.1:8765"
|
||||
configOverride = {
|
||||
"FileSystemPublishEndpoints": {
|
||||
@@ -40,13 +62,20 @@ class APITest(BaseTest):
|
||||
if APITest.aptly_server is None:
|
||||
super(APITest, self).prepare()
|
||||
|
||||
APITest.aptly_out = AptlyStream()
|
||||
|
||||
configPath = os.path.join(os.environ["HOME"], self.aptlyConfigFile)
|
||||
APITest.aptly_server = self._start_process(f"aptly api serve -no-lock -config={configPath} -listen={self.base_url}",)
|
||||
APITest.aptly_server = self._start_process(f"aptly api serve -no-lock -config={configPath} -listen={self.base_url}", stdout=APITest.aptly_out, stderr=APITest.aptly_out)
|
||||
time.sleep(1)
|
||||
else:
|
||||
APITest.aptly_out.clear()
|
||||
|
||||
if os.path.exists(os.path.join(os.environ["HOME"], self.aptlyDir, "upload")):
|
||||
shutil.rmtree(os.path.join(os.environ["HOME"], self.aptlyDir, "upload"))
|
||||
|
||||
def debug_output(self):
|
||||
return APITest.aptly_out.get_contents()
|
||||
|
||||
def run(self):
|
||||
pass
|
||||
|
||||
@@ -79,6 +108,20 @@ class APITest(BaseTest):
|
||||
|
||||
return self.get("/api/tasks/" + str(_id))
|
||||
|
||||
def check_task(self, task):
|
||||
self.check_equal(task.status_code, 200)
|
||||
if task.json()['State'] != TASK_SUCCEEDED:
|
||||
resp2 = self.get("/api/tasks/" + str(task.json()['ID']) + "/output")
|
||||
raise Exception(f"task failed: {resp2.text}")
|
||||
|
||||
def check_task_fail(self, task, expected_output=None):
|
||||
self.check_equal(task.status_code, 200)
|
||||
if task.json()['State'] == TASK_SUCCEEDED:
|
||||
raise Exception("task expected to fail")
|
||||
if expected_output:
|
||||
resp = self.get("/api/tasks/" + str(task.json()['ID']) + "/output")
|
||||
self.check_equal(resp.text, expected_output)
|
||||
|
||||
def put(self, uri, *args, **kwargs):
|
||||
if "json" in kwargs:
|
||||
kwargs["data"] = json.dumps(kwargs.pop("json"))
|
||||
@@ -142,6 +185,8 @@ class APITest(BaseTest):
|
||||
cls.aptly_server.terminate()
|
||||
cls.aptly_server.wait()
|
||||
cls.aptly_server = None
|
||||
if APITest.aptly_out is not None:
|
||||
APITest.aptly_out.close()
|
||||
|
||||
def random_name(self):
|
||||
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(15))
|
||||
|
||||
Reference in New Issue
Block a user