1 | import logging, time, os, sys |
---|
2 | import utils_c4 |
---|
3 | import config_c4 as config |
---|
4 | from c4_run import fileMetadata, dummy, main |
---|
5 | from xceptions import * |
---|
6 | |
---|
7 | from file_utils import installedSupportedNetcdf |
---|
8 | |
---|
9 | #### set up log file #### |
---|
10 | tstring2 = '%4.4i%2.2i%2.2i' % time.gmtime()[0:3] |
---|
11 | testLogFile = '%s__qclog_%s.txt' % ('unitTestsS2',tstring2) |
---|
12 | log = logging.getLogger(testLogFile) |
---|
13 | fHdlr = logging.FileHandler(testLogFile,mode='w') |
---|
14 | fileFormatter = logging.Formatter('%(message)s') |
---|
15 | fHdlr.setFormatter(fileFormatter) |
---|
16 | log.addHandler(fHdlr) |
---|
17 | log.setLevel(logging.INFO) |
---|
18 | |
---|
19 | try: |
---|
20 | fmd = fileMetadata(dummy=True) |
---|
21 | fmd.loadNc( '/dummyPath/v1_day_a_b_1990-1991.nc') |
---|
22 | except: |
---|
23 | print 'Failed to parse a simple dummy file path' |
---|
24 | raise baseException( 'Failed to parse a simple dummy file path' ) |
---|
25 | print 'OK: instantiated fileMetaData and parsed a simple dummy path' |
---|
26 | |
---|
27 | p = dummy() |
---|
28 | p.log = log |
---|
29 | p.abortMessageCount = -1 |
---|
30 | p.pcfg = config.projectConfig( "__dummy" ) |
---|
31 | |
---|
32 | |
---|
33 | module = 'checkFileName' |
---|
34 | c = utils_c4.checkFileName(parent=p) |
---|
35 | |
---|
36 | fn = 'v1_t1_a_b_20060101-20101231.nc' |
---|
37 | testId = '#10.001' |
---|
38 | c.check( fn ) |
---|
39 | if c.errorCount == 0: |
---|
40 | print 'OK: [%s] %s: valid file name with project=__dummy' % (module,fn) |
---|
41 | else: |
---|
42 | print 'Failed [%s] %s: valid file name' % (module,fn) |
---|
43 | |
---|
44 | if sys.version_info >= (2,7): |
---|
45 | ## monitoting file handles uses a "subprocess" method which is not available in python 2.6 |
---|
46 | testId = '#11.001' |
---|
47 | try: |
---|
48 | m = main( args=['-p', '__dummy'], monitorFileHandles=True ) |
---|
49 | print 'OK: [%s]: dummy run completed without exception' % testId |
---|
50 | except: |
---|
51 | print 'Failed [%s]: dummy run triggered exception' % testId |
---|
52 | raise |
---|
53 | raise baseException( 'Failed [%s]: dummy run triggered exception' % testId ) |
---|
54 | |
---|
55 | testId = '#11.002' |
---|
56 | if m.monitor.fhCountMax < 15: |
---|
57 | print 'OK: [%s]: fhCountMax = %s' % ( testId, m.monitor.fhCountMax ) |
---|
58 | else: |
---|
59 | print 'Failed [%s]: fhCountMax = %s' % ( testId, m.monitor.fhCountMax ) |
---|
60 | |
---|
61 | testId = '#11.003' |
---|
62 | try: |
---|
63 | m = main( args=['-p', '__dummy'], abortMessageCount=10 ) |
---|
64 | print 'Failed [%s]: did not trigger exception' % testId |
---|
65 | except: |
---|
66 | print 'OK: [%s]: attempt to trigger exception successful' % testId |
---|
67 | |
---|
68 | |
---|
69 | extras = [ |
---|
70 | ( '/data/work/cmip5/output1/pr_20110323/pr_3hr_HadGEM2-ES_historical_r2i1p1_196001010130-196412302230.nc', 'CMIP5', 0 ), |
---|
71 | ('/data/work/cmip5/output1/pr_20110323/pr_3hr_HadGEM2-ES_historical_r2i1p1_200001010130-200412302230.nc', 'CMIP5', 0 ) ] |
---|
72 | |
---|
73 | |
---|
74 | kt = 0 |
---|
75 | for e in extras: |
---|
76 | kt += 1 |
---|
77 | if os.path.isfile( e[0] ): |
---|
78 | if 'cdms2' in installedSupportedNetcdf: |
---|
79 | testId = '#20.%3.3i' % kt |
---|
80 | m = main( args=['-p', e[1], '-f', e[0], '--force-cdms2','--ld', 'ld_test1' ], abortMessageCount=10 ) |
---|
81 | if m.ok: |
---|
82 | print 'OK: [%s]: successfully checked test file with cdms2' % testId |
---|
83 | else: |
---|
84 | print 'Failed [%s]: incorrect test results' % testId |
---|
85 | |
---|
86 | testId = '#21.%3.3i' % kt |
---|
87 | m = main( args=['-p', e[1], '-f', e[0], '--force-ncq','--ld', 'ld_test2' ], abortMessageCount=10 ) |
---|
88 | if m.ok: |
---|
89 | print 'OK: [%s]: successfully checked test file with ncq3' % testId |
---|
90 | else: |
---|
91 | print 'Failed [%s]: incorrect test results' % testId |
---|
92 | |
---|
93 | if 'netCDF4' in installedSupportedNetcdf: |
---|
94 | testId = '#22.%3.3i' % kt |
---|
95 | m = main( args=['-p', e[1], '-f', e[0], '--force-pync4','--ld', 'ld_test3' ], abortMessageCount=10 ) |
---|
96 | if m.ok: |
---|
97 | print 'OK: [%s]: successfully checked test file with python NetCDF4' % testId |
---|
98 | else: |
---|
99 | print 'Failed [%s]: incorrect test results' % testId |
---|
100 | |
---|
101 | if 'Scientific' in installedSupportedNetcdf: |
---|
102 | testId = '#23.%3.3i' % kt |
---|
103 | m = main( args=['-p', e[1], '-f', e[0], '--force-scientific','--ld', 'ld_test4' ], abortMessageCount=10 ) |
---|
104 | if m.ok: |
---|
105 | print 'OK: [%s]: successfully checked test file with python Scientific' % testId |
---|
106 | else: |
---|
107 | print 'Failed [%s]: incorrect test results' % testId |
---|
108 | |
---|
109 | |
---|