HydraTestContext: support running a sub before running hydra-init.

This commit is contained in:
Graham Christensen 2022-02-11 11:26:27 -05:00
parent 05ca71069f
commit 0bd4a75918

View file

@ -6,6 +6,7 @@ use File::Path qw(make_path);
use File::Basename; use File::Basename;
use Cwd qw(abs_path getcwd); use Cwd qw(abs_path getcwd);
use CliRunners; use CliRunners;
use Hydra::Helper::Exec;
# Set up the environment for running tests. # Set up the environment for running tests.
# #
@ -16,6 +17,9 @@ use CliRunners;
# * use_external_destination_store: Boolean indicating whether hydra should # * use_external_destination_store: Boolean indicating whether hydra should
# use a destination store different from the evaluation store. # use a destination store different from the evaluation store.
# True by default. # True by default.
# * before_init: a sub which is called after the database is up, but before
# hydra-init is executed. It receives the HydraTestContext object as
# its argument.
# #
# This clears several environment variables and sets them to ephemeral # This clears several environment variables and sets them to ephemeral
# values: a temporary database, temporary Nix store, temporary Hydra # values: a temporary database, temporary Nix store, temporary Hydra
@ -63,24 +67,28 @@ sub new {
extra_initdb_args => "--locale C.UTF-8" extra_initdb_args => "--locale C.UTF-8"
); );
$ENV{'HYDRA_DBI'} = $pgsql->dsn; $ENV{'HYDRA_DBI'} = $pgsql->dsn;
system("hydra-init") == 0 or die;
my $self = { my $self = bless {
_db => undef, _db => undef,
db_handle => $pgsql, db_handle => $pgsql,
tmpdir => $dir, tmpdir => $dir,
nix_state_dir => "$dir/nix/var/nix", nix_state_dir => "$dir/nix/var/nix",
testdir => abs_path(dirname(__FILE__) . "/.."), testdir => abs_path(dirname(__FILE__) . "/.."),
jobsdir => abs_path(dirname(__FILE__) . "/../jobs") jobsdir => abs_path(dirname(__FILE__) . "/../jobs")
}; }, $class;
return bless $self, $class; if ($opts{'before_init'}) {
$opts{'before_init'}->($self);
}
expectOkay(5, ("hydra-init"));
return $self;
} }
sub db { sub db {
my ($self, $setup) = @_; my ($self, $setup) = @_;
if (!defined $self->{_db}) { if (!defined $self->{_db}) {
require Hydra::Schema; require Hydra::Schema;
require Hydra::Model::DB; require Hydra::Model::DB;