142 lines
7.2 KiB
Python
142 lines
7.2 KiB
Python
import copy
|
|
import json
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from campaign import SCALARS
|
|
from common import file_sha256
|
|
from sequence_matrix import PROFILE_FLAGS
|
|
import single_formal as single
|
|
|
|
|
|
def matrix_fixture():
|
|
rows = []
|
|
for profile in PROFILE_FLAGS:
|
|
enabled = {'signature': profile == 'signature', 'roa': profile in ('object', 'hybrid'),
|
|
'child_certificate': profile in ('object', 'hybrid'), 'publication_point': profile in ('pp', 'hybrid')}
|
|
for step in range(13):
|
|
ratios = {layer: {'enabled': on, 'hits': step if on else 0, 'queries': step + 1 if on else 0,
|
|
'ratio': step / (step + 1) if on else None} for layer, on in enabled.items()}
|
|
metrics = {'schema': 'rpki-reuse-seven-metrics-v1', 'profile': profile,
|
|
**{key: 1000 + step for key in SCALARS}, 'cache_hit_ratio': ratios}
|
|
rows.append({'profile': profile, 'step': step, 'pass': True, 'seven_metrics': metrics})
|
|
return {'status': 'completed', 'pass': True, 'performance_ready': True, 'scope_sha256': 'scope',
|
|
'tool_sha256': {'binary': 'binary'}, 'runs': rows,
|
|
'restoration': {'rp187rp': 0, 'rp187srv': 0, 'panda_active': True, 'panda_container_running': True}}
|
|
|
|
|
|
class SingleFormalTests(unittest.TestCase):
|
|
def test_one_repeat_keeps_all_65_measurements_and_no_repeat_confidence_claims(self):
|
|
summary = matrix_fixture()
|
|
result = single.report(summary)
|
|
self.assertEqual((result['repeats'], result['pairs']), (1, 65))
|
|
self.assertEqual(len(result['groups']), 25)
|
|
warm = next(g for g in result['groups'] if g['profile'] == 'pp' and g['steps'] == 'incremental')
|
|
self.assertEqual(warm['independent_repeats'], 1)
|
|
self.assertEqual(warm['n_steps'], 12)
|
|
self.assertEqual(warm['metrics']['rp_validation_ns']['mean'], 1006.5)
|
|
self.assertNotIn('stdev', warm['metrics']['rp_validation_ns'])
|
|
self.assertEqual(warm['cache_hit_ratio']['publication_point'], {'hits': 78, 'queries': 90, 'ratio': 78 / 90})
|
|
|
|
def test_csv_has_65_unique_rows_and_raw_hit_denominators(self):
|
|
rows = single.csv_rows(matrix_fixture())
|
|
self.assertEqual(len(rows), 65)
|
|
self.assertEqual(len({(r['repeat'], r['profile'], r['step']) for r in rows}), 65)
|
|
self.assertEqual(rows[-1]['publication_point_hits'], 12)
|
|
self.assertEqual(rows[-1]['publication_point_queries'], 13)
|
|
self.assertTrue(all(r['repeat'] == 1 for r in rows))
|
|
|
|
def test_missing_metric_nonfinite_and_wrong_layer_are_rejected(self):
|
|
for case in ['missing', 'nan', 'negative', 'wrong-layer', 'zero-payload']:
|
|
summary = matrix_fixture()
|
|
metric = summary['runs'][0]['seven_metrics']
|
|
if case == 'missing':
|
|
del metric['repository_sync_ns']
|
|
elif case == 'nan':
|
|
metric['cpu_ns'] = float('nan')
|
|
elif case == 'negative':
|
|
metric['cache_size_bytes'] = -1
|
|
elif case == 'zero-payload':
|
|
metric['update_to_payload_ns'] = 0
|
|
else:
|
|
metric['cache_hit_ratio']['signature'].update(hits=1, queries=1, ratio=1)
|
|
with self.subTest(case=case), self.assertRaises(ValueError):
|
|
single.report(summary)
|
|
|
|
def test_missing_duplicate_and_unrestored_matrices_cannot_finish(self):
|
|
for case in ['missing', 'duplicate', 'restore', 'not-formal']:
|
|
summary = matrix_fixture()
|
|
if case == 'missing':
|
|
summary['runs'].pop()
|
|
elif case == 'duplicate':
|
|
summary['runs'][-1] = summary['runs'][0]
|
|
elif case == 'restore':
|
|
summary['restoration']['panda_active'] = False
|
|
else:
|
|
summary['performance_ready'] = False
|
|
with self.subTest(case=case), self.assertRaises(ValueError):
|
|
single.report(summary)
|
|
|
|
def prerequisite_fixture(self, root):
|
|
tools = {}
|
|
for name in ['binary', 'ccr_compare', 'cir_compare', 'db_stats']:
|
|
path = root / name
|
|
path.write_text(name)
|
|
tools[name] = str(path)
|
|
scope = root / 'scope.json'
|
|
scope.write_text('{}')
|
|
build = {name: file_sha256(Path(path)) for name, path in tools.items()}
|
|
repeat = dict(tools, archive=str(root / 'archive'), scope=str(scope), expected_binary_sha256=build['binary'],
|
|
expected_scope_sha256=file_sha256(scope), formal_metrics=True,
|
|
profile_order=list(PROFILE_FLAGS), run_timeout_secs=1800)
|
|
package = Path(single.__file__).parent
|
|
scripts = {p.name: file_sha256(p) for p in package.glob('*.py') if not p.name.startswith('test_')}
|
|
previous = {k: v for k, v in scripts.items() if k != 'single_formal.py'}
|
|
previous['formal_metrics.py'] = 'old-collector'
|
|
pilot = matrix_fixture()
|
|
pilot.update(config=repeat.copy(), tool_sha256=build, scope_sha256=repeat['expected_scope_sha256'], scripts_sha256=previous)
|
|
pilot['config']['formal_metrics'] = False
|
|
path = root / 'pilot/summary.json'
|
|
path.parent.mkdir()
|
|
(path.parent / 'bin').mkdir()
|
|
(path.parent / 'bin/binary').write_text('binary')
|
|
path.write_text(json.dumps(pilot))
|
|
config = {'pilot_summary': str(path), 'expected_pilot_summary_sha256': file_sha256(path),
|
|
'repeat': repeat, 'expected_tools_sha256': build, 'expected_pilot_scripts_sha256': previous,
|
|
'expected_scripts_sha256': scripts}
|
|
return config
|
|
|
|
def test_preserved_pilot_reused_only_with_identical_inputs_build_and_65_proofs(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
config = self.prerequisite_fixture(Path(temp))
|
|
with patch.object(single, 'require_row_proofs') as proofs:
|
|
result = single.require_prerequisite(config)
|
|
self.assertTrue(result['pass'])
|
|
self.assertEqual(proofs.call_count, 65)
|
|
self.assertEqual(result['pairs_checked'], 65)
|
|
|
|
def test_changed_pilot_scope_binary_order_engine_or_disabled_formal_rejected(self):
|
|
for case in ['pilot-hash', 'scope', 'binary', 'order', 'engine', 'formal-off']:
|
|
with self.subTest(case=case), tempfile.TemporaryDirectory() as temp:
|
|
config = self.prerequisite_fixture(Path(temp))
|
|
if case == 'pilot-hash':
|
|
config['expected_pilot_summary_sha256'] = 'other'
|
|
elif case == 'scope':
|
|
config['repeat']['scope'] = str(Path(temp) / 'other')
|
|
elif case == 'binary':
|
|
Path(config['repeat']['binary']).write_text('changed')
|
|
elif case == 'order':
|
|
config['repeat']['profile_order'] = ['baseline']
|
|
elif case == 'engine':
|
|
config['expected_scripts_sha256']['sequence_matrix.py'] = 'changed'
|
|
else:
|
|
config['repeat']['formal_metrics'] = False
|
|
with patch.object(single, 'require_row_proofs'), self.assertRaises(ValueError):
|
|
single.require_prerequisite(config)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|