diff --git a/modules/spidey_test.py b/modules/spidey_test.py index 53212ea..f06b058 100644 --- a/modules/spidey_test.py +++ b/modules/spidey_test.py @@ -4,19 +4,11 @@ from modules import test_template class SpideyTest(test_template.TestTemplate): - def __init__(self, args, agnpath, test_data, queue): + def __init__(self, args, agnpath, test_data, queue, queue_lock): """ Simple test runner, for testing the detection of Spidey viruses. - :param args: List of arguments - :type args: argparse.Namespace - :param agnpath: The path to the discord folder - :type agnpath: agnostic_path.AgnosticPath - :param test_data: The data to be tested - :type test_data: str - :param queue: The queue to put the status in - :type queue: queue.Queue """ - super().__init__(args, agnpath, test_data, queue) + super().__init__(args, agnpath, test_data, queue, queue_lock) def slowdown(self): """ @@ -60,10 +52,7 @@ class SpideyTest(test_template.TestTemplate): "do also consider a clean windows reinstall.") print("Spidey test finished") - self.set_status("success") - status = self.get_status() - self.queue.append(status) - return self.get_status_code() + self.finish("success") def get_path(self): """ diff --git a/modules/test_interface.py b/modules/test_interface.py index 3c27eaf..dca8d48 100644 --- a/modules/test_interface.py +++ b/modules/test_interface.py @@ -16,6 +16,7 @@ _TEST_MODULES = { "test_walk": ["test_template", "TestWalkTemplate"], } POST_RUN_ALLOWED = False +queue_lock = threading.Lock() class TestRunner: @@ -79,7 +80,7 @@ class TestRunner: for module_name, test_class in self.loaded_test_classes.items(): for i in range(10): print(f"Starting test {module_name}") - tc = test_class(self.args, self.agnostic_path, None, self.finished_tests) + tc = test_class(self.args, self.agnostic_path, None, self.finished_tests, queue_lock) tc = threading.Thread(target=tc.run_test, args=()) tc.name = str(module_name) tc.daemon = True @@ -163,6 +164,7 @@ class TestRunner: :return: The status :rtype: str """ + queue_lock.acquire() nb_tests = len(self.list_of_tests) nb_tests_success = 0 nb_tests_failure = 0 @@ -177,7 +179,7 @@ class TestRunner: nb_tests_skipped += 1 elif test.status == "problem": nb_tests_failure += 1 - + queue_lock.release() nb_tests_ran = nb_tests_success + nb_tests_failure + nb_tests_skipped nb_test_running = nb_tests - nb_tests_ran return global_status( diff --git a/modules/test_template.py b/modules/test_template.py index 748e43b..9d80065 100644 --- a/modules/test_template.py +++ b/modules/test_template.py @@ -4,9 +4,14 @@ A global template for all tests. from modules import internal_io +class TestDataError(Exception): + """ + Raised when the data is not valid. + """ + pass class TestTemplate: - def __init__(self, args, agnpath, test_data, queue): + def __init__(self, args, agnpath, test_data, queue, queue_lock): """ Simple test template. :param args: List of arguments @@ -15,6 +20,10 @@ class TestTemplate: :type agnpath: agnostic_path.AgnosticPath :param test_data: The data to be tested :type test_data: str + :param queue: The queue to put the test status in + :type queue: queue.deque + :param queue_lock: The lock to use when putting the test status in the queue + :type queue_lock: threading.Lock """ self.ptb = args.ptb self.agnostic_path = agnpath @@ -26,6 +35,7 @@ class TestTemplate: self.status = "Test not yet initialized." self.queue = queue self.set_status("idle") + self.queue_lock = queue_lock def run_test(self): """ @@ -82,9 +92,23 @@ class TestTemplate: self.status = self.get_status_from_code(statuscode) return self.status_code + def finish(self, statuscode: str = None, message: str = None): + """ + Finishes the test. + """ + if message is not None: + self.status = message + if statuscode is not None: + self.status_code = statuscode + + self.queue_lock.acquire() + self.queue.append(self.get_status()) + self.queue_lock.release() + return self.get_status_code() + class TestWalkTemplate(TestTemplate): - def __init__(self, args, agnpath, test_data, queue): + def __init__(self, args, agnpath, test_data, queue, queue_lock): """ Simple test template. :param args: List of arguments @@ -94,17 +118,22 @@ class TestWalkTemplate(TestTemplate): :param test_data: The data to be tested :type test_data: str """ - super().__init__(args, agnpath, test_data, queue) + super().__init__(args, agnpath, test_data, queue, queue_lock) + self.data = {} if test_data is None: self.set_status("problem", "No test data given.") def run_test(self): + + self.finish() + return if self.test_data is None: self.set_status("problem", "No test data given.") return self.get_status() self.set_status("running", "The test will begin shortly, please wait...") for path in self.walk(): self.compare(path, "test", lambda x: True, [], {}) + self.set_status("sucess", "The test was a success but kinda not a success.") def walk(self): """ @@ -128,12 +157,19 @@ class TestWalkTemplate(TestTemplate): :param kwargs: Keyword arguments to use on the function :type kwargs: dict """ - if path not in self.test_data["files"]: - raise ValueError("No entry in test data for path: " + str(path)) - if entry_name not in self.test_data["files"][path]: - raise ValueError("No entry in test data for path: " + str(path) + " and entry name: " + str(entry_name)) - data_origin = self.test_data["files"][path][entry_name] - data_result = function(path, *args, **kwargs) - if data_origin != data_result: - return False - return True + + try: + self.data[path] = function(path, *args, **kwargs) + if path not in self.test_data["files"]: + raise TestDataError("No entry in test data for path: " + str(path)) + if entry_name not in self.test_data["files"][path]: + raise TestDataError("No entry in test data for path: " + str(path) + " and entry name: " + str(entry_name)) + data_origin = self.test_data["files"][path][entry_name] + self.data[path] = function(path, *args, **kwargs) + if data_origin != self.data[path]: + return False + return True + except TestDataError as e: + if not self.args.continue_on_error: + self.set_status("error", "Error: " + str(e)) + raise e