2018-04-16 20:19:30 +02:00
|
|
|
"""
|
|
|
|
Common reusable code for tests and tests for pybitmessage process.
|
|
|
|
"""
|
|
|
|
|
2018-04-12 16:29:22 +02:00
|
|
|
import os
|
|
|
|
import signal
|
2018-05-08 16:39:07 +02:00
|
|
|
import subprocess # nosec
|
2018-04-12 16:29:22 +02:00
|
|
|
import tempfile
|
2018-04-16 09:00:23 +02:00
|
|
|
import time
|
2018-04-16 20:19:30 +02:00
|
|
|
import unittest
|
2018-04-12 16:29:22 +02:00
|
|
|
|
|
|
|
import psutil
|
|
|
|
|
|
|
|
|
2018-04-16 09:00:23 +02:00
|
|
|
def put_signal_file(path, filename):
|
2018-04-16 20:19:30 +02:00
|
|
|
"""Creates file, presence of which is a signal about some event."""
|
2018-04-16 09:00:23 +02:00
|
|
|
with open(os.path.join(path, filename), 'wb') as outfile:
|
|
|
|
outfile.write(str(time.time()))
|
|
|
|
|
|
|
|
|
2018-04-12 16:29:22 +02:00
|
|
|
class TestProcessProto(unittest.TestCase):
|
2018-04-16 20:19:30 +02:00
|
|
|
"""Test case implementing common logic for external testing:
|
|
|
|
it starts pybitmessage in setUpClass() and stops it in tearDownClass()
|
|
|
|
"""
|
2018-04-12 16:29:22 +02:00
|
|
|
_process_cmd = ['pybitmessage', '-d']
|
2018-12-18 22:47:34 +01:00
|
|
|
_threads_count = 15
|
2018-04-16 09:00:23 +02:00
|
|
|
_files = (
|
|
|
|
'keys.dat', 'debug.log', 'messages.dat', 'knownnodes.dat',
|
|
|
|
'.api_started', 'unittest.lock'
|
|
|
|
)
|
2018-04-12 16:29:22 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2018-04-16 20:19:30 +02:00
|
|
|
"""Setup environment and start pybitmessage"""
|
2018-04-12 16:29:22 +02:00
|
|
|
cls.home = os.environ['BITMESSAGE_HOME'] = tempfile.gettempdir()
|
2018-04-16 09:00:23 +02:00
|
|
|
put_signal_file(cls.home, 'unittest.lock')
|
2018-05-08 16:39:07 +02:00
|
|
|
subprocess.call(cls._process_cmd) # nosec
|
2018-04-16 09:00:23 +02:00
|
|
|
time.sleep(5)
|
2018-04-12 16:29:22 +02:00
|
|
|
cls.pid = int(cls._get_readline('singleton.lock'))
|
|
|
|
cls.process = psutil.Process(cls.pid)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _get_readline(cls, pfile):
|
|
|
|
pfile = os.path.join(cls.home, pfile)
|
|
|
|
try:
|
|
|
|
return open(pfile, 'rb').readline().strip()
|
|
|
|
except (OSError, IOError):
|
|
|
|
pass
|
|
|
|
|
2019-03-13 16:56:26 +01:00
|
|
|
@classmethod
|
|
|
|
def _stop_process(cls, timeout=5):
|
|
|
|
cls.process.send_signal(signal.SIGTERM)
|
|
|
|
try:
|
|
|
|
cls.process.wait(timeout)
|
|
|
|
except psutil.TimeoutExpired:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2018-04-12 16:29:22 +02:00
|
|
|
@classmethod
|
|
|
|
def _cleanup_files(cls):
|
|
|
|
for pfile in cls._files:
|
|
|
|
try:
|
|
|
|
os.remove(os.path.join(cls.home, pfile))
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2018-04-16 20:19:30 +02:00
|
|
|
"""Ensures that pybitmessage stopped and removes files"""
|
2018-04-12 16:29:22 +02:00
|
|
|
try:
|
2019-03-13 16:56:26 +01:00
|
|
|
if not cls._stop_process():
|
|
|
|
print(open(os.path.join(cls.home, 'debug.log'), 'rb').read())
|
|
|
|
cls.process.kill()
|
|
|
|
except psutil.NoSuchProcess:
|
|
|
|
pass
|
2018-04-12 16:29:22 +02:00
|
|
|
finally:
|
|
|
|
cls._cleanup_files()
|
|
|
|
|
|
|
|
def _test_threads(self):
|
|
|
|
# only count for now
|
|
|
|
# because of https://github.com/giampaolo/psutil/issues/613
|
|
|
|
# PyBitmessage
|
|
|
|
# - addressGenerator
|
|
|
|
# - singleWorker
|
|
|
|
# - SQL
|
|
|
|
# - objectProcessor
|
|
|
|
# - singleCleaner
|
|
|
|
# - singleAPI
|
|
|
|
# - Asyncore
|
|
|
|
# - ReceiveQueue_0
|
|
|
|
# - ReceiveQueue_1
|
|
|
|
# - ReceiveQueue_2
|
|
|
|
# - Announcer
|
|
|
|
# - InvBroadcaster
|
|
|
|
# - AddrBroadcaster
|
|
|
|
# - Downloader
|
|
|
|
self.assertEqual(
|
|
|
|
len(self.process.threads()), self._threads_count)
|
|
|
|
|
|
|
|
|
2019-03-07 11:18:45 +01:00
|
|
|
class TestProcessShutdown(TestProcessProto):
|
|
|
|
"""Separate test case for SIGTERM"""
|
|
|
|
def test_shutdown(self):
|
|
|
|
"""Send to pybitmessage SIGTERM and ensure it stopped"""
|
2019-03-13 16:56:26 +01:00
|
|
|
# longer wait time because it's not a benchmark
|
|
|
|
self.assertTrue(
|
|
|
|
self._stop_process(20),
|
|
|
|
'%s has not stopped in 20 sec' % ' '.join(self._process_cmd))
|
2019-03-07 11:18:45 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
"""Special teardown because pybitmessage is already stopped"""
|
|
|
|
cls._cleanup_files()
|
|
|
|
|
|
|
|
|
2018-04-12 16:29:22 +02:00
|
|
|
class TestProcess(TestProcessProto):
|
2018-04-16 20:19:30 +02:00
|
|
|
"""A test case for pybitmessage process"""
|
2018-04-12 16:29:22 +02:00
|
|
|
def test_process_name(self):
|
|
|
|
"""Check PyBitmessage process name"""
|
|
|
|
self.assertEqual(self.process.name(), 'PyBitmessage')
|
|
|
|
|
|
|
|
def test_files(self):
|
|
|
|
"""Check existence of PyBitmessage files"""
|
|
|
|
for pfile in self._files:
|
2018-04-16 09:00:23 +02:00
|
|
|
if pfile.startswith('.'):
|
|
|
|
continue
|
2018-04-12 16:29:22 +02:00
|
|
|
self.assertIsNot(
|
|
|
|
self._get_readline(pfile), None,
|
|
|
|
'Failed to read file %s' % pfile
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_threads(self):
|
|
|
|
"""Testing PyBitmessage threads"""
|
|
|
|
self._test_threads()
|