2020-03-19 04:13:56 +05:30
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import shutil
|
|
|
|
import logging
|
|
|
|
import logging.config
|
2012-02-16 02:24:14 +05:30
|
|
|
from optparse import OptionParser
|
2021-01-15 01:42:29 +05:30
|
|
|
from pathlib import Path
|
2011-12-15 20:23:10 +05:30
|
|
|
|
2020-03-19 04:13:56 +05:30
|
|
|
import damask
|
|
|
|
|
|
|
|
class Test:
|
2016-03-04 22:23:55 +05:30
|
|
|
"""
|
|
|
|
General class for testing.
|
|
|
|
|
|
|
|
Is sub-classed by the individual tests.
|
|
|
|
"""
|
2012-01-18 15:04:49 +05:30
|
|
|
|
|
|
|
variants = []
|
2020-03-19 04:13:56 +05:30
|
|
|
|
2016-09-05 04:16:53 +05:30
|
|
|
def __init__(self, **kwargs):
|
2019-11-22 17:46:08 +05:30
|
|
|
"""New test."""
|
2016-09-05 04:16:53 +05:30
|
|
|
defaults = {'description': '',
|
2016-09-11 00:14:46 +05:30
|
|
|
'keep': False,
|
|
|
|
'accept': False,
|
|
|
|
'updateRequest': False,
|
2016-09-20 10:40:07 +05:30
|
|
|
'show': False,
|
|
|
|
'select': None,
|
2016-09-05 04:16:53 +05:30
|
|
|
}
|
|
|
|
for arg in defaults.keys():
|
|
|
|
setattr(self,arg,kwargs.get(arg) if kwargs.get(arg) else defaults[arg])
|
2020-03-19 04:13:56 +05:30
|
|
|
|
2016-09-11 00:14:46 +05:30
|
|
|
fh = logging.FileHandler('test.log') # create file handler which logs even debug messages
|
2014-06-04 21:04:35 +05:30
|
|
|
fh.setLevel(logging.DEBUG)
|
2016-09-02 01:07:49 +05:30
|
|
|
fh.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s: \n%(message)s'))
|
|
|
|
|
2016-09-11 00:14:46 +05:30
|
|
|
ch = logging.StreamHandler(stream=sys.stdout) # create console handler with a higher log level
|
2014-06-04 21:04:35 +05:30
|
|
|
ch.setLevel(logging.INFO)
|
2016-09-02 01:07:49 +05:30
|
|
|
ch.setFormatter(logging.Formatter('%(message)s'))
|
|
|
|
|
|
|
|
logger = logging.getLogger()
|
2014-06-04 21:04:35 +05:30
|
|
|
logger.addHandler(fh)
|
|
|
|
logger.addHandler(ch)
|
2016-09-02 01:07:49 +05:30
|
|
|
logger.setLevel(0)
|
2014-06-04 21:04:35 +05:30
|
|
|
|
2016-09-02 01:07:49 +05:30
|
|
|
logging.info('\n'.join(['+'*40,
|
|
|
|
'-'*40,
|
2016-09-05 04:16:53 +05:30
|
|
|
'| '+self.description,
|
2016-09-02 01:07:49 +05:30
|
|
|
'-'*40,
|
|
|
|
]))
|
2014-06-04 21:04:35 +05:30
|
|
|
|
2014-06-03 16:00:51 +05:30
|
|
|
self.dirBase = os.path.dirname(os.path.realpath(sys.modules[self.__class__.__module__].__file__))
|
2014-06-11 23:16:26 +05:30
|
|
|
|
2021-03-27 01:26:55 +05:30
|
|
|
self.parser = OptionParser(description = f'{self.description} (Test class version: {damask.version})',
|
2016-09-02 01:07:49 +05:30
|
|
|
usage = './test.py [options]')
|
2016-09-05 04:16:53 +05:30
|
|
|
self.parser.add_option("-k", "--keep",
|
2016-09-02 01:07:49 +05:30
|
|
|
action = "store_true",
|
2016-09-05 04:16:53 +05:30
|
|
|
dest = "keep",
|
|
|
|
help = "keep current results, just run postprocessing")
|
|
|
|
self.parser.add_option("--ok", "--accept",
|
2016-09-02 01:07:49 +05:30
|
|
|
action = "store_true",
|
|
|
|
dest = "accept",
|
2016-12-01 04:48:03 +05:30
|
|
|
help = "calculate results but always consider test as successful")
|
2016-09-20 10:40:07 +05:30
|
|
|
self.parser.add_option("-l", "--list",
|
|
|
|
action = "store_true",
|
|
|
|
dest = "show",
|
2016-12-01 04:48:03 +05:30
|
|
|
help = "show all test variants without actual calculation")
|
2016-09-20 10:40:07 +05:30
|
|
|
self.parser.add_option("-s", "--select",
|
|
|
|
dest = "select",
|
2018-01-29 07:24:25 +05:30
|
|
|
help = "run test(s) of given name only")
|
2016-09-05 04:16:53 +05:30
|
|
|
self.parser.set_defaults(keep = self.keep,
|
|
|
|
accept = self.accept,
|
2016-09-11 00:14:46 +05:30
|
|
|
update = self.updateRequest,
|
2016-09-20 10:40:07 +05:30
|
|
|
show = self.show,
|
|
|
|
select = self.select,
|
2016-09-02 01:07:49 +05:30
|
|
|
)
|
2014-06-11 23:16:26 +05:30
|
|
|
|
2020-03-19 04:13:56 +05:30
|
|
|
|
2017-04-30 22:45:53 +05:30
|
|
|
def variantName(self,variant):
|
|
|
|
"""Generate name of (numerical) variant."""
|
|
|
|
return str(variant)
|
|
|
|
|
2014-06-03 18:39:52 +05:30
|
|
|
def execute(self):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Run all variants and report first failure."""
|
2016-09-05 04:16:53 +05:30
|
|
|
if not self.options.keep:
|
2015-12-15 20:00:17 +05:30
|
|
|
self.clean()
|
|
|
|
self.prepareAll()
|
2016-09-02 01:07:49 +05:30
|
|
|
|
2017-05-01 04:37:49 +05:30
|
|
|
for variant,object in enumerate(self.variants):
|
|
|
|
name = self.variantName(variant)
|
2016-09-20 10:40:07 +05:30
|
|
|
if self.options.show:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'{variant+1}: {name}')
|
2016-12-01 04:48:03 +05:30
|
|
|
elif self.options.select is not None \
|
2018-01-29 07:24:25 +05:30
|
|
|
and not (name in self.options.select or str(variant+1) in self.options.select):
|
2016-09-20 10:40:07 +05:30
|
|
|
pass
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
if not self.options.keep:
|
|
|
|
self.prepare(variant)
|
|
|
|
self.run(variant)
|
|
|
|
|
|
|
|
self.postprocess(variant)
|
2020-03-19 04:13:56 +05:30
|
|
|
|
2016-09-20 10:40:07 +05:30
|
|
|
if self.options.update:
|
2020-06-25 01:04:51 +05:30
|
|
|
if self.update(variant) != 0: logging.critical(f'update for "{name}" failed.')
|
2016-09-20 10:40:07 +05:30
|
|
|
elif not (self.options.accept or self.compare(variant)): # no update, do comparison
|
|
|
|
return variant+1 # return culprit
|
|
|
|
|
2016-12-01 04:48:03 +05:30
|
|
|
except Exception as e:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'exception during variant execution: "{e}"')
|
2018-01-30 07:57:05 +05:30
|
|
|
return variant+1 # return culprit
|
2016-09-05 04:16:53 +05:30
|
|
|
return 0
|
2020-03-19 04:13:56 +05:30
|
|
|
|
2012-01-18 15:04:49 +05:30
|
|
|
def clean(self):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Delete directory tree containing current results."""
|
2012-01-18 15:04:49 +05:30
|
|
|
try:
|
|
|
|
shutil.rmtree(self.dirCurrent())
|
2019-11-22 17:46:08 +05:30
|
|
|
except FileNotFoundError:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.warning(f'removal of directory "{self.dirCurrent()}" not possible...')
|
2012-01-18 15:04:49 +05:30
|
|
|
|
|
|
|
try:
|
|
|
|
os.mkdir(self.dirCurrent())
|
2016-09-05 04:16:53 +05:30
|
|
|
return True
|
2019-11-22 17:46:08 +05:30
|
|
|
except FileExistsError:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'creation of directory "{self.dirCurrent()}" failed.')
|
2016-09-05 04:16:53 +05:30
|
|
|
return False
|
2020-03-19 04:13:56 +05:30
|
|
|
|
2012-12-12 22:40:04 +05:30
|
|
|
def prepareAll(self):
|
2019-11-22 17:46:08 +05:30
|
|
|
"""Do all necessary preparations for the whole test."""
|
2012-12-12 22:40:04 +05:30
|
|
|
return True
|
2012-01-18 15:04:49 +05:30
|
|
|
|
|
|
|
def prepare(self,variant):
|
2019-11-22 17:46:08 +05:30
|
|
|
"""Do all necessary preparations for the run of each test variant."""
|
2012-01-18 15:04:49 +05:30
|
|
|
return True
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-01-18 15:04:49 +05:30
|
|
|
|
|
|
|
def run(self,variant):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Execute the requested test variant."""
|
2012-01-18 15:04:49 +05:30
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def postprocess(self,variant):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Perform post-processing of generated results for this test variant."""
|
2012-01-18 15:04:49 +05:30
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def compare(self,variant):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Compare reference to current results."""
|
2012-01-18 15:04:49 +05:30
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def update(self,variant):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Update reference with current results."""
|
2016-09-05 04:16:53 +05:30
|
|
|
logging.critical('update not supported.')
|
2016-09-11 00:14:46 +05:30
|
|
|
return 1
|
2012-01-18 15:04:49 +05:30
|
|
|
|
|
|
|
|
|
|
|
def dirReference(self):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Directory containing reference results of the test."""
|
2012-01-18 15:04:49 +05:30
|
|
|
return os.path.normpath(os.path.join(self.dirBase,'reference/'))
|
|
|
|
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2012-01-18 15:04:49 +05:30
|
|
|
def dirCurrent(self):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Directory containing current results of the test."""
|
2012-01-18 15:04:49 +05:30
|
|
|
return os.path.normpath(os.path.join(self.dirBase,'current/'))
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2015-12-04 03:22:03 +05:30
|
|
|
def fileInRoot(self,dir,file):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Path to a file in the root directory of DAMASK."""
|
2021-01-15 01:42:29 +05:30
|
|
|
return str(Path(os.environ['DAMASK_ROOT'])/dir/file)
|
2015-12-04 03:22:03 +05:30
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-01-18 15:04:49 +05:30
|
|
|
def fileInReference(self,file):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Path to a file in the refrence directory for the test."""
|
2012-01-18 15:04:49 +05:30
|
|
|
return os.path.join(self.dirReference(),file)
|
|
|
|
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2012-01-18 15:04:49 +05:30
|
|
|
def fileInCurrent(self,file):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""Path to a file in the current results directory for the test."""
|
2012-01-18 15:04:49 +05:30
|
|
|
return os.path.join(self.dirCurrent(),file)
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2015-12-04 03:22:03 +05:30
|
|
|
def copy(self, mapA, mapB,
|
|
|
|
A = [], B = []):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""
|
2016-10-25 00:46:29 +05:30
|
|
|
Copy list of files from (mapped) source to target.
|
2016-03-04 22:23:55 +05:30
|
|
|
|
2015-12-04 03:22:03 +05:30
|
|
|
mapA/B is one of self.fileInX.
|
2016-03-04 22:23:55 +05:30
|
|
|
"""
|
2015-12-04 03:22:03 +05:30
|
|
|
if not B or len(B) == 0: B = A
|
|
|
|
|
2016-09-11 22:33:32 +05:30
|
|
|
for source,target in zip(list(map(mapA,A)),list(map(mapB,B))):
|
2015-12-04 03:22:03 +05:30
|
|
|
try:
|
2016-09-03 00:00:08 +05:30
|
|
|
shutil.copy2(source,target)
|
2019-11-22 17:46:08 +05:30
|
|
|
except FileNotFoundError:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'error copying {source} to {target}')
|
2019-11-22 17:46:08 +05:30
|
|
|
raise FileNotFoundError
|
2015-12-04 03:22:03 +05:30
|
|
|
|
|
|
|
|
2012-07-17 18:34:57 +05:30
|
|
|
def copy_Reference2Current(self,sourcefiles=[],targetfiles=[]):
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-07-17 18:34:57 +05:30
|
|
|
if len(targetfiles) == 0: targetfiles = sourcefiles
|
2017-03-05 23:34:06 +05:30
|
|
|
for i,f in enumerate(sourcefiles):
|
2012-02-23 23:14:09 +05:30
|
|
|
try:
|
2017-03-05 23:34:06 +05:30
|
|
|
shutil.copy2(self.fileInReference(f),self.fileInCurrent(targetfiles[i]))
|
2019-11-22 17:46:08 +05:30
|
|
|
except FileNotFoundError:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'Reference2Current: Unable to copy file "{f}"')
|
2019-11-22 17:46:08 +05:30
|
|
|
raise FileNotFoundError
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2013-03-26 17:36:19 +05:30
|
|
|
def copy_Base2Current(self,sourceDir,sourcefiles=[],targetfiles=[]):
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2020-09-15 10:28:06 +05:30
|
|
|
source = os.path.normpath(os.path.join(self.dirBase,'../../..',sourceDir))
|
2013-03-26 17:36:19 +05:30
|
|
|
if len(targetfiles) == 0: targetfiles = sourcefiles
|
2017-03-05 23:34:06 +05:30
|
|
|
for i,f in enumerate(sourcefiles):
|
2013-03-26 17:36:19 +05:30
|
|
|
try:
|
2017-03-05 23:34:06 +05:30
|
|
|
shutil.copy2(os.path.join(source,f),self.fileInCurrent(targetfiles[i]))
|
2019-11-22 17:46:08 +05:30
|
|
|
except FileNotFoundError:
|
2017-03-05 23:34:06 +05:30
|
|
|
logging.error(os.path.join(source,f))
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'Base2Current: Unable to copy file "{f}"')
|
2019-11-22 17:46:08 +05:30
|
|
|
raise FileNotFoundError
|
2013-03-26 17:36:19 +05:30
|
|
|
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2012-07-17 18:34:57 +05:30
|
|
|
def copy_Current2Reference(self,sourcefiles=[],targetfiles=[]):
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-07-17 18:34:57 +05:30
|
|
|
if len(targetfiles) == 0: targetfiles = sourcefiles
|
2017-03-05 23:34:06 +05:30
|
|
|
for i,f in enumerate(sourcefiles):
|
2012-07-17 18:34:57 +05:30
|
|
|
try:
|
2017-03-05 23:34:06 +05:30
|
|
|
shutil.copy2(self.fileInCurrent(f),self.fileInReference(targetfiles[i]))
|
2019-11-22 17:46:08 +05:30
|
|
|
except FileNotFoundError:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'Current2Reference: Unable to copy file "{f}"')
|
2019-11-22 17:46:08 +05:30
|
|
|
raise FileNotFoundError
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-07-17 18:34:57 +05:30
|
|
|
def copy_Current2Current(self,sourcefiles=[],targetfiles=[]):
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2017-03-05 23:34:06 +05:30
|
|
|
for i,f in enumerate(sourcefiles):
|
2012-02-23 23:14:09 +05:30
|
|
|
try:
|
2017-03-05 23:34:06 +05:30
|
|
|
shutil.copy2(self.fileInReference(f),self.fileInCurrent(targetfiles[i]))
|
2019-11-22 17:46:08 +05:30
|
|
|
except FileNotFoundError:
|
2020-06-25 01:04:51 +05:30
|
|
|
logging.critical(f'Current2Current: Unable to copy file "{f}"')
|
2019-11-22 17:46:08 +05:30
|
|
|
raise FileNotFoundError
|
|
|
|
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2021-03-27 12:05:49 +05:30
|
|
|
def execute_inCurrentDir(self,cmd,env=None):
|
2014-10-03 02:57:03 +05:30
|
|
|
|
2014-06-04 21:04:35 +05:30
|
|
|
logging.info(cmd)
|
2021-03-27 12:05:49 +05:30
|
|
|
out,error = damask.util.execute(cmd,self.dirCurrent())
|
2014-06-03 18:39:52 +05:30
|
|
|
|
2014-10-03 02:57:03 +05:30
|
|
|
logging.info(error)
|
|
|
|
logging.debug(out)
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2014-10-03 02:57:03 +05:30
|
|
|
return out,error
|
|
|
|
|
2016-09-03 00:00:08 +05:30
|
|
|
|
2012-01-31 18:40:14 +05:30
|
|
|
def report_Success(self,culprit):
|
2016-09-03 00:00:08 +05:30
|
|
|
|
|
|
|
ret = culprit
|
|
|
|
|
2013-03-05 01:09:13 +05:30
|
|
|
if culprit == 0:
|
2018-01-29 07:24:25 +05:30
|
|
|
count = len(self.variants) if self.options.select is None else len(self.options.select)
|
2020-09-15 10:28:06 +05:30
|
|
|
msg = ('Test passed.' if count == 1 else f'All {count} tests passed.') + '\a\a\a'
|
2016-09-03 00:00:08 +05:30
|
|
|
elif culprit == -1:
|
2018-01-29 07:24:25 +05:30
|
|
|
msg = 'Warning: could not start test...'
|
2016-09-03 00:00:08 +05:30
|
|
|
ret = 0
|
2012-01-27 15:25:19 +05:30
|
|
|
else:
|
2020-06-25 01:04:51 +05:30
|
|
|
msg = f'Test "{self.variantName(culprit-1)}" failed.'
|
2016-09-03 00:00:08 +05:30
|
|
|
|
|
|
|
logging.critical('\n'.join(['*'*40,msg,'*'*40]) + '\n')
|
|
|
|
return ret
|