Merge pull request #1076 from DeterminateSystems/HydraTestContext

HydraTestContext: init a context object for helpers
This commit is contained in:
Graham Christensen 2021-12-14 21:22:43 -05:00 committed by GitHub
commit 42f0dee441
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 347 additions and 220 deletions

View file

@ -2,31 +2,25 @@ use feature 'unicode_strings';
use strict; use strict;
use warnings; use warnings;
use Setup; use Setup;
my %ctx = test_init();
require Hydra::Schema;
require Hydra::Model::DB;
use Test2::V0; use Test2::V0;
my $ctx = test_context();
require Hydra; # calls setup() require Hydra; # calls setup()
my $db = Hydra::Model::DB->new;
hydra_setup($db);
require Hydra::View::TT; require Hydra::View::TT;
require Catalyst::Test;
my $db = $ctx->db;
# The following lines are a cheap and hacky trick to get $c, # The following lines are a cheap and hacky trick to get $c,
# there is no other reason to call /. # there is no other reason to call /.
require Catalyst::Test;
Catalyst::Test->import('Hydra'); Catalyst::Test->import('Hydra');
my ($_request, $c) = ctx_request('/'); my ($_request, $c) = ctx_request('/');
my $project = $db->resultset('Projects')->create({name => "tests", displayname => "", owner => "root"}); my $project = $db->resultset('Projects')->create({name => "tests", displayname => "", owner => "root"});
my $jobset = createBaseJobset("example", "bogus.nix", $ctx{jobsdir}); my $jobset = createBaseJobset("example", "bogus.nix", $ctx->jobsdir);
my $job = "myjob"; my $job = "myjob";

View file

@ -1,43 +1,36 @@
use strict; use strict;
use warnings; use warnings;
use Setup; use Setup;
my %ctx = test_init();
require Hydra::Schema;
require Hydra::Model::DB;
use Test2::V0; use Test2::V0;
my $db = Hydra::Model::DB->new; my $ctx = test_context();
hydra_setup($db);
# Test build products # Test build products
my $jobset = createBaseJobset("build-products", "build-products.nix", $ctx{jobsdir}); my $builds = $ctx->makeAndEvaluateJobset(
expression => "build-products.nix",
build => 1
);
ok(evalSucceeds($jobset), "Evaluating jobs/build-products.nix should exit with return code 0"); subtest "For the build job 'simple'" => sub {
is(nrQueuedBuildsForJobset($jobset), 2, "Evaluating jobs/build-products.nix should result in 2 builds"); my $build = $builds->{"simple"};
for my $build (queuedBuildsForJobset($jobset)) { is($build->finished, 1, "Build should have finished");
subtest "For the build job '" . $build->job . "'" => sub { is($build->buildstatus, 0, "Build should have buildstatus 0");
ok(runBuild($build), "Build should exit with return code 0");
my $newbuild = $db->resultset('Builds')->find($build->id);
is($newbuild->finished, 1, "Build should have finished"); my $buildproduct = $build->buildproducts->next;
is($newbuild->buildstatus, 0, "Build should have buildstatus 0"); is($buildproduct->name, "text.txt", "We should have \"text.txt\"");
};
my $buildproducts = $db->resultset('BuildProducts')->search({ build => $build->id }); subtest "For the build job 'with_spaces'" => sub {
my $buildproduct = $buildproducts->next; my $build = $builds->{"with_spaces"};
if($build->job eq "simple") { is($build->finished, 1, "Build should have finished");
is($buildproduct->name, "text.txt", "We should have \"text.txt\""); is($build->buildstatus, 0, "Build should have buildstatus 0");
} elsif ($build->job eq "with_spaces") {
is($buildproduct->name, "some text.txt", "We should have: \"some text.txt\""); my $buildproduct = $build->buildproducts->next;
} is($buildproduct->name, "some text.txt", "We should have: \"some text.txt\"");
}; };
}
done_testing; done_testing;

View file

@ -2,31 +2,34 @@ use feature 'unicode_strings';
use strict; use strict;
use warnings; use warnings;
use Setup; use Setup;
my %ctx = test_init();
require Hydra::Schema;
require Hydra::Model::DB;
use Test2::V0; use Test2::V0;
my $db = Hydra::Model::DB->new; my $ctx = test_context();
hydra_setup($db);
my $project = $db->resultset('Projects')->create({name => "tests", displayname => "", owner => "root"}); my $builds = $ctx->makeAndEvaluateJobset(
expression => "basic.nix",
build => 1
);
# Most basic test case, no parameters subtest "Build: succeed_with_failed" => sub {
my $jobset = createBaseJobset("basic", "basic.nix", $ctx{jobsdir}); my $build = $builds->{"succeed_with_failed"};
ok(evalSucceeds($jobset), "Evaluating jobs/basic.nix should exit with return code 0"); is($build->finished, 1, "Build should be finished.");
is(nrQueuedBuildsForJobset($jobset), 3, "Evaluating jobs/basic.nix should result in 3 builds"); is($build->buildstatus, 6, "succeeeded-but-failed should have buildstatus 6.");
};
for my $build (queuedBuildsForJobset($jobset)) { subtest "Build: empty_dir" => sub {
ok(runBuild($build), "Build '".$build->job."' from jobs/basic.nix should exit with return code 0"); my $build = $builds->{"empty_dir"};
my $newbuild = $db->resultset('Builds')->find($build->id);
is($newbuild->finished, 1, "Build '".$build->job."' from jobs/basic.nix should be finished."); is($build->finished, 1, "Build should be finished.");
my $expected = $build->job eq "fails" ? 1 : $build->job =~ /with_failed/ ? 6 : 0; is($build->buildstatus, 0, "Should have succeeded.");
is($newbuild->buildstatus, $expected, "Build '".$build->job."' from jobs/basic.nix should have buildstatus $expected."); };
}
subtest "Build: fails" => sub {
my $build = $builds->{"fails"};
is($build->finished, 1, "Build should be finished.");
is($build->buildstatus, 1, "Should have failed.");
};
done_testing; done_testing;

50
t/lib/CliRunners.pm Normal file
View file

@ -0,0 +1,50 @@
use warnings;
use strict;
package CliRunners;
our @ISA = qw(Exporter);
our @EXPORT = qw(
evalSucceeds runBuild sendNotifications
captureStdoutStderr);
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 evalSucceeds {
my ($jobset) = @_;
my ($res, $stdout, $stderr) = captureStdoutStderr(60, ("hydra-eval-jobset", $jobset->project->name, $jobset->name));
$jobset->discard_changes; # refresh from DB
chomp $stdout; chomp $stderr;
print STDERR "Evaluation errors for jobset ".$jobset->project->name.":".$jobset->name.": \n".$jobset->errormsg."\n" if $jobset->errormsg;
print STDERR "STDOUT: $stdout\n" if $stdout ne "";
print STDERR "STDERR: $stderr\n" if $stderr ne "";
return !$res;
}
sub runBuild {
my ($build) = @_;
my ($res, $stdout, $stderr) = captureStdoutStderr(60, ("hydra-queue-runner", "-vvvv", "--build-one", $build->id));
if ($res) {
print STDERR "Queue runner stdout: $stdout\n" if $stdout ne "";
print STDERR "Queue runner stderr: $stderr\n" if $stderr ne "";
}
return !$res;
}
sub sendNotifications() {
my ($res, $stdout, $stderr) = captureStdoutStderr(60, ("hydra-notify", "--queued-only"));
if ($res) {
print STDERR "hydra notify stdout: $stdout\n" if $stdout ne "";
print STDERR "hydra notify stderr: $stderr\n" if $stderr ne "";
}
return !$res;
}
1;

195
t/lib/HydraTestContext.pm Normal file
View file

@ -0,0 +1,195 @@
use strict;
use warnings;
package HydraTestContext;
use File::Path qw(make_path);
use File::Basename;
use Cwd qw(abs_path getcwd);
use CliRunners;
# Set up the environment for running tests.
#
# Hash Parameters:
#
# * hydra_config: configuration for the Hydra processes for your test.
# * nix_config: text to include in the test's nix.conf
# * use_external_destination_store: Boolean indicating whether hydra should
# use a destination store different from the evaluation store.
# True by default.
#
# 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 new {
my ($class, %opts) = @_;
my $dir = File::Temp->newdir();
$ENV{'HYDRA_DATA'} = "$dir/hydra-data";
mkdir $ENV{'HYDRA_DATA'};
$ENV{'NIX_CONF_DIR'} = "$dir/nix/etc/nix";
make_path($ENV{'NIX_CONF_DIR'});
my $nixconf = "$ENV{'NIX_CONF_DIR'}/nix.conf";
my $nix_config = "sandbox = false\n" . ($opts{'nix_config'} || "");
write_file($nixconf, $nix_config);
$ENV{'HYDRA_CONFIG'} = "$dir/hydra.conf";
my $hydra_config = $opts{'hydra_config'} || "";
if ($opts{'use_external_destination_store'} // 1) {
$hydra_config = "store_uri = file:$dir/nix/dest-store\n" . $hydra_config;
}
write_file($ENV{'HYDRA_CONFIG'}, $hydra_config);
$ENV{'NIX_LOG_DIR'} = "$dir/nix/var/log/nix";
$ENV{'NIX_REMOTE_SYSTEMS'} = '';
$ENV{'NIX_REMOTE'} = '';
$ENV{'NIX_STATE_DIR'} = "$dir/nix/var/nix";
$ENV{'NIX_STORE_DIR'} = "$dir/nix/store";
my $pgsql = Test::PostgreSQL->new(
extra_initdb_args => "--locale C.UTF-8"
);
$ENV{'HYDRA_DBI'} = $pgsql->dsn;
system("hydra-init") == 0 or die;
my $self = {
_db => undef,
db_handle => $pgsql,
tmpdir => $dir,
testdir => abs_path(dirname(__FILE__) . "/.."),
jobsdir => abs_path(dirname(__FILE__) . "/../jobs")
};
return bless $self, $class;
}
sub db {
my ($self, $setup) = @_;
if (!defined $self->{_db}) {
require Hydra::Schema;
require Hydra::Model::DB;
$self->{_db} = Hydra::Model::DB->new();
if (!(defined $setup && $setup == 0)) {
$self->{_db}->resultset('Users')->create({
username => "root",
emailaddress => 'root@invalid.org',
password => ''
});
}
}
return $self->{_db};
}
sub tmpdir {
my ($self) = @_;
return $self->{tmpdir};
}
sub testdir {
my ($self) = @_;
return $self->{testdir};
}
sub jobsdir {
my ($self) = @_;
return $self->{jobsdir};
}
# Create a jobset, evaluate it, and optionally build the jobs.
#
# In return, you get a hash of all the Builds records, keyed
# by their Nix attribute name.
#
# This always uses an `expression` from the `jobsdir` directory.
#
# Hash Parameters:
#
# * expression: The file in the jobsdir directory to evaluate
# * build: Bool. Attempt to build all the resulting jobs. Default: false.
sub makeAndEvaluateJobset {
my ($self, %opts) = @_;
my $expression = $opts{'expression'} || die "Mandatory 'expression' option not passed to makeAndEValuateJobset.";
my $should_build = $opts{'build'} // 0;
# Create a new user for this test
my $user = $self->db()->resultset('Users')->create({
username => rand_chars(),
emailaddress => rand_chars() . '@example.org',
password => ''
});
# Create a new project for this test
my $project = $self->db()->resultset('Projects')->create({
name => rand_chars(),
displayname => rand_chars(),
owner => $user->username
});
# Create a new jobset for this test and set up the inputs
my $jobset = $project->jobsets->create({
name => rand_chars(),
nixexprinput => "jobs",
nixexprpath => $expression,
emailoverride => ""
});
my $jobsetinput = $jobset->jobsetinputs->create({name => "jobs", type => "path"});
$jobsetinput->jobsetinputalts->create({altnr => 0, value => $self->jobsdir});
evalSucceeds($jobset) or die "Evaluating jobs/$expression should exit with return code 0";
my $builds = {};
for my $build ($jobset->builds) {
if ($should_build) {
runBuild($build) or die "Build '".$build->job."' from jobs/$expression should exit with return code 0";
$build->discard_changes();
}
$builds->{$build->job} = $build;
}
return $builds;
}
sub DESTROY
{
my ($self) = @_;
$self->db(0)->schema->storage->disconnect();
$self->{db_handle}->stop();
}
sub write_file {
my ($path, $text) = @_;
open(my $fh, '>', $path) or die "Could not open file '$path' $!";
print $fh $text || "";
close $fh;
}
sub rand_chars {
return sprintf("%08X", rand(0xFFFFFFFF));
}
1;

View file

@ -8,76 +8,35 @@ use File::Temp;
use File::Path qw(make_path); 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;
our @ISA = qw(Exporter); our @ISA = qw(Exporter);
our @EXPORT = qw(test_init hydra_setup write_file nrBuildsForJobset queuedBuildsForJobset our @EXPORT = qw(test_context test_init hydra_setup write_file nrBuildsForJobset queuedBuildsForJobset
nrQueuedBuildsForJobset createBaseJobset createJobsetWithOneInput nrQueuedBuildsForJobset createBaseJobset createJobsetWithOneInput
evalSucceeds runBuild sendNotifications updateRepository evalSucceeds runBuild sendNotifications updateRepository
captureStdoutStderr); captureStdoutStderr);
# Set up the environment for running tests. # Set up the environment for running tests.
# #
# Hash Parameters: # See HydraTestContext::new for documentation
sub test_context {
require HydraTestContext;
return HydraTestContext->new(@_);
}
# Set up the environment for running tests.
# #
# * hydra_config: configuration for the Hydra processes for your test. # See HydraTestContext::new for documentation
# * nix_config: text to include in the test's nix.conf
# * use_external_destination_store: Boolean indicating whether hydra should
# use a destination store different from the evaluation store.
# True by default.
#
# 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 { sub test_init {
my %opts = @_; require HydraTestContext;
my $ctx = HydraTestContext->new(@_);
my $dir = File::Temp->newdir();
$ENV{'HYDRA_DATA'} = "$dir/hydra-data";
mkdir $ENV{'HYDRA_DATA'};
$ENV{'NIX_CONF_DIR'} = "$dir/nix/etc/nix";
make_path($ENV{'NIX_CONF_DIR'});
my $nixconf = "$ENV{'NIX_CONF_DIR'}/nix.conf";
my $nix_config = "sandbox = false\n" . ($opts{'nix_config'} || "");
write_file($nixconf, $nix_config);
$ENV{'HYDRA_CONFIG'} = "$dir/hydra.conf";
my $hydra_config = $opts{'hydra_config'} || "";
if ($opts{'use_external_destination_store'} // 1) {
$hydra_config = "store_uri = file:$dir/nix/dest-store\n" . $hydra_config;
}
write_file($ENV{'HYDRA_CONFIG'}, $hydra_config);
$ENV{'NIX_LOG_DIR'} = "$dir/nix/var/log/nix";
$ENV{'NIX_REMOTE_SYSTEMS'} = '';
$ENV{'NIX_REMOTE'} = '';
$ENV{'NIX_STATE_DIR'} = "$dir/nix/var/nix";
$ENV{'NIX_STORE_DIR'} = "$dir/nix/store";
my $pgsql = Test::PostgreSQL->new(
extra_initdb_args => "--locale C.UTF-8"
);
$ENV{'HYDRA_DBI'} = $pgsql->dsn;
system("hydra-init") == 0 or die;
return ( return (
db => $pgsql, context => $ctx,
tmpdir => $dir, tmpdir => $ctx->tmpdir,
testdir => abs_path(dirname(__FILE__) . "/.."), testdir => $ctx->testdir,
jobsdir => abs_path(dirname(__FILE__) . "/../jobs") jobsdir => $ctx->jobsdir
); )
} }
sub write_file { sub write_file {
@ -87,15 +46,6 @@ sub write_file {
close $fh; close $fh;
} }
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 { sub hydra_setup {
my ($db) = @_; my ($db) = @_;
$db->resultset('Users')->create({ username => "root", emailaddress => 'root@invalid.org', password => '' }); $db->resultset('Users')->create({ username => "root", emailaddress => 'root@invalid.org', password => '' });
@ -145,36 +95,6 @@ sub createJobsetWithOneInput {
return $jobset; return $jobset;
} }
sub evalSucceeds {
my ($jobset) = @_;
my ($res, $stdout, $stderr) = captureStdoutStderr(60, ("hydra-eval-jobset", $jobset->project->name, $jobset->name));
$jobset->discard_changes; # refresh from DB
chomp $stdout; chomp $stderr;
print STDERR "Evaluation errors for jobset ".$jobset->project->name.":".$jobset->name.": \n".$jobset->errormsg."\n" if $jobset->errormsg;
print STDERR "STDOUT: $stdout\n" if $stdout ne "";
print STDERR "STDERR: $stderr\n" if $stderr ne "";
return !$res;
}
sub runBuild {
my ($build) = @_;
my ($res, $stdout, $stderr) = captureStdoutStderr(60, ("hydra-queue-runner", "-vvvv", "--build-one", $build->id));
if ($res) {
print STDERR "Queue runner stdout: $stdout\n" if $stdout ne "";
print STDERR "Queue runner stderr: $stderr\n" if $stderr ne "";
}
return !$res;
}
sub sendNotifications() {
my ($res, $stdout, $stderr) = captureStdoutStderr(60, ("hydra-notify", "--queued-only"));
if ($res) {
print STDERR "hydra notify stdout: $stdout\n" if $stdout ne "";
print STDERR "hydra notify stderr: $stderr\n" if $stderr ne "";
}
return !$res;
}
sub updateRepository { sub updateRepository {
my ($scm, $update, $scratchdir) = @_; my ($scm, $update, $scratchdir) = @_;
my $curdir = getcwd; my $curdir = getcwd;

View file

@ -2,34 +2,21 @@ use feature 'unicode_strings';
use strict; use strict;
use warnings; use warnings;
use Setup; use Setup;
use Test2::V0;
my %ctx = test_init( my $ctx = test_context(
nix_config => q| nix_config => q|
system-features = test-system-feature system-features = test-system-feature
| |
); );
require Hydra::Schema; my $builds = $ctx->makeAndEvaluateJobset(
require Hydra::Model::DB; expression => "default-machine-file.nix",
build => 1,
);
use Test2::V0; my $build = $builds->{"requireExperimentalFeatures"};
is($build->finished, 1, "Build should be finished.");
my $db = Hydra::Model::DB->new; is($build->buildstatus, 0, "Build status should be zero");
hydra_setup($db);
my $project = $db->resultset('Projects')->create({name => "tests", displayname => "", owner => "root"});
my $jobset = createBaseJobset("default-machine-file", "default-machine-file.nix", $ctx{jobsdir});
ok(evalSucceeds($jobset), "Evaluating jobs/default-machine-file.nix should exit with return code 0");
is(nrQueuedBuildsForJobset($jobset), 1, "Evaluating jobs/default-machine-file.nix should result in 1 build");
for my $build (queuedBuildsForJobset($jobset)) {
ok(runBuild($build), "Build '".$build->job."' from jobs/default-machine-file.nix should exit with return code 0");
my $newbuild = $db->resultset('Builds')->find($build->id);
is($newbuild->finished, 1, "Build '".$build->job."' from jobs/default-machine-file.nix should be finished.");
my $expected = $build->job eq "fails" ? 1 : $build->job =~ /with_failed/ ? 6 : 0;
is($newbuild->buildstatus, $expected, "Build '".$build->job."' from jobs/default-machine-file.nix should have buildstatus $expected.");
}
done_testing; done_testing;

View file

@ -1,12 +1,12 @@
use feature 'unicode_strings';
use strict; use strict;
use warnings; use warnings;
use JSON::MaybeXS; use JSON::MaybeXS;
use Setup; use Setup;
use Test2::V0;
my $binarycachedir = File::Temp->newdir(); my $binarycachedir = File::Temp->newdir();
my %ctx = test_init( my $ctx = test_context(
nix_config => qq| nix_config => qq|
experimental-features = nix-command experimental-features = nix-command
substituters = file://${binarycachedir}?trusted=1 substituters = file://${binarycachedir}?trusted=1
@ -18,13 +18,9 @@ my %ctx = test_init(
</runcommand> </runcommand>
|); |);
require Hydra::Schema;
require Hydra::Model::DB;
use Test2::V0;
# Check that hydra's queue runner sends notifications. # Check that hydra's queue runner sends notifications.
# #
# The prelude to the test prebuilds one attribute and puts it in a # The prelude to the test prebuilds one attribute and puts it in a
# binary cache. The jobset will try to build that job plus another, # binary cache. The jobset will try to build that job plus another,
# and we'll be able to check the behavior in both cases. # and we'll be able to check the behavior in both cases.
@ -40,63 +36,55 @@ subtest "Pre-build the job, upload to the cache, and then delete locally" => sub
my $scratchlogdir = File::Temp->newdir(); my $scratchlogdir = File::Temp->newdir();
$ENV{'NIX_LOG_DIR'} = "$scratchlogdir"; $ENV{'NIX_LOG_DIR'} = "$scratchlogdir";
my $outlink = "$ctx{tmpdir}/basic-canbesubstituted"; my $outlink = $ctx->tmpdir . "/basic-canbesubstituted";
is(system("nix-build '${ctx{jobsdir}}/notifications.nix' -A canbesubstituted --out-link '${outlink}'"), 0, "Building notifications.nix succeeded"); is(system('nix-build', $ctx->jobsdir . '/notifications.nix', '-A', 'canbesubstituted', '--out-link', $outlink), 0, "Building notifications.nix succeeded");
is(system("nix copy --to 'file://${binarycachedir}' '${outlink}'"), 0, "Copying the closure to the binary cache succeeded"); is(system('nix', 'copy', '--to', "file://${binarycachedir}", $outlink), 0, "Copying the closure to the binary cache succeeded");
my $outpath = readlink($outlink); my $outpath = readlink($outlink);
# Delete the store path and all of the system's garbage # Delete the store path and all of the system's garbage
is(unlink($outlink), 1, "Deleting the GC root succeeds"); is(unlink($outlink), 1, "Deleting the GC root succeeds");
is(system("nix log '$outpath'"), 0, "Reading the output's log succeeds"); is(system('nix', 'log', $outpath), 0, "Reading the output's log succeeds");
is(system("nix-store --delete '$outpath'"), 0, "Deleting the notifications.nix output succeeded"); is(system('nix-store', '--delete', $outpath), 0, "Deleting the notifications.nix output succeeded");
is(system("nix-collect-garbage"), 0, "Delete all the system's garbage"); is(system("nix-collect-garbage"), 0, "Delete all the system's garbage");
}; };
subtest "Ensure substituting the job works, but reading the log fails" => sub { subtest "Ensure substituting the job works, but reading the log fails" => sub {
# Build the store path, with --max-jobs 0 to prevent builds # Build the store path, with --max-jobs 0 to prevent builds
my $outlink = "$ctx{tmpdir}/basic-canbesubstituted"; my $outlink = $ctx->tmpdir . "/basic-canbesubstituted";
is(system("nix-build '${ctx{jobsdir}}/notifications.nix' -A canbesubstituted --max-jobs 0 --out-link '${outlink}'"), 0, "Building notifications.nix succeeded"); is(system('nix-build', $ctx->jobsdir . '/notifications.nix', '-A', 'canbesubstituted', '--max-jobs', '0', '--out-link', $outlink), 0, "Building notifications.nix succeeded");
my $outpath = readlink($outlink); my $outpath = readlink($outlink);
# Verify trying to read this path's log fails, since we substituted it # Verify trying to read this path's log fails, since we substituted it
isnt(system("nix log '$outpath'"), 0, "Reading the deleted output's log fails"); isnt(system('nix', 'log', $outpath), 0, "Reading the deleted output's log fails");
# Delete the store path again and all of the store's garbage, ensuring # Delete the store path again and all of the store's garbage, ensuring
# Hydra will try to build it. # Hydra will try to build it.
is(unlink($outlink), 1, "Deleting the GC root succeeds"); is(unlink($outlink), 1, "Deleting the GC root succeeds");
is(system("nix-store --delete '$outpath'"), 0, "Deleting the basic output succeeded"); is(system('nix-store', '--delete', $outpath), 0, "Deleting the notifications.nix output succeeded");
is(system("nix-collect-garbage"), 0, "Delete all the system's garbage"); is(system("nix-collect-garbage"), 0, "Delete all the system's garbage");
}; };
my $db = Hydra::Model::DB->new; my $db = $ctx->db();
hydra_setup($db);
my $jobset = createBaseJobset("queue-runner-notifs", "notifications.nix", $ctx{jobsdir});
my $dbh = $db->storage->dbh; my $dbh = $db->storage->dbh;
$dbh->do("listen build_started"); $dbh->do("listen build_started");
$dbh->do("listen build_finished"); $dbh->do("listen build_finished");
$dbh->do("listen step_finished"); $dbh->do("listen step_finished");
subtest "Evaluation of the jobset" => sub { my $builds = $ctx->makeAndEvaluateJobset(
ok(evalSucceeds($jobset), "Evaluation should exit with return code 0"); expression => "notifications.nix",
is(nrQueuedBuildsForJobset($jobset), 2, "Evaluation should result in 2 builds"); build => 1
}; );
my @builds = queuedBuildsForJobset($jobset);
subtest "Build: substitutable, canbesubstituted" => sub { subtest "Build: substitutable, canbesubstituted" => sub {
my ($build) = grep { $_->nixname eq "can-be-substituted" } @builds; my $build = $builds->{"canbesubstituted"};
ok(runBuild($build), "Build should exit with return code 0");
my $newbuild = $db->resultset('Builds')->find($build->id); is($build->finished, 1, "Build should be finished.");
is($newbuild->finished, 1, "Build should be finished."); is($build->buildstatus, 0, "Build should have buildstatus 0.");
is($newbuild->buildstatus, 0, "Build should have buildstatus 0.");
# Verify that hydra-notify will process this job, even if hydra-notify isn't # Verify that hydra-notify will process this job, even if hydra-notify isn't
# running at the time. # running at the time.
isnt($newbuild->notificationpendingsince, undef, "The build has a pending notification"); isnt($build->notificationpendingsince, undef, "The build has a pending notification");
subtest "First notification: build_finished" => sub { subtest "First notification: build_finished" => sub {
my ($channelName, $pid, $payload) = @{$dbh->func("pg_notifies")}; my ($channelName, $pid, $payload) = @{$dbh->func("pg_notifies")};
@ -106,16 +94,13 @@ subtest "Build: substitutable, canbesubstituted" => sub {
}; };
subtest "Build: not substitutable, unsubstitutable" => sub { subtest "Build: not substitutable, unsubstitutable" => sub {
my ($build) = grep { $_->nixname eq "unsubstitutable" } @builds; my $build = $builds->{"unsubstitutable"};
ok(runBuild($build), "Build should exit with return code 0"); is($build->finished, 1, "Build should be finished.");
is($build->buildstatus, 0, "Build should have buildstatus 0.");
my $newbuild = $db->resultset('Builds')->find($build->id);
is($newbuild->finished, 1, "Build should be finished.");
is($newbuild->buildstatus, 0, "Build should have buildstatus 0.");
# Verify that hydra-notify will process this job, even if hydra-notify isn't # Verify that hydra-notify will process this job, even if hydra-notify isn't
# running at the time. # running at the time.
isnt($newbuild->notificationpendingsince, undef, "The build has a pending notification"); isnt($build->notificationpendingsince, undef, "The build has a pending notification");
subtest "First notification: build_started" => sub { subtest "First notification: build_started" => sub {
my ($channelName, $pid, $payload) = @{$dbh->func("pg_notifies")}; my ($channelName, $pid, $payload) = @{$dbh->func("pg_notifies")};