added cleanup test and test-re-runner automation script

master
Kevin Froman 2020-03-09 21:43:18 -05:00
parent 4e053f1b30
commit a8290e2ac3
7 changed files with 90 additions and 10 deletions

10
.vscode/tasks.json vendored
View File

@ -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",

View File

@ -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
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

View File

@ -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:"))

View File

@ -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')

View File

@ -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 <https://www.gnu.org/licenses/>.
"""
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)

View File

@ -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):
'''

40
tests/test_cleanup.py Normal file
View File

@ -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()