Create an ephemeral PostgreSQL database per test

This commit is contained in:
Graham Christensen 2021-02-22 13:47:41 -05:00
parent b15d8edab1
commit fe1f2f0806
No known key found for this signature in database
GPG key ID: FE918C3A98C1030F
3 changed files with 34 additions and 10 deletions

View file

@ -13,10 +13,16 @@ struct Connection : pqxx::connection
{
using namespace nix;
auto s = getEnv("HYDRA_DBI").value_or("dbi:Pg:dbname=hydra;");
std::string prefix = "dbi:Pg:";
if (std::string(s, 0, prefix.size()) != prefix)
throw Error("$HYDRA_DBI does not denote a PostgreSQL database");
return concatStringsSep(" ", tokenizeString<Strings>(string(s, prefix.size()), ";"));
std::string lower_prefix = "dbi:Pg:";
std::string upper_prefix = "DBI:Pg:";
if ((std::string(s, 0, lower_prefix.size()) == lower_prefix) ||
(std::string(s, 0, upper_prefix.size()) == upper_prefix)) {
return concatStringsSep(" ", tokenizeString<Strings>(string(s, lower_prefix.size()), ";"));
}
throw Error("$HYDRA_DBI does not denote a PostgreSQL database");
}
};

View file

@ -1,13 +1,16 @@
use strict;
use Hydra::Schema;
use Hydra::Model::DB;
use Cwd;
use Setup;
my $db = Hydra::Model::DB->new;
my $pgsql = dbinit();
my $dsn = $pgsql->dsn;
require Hydra::Schema;
require Hydra::Model::DB;
use Test::Simple tests => 76;
my $db = Hydra::Model::DB->new;
hydra_setup($db);
my $res;

View file

@ -2,12 +2,27 @@ package Setup;
use strict;
use Exporter;
use Hydra::Helper::Nix;
use Hydra::Model::DB;
use Test::PostgreSQL;
use Cwd;
our @ISA = qw(Exporter);
our @EXPORT = qw(hydra_setup nrBuildsForJobset queuedBuildsForJobset nrQueuedBuildsForJobset createBaseJobset createJobsetWithOneInput evalSucceeds runBuild updateRepository);
our @EXPORT = qw(dbinit hydra_setup nrBuildsForJobset queuedBuildsForJobset nrQueuedBuildsForJobset createBaseJobset createJobsetWithOneInput evalSucceeds runBuild updateRepository);
sub dbinit() {
my $pgsql = Test::PostgreSQL->new();
$ENV{'HYDRA_DBI'} = $pgsql->dsn;
system("hydra-init") == 0 or die;
return $pgsql;
}
sub captureStdoutStderr {
# "Lazy"-load Hydra::Helper::Nix to avoid the compile-time
# import of Hydra::Model::DB. Early loading of the DB class
# causes fixation of the DSN, and we need to fixate it after
# the temporary DB is setup.
require Hydra::Helper::Nix;
return Hydra::Helper::Nix::captureStdoutStderr(@_)
}
sub hydra_setup {
my ($db) = @_;