1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | """ |
---|
4 | @author: morton |
---|
5 | |
---|
6 | Don Morton |
---|
7 | Boreal Scientific Computing LLC, Fairbanks, Alaska, USA |
---|
8 | Don.Morton@borealscicomp.com |
---|
9 | http://www.borealscicomp.com/ |
---|
10 | |
---|
11 | |
---|
12 | @contributors |
---|
13 | |
---|
14 | Christian Maurer |
---|
15 | ZAMG, Vienna, Austria |
---|
16 | christian.maurer@zamg.ac.at |
---|
17 | Delia Arnold |
---|
18 | ZAMG, Vienna, Austria |
---|
19 | delia.arnold-arias@zamg.ac.at |
---|
20 | |
---|
21 | """ |
---|
22 | |
---|
23 | |
---|
24 | |
---|
25 | #import tempfile |
---|
26 | import argparse |
---|
27 | import os |
---|
28 | import uuid |
---|
29 | import sys |
---|
30 | import shutil |
---|
31 | |
---|
32 | import distrotest.TestSuite as TS |
---|
33 | |
---|
34 | import flextest.FlexpartCase as FlexpartCase |
---|
35 | import flextest.FlexpartExecutable as Fexec |
---|
36 | import flextest.FlexpartErrors as FlexpartErrors |
---|
37 | import flextest.flexread.FlexpartOutput as FlexpartOutput |
---|
38 | import flextest.OutputCompare as OutputCompare |
---|
39 | |
---|
40 | # Set up for argument parsing |
---|
41 | |
---|
42 | # This used for checking presence of any files listed in arguments |
---|
43 | def is_valid_file(parser, arg): |
---|
44 | if not os.path.exists(arg): |
---|
45 | parser.error("File %s not found" % arg) |
---|
46 | else: |
---|
47 | return arg |
---|
48 | |
---|
49 | |
---|
50 | cmdline_makefile_path = None # Initial value for the makefile from command line |
---|
51 | |
---|
52 | parser = argparse.ArgumentParser() |
---|
53 | |
---|
54 | parser.add_argument("xml_file", help="Full path to XML spec file", |
---|
55 | action="store") |
---|
56 | |
---|
57 | parser.add_argument("-m", "--makefile", |
---|
58 | help="Full path to makefile", |
---|
59 | action="store", dest="cmdline_makefile_path") |
---|
60 | |
---|
61 | parser.add_argument("-k", "--keeptemp", |
---|
62 | help="Keep temporary directories", |
---|
63 | action="store_true") |
---|
64 | |
---|
65 | args = parser.parse_args() |
---|
66 | |
---|
67 | #print 'args.xml_file: ', args.xml_file |
---|
68 | #print 'args.cmdline_makefile_path: ', args.cmdline_makefile_path |
---|
69 | |
---|
70 | # Get the command line arguments after parsing. Some might be "None" |
---|
71 | xml_file = args.xml_file |
---|
72 | cmdline_makefile_path = args.cmdline_makefile_path |
---|
73 | |
---|
74 | # By default, temp directories will be cleaned up unless test fails. We can choose |
---|
75 | # to keep these with the appropriate command-line flag -k or --keeptemp |
---|
76 | if args.keeptemp: |
---|
77 | clean_up = False |
---|
78 | print 'Will retain temporary directories' |
---|
79 | else: |
---|
80 | clean_up = True |
---|
81 | print 'Will delete temporary directories if test is successful' |
---|
82 | #sys.exit() |
---|
83 | |
---|
84 | # Bring in the XML filename from the command line |
---|
85 | |
---|
86 | XML_FILE = [xml_file] |
---|
87 | |
---|
88 | |
---|
89 | |
---|
90 | # This is where lots of temporary directories are going to be created. |
---|
91 | # User needs to make sure it is OK to put them in here. Note that there |
---|
92 | # is currently no clean up of these directories if a test fails. |
---|
93 | # The logic is that users |
---|
94 | # might want to go back and look at the test directories, so auto-cleanup |
---|
95 | # prevents that. |
---|
96 | SCRATCH_DIR = '/tmp' |
---|
97 | |
---|
98 | |
---|
99 | t = TS.TestSuite(xml_files=XML_FILE) |
---|
100 | |
---|
101 | distro_list = t.get_distribution_list() |
---|
102 | |
---|
103 | the_distro = distro_list[0] |
---|
104 | |
---|
105 | ''' |
---|
106 | print the_distro.get_descr() |
---|
107 | print the_distro.get_distro_path() |
---|
108 | print the_distro.get_makefile_path() |
---|
109 | print the_distro.get_parmod_path() |
---|
110 | ''' |
---|
111 | |
---|
112 | # Create a temporary directory for compiling the distribution |
---|
113 | distro_destdir_name = SCRATCH_DIR + '/distrotest_' + str(uuid.uuid4()) |
---|
114 | print 'distro_destdir_name: ' + distro_destdir_name |
---|
115 | |
---|
116 | |
---|
117 | good_init = False |
---|
118 | compile_success = False |
---|
119 | all_success = False |
---|
120 | try: |
---|
121 | srcdir = os.path.realpath(the_distro.get_distro_path()) |
---|
122 | |
---|
123 | # The makefile path will come from command line, if available, |
---|
124 | if cmdline_makefile_path: |
---|
125 | makefile = cmdline_makefile_path |
---|
126 | else: |
---|
127 | print 'WARNING... makefile path not specified' |
---|
128 | |
---|
129 | print 'using makefile: ' + str(makefile) |
---|
130 | |
---|
131 | parmodfile = os.path.realpath(the_distro.get_parmod_path()) |
---|
132 | exec_name = the_distro.get_exec_name() |
---|
133 | exec_obj = Fexec.FlexpartExecutable(srcdir=srcdir, |
---|
134 | destdir=distro_destdir_name, |
---|
135 | makefile=makefile, |
---|
136 | parmodfile=parmodfile, |
---|
137 | executable_name=exec_name) |
---|
138 | good_init = True |
---|
139 | except Exception as e: |
---|
140 | print 'Bad instantiation: ' + str(e) |
---|
141 | pass |
---|
142 | |
---|
143 | if good_init: |
---|
144 | print 'Executable exists: ' + str(exec_obj.executable_exists()) |
---|
145 | |
---|
146 | |
---|
147 | |
---|
148 | # Try to compile it |
---|
149 | print; print '============================'; print |
---|
150 | print 'compile test...' |
---|
151 | print 'Compile directory: ' + distro_destdir_name |
---|
152 | compile_success = exec_obj.compile_it() |
---|
153 | |
---|
154 | print 'compile_success: ' + str(compile_success) |
---|
155 | print 'Executable exists: ' + str(exec_obj.executable_exists()) |
---|
156 | |
---|
157 | if compile_success: |
---|
158 | flexpart_executable = distro_destdir_name + '/' + exec_name |
---|
159 | else: |
---|
160 | print |
---|
161 | print '*** COMPILE TEST FAILED ***' |
---|
162 | print |
---|
163 | print 'If Executable exists is True, it is probably finding an old executable' |
---|
164 | print 'The test distribution is located in: ' + distro_destdir_name |
---|
165 | print 'The makefile being used is: ' + makefile |
---|
166 | print 'You should try to go there and see if you can find error by compiling by hand' |
---|
167 | |
---|
168 | print; print '============================'; print |
---|
169 | |
---|
170 | # Next, get the met cases for this distro list and iterate through them |
---|
171 | |
---|
172 | if compile_success: |
---|
173 | |
---|
174 | # We add a logical variable that will indicate us whether any |
---|
175 | # of the tests failed and, if so, prevent the erasing of |
---|
176 | # the temporal directories |
---|
177 | all_success = True |
---|
178 | list_all_cases = [] |
---|
179 | |
---|
180 | met_case_list = the_distro.get_met_case_list() |
---|
181 | print met_case_list |
---|
182 | for the_met_case in met_case_list: |
---|
183 | |
---|
184 | |
---|
185 | print; print '****************************' |
---|
186 | the_descr = the_met_case.get_descr() |
---|
187 | print 'Running MetCase: ' + the_descr |
---|
188 | the_metfile_dir = os.path.realpath(the_met_case.get_metfile_dir()) |
---|
189 | print 'Met file dir: ' + the_metfile_dir |
---|
190 | try: |
---|
191 | the_metnestfile_dir = os.path.realpath(the_met_case.get_metnestfile_dir()) |
---|
192 | print 'Met Nest file dir: ' + the_metnestfile_dir |
---|
193 | except: |
---|
194 | #print ' ... no nested met input used' |
---|
195 | the_metnestfile_dir = None |
---|
196 | |
---|
197 | print '****************************'; print |
---|
198 | |
---|
199 | # Iterate through each of the run cases in the met_case |
---|
200 | run_case_list = the_met_case.get_run_case_list() |
---|
201 | |
---|
202 | for the_run_case in run_case_list: |
---|
203 | the_descr = the_run_case.get_descr() |
---|
204 | print 'Running RunCase: ' + the_descr |
---|
205 | |
---|
206 | case_dir = os.path.realpath(the_run_case.get_case_dir()) |
---|
207 | print 'Case dir: ' + case_dir |
---|
208 | case_rundir = SCRATCH_DIR + '/caserun_' + str(uuid.uuid4()) |
---|
209 | list_all_cases.append(case_rundir) # add the case_dir into a list for cleaning |
---|
210 | control_data_dir = os.path.realpath(the_run_case.get_control_data_dir()) |
---|
211 | |
---|
212 | basic_test_list = the_run_case.get_test_list() |
---|
213 | run_success = False |
---|
214 | |
---|
215 | print; print '============================' |
---|
216 | print 'Case Test ' + str(the_descr) + '...' |
---|
217 | print 'Case template directory: ' + case_dir |
---|
218 | print 'Case run directory: ' + case_rundir |
---|
219 | print 'Control data directory: ' + control_data_dir |
---|
220 | print 'Met file dir: ' + the_metfile_dir |
---|
221 | if the_metnestfile_dir: |
---|
222 | print 'Met Nest file dir: ' + the_metnestfile_dir |
---|
223 | print 'Executable: ' + flexpart_executable |
---|
224 | print 'Number of basic tests: ' + str(len(basic_test_list)) |
---|
225 | print '============================'; print |
---|
226 | |
---|
227 | |
---|
228 | # Create the case object |
---|
229 | case_obj = FlexpartCase.FlexpartCase( |
---|
230 | src_dir=case_dir, |
---|
231 | dest_dir=case_rundir, |
---|
232 | met_dir=the_metfile_dir, |
---|
233 | met_nest_dir=the_metnestfile_dir, |
---|
234 | flexpart_exe=flexpart_executable |
---|
235 | ) |
---|
236 | |
---|
237 | # Run the case |
---|
238 | run_val = case_obj.run() |
---|
239 | |
---|
240 | # Test for success |
---|
241 | run_success = case_obj.success() |
---|
242 | print 'run_success: ' + str(run_success) |
---|
243 | print 'Execution time: %7.2E seconds' % \ |
---|
244 | (case_obj.execution_time_seconds()) |
---|
245 | if not run_success: |
---|
246 | all_success = False # to know wheter any of the tests failed |
---|
247 | print 'run test failed' |
---|
248 | print 'The test distribution is located in: ' + case_rundir |
---|
249 | print 'The FLEXPART executable being used is: ' + flexpart_executable |
---|
250 | print 'You should try to go there and see if you can find error by running by hand' |
---|
251 | print 'There is a file named stdout.txt in there which might give a clue' |
---|
252 | |
---|
253 | print; print '============================'; print |
---|
254 | |
---|
255 | |
---|
256 | |
---|
257 | if run_success: |
---|
258 | |
---|
259 | |
---|
260 | output_compare = OutputCompare.OutputCompare(output_dir=case_rundir + '/output', |
---|
261 | control_output_dir=control_data_dir) |
---|
262 | |
---|
263 | #print output_compare.query_test_types() |
---|
264 | |
---|
265 | |
---|
266 | for the_basic_test in basic_test_list: |
---|
267 | |
---|
268 | the_descr = the_basic_test.get_descr() |
---|
269 | test_type = the_basic_test.get_test_type() |
---|
270 | threshold = the_basic_test.get_threshold() |
---|
271 | |
---|
272 | |
---|
273 | print; print '-----------------------' |
---|
274 | print 'Basic Test' |
---|
275 | print 'Description: ' + the_descr |
---|
276 | print 'Test type: ' + test_type |
---|
277 | print 'Threshold: %7.1E' % (threshold) |
---|
278 | |
---|
279 | |
---|
280 | the_error = output_compare.calculate_test_minus_control(test_type=test_type) |
---|
281 | |
---|
282 | print 'Test performed. Error = %7.1E' % (the_error) |
---|
283 | if the_error > threshold: |
---|
284 | all_success = False |
---|
285 | print 'Test failed...' |
---|
286 | print ' Test data is in: ' + case_rundir + '/output' |
---|
287 | print ' Control data is in: ' + control_data_dir |
---|
288 | else: |
---|
289 | print 'Test passed' |
---|
290 | |
---|
291 | print '-----------------------'; print |
---|
292 | |
---|
293 | if clean_up: |
---|
294 | if all_success: |
---|
295 | print 'All tests passed, erasing temporary directories' |
---|
296 | print distro_destdir_name |
---|
297 | shutil.rmtree(distro_destdir_name) |
---|
298 | for item in list_all_cases: |
---|
299 | print item |
---|
300 | shutil.rmtree(item) |
---|
301 | else: |
---|
302 | print 'Some of the tests failed, temp dirs not erased' |
---|
303 | print ' WARNING: remember to remove directories manually' |
---|
304 | print ' when you have finished checking them' |
---|
305 | |
---|
306 | |
---|
307 | |
---|
308 | |
---|
309 | |
---|
310 | |
---|
311 | |
---|
312 | |
---|
313 | |
---|
314 | |
---|
315 | |
---|