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