Files
OpenBench/UnitTests/test_pipeline_graph.py
T

327 lines
15 KiB
Python

#!/usr/bin/python3
import copy
import os
import sys
import unittest
# Needed to include from ../OpenBench/pipeline_graph.py, which has no Django dependencies
PARENT = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.path.pardir)
sys.path.insert(0, os.path.abspath(PARENT))
from OpenBench.pipeline_graph import *
def test_payload(**changes):
payload = {
'dev_engine' : 'Stockfish', 'dev_repo' : 'https://github.com/o/r', 'dev_branch' : 'dev',
'dev_network' : '', 'dev_options' : 'Threads=1 Hash=8', 'dev_time_control' : '8.0+0.08',
'base_engine' : 'Stockfish', 'base_repo' : 'https://github.com/o/r', 'base_branch' : 'master',
'base_network' : '', 'base_options' : 'Threads=1 Hash=8', 'base_time_control' : '8.0+0.08',
'book_name' : 'UHO.epd', 'upload_pgns' : 'FALSE', 'priority' : '0', 'throughput' : '1000',
'syzygy_wdl' : 'OPTIONAL', 'syzygy_adj' : 'OPTIONAL',
'win_adj' : 'movecount=3 score=400', 'draw_adj' : 'movenumber=40 movecount=8 score=10',
'workload_size' : '32', 'test_mode' : 'SPRT',
'test_bounds' : '[0.00, 2.00]', 'test_confidence' : '[0.10, 0.10]',
}
payload.update(changes)
return payload
def test_step(name, **changes):
return { 'name' : name, 'kind' : 'TEST', 'payload' : test_payload(**changes) }
def tune_step():
payload = { key : value for key, value in test_payload().items() if not key.startswith('base_') }
for key in ('workload_size', 'test_mode', 'test_bounds', 'test_confidence'): del payload[key]
payload.update({
'spsa_alpha' : '0.602', 'spsa_gamma' : '0.101', 'spsa_A_ratio' : '0.1',
'spsa_iterations' : '1000', 'spsa_pairs_per' : '8',
'spsa_reporting_type' : 'BULK', 'spsa_distribution_type' : 'SINGLE',
'spsa_inputs' : 'Aspiration, int, 20, 5, 50, 4, 0.002' })
return { 'name' : 'Tune', 'kind' : 'TUNE', 'payload' : payload }
def datagen_step():
payload = test_payload()
for key in ('test_mode', 'test_bounds', 'test_confidence'): del payload[key]
payload.update({ 'datagen_max_games' : '1000', 'datagen_custom_genfens' : '', 'datagen_play_reverses' : 'NO' })
return { 'name' : 'Data', 'kind' : 'DATAGEN', 'payload' : payload }
def stc_then_ltc():
return {
'version' : 1, 'start' : 'stc',
'steps' : {
'stc' : test_step('STC'),
'ltc' : test_step('LTC', dev_time_control='40.0+0.40', base_time_control='40.0+0.40'),
'retry' : test_step('STC wider', test_bounds='[-1.00, 3.00]'),
},
'edges' : [
{ 'from' : 'stc', 'on' : 'passed', 'to' : 'ltc' },
{ 'from' : 'stc', 'on' : 'failed', 'to' : 'retry' },
],
}
def codes(issues, level=None):
return sorted(i['code'] for i in issues if level is None or i['level'] == level)
class ValidationTests(unittest.TestCase):
def test_valid_graph_has_no_errors(self):
issues = validate_graph(stc_then_ltc())
self.assertFalse(has_errors(issues), issues)
def test_stopped_branch_is_reported_as_info_only(self):
issues = validate_graph(stc_then_ltc())
self.assertEqual(codes(issues, 'info'), ['branch.uncovered'])
self.assertIn('stopped', issues[0]['message'])
def test_garbage_input(self):
for garbage in (None, [], 'x', 5, {}):
self.assertTrue(has_errors(validate_graph(garbage)))
def test_missing_start(self):
graph = stc_then_ltc(); graph['start'] = 'nope'
self.assertIn('start.missing', codes(validate_graph(graph), 'error'))
def test_bad_edge_references(self):
graph = stc_then_ltc()
graph['edges'].append({ 'from' : 'ghost', 'on' : 'passed', 'to' : 'ltc' })
graph['edges'].append({ 'from' : 'ltc', 'on' : 'passed', 'to' : 'ghost' })
graph['edges'].append({ 'from' : 'ltc', 'on' : 'sometimes', 'to' : None })
found = codes(validate_graph(graph), 'error')
self.assertIn('edge.from', found); self.assertIn('edge.to', found); self.assertIn('edge.on', found)
def test_duplicate_edge(self):
graph = stc_then_ltc()
graph['edges'].append(copy.deepcopy(graph['edges'][0]))
self.assertIn('edge.duplicate', codes(validate_graph(graph), 'error'))
def test_missing_and_conditional_payload_fields(self):
graph = stc_then_ltc()
del graph['steps']['ltc']['payload']['dev_repo']
del graph['steps']['stc']['payload']['test_bounds']
graph['steps']['retry']['payload']['test_mode'] = 'GAMES'
issues = validate_graph(graph)
messages = ' '.join(i['message'] for i in issues if i['code'] == 'payload.missing')
self.assertIn('dev_repo', messages); self.assertIn('test_bounds', messages); self.assertIn('test_max_games', messages)
def test_bad_kind_mode_and_value_types(self):
graph = stc_then_ltc()
graph['steps']['stc']['kind'] = 'WHAT'
graph['steps']['ltc']['payload']['test_mode'] = 'NOPE'
graph['steps']['retry']['payload']['priority'] = True
found = codes(validate_graph(graph), 'error')
self.assertIn('step.kind', found); self.assertIn('payload.test_mode', found); self.assertIn('payload.type', found)
def test_numbers_are_allowed_in_payload(self):
graph = stc_then_ltc()
graph['steps']['stc']['payload']['priority'] = 5
graph['steps']['stc']['payload']['throughput'] = 1000.0
self.assertFalse(has_errors(validate_graph(graph)))
def test_bad_step_ids(self):
graph = stc_then_ltc()
graph['steps']['has space'] = test_step('x')
self.assertIn('step.id', codes(validate_graph(graph), 'error'))
def test_edge_override_must_be_known_field(self):
graph = stc_then_ltc()
graph['edges'][0]['set'] = { 'rm_rf' : '1' }
self.assertIn('edge.set.key', codes(validate_graph(graph), 'error'))
def test_limits(self):
for key, value in (('max_visits', 0), ('max_visits', 11), ('max_steps', 51), ('max_steps', 'x'), ('max_steps', True)):
graph = stc_then_ltc(); graph[key] = value
self.assertIn('limits.%s' % (key), codes(validate_graph(graph), 'error'), (key, value))
def test_too_many_steps(self):
graph = stc_then_ltc()
for i in range(LIMIT_STEPS): graph['steps']['s%d' % (i)] = test_step('s')
self.assertIn('steps.limit', codes(validate_graph(graph), 'error'))
def test_unreachable_step_is_a_warning(self):
graph = stc_then_ltc()
graph['steps']['island'] = test_step('Island')
issues = validate_graph(graph)
self.assertFalse(has_errors(issues))
self.assertEqual([i['step'] for i in issues if i['code'] == 'step.unreachable'], ['island'])
def test_loop_is_a_warning_naming_the_cap(self):
graph = stc_then_ltc()
graph['edges'].append({ 'from' : 'retry', 'on' : 'failed', 'to' : 'stc' })
issues = validate_graph(graph)
self.assertFalse(has_errors(issues))
loops = [i for i in issues if i['code'] == 'loop']
self.assertEqual(len(loops), 1)
self.assertIn('capped at 3', loops[0]['message'])
def test_self_loop_is_detected(self):
graph = stc_then_ltc()
graph['edges'][1]['to'] = 'stc'
self.assertIn('loop', codes(validate_graph(graph), 'warning'))
def test_tune_and_datagen_steps_are_valid(self):
graph = stc_then_ltc()
graph['steps']['tune'] = tune_step()
graph['steps']['data'] = datagen_step()
graph['edges'].append({ 'from' : 'ltc', 'on' : 'passed', 'to' : 'tune' })
graph['edges'].append({ 'from' : 'tune', 'on' : 'passed', 'to' : 'data' })
self.assertFalse(has_errors(validate_graph(graph)), validate_graph(graph))
def test_missing_tune_fields(self):
graph = stc_then_ltc()
graph['steps']['tune'] = tune_step()
del graph['steps']['tune']['payload']['spsa_inputs']
graph['edges'].append({ 'from' : 'ltc', 'on' : 'passed', 'to' : 'tune' })
self.assertIn('payload.missing', codes(validate_graph(graph), 'error'))
def test_impossible_outcome_for_tunes_and_datagen(self):
graph = stc_then_ltc()
graph['steps']['tune'] = tune_step()
graph['steps']['data'] = datagen_step()
graph['edges'].append({ 'from' : 'ltc', 'on' : 'passed', 'to' : 'tune' })
graph['edges'].append({ 'from' : 'tune', 'on' : 'failed', 'to' : 'data' })
graph['edges'].append({ 'from' : 'data', 'on' : 'failed', 'to' : None })
issues = validate_graph(graph)
self.assertFalse(has_errors(issues), issues)
self.assertEqual([i['step'] for i in issues if i['code'] == 'edge.impossible'], ['tune', 'data'])
def test_failed_edge_from_a_test_is_not_impossible(self):
self.assertNotIn('edge.impossible', codes(validate_graph(stc_then_ltc())))
def test_any_edge_suppresses_coverage_info(self):
graph = stc_then_ltc()
graph['edges'] = [{ 'from' : 'stc', 'on' : 'any', 'to' : 'ltc' }]
self.assertNotIn('branch.uncovered', codes(validate_graph(graph)))
class ClassifyTests(unittest.TestCase):
def classify(self, **kw):
args = dict(finished=False, deleted=False, passed=False, failed=False, test_mode='SPRT', games=0, spsa=None)
args.update(kw)
return classify_outcome(**args)
def test_running_test_has_no_outcome(self):
self.assertIsNone(self.classify())
def test_passed_and_failed(self):
self.assertEqual(self.classify(finished=True, passed=True), 'passed')
self.assertEqual(self.classify(finished=True, failed=True), 'failed')
def test_finished_without_verdict_means_stopped(self):
self.assertEqual(self.classify(finished=True), 'stopped')
def test_deleted_while_running_means_stopped(self):
self.assertEqual(self.classify(deleted=True), 'stopped')
def test_passed_then_deleted_stays_passed(self):
self.assertEqual(self.classify(finished=True, passed=True, deleted=True), 'passed')
def test_completed_tune_counts_as_passed(self):
spsa = { 'pairs_per' : 4, 'iterations' : 10 }
self.assertEqual(self.classify(finished=True, test_mode='SPSA', games=80, spsa=spsa), 'passed')
self.assertEqual(self.classify(finished=True, test_mode='SPSA', games=200, spsa=spsa), 'passed')
def test_stopped_tune_is_stopped(self):
spsa = { 'pairs_per' : 4, 'iterations' : 10 }
self.assertEqual(self.classify(finished=True, test_mode='SPSA', games=79, spsa=spsa), 'stopped')
def test_tune_without_targets_is_never_passed(self):
self.assertEqual(self.classify(finished=True, test_mode='SPSA', games=100, spsa={}), 'stopped')
class PlanTests(unittest.TestCase):
def test_initial_fire(self):
fire = initial_fire(stc_then_ltc())
self.assertEqual((fire.step_key, fire.kind, fire.via), ('stc', 'TEST', ''))
self.assertTrue(all(isinstance(v, str) for v in fire.payload.values()))
def test_passed_goes_to_ltc(self):
plan = plan_next(stc_then_ltc(), 'stc', 'passed', { 'stc' : 1 }, 1)
self.assertEqual([f.step_key for f in plan.fires], ['ltc'])
self.assertEqual(plan.fires[0].payload['dev_time_control'], '40.0+0.40')
self.assertEqual(plan.fires[0].via, 'passed')
def test_failed_goes_to_retry(self):
plan = plan_next(stc_then_ltc(), 'stc', 'failed', { 'stc' : 1 }, 1)
self.assertEqual([f.step_key for f in plan.fires], ['retry'])
def test_unhandled_outcome_ends_the_run_with_a_note(self):
plan = plan_next(stc_then_ltc(), 'stc', 'stopped', { 'stc' : 1 }, 1)
self.assertEqual(plan.fires, [])
self.assertIn('no edge', plan.notes[0])
def test_terminal_step_ends_the_run(self):
plan = plan_next(stc_then_ltc(), 'ltc', 'passed', { 'stc' : 1, 'ltc' : 1 }, 2)
self.assertEqual(plan.fires, [])
def test_explicit_end_edge(self):
graph = stc_then_ltc()
graph['edges'].append({ 'from' : 'ltc', 'on' : 'any', 'to' : None })
self.assertFalse(has_errors(validate_graph(graph)))
plan = plan_next(graph, 'ltc', 'failed', { 'ltc' : 1 }, 2)
self.assertEqual(plan.fires, []); self.assertIn('ended', plan.notes[0])
def test_edge_overrides_apply_and_do_not_leak(self):
graph = stc_then_ltc()
graph['edges'][0]['set'] = { 'priority' : 9, 'test_bounds' : '[0.00, 5.00]' }
plan = plan_next(graph, 'stc', 'passed', {}, 1)
self.assertEqual(plan.fires[0].payload['priority'], '9')
self.assertEqual(plan.fires[0].payload['test_bounds'], '[0.00, 5.00]')
self.assertEqual(graph['steps']['ltc']['payload']['priority'], '0') # Graph is untouched
def test_fan_out_starts_every_matching_edge(self):
graph = stc_then_ltc()
graph['edges'].append({ 'from' : 'stc', 'on' : 'passed', 'to' : 'retry' })
plan = plan_next(graph, 'stc', 'passed', { 'stc' : 1 }, 1)
self.assertEqual(sorted(f.step_key for f in plan.fires), ['ltc', 'retry'])
def test_any_and_specific_edge_to_same_target_fire_once(self):
graph = stc_then_ltc()
graph['edges'].append({ 'from' : 'stc', 'on' : 'any', 'to' : 'ltc' })
plan = plan_next(graph, 'stc', 'passed', { 'stc' : 1 }, 1)
self.assertEqual([f.step_key for f in plan.fires], ['ltc'])
self.assertEqual(len(plan.notes), 1)
def test_loop_is_capped_by_max_visits(self):
graph = stc_then_ltc()
graph['edges'][1]['to'] = 'stc'
graph['max_visits'] = 3
plan = plan_next(graph, 'stc', 'failed', { 'stc' : 1 }, 1); self.assertEqual(len(plan.fires), 1)
plan = plan_next(graph, 'stc', 'failed', { 'stc' : 2 }, 2); self.assertEqual(len(plan.fires), 1)
plan = plan_next(graph, 'stc', 'failed', { 'stc' : 3 }, 3)
self.assertEqual(plan.fires, []); self.assertIn('limit of 3 runs', plan.notes[0])
def test_total_workloads_are_capped_by_max_steps(self):
graph = stc_then_ltc(); graph['max_steps'] = 2
plan = plan_next(graph, 'stc', 'passed', { 'stc' : 1 }, 2)
self.assertEqual(plan.fires, []); self.assertIn('limit of 2 workloads', plan.notes[0])
def test_max_steps_counts_fires_of_the_same_plan(self):
graph = stc_then_ltc(); graph['max_steps'] = 2
graph['edges'].append({ 'from' : 'stc', 'on' : 'passed', 'to' : 'retry' })
plan = plan_next(graph, 'stc', 'passed', { 'stc' : 1 }, 1)
self.assertEqual(len(plan.fires), 1)
def test_fires_are_independent_copies(self):
graph = stc_then_ltc()
plan = plan_next(graph, 'stc', 'passed', {}, 1)
plan.fires[0].payload['priority'] = '99'
self.assertEqual(graph['steps']['ltc']['payload']['priority'], '0')
class RunStatusTests(unittest.TestCase):
def test_status(self):
self.assertEqual(run_status([]), 'COMPLETED')
self.assertEqual(run_status(['PASSED', 'FAILED']), 'COMPLETED')
self.assertEqual(run_status(['PASSED', 'RUNNING']), 'RUNNING')
self.assertEqual(run_status(['QUEUED']), 'RUNNING')
self.assertEqual(run_status(['LAUNCHING', 'ERROR']), 'RUNNING')
self.assertEqual(run_status(['PASSED', 'ERROR']), 'ERROR')
if __name__ == '__main__':
unittest.main()