From a8290e2ac3adf828ddd44a5a71245f3714dcbe4a Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Mon, 9 Mar 2020 21:43:18 -0500 Subject: [PATCH] added cleanup test and test-re-runner automation script --- .vscode/tasks.json | 10 ++++++++ scripts/README.md | 3 ++- scripts/enable-dev-config.py | 2 ++ scripts/run-unit-test-by-name.py | 20 ++++++++++++++++ src/etc/cleanup/__init__.py | 23 +++++++++++------- src/onionrplugins/onionrevents.py | 2 +- tests/test_cleanup.py | 40 +++++++++++++++++++++++++++++++ 7 files changed, 90 insertions(+), 10 deletions(-) create mode 100755 scripts/run-unit-test-by-name.py create mode 100644 tests/test_cleanup.py diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 8f7f8ca5..8d91b679 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -23,6 +23,16 @@ "panel": "new" } }, + { + "label": "Run test by name", + "type": "shell", + "command": "source venv/bin/activate; scripts/run-unit-test-by-name.py", + "group": "test", + "presentation": { + "reveal": "always", + "panel": "new" + } + }, { "label": "Enable dev config", "type": "process", diff --git a/scripts/README.md b/scripts/README.md index a91e9c63..8175b75c 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -3,4 +3,5 @@ This directory contains useful scripts and utilities that don't make sense to in passphrase-generator.py: very simple utility to generate and print a strong passphrase to stdout. 256 bits of entropy by default. enable-dev-config.py/disable-dev-config.py: enable/disable dev default config setup block-spammer.py: attack tool for spamming blocks -announce-attack.py: flood a node with false nodes \ No newline at end of file +announce-attack.py: flood a node with false nodes +run-unit-test-by-name: runs a unit test (no browser, runtime or intgegration test) by name \ No newline at end of file diff --git a/scripts/enable-dev-config.py b/scripts/enable-dev-config.py index 8fd3be7f..7a156f5c 100755 --- a/scripts/enable-dev-config.py +++ b/scripts/enable-dev-config.py @@ -4,6 +4,8 @@ import json +input("enter to continue") # hack to avoid vscode term input + conf = json.load(open('static-data/default_config.json', 'r')) block_pow = int(input("Block POW level:")) diff --git a/scripts/run-unit-test-by-name.py b/scripts/run-unit-test-by-name.py new file mode 100755 index 00000000..95826ba8 --- /dev/null +++ b/scripts/run-unit-test-by-name.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +import os +from time import sleep + +input("enter to continue") # hack to avoid vscode term input + +test = input("test file name:") +if not test.endswith('.py'): + test += '.py' + +if os.path.exists(f'tests/{test}'): + try: + while True: + os.system(f'python3 tests/{test}') + if input("Enter to run again or n to stop:").lower().strip() == 'n': + break + except KeyboardInterrupt: + pass +else: + print('No test found') diff --git a/src/etc/cleanup/__init__.py b/src/etc/cleanup/__init__.py index 1a5b651b..ac6bab3f 100644 --- a/src/etc/cleanup/__init__.py +++ b/src/etc/cleanup/__init__.py @@ -1,8 +1,10 @@ -""" - Onionr - Private P2P Communication +"""Onionr - Private P2P Communication. - cleanup files +cleanup run files """ +import os + +import filepaths """ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,7 +19,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . """ -import os, filepaths + def _safe_remove(path): try: @@ -25,8 +27,13 @@ def _safe_remove(path): except FileNotFoundError: pass + def delete_run_files(): - _safe_remove(filepaths.public_API_host_file) - _safe_remove(filepaths.private_API_host_file) - _safe_remove(filepaths.daemon_mark_file) - _safe_remove(filepaths.lock_file) + """Delete run files, do not error if not found. + + Test: test_cleanup.py + """ + _safe_remove(filepaths.public_API_host_file) + _safe_remove(filepaths.private_API_host_file) + _safe_remove(filepaths.daemon_mark_file) + _safe_remove(filepaths.lock_file) diff --git a/src/onionrplugins/onionrevents.py b/src/onionrplugins/onionrevents.py index d07656cf..b0bba935 100755 --- a/src/onionrplugins/onionrevents.py +++ b/src/onionrplugins/onionrevents.py @@ -42,7 +42,7 @@ def __event_caller(event_name, data = {}): plugins.disable(plugin, stop_event = False) except Exception as e: logger.warn('Event "%s" failed for plugin "%s".' % (event_name, plugin), terminal=True) - logger.debug(str(e), terminal=True) + logger.debug((event_name + ' - ' + plugin + ' - ' + str(e)), terminal=True) def event(event_name, data = {}, threaded = True): ''' diff --git a/tests/test_cleanup.py b/tests/test_cleanup.py new file mode 100644 index 00000000..9df2c182 --- /dev/null +++ b/tests/test_cleanup.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +import sys, os +sys.path.append(".") +sys.path.append("src/") +import uuid +TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/' +print("Test directory:", TEST_DIR) +os.environ["ONIONR_HOME"] = TEST_DIR +import unittest, json + +from utils import identifyhome, createdirs +createdirs.create_dirs() +from etc.cleanup import delete_run_files +import filepaths + + +run_file_paths = [ + filepaths.public_API_host_file, + filepaths.private_API_host_file, + filepaths.daemon_mark_file, + filepaths.lock_file] + +def _run_paths_exist(): + for f in run_file_paths: + if not os.path.exists(f): + return False + return True + +class TestDeleteRunFiles(unittest.TestCase): + def test_delete_run_files(self): + for x in run_file_paths: + with open(x, 'w') as f: + f.write("") + self.assertTrue(_run_paths_exist()) + delete_run_files() + self.assertFalse(_run_paths_exist()) + + + +unittest.main()