+ now require runtime tests to generate unittest result that is somewhat fresh

+ add runtime test for webpass needing to be set
This commit is contained in:
Kevin Froman 2020-01-29 22:56:47 -06:00
parent 6624a80c68
commit 8a3f84097a
12 changed files with 115 additions and 17 deletions

View File

@ -2,6 +2,8 @@
Hooks to handle daemon events Hooks to handle daemon events
""" """
from threading import Thread
from .removefrominsertqueue import remove_from_insert_queue from .removefrominsertqueue import remove_from_insert_queue
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
@ -67,10 +69,11 @@ def daemon_event_handlers(shared_state: 'TooMany'):
comm_inst.offlinePeers = [] comm_inst.offlinePeers = []
def test_runtime(): def test_runtime():
comm_inst.shared_state.get_by_string( Thread(target=comm_inst.shared_state.get_by_string(
"OnionrRunTestManager").run_tests() "OnionrRunTestManager").run_tests).start()
events_api.register_listener(remove_from_insert_queue_wrapper) events_api.register_listener(remove_from_insert_queue_wrapper)
events_api.register_listener(print_test) events_api.register_listener(print_test)
events_api.register_listener(upload_event) events_api.register_listener(upload_event)
events_api.register_listener(test_runtime)

View File

@ -134,7 +134,6 @@ def daemon():
events.event('daemon_start') events.event('daemon_start')
communicator.startCommunicator(shared_state) communicator.startCommunicator(shared_state)
localcommand.local_command('shutdown')
if not offline_mode: if not offline_mode:
net.killTor() net.killTor()

View File

@ -25,11 +25,12 @@ def do_runtime_test():
"""Send runtime test daemon queue command.""" """Send runtime test daemon queue command."""
spawn( spawn(
localcommand.local_command, localcommand.local_command,
f'/daemon-event/test_runtime', f'daemon-event/test_runtime',
post=True, post=True,
is_json=True, is_json=True,
postData={} postData={},
).get(10) maxWait=300
).get(300)
do_runtime_test.onionr_help = "If Onionr is running, " # type: ignore do_runtime_test.onionr_help = "If Onionr is running, " # type: ignore

View File

@ -66,6 +66,8 @@ def local_command(command, data='', silent = True, post=False,
if data != '': if data != '':
data = '&data=' + urllib.parse.quote_plus(data) data = '&data=' + urllib.parse.quote_plus(data)
payload = 'http://%s/%s%s' % (hostname, command, data) payload = 'http://%s/%s%s' % (hostname, command, data)
if not config.get('client.webpassword'):
config.reload()
try: try:
if post: if post:
@ -89,5 +91,4 @@ def local_command(command, data='', silent = True, post=False,
if not silent: if not silent:
logger.error('Failed to make local request (command: %s):%s' % (command, error), terminal=True) logger.error('Failed to make local request (command: %s):%s' % (command, error), terminal=True)
ret_data = False ret_data = False
return ret_data return ret_data

View File

@ -1,13 +1,15 @@
""" """Onionr - Private P2P Communication.
Onionr - Private P2P Communication
Test Onionr as it is running Test Onionr as it is running
""" """
import os
import logger import logger
from onionrutils import epoch from onionrutils import epoch
from . import uicheck, inserttest, stresstest from . import uicheck, inserttest, stresstest
from . import ownnode from . import ownnode
from .webpasstest import webpass_test
""" """
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -27,9 +29,12 @@ RUN_TESTS = [uicheck.check_ui,
inserttest.insert_bin_test, inserttest.insert_bin_test,
ownnode.test_tor_adder, ownnode.test_tor_adder,
ownnode.test_own_node, ownnode.test_own_node,
stresstest.stress_test_block_insert stresstest.stress_test_block_insert,
webpass_test
] ]
SUCCESS_FILE = os.path.dirname(os.path.realpath(__file__)) + '/../../tests/runtime-result.txt'
class OnionrRunTestManager: class OnionrRunTestManager:
def __init__(self): def __init__(self):
@ -40,6 +45,11 @@ class OnionrRunTestManager:
cur_time = epoch.get_epoch() cur_time = epoch.get_epoch()
logger.info(f"Doing runtime tests at {cur_time}") logger.info(f"Doing runtime tests at {cur_time}")
try:
os.remove(SUCCESS_FILE)
except FileNotFoundError:
pass
try: try:
for i in RUN_TESTS: for i in RUN_TESTS:
last = i last = i
@ -47,3 +57,9 @@ class OnionrRunTestManager:
logger.info("[RUNTIME TEST] " + last.__name__ + " passed") logger.info("[RUNTIME TEST] " + last.__name__ + " passed")
except (ValueError, AttributeError): except (ValueError, AttributeError):
logger.error(last.__name__ + ' failed') logger.error(last.__name__ + ' failed')
else:
ep = str(epoch.get_epoch())
logger.info(f'All runtime tests passed at {ep}')
with open(SUCCESS_FILE, 'w') as f:
f.write(ep)

View File

@ -29,10 +29,9 @@ def test_own_node(test_manager):
if config.get('general.security_level', 0) > 0: if config.get('general.security_level', 0) > 0:
return return
own_tor_address = gettransports.get()[0] own_tor_address = gettransports.get()[0]
print(socks_port)
if 'this is an onionr node' \ if 'this is an onionr node' \
not in basicrequests.do_get_request(own_tor_address, not in basicrequests.do_get_request('http://' + own_tor_address,
port=socks_port).lower(): port=socks_port, ignoreAPI=True).lower():
logger.warn('Own node not reachable in test') logger.warn('Own node not reachable in test')
raise ValueError raise ValueError
@ -47,7 +46,6 @@ def test_tor_adder(test_manager):
raise ValueError('No Tor node address created yet') raise ValueError('No Tor node address created yet')
if hs not in gettransports.get(): if hs not in gettransports.get():
print(hs in gettransports.get(), 'meme')
logger.error('gettransports Tor not same as file: %s %s' % logger.error('gettransports Tor not same as file: %s %s' %
(hs, gettransports.get())) (hs, gettransports.get()))
raise ValueError('gettransports Tor not same as file') raise ValueError('gettransports Tor not same as file')

View File

@ -5,5 +5,5 @@ def check_ui(test_manager):
result = localcommand.local_command(point) result = localcommand.local_command(point)
if not result: raise ValueError if not result: raise ValueError
result = result.lower() result = result.lower()
if not 'script' in result: if 'script' not in result:
raise ValueError(f'uicheck failed on {point}') raise ValueError(f'uicheck failed on {point}')

View File

@ -0,0 +1,11 @@
import requests
from onionrutils import localcommand
def webpass_test(test_manager):
if requests.get('http://' + localcommand.get_hostname() + '/ping') == \
'pong!':
raise ValueError
if localcommand.local_command('ping') != 'pong!':
raise ValueError('Could not ping with normal localcommand in webpasstest')

View File

@ -5,4 +5,4 @@
<p>Onionr is a decentralized, distributed peer-to-peer data communication network.</p> <p>Onionr is a decentralized, distributed peer-to-peer data communication network.</p>
<p>To learn more about Onionr, see the website at <a href="http://onionr.onionkvc5ibm37bmxwr56bdxcdnb6w3wm4bdghh5qo6f6za7gn7styid.onion ">http://onionr.onionkvc5ibm37bmxwr56bdxcdnb6w3wm4bdghh5qo6f6za7gn7styid.onion</a> or <a href='https://onionr.net/'>Onionr.net</a></p> <p>To learn more about Onionr, see the website at <a href="http://onionrbak72t5zhbzuey2fdkpczlvhowgcpqc6uoyrd3uxztzxwz5cyd.onion/">http://onionrbak72t5zhbzuey2fdkpczlvhowgcpqc6uoyrd3uxztzxwz5cyd.onion/</a> or <a href='https://onionr.net/'>Onionr.net</a></p>

1
tests/runtime-result.txt Normal file
View File

@ -0,0 +1 @@
1580360010

View File

@ -0,0 +1,26 @@
#!/usr/bin/env python3
import sys, os, time, math
sys.path.append(".")
sys.path.append("src/")
import unittest, uuid
import json
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
print("Test directory:", TEST_DIR)
os.environ["ONIONR_HOME"] = TEST_DIR
SUCCESS_FILE = os.path.dirname(__file__) + '/runtime-result.txt'
from utils import identifyhome, createdirs
from onionrsetup import setup_config
createdirs.create_dirs()
setup_config()
class TestRuntimeFile(unittest.TestCase):
def test_runtime_result(self):
self.assertTrue(os.path.exists(SUCCESS_FILE))
with open(SUCCESS_FILE, 'r') as result_file:
self.assertLess(math.floor(time.time()) - int(result_file.read()), 600)
unittest.main()

View File

@ -0,0 +1,42 @@
#!/usr/bin/env python3
import sys, os
import threading
from gevent import sleep
import requests
sys.path.append(".")
sys.path.append("src/")
import unittest, uuid
import json
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
print("Test directory:", TEST_DIR)
os.environ["ONIONR_HOME"] = TEST_DIR
from utils import identifyhome, createdirs
from onionrsetup import setup_config
createdirs.create_dirs()
setup_config()
from onionrcommands import daemonlaunch
from onionrutils import localcommand, getclientapiserver
import config
class TestWebpass(unittest.TestCase):
def test_needs_webpass(self):
config.set('general.use_bootstrap', False)
threading.Thread(target=daemonlaunch.start).start()
while localcommand.local_command('/ping') != 'pong!':
sleep(1)
self.assertNotEqual(
requests.get('http://' + getclientapiserver.get_client_API_server() + '/ping'),
'pong!'
)
while True:
try:
daemonlaunch.kill_daemon()
except KeyError:
sleep(1)
unittest.main()