Commit 1de34e89 authored by Michael Salim's avatar Michael Salim

Custom Balsam testing framework: because Django tests do not play well with concurrency

parent e4ea2070
......@@ -44,7 +44,7 @@ def run(job):
with cd(job.workdir) as _, open(outname, 'wb') as outf:
try:
status_msg(job.id, "RUNNING", msg="executing from mpi_ensemble")
proc = Popen(job.cmd, stdout=outf, stderr=STDOUT)
proc = Popen(job.cmd, stdout=outf, stderr=STDOUT, cwd=job.workdir)
retcode = proc.wait()
except Exception as e:
status_msg(job.id, "FAILED", msg=str(e))
......
import sys
import os
import django
import tempfile
import unittest
tempdir = tempfile.TemporaryDirectory()
os.environ['BALSAM_TEST_DIRECTORY'] = tempdir.name
os.environ['BALSAM_TEST']='1'
os.environ['DJANGO_SETTINGS_MODULE'] = 'argobalsam.settings'
django.setup()
if __name__ == "__main__":
loader = unittest.defaultTestLoader
if len(sys.argv) > 1:
names = sys.argv[1:]
suite = loader.loadTestsFromNames(names)
else:
suite = loader.discover('tests')
unittest.TextTestRunner(verbosity=2).run(suite)
import os
import unittest
from django.core.management import call_command
from django import db
from django.conf import settings
class BalsamTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
call_command('makemigrations',interactive=False,verbosity=0)
call_command('migrate',interactive=False,verbosity=0)
assert os.path.exists(settings.DATABASES['default']['NAME'])
@classmethod
def tearDownClass(cls):
pass
def setUp(self):
pass # to be implemented by test cases
def tearDown(self):
if not db.connection.settings_dict['NAME'].endswith('test_db.sqlite3'):
raise RuntimeError("Test DB not configured")
call_command('flush',interactive=False,verbosity=0)
from tests.BalsamTestCase import BalsamTestCase
import subprocess
def cmdline(cmd):
'''Return string output from a command line'''
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return p.communicate()[0].decode('utf-8')
class BalsamLauncherTests(BalsamTestCase):
def test_whatever(self):
stdout = cmdline('balsam ls')
self.assertIn('No jobs found matching query', stdout)
from django.test import TestCase
from tests.BalsamTestCase import BalsamTestCase
from balsam.models import BalsamJob, InvalidStateError
class BalsamJobDBTests(TestCase):
class BalsamJobDBTests(BalsamTestCase):
'''Exercise direct manipulation of BalsamJob database'''
def test_basic_addition(self):
""" A job is added to the Balsam Job database.
Updating state causes version number to change."""
jobs = BalsamJob.objects.all()
self.assertQuerysetEqual(jobs, [])
self.assertEqual(list(jobs), [])
newjob = BalsamJob()
version_old = newjob.version
newjob.update_state('PREPROCESSING', 'using pre.py')
newjob.update_state('PREPROCESSED', 'using pre.py')
version_new = newjob.version
self.assertNotEqual(version_old, version_new)
def test_first_save(self):
'''The first and second Job DB saves don't cause problems'''
newjob = BalsamJob()
newjob.save()
newjob.nodes = 10
......@@ -43,6 +44,7 @@ class BalsamJobDBTests(TestCase):
job1.save(update_fields=['description'])
def test_get_set_parents(self):
'''Can add and retreive parents from jobs'''
job1, job2, job3 = (BalsamJob() for i in range(3))
job1.name = "parent1"
job1.save(update_fields=['name'])
......
from tests.BalsamTestCase import BalsamTestCase
from balsam.models import BalsamJob
from django import db
class DumbTestCase(BalsamTestCase):
def setUp(self):
BalsamJob.objects.create(name="hello_testing!")
assert db.connection.settings_dict['NAME'].endswith('test_db.sqlite3')
def test_can_read(self):
job = BalsamJob.objects.get(name__icontains="testing!")
self.assertEqual(job.name, "hello_testing!")
import os,sys,logging
import socket
from django.core.management import call_command
logger = logging.getLogger('console')
try:
INSTALL_PATH = os.environ['ARGOBALSAM_INSTALL_PATH']
DATA_PATH = os.path.join(INSTALL_PATH,'data')
ALLOWED_EXE_PATH = os.path.join(INSTALL_PATH,'exe')
INSTALL_PATH = os.environ['ARGOBALSAM_INSTALL_PATH']
except KeyError as e:
logger.error('Environment not setup: ' + str(e))
raise
#------------------------------
# DATABASE CONFIG INFO
#------------------------------
......@@ -31,10 +31,18 @@ if USING_DB_LOGIN:
default_db['USER'] = DBUSER
default_db['PASSWORD'] = DBPASS
DATABASES = {
'default': default_db,
'OPTIONS' : {'timeout':500000.0,}
}
testing = os.environ.get('BALSAM_TEST', '')
if testing:
INSTALL_PATH = os.environ['BALSAM_TEST_DIRECTORY']
db_test = default_db.copy()
db_test['NAME'] = os.path.join(INSTALL_PATH, 'test_db.sqlite3')
DATABASES = {'default':db_test}
else:
DATABASES = {'default':default_db}
DATA_PATH = os.path.join(INSTALL_PATH,'data')
ALLOWED_EXE_PATH = os.path.join(INSTALL_PATH,'exe')
#------------------------------
# BALSAM CONFIG INFO
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment