From b2520267a91f0e2284e8dce766ebc64a423d977d Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Tue, 23 Feb 2021 16:08:25 -0500 Subject: [PATCH] Test setup: support arbitrary hydra config --- tests/lib/Setup.pm | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/tests/lib/Setup.pm b/tests/lib/Setup.pm index eba61aef..5e2bc91e 100644 --- a/tests/lib/Setup.pm +++ b/tests/lib/Setup.pm @@ -10,7 +10,30 @@ use Cwd; our @ISA = qw(Exporter); our @EXPORT = qw(test_init hydra_setup nrBuildsForJobset queuedBuildsForJobset nrQueuedBuildsForJobset createBaseJobset createJobsetWithOneInput evalSucceeds runBuild updateRepository); -sub test_init() { +# Set up the environment for running tests. +# +# Hash Parameters: +# +# * hydra_config: configuration for the Hydra processes for your test. +# +# This clears several environment variables and sets them to ephemeral +# values: a temporary database, temporary Nix store, temporary Hydra +# data directory, etc. +# +# Note: This function must run _very_ early, before nearly any Hydra +# libraries are loaded. To use this, you very likely need to `use Setup` +# and then run `test_init`, and then `require` the Hydra libraries you +# need. +# +# It returns a tuple: a handle to a temporary directory and a handle to +# the postgres service. If either of these variables go out of scope, +# those resources are released and the test environment becomes invalid. +# +# Look at the top of an existing `.t` file to see how this should be used +# in practice. +sub test_init { + my %opts = @_; + my $dir = File::Temp->newdir(); $ENV{'HYDRA_DATA'} = "$dir/hydra-data"; @@ -22,6 +45,12 @@ sub test_init() { print $fh "sandbox = false\n"; close $fh; + $ENV{'HYDRA_CONFIG'} = "$dir/hydra.conf"; + + open(my $fh, '>', $ENV{'HYDRA_CONFIG'}) or die "Could not open file '" . $ENV{'HYDRA_CONFIG'}. " $!"; + print $fh $opts{'hydra_config'} || ""; + close $fh; + $ENV{'NIX_STATE_DIR'} = "$dir/nix/var/nix"; $ENV{'NIX_MANIFESTS_DIR'} = "$dir/nix/var/nix/manifests";