From 387fe8005a2fb813d4ac842ea2cbaa23ba48e9a0 Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Thu, 4 Mar 2021 12:38:56 -0800 Subject: [PATCH] tests/input-types: split out scminputs into individual tests This makes the test faster (by removing it and replacing it with a `TestScmInput` module that exports the `testScmInput` subroutine). Now, all the input tests can be run in parallel. Some of the `tests/jobs/*-update.sh` scripts were "broken" (e.g. tests failed for various reasons on my machine), so I fixed those up as well. Co-authored-by: gustavderdrache --- .gitignore | 1 + tests/input-types.t | 122 ------------------------------ tests/input-types/bzr-checkout.t | 28 +++++++ tests/input-types/bzr.t | 28 +++++++ tests/input-types/darcs.t | 28 +++++++ tests/input-types/deepgit.t | 29 +++++++ tests/input-types/git-rev.t | 29 +++++++ tests/input-types/git.t | 28 +++++++ tests/input-types/hg.t | 28 +++++++ tests/input-types/svn-checkout.t | 28 +++++++ tests/input-types/svn.t | 28 +++++++ tests/jobs/bzr-checkout-update.sh | 6 ++ tests/jobs/git-rev-update.sh | 1 + tests/jobs/git-update.sh | 1 + tests/jobs/svn-checkout-update.sh | 5 ++ tests/lib/TestScmInput.pm | 74 ++++++++++++++++++ 16 files changed, 342 insertions(+), 122 deletions(-) delete mode 100644 tests/input-types.t create mode 100644 tests/input-types/bzr-checkout.t create mode 100644 tests/input-types/bzr.t create mode 100644 tests/input-types/darcs.t create mode 100644 tests/input-types/deepgit.t create mode 100644 tests/input-types/git-rev.t create mode 100644 tests/input-types/git.t create mode 100644 tests/input-types/hg.t create mode 100644 tests/input-types/svn-checkout.t create mode 100644 tests/input-types/svn.t create mode 100644 tests/lib/TestScmInput.pm diff --git a/.gitignore b/.gitignore index 5a0ab4a2..4cfd7cae 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ Makefile.in /tests/.git* /tests/.hg* /tests/nix +/tests/data /inst hydra-config.h hydra-config.h.in diff --git a/tests/input-types.t b/tests/input-types.t deleted file mode 100644 index bda15b96..00000000 --- a/tests/input-types.t +++ /dev/null @@ -1,122 +0,0 @@ -use strict; -use Cwd; -use Setup; - -(my $datadir, my $pgsql) = test_init(); - -require Hydra::Schema; -require Hydra::Model::DB; - -use Test2::V0; - -my $db = Hydra::Model::DB->new; -hydra_setup($db); - -my $testdir = getcwd; -my $scratchdir = "$datadir/scratch"; -mkdir $scratchdir; -my $jobsBaseUri = "file://".$scratchdir; - -# Test scm inputs -my @scminputs = ( - { - name => "svn", - nixexpr => "svn-input.nix", - type => "svn", - uri => "$jobsBaseUri/svn-repo", - update => $testdir . "/jobs/svn-update.sh" - }, - { - name => "svn-checkout", - nixexpr => "svn-checkout-input.nix", - type => "svn-checkout", - uri => "$jobsBaseUri/svn-checkout-repo", - update => $testdir . "/jobs/svn-checkout-update.sh" - }, - { - name => "git", - nixexpr => "git-input.nix", - type => "git", - uri => "$jobsBaseUri/git-repo", - update => $testdir . "/jobs/git-update.sh" - }, - { - name => "git-rev", - nixexpr => "git-rev-input.nix", - type => "git", - uri => "$jobsBaseUri/git-repo 7f60df502b96fd54bbfa64dd94b56d936a407701", - update => $testdir . "/jobs/git-rev-update.sh" - }, - { - name => "deepgit", - nixexpr => "deepgit-input.nix", - type => "git", - uri => "$jobsBaseUri/git-repo master 1", - update => $testdir . "/jobs/git-update.sh" - }, - { - name => "bzr", - nixexpr => "bzr-input.nix", - type => "bzr", - uri => "$jobsBaseUri/bzr-repo", - update => $testdir . "/jobs/bzr-update.sh" - }, - { - name => "bzr-checkout", - nixexpr => "bzr-checkout-input.nix", - type => "bzr-checkout", - uri => "$jobsBaseUri/bzr-checkout-repo", - update => $testdir . "/jobs/bzr-checkout-update.sh" - }, - { - name => "hg", - nixexpr => "hg-input.nix", - type => "hg", - uri => "$jobsBaseUri/hg-repo", - update => $testdir . "/jobs/hg-update.sh" - }, - { - name => "darcs", - nixexpr => "darcs-input.nix", - type => "darcs", - uri => "$jobsBaseUri/darcs-repo", - update => $testdir . "/jobs/darcs-update.sh" - } -); - -foreach my $scm ( @scminputs ) { - my $scmName = $scm->{"name"}; - - subtest "With the SCM input named $scmName" => sub { - my $nixexpr = $scm->{"nixexpr"}; - my $type = $scm->{"type"}; - my $uri = $scm->{"uri"}; - my $update = $scm->{"update"}; - my $jobset = createJobsetWithOneInput($scmName, $nixexpr, "src", $type, $uri); - - my $state = 0; - my $q = 0; - my ($loop, $updated) = updateRepository($scmName, $update, $scratchdir); - while($loop) { - subtest "Mutation number $state" => sub { - # Verify that it can be fetched and possibly queued. - ok(evalSucceeds($jobset), "Evaluating nix-expression."); - - # Verify that the evaluation has queued a new job and evaluate again to ... - if ($updated) { - $q++; - is(nrQueuedBuildsForJobset($jobset), $q, "Expect $q jobs in the queue."); - ok(evalSucceeds($jobset), "Evaluating nix-expression again."); - } - - # ... check that it is deterministic and not queued again. - is(nrQueuedBuildsForJobset($jobset), $q, "Expect deterministic evaluation."); - - $state++; - ($loop, $updated) = updateRepository($scmName, $update, $scratchdir); - }; - } - }; -} - -done_testing; diff --git a/tests/input-types/bzr-checkout.t b/tests/input-types/bzr-checkout.t new file mode 100644 index 00000000..2bcf556f --- /dev/null +++ b/tests/input-types/bzr-checkout.t @@ -0,0 +1,28 @@ +use strict; +use Cwd; +use Setup; +use TestScmInput; + +(my $datadir, my $pgsql) = test_init(); + +require Hydra::Schema; +require Hydra::Model::DB; + +use Test2::V0; + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +# Tests the creation of a Hydra jobset using a bzr checkout as input. +testScmInput( + type => 'bzr-checkout', + expr => 'bzr-checkout-input.nix', + uri => 'bzr-checkout-repo', + update => 'jobs/bzr-checkout-update.sh', + + # directories + datadir => $datadir, + testdir => getcwd, +); + +done_testing; diff --git a/tests/input-types/bzr.t b/tests/input-types/bzr.t new file mode 100644 index 00000000..345f5aca --- /dev/null +++ b/tests/input-types/bzr.t @@ -0,0 +1,28 @@ +use strict; +use Cwd; +use Setup; +use TestScmInput; + +(my $datadir, my $pgsql) = test_init(); + +require Hydra::Schema; +require Hydra::Model::DB; + +use Test2::V0; + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +# Tests the creation of a Hydra jobset using a bzr repo as input. +testScmInput( + type => 'bzr', + expr => 'bzr-input.nix', + uri => 'bzr-repo', + update => 'jobs/bzr-update.sh', + + # directories + datadir => $datadir, + testdir => getcwd, +); + +done_testing; diff --git a/tests/input-types/darcs.t b/tests/input-types/darcs.t new file mode 100644 index 00000000..afbbd630 --- /dev/null +++ b/tests/input-types/darcs.t @@ -0,0 +1,28 @@ +use strict; +use Cwd; +use Setup; +use TestScmInput; + +(my $datadir, my $pgsql) = test_init(); + +require Hydra::Schema; +require Hydra::Model::DB; + +use Test2::V0; + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +# Tests the creation of a Hydra jobset using a darcs repo as input. +testScmInput( + type => 'darcs', + expr => 'darcs-input.nix', + uri => 'darcs-repo', + update => 'jobs/darcs-update.sh', + + # directories + datadir => $datadir, + testdir => getcwd, +); + +done_testing; diff --git a/tests/input-types/deepgit.t b/tests/input-types/deepgit.t new file mode 100644 index 00000000..7db0e2ce --- /dev/null +++ b/tests/input-types/deepgit.t @@ -0,0 +1,29 @@ +use strict; +use Cwd; +use Setup; +use TestScmInput; + +(my $datadir, my $pgsql) = test_init(); + +require Hydra::Schema; +require Hydra::Model::DB; + +use Test2::V0; + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +# Tests the creation of a Hydra jobset using a deep git clone as input. +testScmInput( + type => 'git', + name => 'deepgit', + expr => 'deepgit-input.nix', + uri => 'git-repo master 1', + update => 'jobs/git-update.sh', + + # directories + datadir => $datadir, + testdir => getcwd, +); + +done_testing; diff --git a/tests/input-types/git-rev.t b/tests/input-types/git-rev.t new file mode 100644 index 00000000..58d23a99 --- /dev/null +++ b/tests/input-types/git-rev.t @@ -0,0 +1,29 @@ +use strict; +use Cwd; +use Setup; +use TestScmInput; + +(my $datadir, my $pgsql) = test_init(); + +require Hydra::Schema; +require Hydra::Model::DB; + +use Test2::V0; + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +# Tests the creation of a Hydra jobset using a git revision as input. +testScmInput( + type => 'git', + name => 'git-rev', + expr => 'git-rev-input.nix', + uri => 'git-repo 7f60df502b96fd54bbfa64dd94b56d936a407701', + update => 'jobs/git-rev-update.sh', + + # directories + datadir => $datadir, + testdir => getcwd, +); + +done_testing; diff --git a/tests/input-types/git.t b/tests/input-types/git.t new file mode 100644 index 00000000..82eafa97 --- /dev/null +++ b/tests/input-types/git.t @@ -0,0 +1,28 @@ +use strict; +use Cwd; +use Setup; +use TestScmInput; + +(my $datadir, my $pgsql) = test_init(); + +require Hydra::Schema; +require Hydra::Model::DB; + +use Test2::V0; + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +# Tests the creation of a Hydra jobset using a git repo as input. +testScmInput( + type => 'git', + expr => 'git-input.nix', + uri => 'git-repo', + update => 'jobs/git-update.sh', + + # directories + datadir => $datadir, + testdir => getcwd, +); + +done_testing; diff --git a/tests/input-types/hg.t b/tests/input-types/hg.t new file mode 100644 index 00000000..d0bdc8d8 --- /dev/null +++ b/tests/input-types/hg.t @@ -0,0 +1,28 @@ +use strict; +use Cwd; +use Setup; +use TestScmInput; + +(my $datadir, my $pgsql) = test_init(); + +require Hydra::Schema; +require Hydra::Model::DB; + +use Test2::V0; + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +# Tests the creation of a Hydra jobset using a hg repo as input. +testScmInput( + type => 'hg', + expr => 'hg-input.nix', + uri => 'hg-repo', + update => 'jobs/hg-update.sh', + + # directories + datadir => $datadir, + testdir => getcwd, +); + +done_testing; diff --git a/tests/input-types/svn-checkout.t b/tests/input-types/svn-checkout.t new file mode 100644 index 00000000..7c8543c3 --- /dev/null +++ b/tests/input-types/svn-checkout.t @@ -0,0 +1,28 @@ +use strict; +use Cwd; +use Setup; +use TestScmInput; + +(my $datadir, my $pgsql) = test_init(); + +require Hydra::Schema; +require Hydra::Model::DB; + +use Test2::V0; + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +# Tests the creation of a Hydra jobset using a svn checkout as input. +testScmInput( + type => 'svn-checkout', + expr => 'svn-checkout-input.nix', + uri => 'svn-checkout-repo', + update => 'jobs/svn-checkout-update.sh', + + # directories + datadir => $datadir, + testdir => getcwd, +); + +done_testing; diff --git a/tests/input-types/svn.t b/tests/input-types/svn.t new file mode 100644 index 00000000..091a6c74 --- /dev/null +++ b/tests/input-types/svn.t @@ -0,0 +1,28 @@ +use strict; +use Cwd; +use Setup; +use TestScmInput; + +(my $datadir, my $pgsql) = test_init(); + +require Hydra::Schema; +require Hydra::Model::DB; + +use Test2::V0; + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +# Tests the creation of a Hydra jobset using a svn repo as input. +testScmInput( + type => 'svn', + expr => 'svn-input.nix', + uri => 'svn-repo', + update => 'jobs/svn-update.sh', + + # directories + datadir => $datadir, + testdir => getcwd, +); + +done_testing; diff --git a/tests/jobs/bzr-checkout-update.sh b/tests/jobs/bzr-checkout-update.sh index 0aaeca14..7eb56466 100755 --- a/tests/jobs/bzr-checkout-update.sh +++ b/tests/jobs/bzr-checkout-update.sh @@ -9,8 +9,14 @@ else state=0; fi +export BZR_HOME; # Set by the Makefile case $state in (0) echo "::Create repo. -- continue -- updated::" + bzr init bzr-repo + bzr whoami "build " -d bzr-repo + touch bzr-repo/bzr-file + bzr add bzr-repo/bzr-file + bzr commit -m "add bzr-file" bzr-repo/bzr-file ln -s bzr-repo bzr-checkout-repo ;; (*) echo "::End. -- stop -- nothing::" ;; diff --git a/tests/jobs/git-rev-update.sh b/tests/jobs/git-rev-update.sh index d48268f4..e91af1f9 100755 --- a/tests/jobs/git-rev-update.sh +++ b/tests/jobs/git-rev-update.sh @@ -3,6 +3,7 @@ set -e repo=git-repo export HOME=$(pwd) +export XDG_CONFIG_HOME=$(pwd)/.config STATE_FILE=$(pwd)/.git-rev-state if test -e $STATE_FILE; then state=1 diff --git a/tests/jobs/git-update.sh b/tests/jobs/git-update.sh index afe59717..7c983ccf 100755 --- a/tests/jobs/git-update.sh +++ b/tests/jobs/git-update.sh @@ -4,6 +4,7 @@ set -e repo=git-repo export HOME=$(pwd) +export XDG_CONFIG_HOME=$(pwd)/.config STATE_FILE=$(pwd)/.git-state if test -e $STATE_FILE; then state=$(cat $STATE_FILE) diff --git a/tests/jobs/svn-checkout-update.sh b/tests/jobs/svn-checkout-update.sh index 7aa6c868..01c6c8e9 100755 --- a/tests/jobs/svn-checkout-update.sh +++ b/tests/jobs/svn-checkout-update.sh @@ -11,6 +11,11 @@ fi case $state in (0) echo "::Create repo. -- continue -- updated::" + svnadmin create svn-repo + svn co file://$PWD/$repo svn-checkout + touch svn-checkout/svn-file + svn add svn-checkout/svn-file + svn commit -m "add svn file" svn-checkout/svn-file ln -s svn-repo svn-checkout-repo ;; (*) echo "::End. -- stop -- nothing::" ;; diff --git a/tests/lib/TestScmInput.pm b/tests/lib/TestScmInput.pm new file mode 100644 index 00000000..3aa720d0 --- /dev/null +++ b/tests/lib/TestScmInput.pm @@ -0,0 +1,74 @@ +package TestScmInput; +use warnings; +use strict; + +use Exporter; +use Test2::V0; + +use Setup; + +our @ISA = qw(Exporter); +our @EXPORT = qw(testScmInput); + +# Generic test for the various SCM types Hydra supports. +# +# Takes input in the form of: +# +# ( +# type => "input type", +# name => "jobset name", # defaults to the input's type +# uri => "uri", +# update => "script for updating the input", +# datadir => "data dir", # returned from `test_init()` subroutine +# testdir => "the hydra tests directory", # usually just `getcwd` +# ) +# +# and runs a test that constructs a jobset from the specified input. +sub testScmInput { + # Collect named args, dying if a required arg is missing + my %args = @_; + my $type = $args{type} // die "required arg 'type' missing"; + my $expr = $args{expr} // die "required arg 'expr' missing"; + + # $name is optional and defaults to $type + my $name = $args{name} // $type; + + # Get directories + my $testdir = $args{testdir} // die "required arg 'testdir' missing"; + my $datadir = $args{datadir} // die "required arg 'datadir' missing"; + + my $update = $args{update} // die "required arg 'update' missing"; + $update = "$testdir/$update"; + + # Create scratch locations + my $scratchdir = "$datadir/scratch"; + mkdir $scratchdir or die "mkdir($scratchdir): $!\n"; + + # $uri and $update are constructed from the directories + my $uri = $args{uri} // die "required arg 'uri' missing"; + $uri = "file://$scratchdir/$uri"; + + subtest "With the SCM input named $name" => sub { + my $jobset = createJobsetWithOneInput($name, $expr, 'src', $type, $uri); + + my ($mutations, $queueSize) = (0, 0); + + my ($loop, $updated) = updateRepository($name, $update, $scratchdir); + while ($loop) { + subtest "Mutation number $mutations" => sub { + ok(evalSucceeds($jobset), "Evaluating nix-expression."); + + if ($updated) { + $queueSize++; + is(nrQueuedBuildsForJobset($jobset), $queueSize, "Expect $queueSize jobs in the queue."); + ok(evalSucceeds($jobset), "Evaluating nix-expression again."); + } + + is(nrQueuedBuildsForJobset($jobset), $queueSize, "Expect deterministic evaluation."); + + $mutations++; + ($loop, $updated) = updateRepository($name, $update, $scratchdir); + }; + } + }; +}