1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
#! @PYTHON@
import subprocess
import sys
import os
import glob
from collections import defaultdict
#DOCUMENTATION/TUTORIAL @ https://gitlab.dkrz.de/mpim-sw/cdo/-/wikis/Writing-CDO-tests/edit
BUILD_DIR = "@abs_top_builddir@"
CDO="@abs_top_builddir@/src/cdo $CDO_DEBUG"
DATAPATH="@abs_top_srcdir@/test/data"
lineLength = 80
"""
Overview:
print_flush
cdo_check_req
class TAPTest => group of commands
class TestModule => group of TAPTests
"""
formats= {"srv" : "SERVICE",
"ext" : "EXTRA",
"ieg" : "IEG",
"grb" : "GRIB",
"grb2" : "GRIB_API",
"nc" : "netCDF",
"nc2" : "netCDF2",
"nc4" : "netCDF4",
"nc5" : "netCDF5",
"nczarr" : "netCDF zarr"
}
formats = defaultdict(lambda: "undefined", formats)
def fileformat(p_format):
return (cdo_check_req("has-{}".format(p_format)), formats[p_format])
def print_flush(*args):
"""
print_flush function that uses options flush=True.
Required for the correct order of output
"""
print(*args)
sys.stdout.flush()
def print_seperator(symbol,length,spacing=0):
section = symbol + (spacing *" ")
repititions = int(length / (spacing + len(symbol)))
print_flush((repititions * section).strip())
def run_command(commands):
call_status = -999
with subprocess.Popen(
commands, shell=True, universal_newlines=True
) as proc:
proc.wait()
call_status = proc.returncode
return call_status
def clean(file_or_wildcard):
files = glob.glob(file_or_wildcard)
if(len(files) == 0):
print_flush("WARNING: no files were cleaned ")
for g in files:
if(DATAPATH in g):
print_flush("ERROR, trying to clean up a file from the test data repository")
exit(-1)
if os.path.exists(g):
os.remove(g)
def cdo_check_req(req):
"""
Checks the given requirement (e.g HAS_THREADS) and returns the output
of the cdo --config has-<name> options with given requirement
"""
call = "{CDO} --config {req}".format(CDO=CDO, req=req.lower())
with subprocess.Popen(
[call], shell=True, universal_newlines=True, stdout=subprocess.PIPE
) as proc:
proc.wait()
out, _ = proc.communicate()
return True if "yes" in out else False
#-------------------------------------------------------------------
class TAPTest:
def __init__(self, msg=""):
self.commands = []
self.skip = False
self.message = msg
self.cleanFiles = []
self.expectedRetVal= defaultdict(lambda: 0, [])
def skip_test(self,msg):
self.skip = True
self.message = msg
def execute_commands(self):
for c in self.commands:
wrap_call = True
if any(command in c for command in ["cmp","diff"]):
print_flush("Starting Check: ", c)
warp_call = False
else:
print_flush("Starting Cdo Call: ", c)
wrap_with = os.getenv("CDO_TEST_PREPEND")
if(wrap_call and wrap_with is not None):
call_status = run_command(wrap_with + " " + c)
else:
call_status = run_command(c)
if call_status != self.expectedRetVal[c]:
print_flush("ERROR: Unexpected return value: ", call_status, "expected:", self.expectedRetVal[c])
print_flush("--- FAILURE ---")
return -1;
print_seperator('-',lineLength,3)
return 0
def go(self):
if(self.skip):
return 77
print_seperator('-',lineLength,3)
status = self.execute_commands()
if(status != -1):
print_flush("+++ SUCCESS +++")
self.cleanUp()
return status;
def cleanUp(self):
for f in self.cleanFiles:
clean(f)
def diff(self,a,b):
self.add("{} diff {} {}".format(CDO,a,b))
def clean(self,*args):
for a in args:
self.cleanFiles.append(a)
def add(self, command,expectedRetVal=0):
self.commands.append(command)
self.expectedRetVal[command]=expectedRetVal
#-------------------------------------------------------------------
class TestModule:
def __init__(self):
self.cleanFiles = []
self.commands = []
self.testID = -1;
def cleanUp(self):
for f in self.cleanFiles:
clean(f)
def __get_num_test(self):
num = len([c for c in self.commands if c[1] is True])
if(num == 0):
print_flush("ERROR NO TESTS EXECUTED")
exit(-1)
return num
def add(self, test):
self.commands.append((test,True))
def add_skip(self,msg):
t=TAPTest()
t.skip_test(msg)
self.commands.append((t,True))
def print(self):
for t in self.commands:
for c in t[0].commands:
print_flush(c)
print_flush()
def prepare(self,command):
self.commands.append((command,False))
def clean(self,*args):
for a in args:
self.cleanFiles.append(a)
def __prepare_data(self,c):
print_flush("Preparing data:",c)
status = run_command(c)
if(status != 0):
print_flush("ERROR: Fail in test preperation")
for t in self.commands:
print_flush("not ok: test not executed due to error in test preperation")
exit(-1)
def __run_check(self,t):
print_flush("Running test: %i" % self.testID)
test_status = t.go()
if(test_status == 0):
print_flush(f'ok: {t.message}')
return 0
elif(test_status == 77):
print_flush("ok: # SKIP: {}".format(t.message))
return 77;
else:
print_flush("not ok: {}".format(t.message))
return -1;
print_seperator('-',lineLength)
self.testID += 1
print_flush()
def run(self):
print_flush("1..%s" % self.__get_num_test())
print_seperator('=',lineLength)
retval = 0
self.testID = 1
for c in self.commands:
if(c[1] == False):
self.__prepare_data(c[0])
else:
retval += abs(self.__run_check(c[0]))
self.cleanUp()
exit(0) # important to exit here: tap tests/ctest need this return value
__all__ = ["cdo_check_req","TAPTest","TestModule","print_flush","DATAPATH","CDO","BUILD_DIR","fileformat"]
def main():
testMod = TestModule()
t = TAPTest("check if cdoTest works")
t.add(f'{CDO} --operators')
testMod.add(t)
t = TAPTest("check if cdoTest works error is returned")
t.add(f'{CDO} -add adasd', 1)
testMod.add(t)
retval = testMod.run()
return retval
if __name__ == '__main__':
sys.exit(main()) # next section explains the use of sys.exit
|