-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtests.py
More file actions
268 lines (215 loc) · 9.15 KB
/
tests.py
File metadata and controls
268 lines (215 loc) · 9.15 KB
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
258
259
260
261
262
263
264
265
266
267
268
import os
import re
import json
import yaml
import glob
from xml.etree import ElementTree as ET
class LAMMPSExampleRun(object):
def __init__(self, name, input_script, configuration):
self.name = name
self.input_script = input_script
self.configuration = configuration
def __str__(self):
if "mpi" in self.configuration:
return f"{self.name} (MPI: {self.configuration['mpi']['nprocs']} procs)"
return f"{self.name} (Serial)"
def config_from_logfile(logfile):
"""This converts the logfile filename into a configuration"""
parts = os.path.basename(logfile).split('.')
ref_date = parts[1]
compiler = parts[-2]
nprocs = int(parts[-1])
config = {
"reference" : {
"logfile": logfile,
"date": ref_date
}
}
config['compiler'] = compiler
if nprocs > 1:
config['mpi'] = {'nprocs': nprocs}
return config
class LAMMPSExample(object):
def __init__(self, name, scripts, logfiles):
self.name = name
self.testcases = {}
self.folder = os.path.dirname(scripts[0])
for script in scripts:
script_name = os.path.basename(script)
testcase_name = script_name[3:]
logfile_pattern = re.compile(r"log.(?P<date>[0-9]+[a-zA-Z]{3}[0-9]{2})\." + testcase_name + r"\.(?P<config>(g\+\+|clang)\.[1-9]*)")
run = LAMMPSExampleRun(testcase_name, script, {'mpi': {'nprocs': 8}})
self.testcases[testcase_name] = [run]
for logfile in logfiles:
m = logfile_pattern.match(os.path.basename(logfile))
if m is not None:
try:
configuration = config_from_logfile(logfile)
run = LAMMPSExampleRun(testcase_name, script, configuration)
self.testcases[testcase_name].append(run)
except ValueError:
pass
def save_config(self):
config_file = os.path.join(self.folder, '.testing', 'config.yaml')
config = []
for testcase_name, runs in self.testcases.items():
run_list = []
for run in runs:
logfile = os.path.basename(run.configuration['logfile'])
if 'mpi' in run.configuration:
run_list.append({'logfile': logfile, 'mpi': {'nprocs': run.configuration['mpi']['nprocs']}})
else:
run_list.append({'logfile': logfile})
testcase = {"input_script": f"in.{testcase_name}", "runs": run_list}
config.append(testcase)
os.makedirs(os.path.dirname(config_file),exist_ok=True)
with open(config_file, "w") as f:
yaml.dump(config, f)
class Build(object):
def __init__(self, name, container, settings, commit=None):
self.name = name
self.container = container
self.settings = settings
self.commit = commit
@property
def build_base_dir(self):
if self.commit is not None:
return os.path.join(self.settings.cache_dir, f'builds_{self.commit}')
return os.path.join(self.settings.cache_dir, f'builds')
@property
def build_dir(self):
return os.path.join(self.build_base_dir, self.container.name, self.name)
@property
def build_script(self):
return os.path.join(self.settings.build_scripts_dir, f"{self.name}.sh")
@property
def build_result_file(self):
return os.path.join(self.build_dir, "build_result.json")
@property
def state(self):
if os.path.exists(self.build_result_file):
with open(self.build_result_file, "r") as f:
result = json.load(f)
if result["return_code"] == 0:
return "success"
return "failure"
else:
return "not executed"
class CompilationTest(Build):
def __init__(self, name, container, settings, commit=None):
super(CompilationTest, self).__init__(name, container, settings, commit)
def build(self):
os.makedirs(self.build_dir, exist_ok=True)
LAMMPS_DIR = self.settings.lammps_dir
BUILD_SCRIPTS_DIR = self.settings.build_scripts_dir
try:
return_code = self.container.exec(options=['-B', f'{LAMMPS_DIR}/:{LAMMPS_DIR}/', '-B', f'{BUILD_SCRIPTS_DIR}/:{BUILD_SCRIPTS_DIR}/'],
command=self.build_script,
cwd=self.build_dir)
except KeyboardInterrupt:
return_code = -1
with open(self.build_result_file, "w") as f:
result = {
'return_code': return_code
}
json.dump(result, f)
return return_code == 0
class RunTest(CompilationTest):
def __init__(self, name, container, settings, commit=None):
super(RunTest, self).__init__(name, container, settings, commit)
@property
def scripts_dir(self):
return os.path.join(self.settings.run_tests_scripts_dir, self.name)
@property
def build_script(self):
return os.path.join(self.scripts_dir, "build.sh")
@property
def test_script(self):
return os.path.join(self.scripts_dir, "test.sh")
@property
def test_result_file(self):
return os.path.join(self.build_dir, "test_result.json")
@property
def state(self):
build_state = super(RunTest, self).state
if os.path.exists(self.test_result_file):
with open(self.test_result_file, "r") as f:
result = json.load(f)
if build_state == "success" and result["return_code"] == 0 and len(self.result["failed"]) == 0:
return "success"
return "failure"
elif build_state == "success":
return "pending"
else:
return "not executed"
def test(self):
os.makedirs(self.build_dir, exist_ok=True)
LAMMPS_DIR = self.settings.lammps_dir
BUILD_SCRIPTS_DIR = self.settings.build_scripts_dir
try:
return_code = self.container.exec(options=['-B', f'{LAMMPS_DIR}/:{LAMMPS_DIR}/', '-B', f'{BUILD_SCRIPTS_DIR}/:{BUILD_SCRIPTS_DIR}/'],
command=self.test_script,
cwd=self.build_dir)
except KeyboardInterrupt:
return_code = -1
with open(self.test_result_file, "w") as f:
result = {
'return_code': return_code
}
json.dump(result, f)
return return_code == 0
@property
def result(self):
# TODO remove placeholder strings with actual job names
test_files = glob.glob(os.path.join(self.build_dir, "nosetests-*.xml"))
result_dict = {'passed': [], 'failed': [], "skipped": []}
for result_file in test_files:
testsuite = ET.parse(result_file).getroot()
ntests = int(testsuite.attrib["tests"])
nfailures = int(testsuite.attrib["failures"])
nskip = int(testsuite.attrib["skip"])
result_dict["passed"] += ["passed"] * (ntests - nfailures - nskip)
result_dict["failed"] += ["failed"] * nfailures
result_dict["skipped"] += ["skipped"] * nskip
return result_dict
class UnitTest(RunTest):
def __init__(self, name, container, settings, commit=None):
super(UnitTest, self).__init__(name, container, settings, commit)
@property
def scripts_dir(self):
return os.path.join(self.settings.unit_tests_scripts_dir, self.name)
@property
def result(self):
test_files = glob.glob(os.path.join(self.build_dir, "**", "**", "**", "Test.xml"))
result_dict = {'passed': [], 'failed': [], "skipped": []}
for result_file in test_files:
doc = ET.parse(result_file).getroot()
tests = doc.find("Testing").findall("Test")
for test in tests:
full_name = test.find("FullName").text
status = test.attrib["Status"]
if status not in result_dict:
result_dict[status] = [full_name]
else:
result_dict[status].append(full_name)
return result_dict
class RegressionTest(RunTest):
def __init__(self, name, container, settings, commit=None):
super(RegressionTest, self).__init__(name, container, settings, commit)
@property
def scripts_dir(self):
return os.path.join(self.settings.regression_scripts_dir, self.name)
@property
def result(self):
# TODO remove placeholder strings with actual job names
test_files = glob.glob(os.path.join(self.build_dir, "regression_*.xml"))
result_dict = {'passed': [], 'failed': [], "skipped": []}
for result_file in test_files:
testsuite = ET.parse(result_file).getroot()
ntests = int(testsuite.attrib["tests"])
nfailures = int(testsuite.attrib["failures"])
nskip = int(testsuite.attrib["skip"])
result_dict["passed"] += ["passed"] * (ntests - nfailures - nskip)
result_dict["failed"] += ["failed"] * nfailures
result_dict["skipped"] += ["skipped"] * nskip
return result_dict