Skip to content

Commit 9d71b7e

Browse files
committed
Extract report construction; simplify report concatenation
Signed-off-by: Matthias Büchse <matthias.buechse@alasca.cloud>
1 parent e55b849 commit 9d71b7e

3 files changed

Lines changed: 35 additions & 33 deletions

File tree

Tests/scs-compliance-check.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import os
2121
import os.path
22-
import uuid
2322
import re
2423
import sys
2524
import shlex
@@ -29,7 +28,8 @@
2928
import logging
3029
import yaml
3130

32-
from scs_cert_lib import load_spec, annotate_validity, eval_buckets, TESTCASE_VERDICTS
31+
from scs_cert_lib import load_spec, annotate_validity, eval_buckets, TESTCASE_VERDICTS, \
32+
make_report
3333

3434

3535
logger = logging.getLogger(__name__)
@@ -232,24 +232,6 @@ def print_report(testcase_lookup: dict, tc_ids: list, results: dict, partial=Fal
232232
print(f" > {testcase['url']}")
233233

234234

235-
def create_report(config, spec, log, results):
236-
return {
237-
"uuid": str(uuid.uuid4()),
238-
"creator": "SCS test suite; version=0.1.0", # TODO put actual version of test suite here
239-
"scope": spec['uuid'],
240-
"checked_at": datetime.datetime.now(),
241-
"reference_date": config.checkdate,
242-
"subject": config.subject,
243-
"tests": {
244-
testcase_id: {
245-
"result": value,
246-
}
247-
for testcase_id, value in results.items()
248-
},
249-
"log": log,
250-
}
251-
252-
253235
def main(argv):
254236
"""Entry point for the checker"""
255237
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
@@ -318,7 +300,7 @@ def main(argv):
318300
print(f"- {version['version']}:", end=' ')
319301
print_report(testcase_lookup, version['testcase_ids'], results, partial, config.verbose)
320302
if config.output:
321-
report = create_report(config, spec, log, results)
303+
report = make_report(spec['uuid'], config.subject, results, log)
322304
with open(config.output, 'w', encoding='UTF-8') as fileobj:
323305
yaml.safe_dump(report, fileobj, default_flow_style=False, sort_keys=False, explicit_start=True)
324306
num_error = len([tc_id for tc_id, value in results.items() if value != 1])

Tests/scs-test-runner.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,18 @@ def _run_commands(commands, num_workers=5):
159159
def _concat_files(source_paths, target_path):
160160
with open(target_path, 'wb') as tfileobj:
161161
for path in source_paths:
162-
with open(path, 'rb') as sfileobj:
163-
shutil.copyfileobj(sfileobj, tfileobj)
162+
try:
163+
with open(path, 'rb') as sfileobj:
164+
shutil.copyfileobj(sfileobj, tfileobj)
165+
except FileNotFoundError:
166+
logger.warning(f"Skipping non-extant {path}")
164167

165168

166-
def _move_file(source_path, target_path):
167-
# for Windows people, remove target first, but don't try too hard (Windows is notoriously bad at this)
168-
# this two-stage delete-rename approach does have a tiny (irrelevant) race condition (thx Windows)
169+
def _remove_file(path):
169170
try:
170-
os.remove(target_path)
171+
os.remove(path)
171172
except FileNotFoundError:
172173
pass
173-
os.rename(source_path, target_path)
174174

175175

176176
@cli.command()
@@ -213,18 +213,17 @@ def run(cfg, scopes, subjects, sections, preset, num_workers, monitor_url, repor
213213
logger.debug(f'running tests for scope(s) {", ".join(scopes)} and subject(s) {", ".join(subjects)}')
214214
logger.debug(f'monitor url: {monitor_url}, num_workers: {num_workers}, output: {report_yaml}')
215215
with tempfile.TemporaryDirectory(dir=cfg.cwd) as tdirname:
216-
report_yaml_tmp = os.path.join(tdirname, 'report.yaml')
217216
jobs = [(scope, subject) for scope in scopes for subject in subjects]
218217
outputs = [os.path.join(tdirname, f'report-{idx}.yaml') for idx in range(len(jobs))]
219218
commands = [cfg.build_check_command(job[0], job[1], sections, output) for job, output in zip(jobs, outputs)]
220219
_run_commands(commands, num_workers=num_workers)
221-
_concat_files(outputs, report_yaml_tmp)
222220
if report_yaml is None:
223-
report_yaml = report_yaml_tmp
224-
else:
225-
_move_file(report_yaml_tmp, report_yaml)
221+
report_yaml = os.path.join(tdirname, 'report.yaml')
222+
_concat_files(outputs, report_yaml)
223+
_remove_file(report_yaml + '.sig')
226224
subprocess.run(**cfg.build_sign_command(report_yaml))
227225
subprocess.run(**cfg.build_upload_command(report_yaml, monitor_url))
226+
sys.stdout.write('\n') # curl output does not end in newline
228227
return 0
229228

230229

Tests/scs_cert_lib.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
from collections import defaultdict
1010
from datetime import datetime, date, timedelta
1111
import logging
12+
import uuid
1213

1314

1415
logger = logging.getLogger(__name__)
16+
__VERSION__ = "20260623"
1517

1618
# valid keywords for various parts of the spec, to be checked using `check_keywords`
1719
KEYWORDS = {
@@ -219,3 +221,22 @@ def evaluate(results, testcase_ids) -> int:
219221
results.get(testcase_id, {}).get('result') or 0
220222
for testcase_id in testcase_ids
221223
], default=0)
224+
225+
226+
def make_report(scopeuuid, subject, results, log, creator=None, checked_at=None):
227+
if creator is None:
228+
creator = f"SCS test suite; version={__VERSION__}"
229+
return {
230+
"uuid": str(uuid.uuid4()),
231+
"creator": creator,
232+
"scope": scopeuuid,
233+
"checked_at": checked_at or datetime.now(),
234+
"subject": subject,
235+
"tests": {
236+
testcase_id: {
237+
"result": value,
238+
}
239+
for testcase_id, value in results.items()
240+
},
241+
"log": log,
242+
}

0 commit comments

Comments
 (0)