From f08d0be1bd63c91526affac99e764b1dd0453c3a Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Thu, 25 Feb 2021 19:26:38 -0500 Subject: [PATCH 1/3] tests: replace the flat list of contexts with a hash This way we can return more values without breaking callers. --- tests/build-products.t | 2 +- tests/evaluate-basic.t | 2 +- tests/evaluate-dependent-jobsets.t | 2 +- tests/lib/Setup.pm | 5 ++++- tests/plugins/runcommand.t | 2 +- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/build-products.t b/tests/build-products.t index 3431324c..5f1610f9 100644 --- a/tests/build-products.t +++ b/tests/build-products.t @@ -2,7 +2,7 @@ use strict; use Cwd; use Setup; -(my $datadir, my $pgsql) = test_init(); +my %ctx = test_init(); require Hydra::Schema; require Hydra::Model::DB; diff --git a/tests/evaluate-basic.t b/tests/evaluate-basic.t index 0d58d08f..d143b425 100644 --- a/tests/evaluate-basic.t +++ b/tests/evaluate-basic.t @@ -3,7 +3,7 @@ use strict; use Cwd; use Setup; -(my $datadir, my $pgsql) = test_init(); +my %ctx = test_init(); require Hydra::Schema; require Hydra::Model::DB; diff --git a/tests/evaluate-dependent-jobsets.t b/tests/evaluate-dependent-jobsets.t index 279e1f33..fa849258 100644 --- a/tests/evaluate-dependent-jobsets.t +++ b/tests/evaluate-dependent-jobsets.t @@ -2,7 +2,7 @@ use strict; use Cwd; use Setup; -(my $datadir, my $pgsql) = test_init(); +my %ctx = test_init(); require Hydra::Schema; require Hydra::Model::DB; diff --git a/tests/lib/Setup.pm b/tests/lib/Setup.pm index c697a258..3d1ef0fd 100644 --- a/tests/lib/Setup.pm +++ b/tests/lib/Setup.pm @@ -62,7 +62,10 @@ sub test_init { ); $ENV{'HYDRA_DBI'} = $pgsql->dsn; system("hydra-init") == 0 or die; - return ($dir, $pgsql); + return ( + tmpdir => $dir, + db => $pgsql + ); } sub captureStdoutStderr { diff --git a/tests/plugins/runcommand.t b/tests/plugins/runcommand.t index de78cb83..7788d5c5 100644 --- a/tests/plugins/runcommand.t +++ b/tests/plugins/runcommand.t @@ -5,7 +5,7 @@ use Cwd; use JSON; use Setup; -(my $datadir, my $pgsql) = test_init( +my %ctx = test_init( hydra_config => q| command = cp "$HYDRA_JSON" "$HYDRA_DATA/joboutput.json" From 9a3af13c514698e48cf7de8d424b05b498353a43 Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Thu, 4 Mar 2021 17:22:37 -0800 Subject: [PATCH 2/3] tests: add jobsdir and testdir to ctx hash This allows us to calculate those directories only once, and just pass them around as necessary. --- tests/build-products.t | 2 +- tests/evaluate-basic.t | 4 ++-- tests/evaluate-dependent-jobsets.t | 2 +- tests/input-types/bzr-checkout.t | 7 ++++--- tests/input-types/bzr.t | 7 ++++--- tests/input-types/darcs.t | 7 ++++--- tests/input-types/deepgit.t | 7 ++++--- tests/input-types/git-rev.t | 7 ++++--- tests/input-types/git.t | 7 ++++--- tests/input-types/hg.t | 7 ++++--- tests/input-types/svn-checkout.t | 7 ++++--- tests/input-types/svn.t | 7 ++++--- tests/lib/Setup.pm | 15 +++++++++------ tests/lib/TestScmInput.pm | 3 ++- tests/plugins/runcommand.t | 4 ++-- 15 files changed, 53 insertions(+), 40 deletions(-) diff --git a/tests/build-products.t b/tests/build-products.t index 5f1610f9..740f5b17 100644 --- a/tests/build-products.t +++ b/tests/build-products.t @@ -15,7 +15,7 @@ hydra_setup($db); # Test build products -my $jobset = createBaseJobset("build-products", "build-products.nix"); +my $jobset = createBaseJobset("build-products", "build-products.nix", $ctx{jobsdir}); ok(evalSucceeds($jobset), "Evaluating jobs/build-products.nix should exit with return code 0"); is(nrQueuedBuildsForJobset($jobset), 2, "Evaluating jobs/build-products.nix should result in 2 builds"); diff --git a/tests/evaluate-basic.t b/tests/evaluate-basic.t index d143b425..73d81b4a 100644 --- a/tests/evaluate-basic.t +++ b/tests/evaluate-basic.t @@ -16,7 +16,7 @@ hydra_setup($db); my $project = $db->resultset('Projects')->create({name => "tests", displayname => "", owner => "root"}); # Most basic test case, no parameters -my $jobset = createBaseJobset("basic", "basic.nix"); +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"); @@ -29,4 +29,4 @@ for my $build (queuedBuildsForJobset($jobset)) { is($newbuild->buildstatus, $expected, "Build '".$build->job."' from jobs/basic.nix should have buildstatus $expected."); } -done_testing; \ No newline at end of file +done_testing; diff --git a/tests/evaluate-dependent-jobsets.t b/tests/evaluate-dependent-jobsets.t index fa849258..3b2ac8ec 100644 --- a/tests/evaluate-dependent-jobsets.t +++ b/tests/evaluate-dependent-jobsets.t @@ -13,7 +13,7 @@ my $db = Hydra::Model::DB->new; hydra_setup($db); # Test jobset with 2 jobs, one has parameter of succeeded build of the other -my $jobset = createJobsetWithOneInput("build-output-as-input", "build-output-as-input.nix", "build1", "build", "build1"); +my $jobset = createJobsetWithOneInput("build-output-as-input", "build-output-as-input.nix", "build1", "build", "build1", $ctx{jobsdir}); ok(evalSucceeds($jobset), "Evaluating jobs/build-output-as-input.nix should exit with return code 0"); is(nrQueuedBuildsForJobset($jobset), 1 , "Evaluation should result in 1 build in queue"); diff --git a/tests/input-types/bzr-checkout.t b/tests/input-types/bzr-checkout.t index 2bcf556f..c21151f3 100644 --- a/tests/input-types/bzr-checkout.t +++ b/tests/input-types/bzr-checkout.t @@ -3,7 +3,7 @@ use Cwd; use Setup; use TestScmInput; -(my $datadir, my $pgsql) = test_init(); +my %ctx = test_init(); require Hydra::Schema; require Hydra::Model::DB; @@ -21,8 +21,9 @@ testScmInput( update => 'jobs/bzr-checkout-update.sh', # directories - datadir => $datadir, - testdir => getcwd, + datadir => $ctx{tmpdir}, + testdir => $ctx{testdir}, + jobsdir => $ctx{jobsdir}, ); done_testing; diff --git a/tests/input-types/bzr.t b/tests/input-types/bzr.t index 345f5aca..99f005bc 100644 --- a/tests/input-types/bzr.t +++ b/tests/input-types/bzr.t @@ -3,7 +3,7 @@ use Cwd; use Setup; use TestScmInput; -(my $datadir, my $pgsql) = test_init(); +my %ctx = test_init(); require Hydra::Schema; require Hydra::Model::DB; @@ -21,8 +21,9 @@ testScmInput( update => 'jobs/bzr-update.sh', # directories - datadir => $datadir, - testdir => getcwd, + datadir => $ctx{tmpdir}, + testdir => $ctx{testdir}, + jobsdir => $ctx{jobsdir}, ); done_testing; diff --git a/tests/input-types/darcs.t b/tests/input-types/darcs.t index afbbd630..8a6f02dc 100644 --- a/tests/input-types/darcs.t +++ b/tests/input-types/darcs.t @@ -3,7 +3,7 @@ use Cwd; use Setup; use TestScmInput; -(my $datadir, my $pgsql) = test_init(); +my %ctx = test_init(); require Hydra::Schema; require Hydra::Model::DB; @@ -21,8 +21,9 @@ testScmInput( update => 'jobs/darcs-update.sh', # directories - datadir => $datadir, - testdir => getcwd, + datadir => $ctx{tmpdir}, + testdir => $ctx{testdir}, + jobsdir => $ctx{jobsdir}, ); done_testing; diff --git a/tests/input-types/deepgit.t b/tests/input-types/deepgit.t index 7db0e2ce..d3dfa646 100644 --- a/tests/input-types/deepgit.t +++ b/tests/input-types/deepgit.t @@ -3,7 +3,7 @@ use Cwd; use Setup; use TestScmInput; -(my $datadir, my $pgsql) = test_init(); +my %ctx = test_init(); require Hydra::Schema; require Hydra::Model::DB; @@ -22,8 +22,9 @@ testScmInput( update => 'jobs/git-update.sh', # directories - datadir => $datadir, - testdir => getcwd, + datadir => $ctx{tmpdir}, + testdir => $ctx{testdir}, + jobsdir => $ctx{jobsdir}, ); done_testing; diff --git a/tests/input-types/git-rev.t b/tests/input-types/git-rev.t index 58d23a99..665e742b 100644 --- a/tests/input-types/git-rev.t +++ b/tests/input-types/git-rev.t @@ -3,7 +3,7 @@ use Cwd; use Setup; use TestScmInput; -(my $datadir, my $pgsql) = test_init(); +my %ctx = test_init(); require Hydra::Schema; require Hydra::Model::DB; @@ -22,8 +22,9 @@ testScmInput( update => 'jobs/git-rev-update.sh', # directories - datadir => $datadir, - testdir => getcwd, + datadir => $ctx{tmpdir}, + testdir => $ctx{testdir}, + jobsdir => $ctx{jobsdir}, ); done_testing; diff --git a/tests/input-types/git.t b/tests/input-types/git.t index 82eafa97..eba7c2b2 100644 --- a/tests/input-types/git.t +++ b/tests/input-types/git.t @@ -3,7 +3,7 @@ use Cwd; use Setup; use TestScmInput; -(my $datadir, my $pgsql) = test_init(); +my %ctx = test_init(); require Hydra::Schema; require Hydra::Model::DB; @@ -21,8 +21,9 @@ testScmInput( update => 'jobs/git-update.sh', # directories - datadir => $datadir, - testdir => getcwd, + datadir => $ctx{tmpdir}, + testdir => $ctx{testdir}, + jobsdir => $ctx{jobsdir}, ); done_testing; diff --git a/tests/input-types/hg.t b/tests/input-types/hg.t index d0bdc8d8..756e9653 100644 --- a/tests/input-types/hg.t +++ b/tests/input-types/hg.t @@ -3,7 +3,7 @@ use Cwd; use Setup; use TestScmInput; -(my $datadir, my $pgsql) = test_init(); +my %ctx = test_init(); require Hydra::Schema; require Hydra::Model::DB; @@ -21,8 +21,9 @@ testScmInput( update => 'jobs/hg-update.sh', # directories - datadir => $datadir, - testdir => getcwd, + datadir => $ctx{tmpdir}, + testdir => $ctx{testdir}, + jobsdir => $ctx{jobsdir}, ); done_testing; diff --git a/tests/input-types/svn-checkout.t b/tests/input-types/svn-checkout.t index 7c8543c3..e8dd7d95 100644 --- a/tests/input-types/svn-checkout.t +++ b/tests/input-types/svn-checkout.t @@ -3,7 +3,7 @@ use Cwd; use Setup; use TestScmInput; -(my $datadir, my $pgsql) = test_init(); +my %ctx = test_init(); require Hydra::Schema; require Hydra::Model::DB; @@ -21,8 +21,9 @@ testScmInput( update => 'jobs/svn-checkout-update.sh', # directories - datadir => $datadir, - testdir => getcwd, + datadir => $ctx{tmpdir}, + testdir => $ctx{testdir}, + jobsdir => $ctx{jobsdir}, ); done_testing; diff --git a/tests/input-types/svn.t b/tests/input-types/svn.t index 091a6c74..64e677b6 100644 --- a/tests/input-types/svn.t +++ b/tests/input-types/svn.t @@ -3,7 +3,7 @@ use Cwd; use Setup; use TestScmInput; -(my $datadir, my $pgsql) = test_init(); +my %ctx = test_init(); require Hydra::Schema; require Hydra::Model::DB; @@ -21,8 +21,9 @@ testScmInput( update => 'jobs/svn-update.sh', # directories - datadir => $datadir, - testdir => getcwd, + datadir => $ctx{tmpdir}, + testdir => $ctx{testdir}, + jobsdir => $ctx{jobsdir}, ); done_testing; diff --git a/tests/lib/Setup.pm b/tests/lib/Setup.pm index 3d1ef0fd..3cc9828e 100644 --- a/tests/lib/Setup.pm +++ b/tests/lib/Setup.pm @@ -5,7 +5,8 @@ use Exporter; use Test::PostgreSQL; use File::Temp; use File::Path qw(make_path); -use Cwd; +use File::Basename; +use Cwd qw(abs_path getcwd); our @ISA = qw(Exporter); our @EXPORT = qw(test_init hydra_setup nrBuildsForJobset queuedBuildsForJobset nrQueuedBuildsForJobset createBaseJobset createJobsetWithOneInput evalSucceeds runBuild sendNotifications updateRepository); @@ -63,8 +64,10 @@ sub test_init { $ENV{'HYDRA_DBI'} = $pgsql->dsn; system("hydra-init") == 0 or die; return ( + db => $pgsql, tmpdir => $dir, - db => $pgsql + testdir => abs_path(dirname(__FILE__) . "/.."), + jobsdir => abs_path(dirname(__FILE__) . "/../jobs") ); } @@ -98,7 +101,7 @@ sub nrQueuedBuildsForJobset { } sub createBaseJobset { - my ($jobsetName, $nixexprpath) = @_; + my ($jobsetName, $nixexprpath, $jobspath) = @_; my $db = Hydra::Model::DB->new; my $project = $db->resultset('Projects')->update_or_create({name => "tests", displayname => "", owner => "root"}); @@ -108,14 +111,14 @@ sub createBaseJobset { my $jobsetinputals; $jobsetinput = $jobset->jobsetinputs->create({name => "jobs", type => "path"}); - $jobsetinputals = $jobsetinput->jobsetinputalts->create({altnr => 0, value => getcwd."/jobs"}); + $jobsetinputals = $jobsetinput->jobsetinputalts->create({altnr => 0, value => $jobspath}); return $jobset; } sub createJobsetWithOneInput { - my ($jobsetName, $nixexprpath, $name, $type, $uri) = @_; - my $jobset = createBaseJobset($jobsetName, $nixexprpath); + my ($jobsetName, $nixexprpath, $name, $type, $uri, $jobspath) = @_; + my $jobset = createBaseJobset($jobsetName, $nixexprpath, $jobspath); my $jobsetinput; my $jobsetinputals; diff --git a/tests/lib/TestScmInput.pm b/tests/lib/TestScmInput.pm index 3aa720d0..acf7fa27 100644 --- a/tests/lib/TestScmInput.pm +++ b/tests/lib/TestScmInput.pm @@ -36,6 +36,7 @@ sub testScmInput { # Get directories my $testdir = $args{testdir} // die "required arg 'testdir' missing"; my $datadir = $args{datadir} // die "required arg 'datadir' missing"; + my $jobsdir = $args{jobsdir} // die "required arg 'jobsdir' missing"; my $update = $args{update} // die "required arg 'update' missing"; $update = "$testdir/$update"; @@ -49,7 +50,7 @@ sub testScmInput { $uri = "file://$scratchdir/$uri"; subtest "With the SCM input named $name" => sub { - my $jobset = createJobsetWithOneInput($name, $expr, 'src', $type, $uri); + my $jobset = createJobsetWithOneInput($name, $expr, 'src', $type, $uri, $jobsdir); my ($mutations, $queueSize) = (0, 0); diff --git a/tests/plugins/runcommand.t b/tests/plugins/runcommand.t index 7788d5c5..9dd1fbfe 100644 --- a/tests/plugins/runcommand.t +++ b/tests/plugins/runcommand.t @@ -23,7 +23,7 @@ hydra_setup($db); my $project = $db->resultset('Projects')->create({name => "tests", displayname => "", owner => "root"}); # Most basic test case, no parameters -my $jobset = createBaseJobset("basic", "runcommand.nix"); +my $jobset = createBaseJobset("basic", "runcommand.nix", $ctx{jobsdir}); ok(evalSucceeds($jobset), "Evaluating jobs/runcommand.nix should exit with return code 0"); is(nrQueuedBuildsForJobset($jobset), 1, "Evaluating jobs/runcommand.nix should result in 1 build1"); @@ -138,4 +138,4 @@ subtest "Validate the products match" => sub { }; }; -done_testing; \ No newline at end of file +done_testing; From 014778344c7aeea6b73d17802229e5011d2ac1e0 Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Thu, 4 Mar 2021 23:42:56 -0800 Subject: [PATCH 3/3] tests: remove unnecessary Cwd imports --- tests/build-products.t | 1 - tests/evaluate-basic.t | 1 - tests/evaluate-dependent-jobsets.t | 1 - tests/input-types/bzr-checkout.t | 1 - tests/input-types/bzr.t | 1 - tests/input-types/darcs.t | 1 - tests/input-types/deepgit.t | 1 - tests/input-types/git-rev.t | 1 - tests/input-types/git.t | 1 - tests/input-types/hg.t | 1 - tests/input-types/svn-checkout.t | 1 - tests/input-types/svn.t | 1 - tests/plugins/runcommand.t | 1 - 13 files changed, 13 deletions(-) diff --git a/tests/build-products.t b/tests/build-products.t index 740f5b17..0f558d86 100644 --- a/tests/build-products.t +++ b/tests/build-products.t @@ -1,5 +1,4 @@ use strict; -use Cwd; use Setup; my %ctx = test_init(); diff --git a/tests/evaluate-basic.t b/tests/evaluate-basic.t index 73d81b4a..85d5547b 100644 --- a/tests/evaluate-basic.t +++ b/tests/evaluate-basic.t @@ -1,6 +1,5 @@ use feature 'unicode_strings'; use strict; -use Cwd; use Setup; my %ctx = test_init(); diff --git a/tests/evaluate-dependent-jobsets.t b/tests/evaluate-dependent-jobsets.t index 3b2ac8ec..0bf3a2f0 100644 --- a/tests/evaluate-dependent-jobsets.t +++ b/tests/evaluate-dependent-jobsets.t @@ -1,5 +1,4 @@ use strict; -use Cwd; use Setup; my %ctx = test_init(); diff --git a/tests/input-types/bzr-checkout.t b/tests/input-types/bzr-checkout.t index c21151f3..ee1f90a0 100644 --- a/tests/input-types/bzr-checkout.t +++ b/tests/input-types/bzr-checkout.t @@ -1,5 +1,4 @@ use strict; -use Cwd; use Setup; use TestScmInput; diff --git a/tests/input-types/bzr.t b/tests/input-types/bzr.t index 99f005bc..0cc0a23d 100644 --- a/tests/input-types/bzr.t +++ b/tests/input-types/bzr.t @@ -1,5 +1,4 @@ use strict; -use Cwd; use Setup; use TestScmInput; diff --git a/tests/input-types/darcs.t b/tests/input-types/darcs.t index 8a6f02dc..aa95a8e6 100644 --- a/tests/input-types/darcs.t +++ b/tests/input-types/darcs.t @@ -1,5 +1,4 @@ use strict; -use Cwd; use Setup; use TestScmInput; diff --git a/tests/input-types/deepgit.t b/tests/input-types/deepgit.t index d3dfa646..4d418148 100644 --- a/tests/input-types/deepgit.t +++ b/tests/input-types/deepgit.t @@ -1,5 +1,4 @@ use strict; -use Cwd; use Setup; use TestScmInput; diff --git a/tests/input-types/git-rev.t b/tests/input-types/git-rev.t index 665e742b..866370d2 100644 --- a/tests/input-types/git-rev.t +++ b/tests/input-types/git-rev.t @@ -1,5 +1,4 @@ use strict; -use Cwd; use Setup; use TestScmInput; diff --git a/tests/input-types/git.t b/tests/input-types/git.t index eba7c2b2..0804cc1a 100644 --- a/tests/input-types/git.t +++ b/tests/input-types/git.t @@ -1,5 +1,4 @@ use strict; -use Cwd; use Setup; use TestScmInput; diff --git a/tests/input-types/hg.t b/tests/input-types/hg.t index 756e9653..28552288 100644 --- a/tests/input-types/hg.t +++ b/tests/input-types/hg.t @@ -1,5 +1,4 @@ use strict; -use Cwd; use Setup; use TestScmInput; diff --git a/tests/input-types/svn-checkout.t b/tests/input-types/svn-checkout.t index e8dd7d95..48f07f09 100644 --- a/tests/input-types/svn-checkout.t +++ b/tests/input-types/svn-checkout.t @@ -1,5 +1,4 @@ use strict; -use Cwd; use Setup; use TestScmInput; diff --git a/tests/input-types/svn.t b/tests/input-types/svn.t index 64e677b6..c2eb8cec 100644 --- a/tests/input-types/svn.t +++ b/tests/input-types/svn.t @@ -1,5 +1,4 @@ use strict; -use Cwd; use Setup; use TestScmInput; diff --git a/tests/plugins/runcommand.t b/tests/plugins/runcommand.t index 9dd1fbfe..1945e1dd 100644 --- a/tests/plugins/runcommand.t +++ b/tests/plugins/runcommand.t @@ -1,7 +1,6 @@ use feature 'unicode_strings'; use strict; use warnings; -use Cwd; use JSON; use Setup;