From eef633c1cc15392a43a8e950599b039091a78a9a Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Sat, 15 Jan 2022 13:17:25 -0500 Subject: [PATCH 1/3] tests: create a declarative project spec and the autoconfig machinery --- .gitignore | 1 + configure.ac | 6 +++++- t/jobs/declarative/project.json.in | 24 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 t/jobs/declarative/project.json.in diff --git a/.gitignore b/.gitignore index f4c2ae30..799db665 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,7 @@ Makefile.in /t/nix /t/data /t/jobs/config.nix +t/jobs/declarative/project.json /inst hydra-config.h hydra-config.h.in diff --git a/configure.ac b/configure.ac index 9a32651e..0c823696 100644 --- a/configure.ac +++ b/configure.ac @@ -43,7 +43,7 @@ then NIX_STATE_DIR="$TMPDIR" export NIX_STATE_DIR fi -if NIX_REMOTE=daemon PAGER=cat "$NIX_STORE_PROGRAM" --timeout 123 -q; then +if NIX_REMOTE=daemon PAGER=cat "$NIX_STORE_PROGRAM" --timeout 123 -q; then AC_MSG_RESULT([yes]) else AC_MSG_RESULT([no]) @@ -55,6 +55,9 @@ PKG_CHECK_MODULES([NIX], [nix-main nix-expr nix-store]) testPath="$(dirname $(type -p expr))" AC_SUBST(testPath) +jobsPath="$(realpath ./t/jobs)" +AC_SUBST(jobsPath) + CXXFLAGS+=" -include nix/config.h" AC_CONFIG_FILES([ @@ -72,6 +75,7 @@ AC_CONFIG_FILES([ src/script/Makefile t/Makefile t/jobs/config.nix + t/jobs/declarative/project.json ]) AC_CONFIG_COMMANDS([executable-scripts], []) diff --git a/t/jobs/declarative/project.json.in b/t/jobs/declarative/project.json.in new file mode 100644 index 00000000..3f846863 --- /dev/null +++ b/t/jobs/declarative/project.json.in @@ -0,0 +1,24 @@ +{ + "enabled": 1, + "hidden": false, + "description": "declarative-jobset-example", + "nixexprinput": "src", + "nixexprpath": "declarative/generator.nix", + "checkinterval": 300, + "schedulingshares": 100, + "enableemail": false, + "emailoverride": "", + "keepnr": 3, + "inputs": { + "src": { + "type": "path", + "value": "@jobsPath@", + "emailresponsible": false + }, + "jobspath": { + "type": "string", + "value": "@jobsPath@", + "emailresponsible": false + } + } +} From b2cdde0901fd2f62e308ccb85d2839085c07fc9b Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Sat, 15 Jan 2022 13:42:09 -0500 Subject: [PATCH 2/3] DeclarativeJobsets: test basic functionality --- src/lib/Hydra/Controller/Project.pm | 2 + t/Hydra/Plugin/DeclarativeJobsets/basic.t | 63 +++++++++++++++++++++++ t/jobs/declarative/generator.nix | 29 +++++++++++ t/jobs/declarative/generator.sh | 3 ++ 4 files changed, 97 insertions(+) create mode 100644 t/Hydra/Plugin/DeclarativeJobsets/basic.t create mode 100644 t/jobs/declarative/generator.nix create mode 100755 t/jobs/declarative/generator.sh diff --git a/src/lib/Hydra/Controller/Project.pm b/src/lib/Hydra/Controller/Project.pm index c7e64544..ed3c527c 100644 --- a/src/lib/Hydra/Controller/Project.pm +++ b/src/lib/Hydra/Controller/Project.pm @@ -162,6 +162,8 @@ sub updateProject { , declvalue => trim($c->stash->{params}->{declarative}->{value}) }); if (length($project->declfile)) { + # This logic also exists in the DeclarativeJobets tests. + # TODO: refactor and deduplicate. $project->jobsets->update_or_create( { name=> ".jobsets" , nixexprinput => "" diff --git a/t/Hydra/Plugin/DeclarativeJobsets/basic.t b/t/Hydra/Plugin/DeclarativeJobsets/basic.t new file mode 100644 index 00000000..ed91907c --- /dev/null +++ b/t/Hydra/Plugin/DeclarativeJobsets/basic.t @@ -0,0 +1,63 @@ +use feature 'unicode_strings'; +use strict; +use warnings; +use Test2::V0; +use Setup; + +my $ctx = test_context(); +my $db = $ctx->db; + +my $project = $db->resultset('Projects')->create({ + name => "tests", + displayname => "", + owner => "root", + declfile => "declarative/project.json", + decltype => "path", + declvalue => $ctx->jobsdir, +}); + +subtest "Evaluating and building the top .jobsets jobset" => sub { + # This logic lives in the Project controller. + # Not great to duplicate it here. + # TODO: refactor and deduplicate. + my $jobset = $project->jobsets->create({ + name=> ".jobsets", + nixexprinput => "", + nixexprpath => "", + emailoverride => "", + triggertime => time, + }); + + ok(evalSucceeds($jobset), "Evaluating the declarative jobsets with return code 0"); + is(nrQueuedBuildsForJobset($jobset), 1, "We should have exactly 1 build queued, to build the jobsets data"); + + (my $build) = queuedBuildsForJobset($jobset); + + is($build->job, "jobsets", "The only job should be jobsets"); + ok(runBuild($build), "Build should exit with return code 0"); + my $newbuild = $db->resultset('Builds')->find($build->id); + is($newbuild->finished, 1, "Build should be finished."); + is($newbuild->buildstatus, 0, "Build should have buildstatus 0."); + + ok(sendNotifications(), "Notifications execute successfully."); +}; + +subtest "Validating a new jobset appears" => sub { + my $jobset = $project->jobsets->find({ name => "my-jobset" }); + ok($jobset, "We have a jobset"); + is($jobset->description, "my-declarative-jobset", "The jobset's description matches"); + + subtest "Evaluating and building that jobset works" => sub { + ok(evalSucceeds($jobset), "Evaluating the new jobset with return code 0"); + is(nrQueuedBuildsForJobset($jobset), 1, "We should have exactly 1 build queued"); + + (my $build) = queuedBuildsForJobset($jobset); + + is($build->job, "one_job", "The only job should be jobsets"); + ok(runBuild($build), "Build should exit with return code 0"); + my $newbuild = $db->resultset('Builds')->find($build->id); + is($newbuild->finished, 1, "Build should be finished."); + is($newbuild->buildstatus, 0, "Build should have buildstatus 0."); + }; +}; +done_testing; diff --git a/t/jobs/declarative/generator.nix b/t/jobs/declarative/generator.nix new file mode 100644 index 00000000..278dd9b6 --- /dev/null +++ b/t/jobs/declarative/generator.nix @@ -0,0 +1,29 @@ +{ jobspath, ... }: +with import ../config.nix; +{ + jobsets = mkDerivation { + name = "jobsets"; + builder = ./generator.sh; + jobsets = builtins.toJSON { + my-jobset = { + enabled = 1; + hidden = false; + description = "my-declarative-jobset"; + nixexprinput = "src"; + nixexprpath = "one-job.nix"; + checkinterval = 300; + schedulingshares = 100; + enableemail = false; + emailoverride = ""; + keepnr = 3; + inputs = { + src = { + type = "path"; + value = jobspath; + emailresponsible = false; + }; + }; + }; + }; + }; +} diff --git a/t/jobs/declarative/generator.sh b/t/jobs/declarative/generator.sh new file mode 100755 index 00000000..51c0f0f5 --- /dev/null +++ b/t/jobs/declarative/generator.sh @@ -0,0 +1,3 @@ +#! /bin/sh + +echo "$jobsets" > $out From 17c6bd4fd80a9cc808f93fa369d10c7786cc1e02 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Sun, 9 Jan 2022 10:13:48 -0500 Subject: [PATCH 3/3] DeclarativeJobsets: get the jobset name from the jobset table --- src/lib/Hydra/Plugin/DeclarativeJobsets.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/Hydra/Plugin/DeclarativeJobsets.pm b/src/lib/Hydra/Plugin/DeclarativeJobsets.pm index f59fa858..2713a5da 100644 --- a/src/lib/Hydra/Plugin/DeclarativeJobsets.pm +++ b/src/lib/Hydra/Plugin/DeclarativeJobsets.pm @@ -9,7 +9,7 @@ sub buildFinished { my ($self, $build, $dependents) = @_; my $project = $build->project; - my $jobsetName = $build->get_column('jobset'); + my $jobsetName = $build->jobset->get_column('name'); if (length($project->declfile) && $jobsetName eq ".jobsets" && $build->iscurrent) { handleDeclarativeJobsetBuild($self->{"db"}, $project, $build); }