From ffedbe59961f56e35656e23d32a126a9296469a4 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Thu, 21 Oct 2021 09:34:06 -0400 Subject: [PATCH 1/4] restart/cancelBuilds: always pass resultsets explicitly --- src/lib/Hydra/Controller/Admin.pm | 2 +- src/lib/Hydra/Controller/Build.pm | 4 ++-- src/lib/Hydra/Controller/JobsetEval.pm | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/Hydra/Controller/Admin.pm b/src/lib/Hydra/Controller/Admin.pm index 8dd3c348..059a4419 100644 --- a/src/lib/Hydra/Controller/Admin.pm +++ b/src/lib/Hydra/Controller/Admin.pm @@ -32,7 +32,7 @@ sub machines : Chained('admin') PathPart('machines') Args(0) { sub clear_queue_non_current : Chained('admin') PathPart('clear-queue-non-current') Args(0) { my ($self, $c) = @_; - my $builds = $c->model('DB::Builds')->search( + my $builds = $c->model('DB::Builds')->search_rs( { id => { -in => \ "select id from Builds where id in ((select id from Builds where finished = 0) except (select build from JobsetEvalMembers where eval in (select max(id) from JobsetEvals where hasNewBuilds = 1 group by jobset_id)))" } }); my $n = cancelBuilds($c->model('DB')->schema, $builds); diff --git a/src/lib/Hydra/Controller/Build.pm b/src/lib/Hydra/Controller/Build.pm index a2217da1..72883579 100644 --- a/src/lib/Hydra/Controller/Build.pm +++ b/src/lib/Hydra/Controller/Build.pm @@ -487,7 +487,7 @@ sub restart : Chained('buildChain') PathPart Args(0) { my ($self, $c) = @_; my $build = $c->stash->{build}; requireRestartPrivileges($c, $build->project); - my $n = restartBuilds($c->model('DB')->schema, $c->model('DB::Builds')->search({ id => $build->id })); + my $n = restartBuilds($c->model('DB')->schema, $c->model('DB::Builds')->search_rs({ id => $build->id })); error($c, "This build cannot be restarted.") if $n != 1; $c->flash->{successMsg} = "Build has been restarted."; $c->res->redirect($c->uri_for($self->action_for("build"), $c->req->captures)); @@ -498,7 +498,7 @@ sub cancel : Chained('buildChain') PathPart Args(0) { my ($self, $c) = @_; my $build = $c->stash->{build}; requireCancelBuildPrivileges($c, $build->project); - my $n = cancelBuilds($c->model('DB')->schema, $c->model('DB::Builds')->search({ id => $build->id })); + my $n = cancelBuilds($c->model('DB')->schema, $c->model('DB::Builds')->search_rs({ id => $build->id })); error($c, "This build cannot be cancelled.") if $n != 1; $c->flash->{successMsg} = "Build has been cancelled."; $c->res->redirect($c->uri_for($self->action_for("build"), $c->req->captures)); diff --git a/src/lib/Hydra/Controller/JobsetEval.pm b/src/lib/Hydra/Controller/JobsetEval.pm index 63a11dfa..21c4e25f 100644 --- a/src/lib/Hydra/Controller/JobsetEval.pm +++ b/src/lib/Hydra/Controller/JobsetEval.pm @@ -153,7 +153,7 @@ sub cancel : Chained('evalChain') PathPart('cancel') Args(0) { sub restart { my ($self, $c, $condition) = @_; requireRestartPrivileges($c, $c->stash->{project}); - my $builds = $c->stash->{eval}->builds->search({ finished => 1, buildstatus => $condition }); + my $builds = $c->stash->{eval}->builds->search_rs({ finished => 1, buildstatus => $condition }); my $n = restartBuilds($c->model('DB')->schema, $builds); $c->flash->{successMsg} = "$n builds have been restarted."; $c->res->redirect($c->uri_for($c->controller('JobsetEval')->action_for('view'), $c->req->captures)); From d52e39750367cee6a9de7f6c4ffedfb73b33dfee Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Mon, 25 Oct 2021 22:21:19 -0400 Subject: [PATCH 2/4] Builds controller: add a test for restarting builds, fix restarting builds --- t/Controller/Build/restart.t | 76 ++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 t/Controller/Build/restart.t diff --git a/t/Controller/Build/restart.t b/t/Controller/Build/restart.t new file mode 100644 index 00000000..75904ba4 --- /dev/null +++ b/t/Controller/Build/restart.t @@ -0,0 +1,76 @@ +use feature 'unicode_strings'; +use strict; +use warnings; +use Setup; +use JSON qw(decode_json encode_json); + +my %ctx = test_init(); + +require Hydra::Schema; +require Hydra::Model::DB; +require Hydra::Helper::Nix; + +use Test2::V0; +require Catalyst::Test; +Catalyst::Test->import('Hydra'); +use HTTP::Request::Common qw(POST PUT GET DELETE); + +# This test verifies that creating, reading, updating, and deleting a jobset via +# the HTTP API works as expected. + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +# Create a user to log in to +my $user = $db->resultset('Users')->create({ username => 'alice', emailaddress => 'root@invalid.org', password => '!' }); +$user->setPassword('foobar'); +$user->userroles->update_or_create({ role => 'admin' }); + +my $project = $db->resultset('Projects')->create({name => 'tests', displayname => 'Tests', owner => 'alice'}); + +my $jobset = createBaseJobset("basic", "basic.nix", $ctx{jobsdir}); + +ok(evalSucceeds($jobset), "Evaluating jobs/basic.nix should exit with return code 0"); +is(nrQueuedBuildsForJobset($jobset), 3, "Evaluating jobs/basic.nix should result in 3 builds"); + +my $failing; +for my $build (queuedBuildsForJobset($jobset)) { + ok(runBuild($build), "Build '".$build->job."' from jobs/basic.nix should exit with return code 0"); + my $newbuild = $db->resultset('Builds')->find($build->id); + is($newbuild->finished, 1, "Build '".$build->job."' from jobs/basic.nix should be finished."); + + if ($build->job eq "fails") { + is($newbuild->buildstatus, 1, "Build 'fails' from jobs/basic.nix should have buildstatus 1."); + $failing = $build; + last; + } +} + +isnt($failing, undef, "We should have the failing build to restart"); + +# Login and save cookie for future requests +my $req = request(POST '/login', + Referer => 'http://localhost/', + Content => { + username => 'alice', + password => 'foobar' + } +); +is($req->code, 302, "Logging in gets a 302"); +my $cookie = $req->header("set-cookie"); + + +subtest 'Restart the failing build' => sub { + my $restart = request(PUT '/build/' . $failing->id . '/restart', + Accept => 'application/json', + Content_Type => 'application/json', + Cookie => $cookie, + ); + is($restart->code, 302, "Restarting 302's back to the build"); + is($restart->header("location"), "http://localhost/build/" . $failing->id); + + my $newbuild = $db->resultset('Builds')->find($failing->id); + is($newbuild->finished, 0, "Build 'fails' from jobs/basic.nix should not be finished."); +}; + +done_testing; From 383b395b00b52756104373aa547e4fa3015dded0 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Tue, 26 Oct 2021 09:44:58 -0400 Subject: [PATCH 3/4] Test canceling builds. --- t/Controller/Build/cancel.t | 67 +++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 t/Controller/Build/cancel.t diff --git a/t/Controller/Build/cancel.t b/t/Controller/Build/cancel.t new file mode 100644 index 00000000..5dc5ba8a --- /dev/null +++ b/t/Controller/Build/cancel.t @@ -0,0 +1,67 @@ +use feature 'unicode_strings'; +use strict; +use warnings; +use Setup; +use JSON qw(decode_json encode_json); + +my %ctx = test_init(); + +require Hydra::Schema; +require Hydra::Model::DB; +require Hydra::Helper::Nix; + +use Test2::V0; +require Catalyst::Test; +Catalyst::Test->import('Hydra'); +use HTTP::Request::Common qw(POST PUT GET DELETE); + +# This test verifies that creating, reading, updating, and deleting a jobset via +# the HTTP API works as expected. + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +# Create a user to log in to +my $user = $db->resultset('Users')->create({ username => 'alice', emailaddress => 'root@invalid.org', password => '!' }); +$user->setPassword('foobar'); +$user->userroles->update_or_create({ role => 'admin' }); + +my $project = $db->resultset('Projects')->create({name => 'tests', displayname => 'Tests', owner => 'alice'}); + +my $jobset = createBaseJobset("basic", "basic.nix", $ctx{jobsdir}); + +ok(evalSucceeds($jobset), "Evaluating jobs/basic.nix should exit with return code 0"); +is(nrQueuedBuildsForJobset($jobset), 3, "Evaluating jobs/basic.nix should result in 3 builds"); + +my ($build, @builds) = queuedBuildsForJobset($jobset); +is($build->finished, 0, "Unbuilt build should not be finished."); +is($build->buildstatus, undef, "Unbuilt build should be undefined."); + + +# Login and save cookie for future requests +my $req = request(POST '/login', + Referer => 'http://localhost/', + Content => { + username => 'alice', + password => 'foobar' + } +); +is($req->code, 302, "Logging in gets a 302"); +my $cookie = $req->header("set-cookie"); + + +subtest 'Cancel the build' => sub { + my $restart = request(PUT '/build/' . $build->id . '/cancel', + Accept => 'application/json', + Content_Type => 'application/json', + Cookie => $cookie, + ); + is($restart->code, 302, "Restarting 302's back to the build"); + is($restart->header("location"), "http://localhost/build/" . $build->id); + + my $newbuild = $db->resultset('Builds')->find($build->id); + is($newbuild->finished, 1, "Build 'fails' from jobs/basic.nix should be 'finished'."); + is($newbuild->buildstatus, 4, "Build 'fails' from jobs/basic.nix should be canceled."); +}; + +done_testing; From a62c2a4d7137adbbbe6dd6428a5423c18181595a Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Tue, 26 Oct 2021 10:45:33 -0400 Subject: [PATCH 4/4] JobsetEval/restart-*: Write tests for the behavior --- t/Controller/JobsetEval/restart.t | 100 ++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 t/Controller/JobsetEval/restart.t diff --git a/t/Controller/JobsetEval/restart.t b/t/Controller/JobsetEval/restart.t new file mode 100644 index 00000000..c5957cf7 --- /dev/null +++ b/t/Controller/JobsetEval/restart.t @@ -0,0 +1,100 @@ +use feature 'unicode_strings'; +use strict; +use warnings; +use Setup; +use JSON qw(decode_json encode_json); + +my %ctx = test_init(); + +require Hydra::Schema; +require Hydra::Model::DB; +require Hydra::Helper::Nix; + +use Test2::V0; +require Catalyst::Test; +Catalyst::Test->import('Hydra'); +use HTTP::Request::Common qw(POST PUT GET DELETE); + +# This test verifies that creating, reading, updating, and deleting a jobset via +# the HTTP API works as expected. + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +# Create a user to log in to +my $user = $db->resultset('Users')->create({ username => 'alice', emailaddress => 'root@invalid.org', password => '!' }); +$user->setPassword('foobar'); +$user->userroles->update_or_create({ role => 'admin' }); + +my $project = $db->resultset('Projects')->create({name => 'tests', displayname => 'Tests', owner => 'alice'}); + +my $jobset = createBaseJobset("basic", "basic.nix", $ctx{jobsdir}); + +ok(evalSucceeds($jobset), "Evaluating jobs/basic.nix should exit with return code 0"); +is(nrQueuedBuildsForJobset($jobset), 3, "Evaluating jobs/basic.nix should result in 3 builds"); + +my ($eval, @evals) = $jobset->jobsetevals; +my ($abortedBuild, $failedBuild, @builds) = queuedBuildsForJobset($jobset); + +isnt($eval, undef, "We have an evaluation to restart"); + +# Make the build be aborted +isnt($abortedBuild, undef, "We should have the aborted build to restart"); +$abortedBuild->update({ + finished => 1, + buildstatus => 3, + stoptime => 1, + starttime => 1, + }); + +# Make the build be failed +isnt($failedBuild, undef, "We should have the failed build to restart"); +$failedBuild->update({ + finished => 1, + buildstatus => 5, + stoptime => 1, + starttime => 1, + }); + +# Login and save cookie for future requests +my $req = request(POST '/login', + Referer => 'http://localhost/', + Content => { + username => 'alice', + password => 'foobar' + } +); +is($req->code, 302, "Logging in gets a 302"); +my $cookie = $req->header("set-cookie"); + + +subtest 'Restart all aborted JobsetEval builds' => sub { + my $restart = request(PUT '/eval/' . $eval->id . '/restart-aborted', + Accept => 'application/json', + Content_Type => 'application/json', + Cookie => $cookie, + ); + is($restart->code, 302, "Restarting 302's back to the build"); + is($restart->header("location"), "http://localhost/eval/" . $eval->id); + + my $newAbortedBuild = $db->resultset('Builds')->find($abortedBuild->id); + is($newAbortedBuild->finished, 0, "The aborted build is no longer finished"); + + my $newFailedBuild = $db->resultset('Builds')->find($failedBuild->id); + is($newFailedBuild->finished, 1, "The failed build is still finished"); +}; + +subtest 'Restart all failed JobsetEval builds' => sub { + my $restart = request(PUT '/eval/' . $eval->id . '/restart-failed', + Accept => 'application/json', + Content_Type => 'application/json', + Cookie => $cookie, + ); + is($restart->code, 302, "Restarting 302's back to the build"); + is($restart->header("location"), "http://localhost/eval/" . $eval->id); + + my $newFailedBuild = $db->resultset('Builds')->find($failedBuild->id); + is($newFailedBuild->finished, 0, "The failed build is no longer finished"); +}; + +done_testing;