Pipelines: tests for the New pipeline page

Covers the page, one branch and Enter, and what goes wrong, with the real
scheduler and routing underneath: the first test is made from what the page
filled in, a pass starts the next test with its own settings, anything else
ends the run, and a first test that cannot be made leaves no pipeline behind.

The engines of the config are swapped for one the tests know, so they do not
depend on what a server has set up. The page is read the way a browser reads
it, because the site minifies its pages.

The shared test client now sends X-Forwarded-Proto: https, like the proxy in
front of the site. With SECURE_SSL_REDIRECT on, as in OpenSite/settings.py,
every plain-http request of the test client is otherwise answered with a
redirect to https, and no page test can pass.
This commit is contained in:
claude committed 2026-10-15 11:16:14 +02:00
1 parent 8077809d0b
commit c998e8c0d6
1 file changed
+466 -2
+466 -2
View File
@@ -24,16 +24,21 @@
# The Pipeline pages, with a real ( test ) database and the real URL routing. As
# in tests_pipelines.py, only the call out to Github is replaced.
import copy
import json
import re
from html.parser import HTMLParser
from unittest import mock
from django.contrib.auth.models import User
from django.test import Client
import OpenBench.config
import OpenBench.pipelines as pipelines
import OpenBench.tests_pipelines as base
from OpenBench.models import LogEvent, Pipeline, PipelineRun, Profile, StepRun, Test
from OpenBench.models import LogEvent, Network, Pipeline, PipelineRun, Profile, StepRun, Test
def make_user(name, enabled=True, approver=False):
user = User.objects.create_user(name, '%s@example.com' % (name), 'pw')
@@ -54,7 +59,10 @@ class PagesTestCase(base.PipelineTestCase):
self.dave = make_user('dave', enabled=False) # A disabled account
def client_for(self, user=None, **kwargs):
client = Client(**kwargs)
# The site sends everything to https, as it is behind a proxy that says that it was. So do the tests
client = Client(HTTP_X_FORWARDED_PROTO='https', **kwargs)
if user is not None: client.force_login(user)
return client
@@ -571,3 +579,459 @@ class PreviewTests(PagesTestCase):
self.assertIn('class="pl-graph"', data['svg'])
self.assertNotIn('<script>', data['svg'])
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# QUICK START #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# An engine as Engines/*.json has it, presets only. The page asks the config for
# these, so the tests do not depend on which engines a server happens to have
QUICK_ENGINE = {
'source' : 'https://github.com/o/Engine',
'test_presets' : {
'default' : {
'base_branch' : 'main', 'book_name' : 'UHO.epd',
'test_bounds' : '[0.00, 5.00]', 'test_confidence' : '[0.05, 0.05]',
'win_adj' : 'movecount=3 score=400', 'draw_adj' : 'movenumber=40 movecount=8 score=10',
},
'STC' : { 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '8.0+0.08', 'workload_size' : 32 },
'LTC' : { 'both_options' : 'Threads=1 Hash=64', 'both_time_control' : '40.0+0.4', 'workload_size' : 8 },
},
}
class QuickForm(HTMLParser):
""" The quick page as a browser makes of it: its fields, and what they hold. The
site minifies its pages, so the markup itself is nothing to compare with """
def __init__(self, page):
super().__init__()
self.inside, self.inputs, self.selects, self.attrs, self.buttons, self.details = False, {}, {}, {}, [], []
self._select, self._option = None, None
self.feed(page)
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
if tag == 'form' and attrs.get('action') == '/pipelines/quick/':
self.inside = True
if not self.inside:
return
if tag == 'input':
self.inputs[attrs.get('name')] = attrs
elif tag == 'select':
self._select = attrs.get('id')
self.selects[self._select], self.attrs[self._select] = [], attrs
elif tag == 'option':
self._option = [attrs.get('value'), 'selected' in attrs, '']
self.selects[self._select].append(self._option)
elif tag == 'button':
self.buttons.append(attrs)
elif tag == 'details':
self.details.append(attrs)
def handle_endtag(self, tag):
if tag == 'form' : self.inside = False
if tag == 'option': self._option = None
if tag == 'select': self._select = None
def handle_data(self, data):
if self._option is not None: self._option[2] += data
def chosen(self, select):
""" ( value, text ) of what a select has chosen """
return next((value, text) for value, selected, text in self.selects[select] if selected)
class QuickTests(PagesTestCase):
""" New pipeline: a branch, then Enter. The real scheduler and the real routing,
with the engines of the config swapped for the one above """
def setUp(self):
super().setUp()
patch = mock.patch.dict(OpenBench.config.OPENBENCH_CONFIG, engines={ 'Engine' : copy.deepcopy(QUICK_ENGINE) }, books={ 'UHO.epd' : {} })
patch.start(); self.addCleanup(patch.stop)
self.engines = OpenBench.config.OPENBENCH_CONFIG['engines']
def page(self, url='/pipelines/quick/'):
return self.client_for(self.user).get(url).content.decode()
def post(self, data, user=None, **kwargs):
return self.client_for(user or self.user, **kwargs).post('/pipelines/quick/', data)
def fields_of(self, pipeline, step='step1'):
return pipeline.graph['steps'][step]['payload']
# The page
def test_the_page_asks_for_a_branch_and_offers_the_presets(self):
page = self.page()
form = QuickForm(page)
self.assertIn('autofocus', form.inputs['dev_branch'])
self.assertIn('required', form.inputs['dev_branch'])
self.assertEqual(form.chosen('first'), ('STC', 'STC (8.0+0.08)'))
self.assertEqual(form.chosen('then'), ('LTC', 'Then LTC (40.0+0.4)'))
self.assertEqual([option[0] for option in form.selects['first']], ['STC', 'LTC'])
self.assertEqual((form.selects['then'][0][0], form.selects['then'][0][2]), ('', 'Stop there'))
self.assertEqual([option[0] for option in form.selects['then']], ['', 'STC', 'LTC'])
self.assertIn('href="/pipelines/new/"', page)
def test_the_first_button_in_the_form_is_the_one_that_enter_presses(self):
buttons = QuickForm(self.page()).buttons
self.assertEqual([(button['name'], button['value']) for button in buttons], [('action', 'run'), ('action', 'save')])
self.assertEqual([button['type'] for button in buttons], ['submit', 'submit'])
def test_the_base_branch_is_shown_as_what_it_would_be(self):
form = QuickForm(self.page())
self.assertEqual(form.inputs['base_branch']['placeholder'], 'main')
self.assertEqual(form.inputs['base_branch']['value'], '') # Blank: whatever the presets say
self.assertNotIn('required', form.inputs['base_branch'])
self.assertNotIn('open', form.details[0]) # Nothing in there needs doing
def test_a_single_engine_is_not_asked_about(self):
form = QuickForm(self.page())
self.assertEqual((form.inputs['engine']['type'], form.inputs['engine']['value']), ('hidden', 'Engine'))
self.assertNotIn('engine', form.selects)
def test_several_engines_can_be_switched_between_without_losing_the_branch(self):
self.engines['Second'] = copy.deepcopy(QUICK_ENGINE)
form = QuickForm(self.page())
self.assertEqual([option[0] for option in form.selects['engine']], ['Engine', 'Second'])
self.assertNotIn('engine', form.inputs)
self.assertIn("encodeURIComponent(document.getElementById('dev_branch').value)", form.attrs['engine']['onchange'])
self.assertIn('&dev_branch=', form.attrs['engine']['onchange'])
form = QuickForm(self.page('/pipelines/quick/?engine=Second&dev_branch=feature'))
self.assertEqual(form.chosen('engine'), ('Second', 'Second'))
self.assertEqual(form.inputs['dev_branch']['value'], 'feature')
def test_the_default_engine_of_the_person_is_the_one_shown(self):
self.engines['Second'] = copy.deepcopy(QUICK_ENGINE)
self.profile.engine = 'Second'; self.profile.save()
self.assertEqual(QuickForm(self.page()).chosen('engine'), ('Second', 'Second'))
def test_an_engine_with_one_preset_runs_just_that(self):
del self.engines['Engine']['test_presets']['LTC']
form = QuickForm(self.page())
self.assertEqual(form.chosen('first'), ('STC', 'STC (8.0+0.08)'))
self.assertEqual(form.chosen('then'), ('', 'Stop there'))
self.post({ 'dev_branch' : 'my-branch' })
pipeline = Pipeline.objects.get()
self.assertEqual((list(pipeline.graph['steps']), pipeline.graph['edges']), (['step1'], []))
self.assertEqual(pipeline.name, 'my-branch: STC')
def test_an_engine_without_an_stc_starts_with_the_first_preset_it_lists(self):
presets = self.engines['Engine']['test_presets']
self.engines['Engine']['test_presets'] = { 'default' : presets['default'], 'Fast' : presets['STC'], 'LTC' : presets['LTC'] }
form = QuickForm(self.page())
self.assertEqual(form.chosen('first')[0], 'Fast')
self.assertEqual(form.chosen('then')[0], 'LTC')
self.post({ 'dev_branch' : 'my-branch' })
self.assertEqual(Pipeline.objects.get().name, 'my-branch: Fast then LTC')
def test_an_engine_without_test_presets_sends_the_person_to_the_editor(self):
self.engines['Engine']['test_presets'] = { 'default' : {} }
response = self.client_for(self.user).get('/pipelines/quick/')
self.assertEqual((response.status_code, response['Location']), (302, '/pipelines/new/'))
def test_a_server_without_engines_says_so(self):
self.engines.clear()
response = self.client_for(self.user).get('/pipelines/quick/')
self.assertEqual((response.status_code, response['Location']), (302, '/pipelines/'))
def test_visitors_log_in_and_disabled_accounts_are_told(self):
anonymous = self.client_for().get('/pipelines/quick/')
disabled = self.client_for(self.dave).get('/pipelines/quick/')
self.assertEqual((anonymous.status_code, anonymous['Location']), (302, '/login/'))
self.assertEqual((disabled.status_code, disabled['Location']), (302, '/index/'))
def test_nobody_can_post_who_may_not_see(self):
self.assertEqual(self.post({ 'dev_branch' : 'x' }, self.dave)['Location'], '/index/')
self.assertEqual(self.client_for().post('/pipelines/quick/', { 'dev_branch' : 'x' })['Location'], '/login/')
self.assertEqual((Pipeline.objects.count(), len(self.creator.calls)), (0, 0))
def test_it_needs_the_csrf_token(self):
response = self.post({ 'dev_branch' : 'x' }, enforce_csrf_checks=True)
self.assertEqual(response.status_code, 403)
self.assertEqual(Pipeline.objects.count(), 0)
def test_the_list_offers_both_ways_to_make_one(self):
page = self.page('/pipelines/')
self.assertIn('href="/pipelines/quick/"', page)
self.assertIn('href="/pipelines/new/"', page)
# One branch, and Enter
def test_a_branch_is_all_it_takes(self):
response = self.post({ 'dev_branch' : 'my-branch' })
pipeline = Pipeline.objects.get()
run = PipelineRun.objects.get()
self.assertEqual(response['Location'], '/pipelines/run/%d/' % run.id)
self.assertEqual((pipeline.name, pipeline.author), ('my-branch: STC then LTC', 'alice'))
self.assertEqual(list(pipeline.graph['steps']), ['step1', 'step2'])
self.assertEqual(pipeline.graph['edges'], [{ 'from' : 'step1', 'on' : 'passed', 'to' : 'step2' }])
self.assertEqual(run.starter, 'alice')
self.assertEqual((StepRun.objects.get().status, Test.objects.count()), ('RUNNING', 1))
def test_what_a_browser_sends_when_enter_is_pressed(self):
response = self.post({ 'engine' : 'Engine', 'dev_branch' : 'my-branch', 'first' : 'STC', 'then' : 'LTC',
'base_branch' : '', 'name' : '', 'action' : 'run' })
self.assertIn('/pipelines/run/', response['Location'])
self.assertEqual(Pipeline.objects.get().name, 'my-branch: STC then LTC')
def test_it_makes_the_settings_that_create_test_would(self):
self.post({ 'dev_branch' : 'my-branch' })
pipeline = Pipeline.objects.get()
stc, ltc = self.fields_of(pipeline, 'step1'), self.fields_of(pipeline, 'step2')
self.assertEqual((stc['dev_branch'], stc['base_branch']), ('my-branch', 'main'))
self.assertEqual((stc['dev_time_control'], stc['base_time_control']), ('8.0+0.08', '8.0+0.08'))
self.assertEqual((ltc['dev_time_control'], ltc['dev_options']), ('40.0+0.4', 'Threads=1 Hash=64'))
self.assertEqual((stc['dev_engine'], stc['dev_repo']), ('Engine', 'https://github.com/o/Engine'))
self.assertEqual((stc['test_mode'], stc['test_bounds'], stc['workload_size']), ('SPRT', '[0.00, 5.00]', '32'))
self.assertEqual((stc['syzygy_wdl'], stc['priority'], stc['throughput']), ('DISABLED', '0', '1000'))
def test_the_repo_and_the_network_of_the_person_are_used(self):
self.profile.repos = { 'Engine' : 'https://github.com/alice/Engine' }; self.profile.save()
Network.objects.create(default=True, sha256='abcd1234', name='n.nn', engine='Engine', author='alice')
self.post({ 'dev_branch' : 'my-branch' })
stc = self.fields_of(Pipeline.objects.get())
self.assertEqual((stc['dev_repo'], stc['base_repo']), ('https://github.com/alice/Engine',) * 2)
self.assertEqual((stc['dev_network'], stc['base_network']), ('abcd1234',) * 2)
def test_stopping_after_the_first_test(self):
self.post({ 'dev_branch' : 'my-branch', 'first' : 'LTC', 'then' : '' })
pipeline = Pipeline.objects.get()
self.assertEqual((pipeline.name, list(pipeline.graph['steps']), pipeline.graph['edges']), ('my-branch: LTC', ['step1'], []))
def test_a_name_and_a_base_branch_can_be_given(self):
self.post({ 'dev_branch' : 'my-branch', 'name' : 'Nightly check', 'base_branch' : 'release' })
pipeline = Pipeline.objects.get()
self.assertEqual(pipeline.name, 'Nightly check')
self.assertEqual(self.fields_of(pipeline, 'step1')['base_branch'], 'release')
self.assertEqual(self.fields_of(pipeline, 'step2')['base_branch'], 'release')
def test_a_branch_with_padding_is_trimmed(self):
self.post({ 'dev_branch' : ' my-branch ' })
self.assertEqual(self.fields_of(Pipeline.objects.get())['dev_branch'], 'my-branch')
def test_saving_does_not_start_anything(self):
response = self.post({ 'dev_branch' : 'my-branch', 'action' : 'save' })
pipeline = Pipeline.objects.get()
self.assertEqual(response['Location'], '/pipelines/%d/' % pipeline.id)
self.assertEqual((PipelineRun.objects.count(), len(self.creator.calls)), (0, 0))
def test_what_happened_is_written_down(self):
self.post({ 'dev_branch' : 'my-branch', 'action' : 'save' })
pipeline = Pipeline.objects.get()
self.assertEqual(self.events(), [('alice', 'PIPELINE #%d CREATED' % pipeline.id)])
def test_the_first_test_is_made_with_what_the_page_filled_in(self):
self.post({ 'dev_branch' : 'my-branch' })
made = self.creator.calls[0].POST
self.assertEqual((made['dev_branch'], made['base_branch']), ('my-branch', 'main'))
self.assertEqual((made['dev_time_control'], made['dev_options']), ('8.0+0.08', 'Threads=1 Hash=8'))
self.assertEqual(len(self.creator.calls), 1) # Only the first, so far
def test_a_pass_starts_the_next_test_with_its_own_settings(self):
self.post({ 'dev_branch' : 'my-branch' })
run = PipelineRun.objects.get()
self.finish(self.steps(run)['step1'], passed=True)
self.assertEqual(self.keys(run), ['step1', 'step2'])
self.assertEqual(self.status(run), 'RUNNING')
self.assertEqual(self.creator.calls[1].POST['dev_time_control'], '40.0+0.4')
self.assertEqual(self.creator.calls[1].POST['dev_branch'], 'my-branch')
def test_anything_but_a_pass_ends_it(self):
self.post({ 'dev_branch' : 'my-branch' })
run = PipelineRun.objects.get()
self.finish(self.steps(run)['step1'], failed=True)
self.assertEqual(self.keys(run), ['step1'])
self.assertEqual(self.status(run), 'COMPLETED')
self.assertEqual(len(self.creator.calls), 1)
def test_what_is_made_here_opens_everywhere_else(self):
self.post({ 'dev_branch' : 'my-branch' })
pipeline, run = Pipeline.objects.get(), PipelineRun.objects.get()
client = self.client_for(self.user)
for url in ('/pipelines/', '/pipelines/%d/' % pipeline.id):
response = client.get(url)
self.assertEqual(response.status_code, 200, url)
self.assertIn('my-branch: STC then LTC', response.content.decode(), url)
for url in ('/pipelines/%d/edit/' % pipeline.id, '/pipelines/run/%d/' % run.id):
self.assertEqual(client.get(url).status_code, 200, url)
# What goes wrong
def test_no_branch_asks_for_one_and_makes_nothing(self):
response = self.post({ 'dev_branch' : ' ' })
self.assertEqual(response.status_code, 200)
self.assertIn('Type the name of the branch to test', response.content.decode())
self.assertEqual((Pipeline.objects.count(), PipelineRun.objects.count()), (0, 0))
def test_a_branch_that_cannot_be_one_is_refused_and_kept_on_the_page(self):
response = self.post({ 'dev_branch' : 'two words' })
page = response.content.decode()
self.assertEqual(response.status_code, 200)
self.assertIn('cannot be a branch', page)
self.assertEqual(QuickForm(page).inputs['dev_branch']['value'], 'two words')
self.assertEqual(Pipeline.objects.count(), 0)
def test_a_base_branch_that_cannot_be_one_is_refused(self):
response = self.post({ 'dev_branch' : 'ok', 'base_branch' : 'no good' })
page = response.content.decode()
self.assertIn('cannot be a branch', page)
self.assertIn('open', QuickForm(page).details[0]) # Where it was typed
self.assertEqual(Pipeline.objects.count(), 0)
def test_a_preset_that_does_not_exist_is_refused(self):
for data in ({ 'first' : 'NOPE' }, { 'then' : 'NOPE' }):
response = self.post(dict(data, dev_branch='ok'))
self.assertEqual(response.status_code, 200, data)
self.assertEqual(Pipeline.objects.count(), 0, data)
def test_a_first_test_that_could_not_be_made_is_reported_and_leaves_nothing_behind(self):
self.creator.errors = ['my-branch could not be found']
response = self.post({ 'dev_branch' : 'my-branch' })
page = response.content.decode()
self.assertEqual(response.status_code, 200)
self.assertIn('The first test could not be created', page)
self.assertIn('my-branch could not be found', page)
self.assertEqual(QuickForm(page).inputs['dev_branch']['value'], 'my-branch') # Typed once is enough
self.assertEqual((Pipeline.objects.count(), PipelineRun.objects.count(), StepRun.objects.count()), (0, 0, 0))
self.assertEqual(Test.objects.count(), 0)
events = self.events()
self.assertTrue(events[0][1].endswith('CREATED'))
self.assertTrue(events[-1][1].endswith('DISCARDED'))
def test_the_second_try_works(self):
self.creator.errors = ['my-branch could not be found']
self.post({ 'dev_branch' : 'my-branch' })
self.creator.errors = None
response = self.post({ 'dev_branch' : 'my-branch' })
self.assertIn('/pipelines/run/', response['Location'])
self.assertEqual((Pipeline.objects.count(), PipelineRun.objects.count()), (1, 1))
def test_presets_that_leave_things_out_are_named(self):
self.engines['Engine']['test_presets']['Bare'] = {}
response = self.post({ 'dev_branch' : 'ok', 'first' : 'Bare', 'then' : '' })
page = response.content.decode()
self.assertIn('leave out', page)
self.assertIn('dev time control (Bare)', page)
self.assertEqual(Pipeline.objects.count(), 0)
def test_an_engine_that_names_no_base_branch_asks_for_it(self):
del self.engines['Engine']['test_presets']['default']['base_branch']
form = QuickForm(self.page())
self.assertIn('open', form.details[0])
self.assertIn('required', form.inputs['base_branch'])
self.assertEqual(form.inputs['base_branch']['placeholder'], 'The branch to compare against')
refused = self.post({ 'dev_branch' : 'ok' })
self.assertIn('Type the base branch', refused.content.decode())
self.assertEqual(Pipeline.objects.count(), 0)
accepted = self.post({ 'dev_branch' : 'ok', 'base_branch' : 'trunk' })
self.assertIn('/pipelines/run/', accepted['Location'])
self.assertEqual(self.fields_of(Pipeline.objects.get())['base_branch'], 'trunk')
def test_nothing_typed_can_inject_markup(self):
self.creator.errors = ['<script>alert(1)</script> could not be found']
page = self.post({ 'dev_branch' : '<u>zzz</u>', 'name' : '<b>nn</b>' }).content.decode()
self.assertNotIn('<script>alert', page)
self.assertNotIn('<u>zzz</u>', page)
self.assertNotIn('<b>nn</b>', page)
self.assertIn('&lt;u&gt;zzz&lt;/u&gt;', page)