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