Skip to content

Commit

Permalink
Merge pull request #1435 from obsidiansystems/flake-tests
Browse files Browse the repository at this point in the history
Test using Hydra with flakes
  • Loading branch information
Ericson2314 authored Feb 7, 2025
2 parents 250668a + 8a8ac14 commit 8d78648
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 9 deletions.
67 changes: 67 additions & 0 deletions t/evaluator/evaluate-flake.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use feature 'unicode_strings';
use strict;
use warnings;
use Setup;
use Test2::V0;
use File::Copy qw(cp);

my $ctx = test_context(
nix_config => qq|
experimental-features = nix-command flakes
|,
hydra_config => q|
<runcommand>
evaluator_pure_eval = false
</runcommand>
|
);

sub checkFlake {
my ($flake) = @_;

cp($ctx->jobsdir . "/basic.nix", $ctx->jobsdir . "/" . $flake);
cp($ctx->jobsdir . "/config.nix", $ctx->jobsdir . "/" . $flake);
cp($ctx->jobsdir . "/empty-dir-builder.sh", $ctx->jobsdir . "/" . $flake);
cp($ctx->jobsdir . "/fail.sh", $ctx->jobsdir . "/" . $flake);
cp($ctx->jobsdir . "/succeed-with-failed.sh", $ctx->jobsdir . "/" . $flake);

chmod 0755, $ctx->jobsdir . "/" . $flake . "/empty-dir-builder.sh";
chmod 0755, $ctx->jobsdir . "/" . $flake . "/fail.sh";
chmod 0755, $ctx->jobsdir . "/" . $flake . "/succeed-with-failed.sh";

my $builds = $ctx->makeAndEvaluateJobset(
flake => 'path:' . $ctx->jobsdir . "/" . $flake,
build => 1
);

subtest "Build: succeed_with_failed" => sub {
my $build = $builds->{"succeed_with_failed"};

is($build->finished, 1, "Build should be finished.");
is($build->buildstatus, 6, "succeeeded-but-failed should have buildstatus 6.");
};

subtest "Build: empty_dir" => sub {
my $build = $builds->{"empty_dir"};

is($build->finished, 1, "Build should be finished.");
is($build->buildstatus, 0, "Should have succeeded.");
};

subtest "Build: fails" => sub {
my $build = $builds->{"fails"};

is($build->finished, 1, "Build should be finished.");
is($build->buildstatus, 1, "Should have failed.");
};
}

subtest "Flake using `checks`" => sub {
checkFlake 'flake-checks'
};

subtest "Flake using `hydraJobs`" => sub {
checkFlake 'flake-hydraJobs'
};

done_testing;
6 changes: 6 additions & 0 deletions t/jobs/flake-checks/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
outputs = { ... }: {
checks =
import ./basic.nix;
};
}
6 changes: 6 additions & 0 deletions t/jobs/flake-hydraJobs/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
outputs = { ... }: {
hydraJobs =
import ./basic.nix;
};
}
42 changes: 33 additions & 9 deletions t/lib/HydraTestContext.pm
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,25 @@ sub nix_state_dir {
sub makeAndEvaluateJobset {
my ($self, %opts) = @_;

my $expression = $opts{'expression'} || die "Mandatory 'expression' option not passed to makeAndEvaluateJobset.\n";
my $expression = $opts{'expression'};
my $flake = $opts{'flake'};
if (not $expression and not $flake) {
die "One of 'expression' or 'flake' must be passed to makeEvaluateJobset.\n";
}

my $jobsdir = $opts{'jobsdir'} // $self->jobsdir;
my $should_build = $opts{'build'} // 0;

my $jobsetCtx = $self->makeJobset(
expression => $expression,
my %args = (
jobsdir => $jobsdir,
);
if ($expression) {
$args{expression} = $expression;
}
if ($flake) {
$args{flake} = $flake;
}
my $jobsetCtx = $self->makeJobset(%args);
my $jobset = $jobsetCtx->{"jobset"};

evalSucceeds($jobset) or die "Evaluating jobs/$expression should exit with return code 0.\n";
Expand All @@ -195,7 +206,7 @@ sub makeAndEvaluateJobset {
#
# In return, you get a hash of the user, project, and jobset records.
#
# This always uses an `expression` from the `jobsdir` directory.
# This always uses an `expression` or `flake` from the `jobsdir` directory.
#
# Hash Parameters:
#
Expand All @@ -204,7 +215,12 @@ sub makeAndEvaluateJobset {
sub makeJobset {
my ($self, %opts) = @_;

my $expression = $opts{'expression'} || die "Mandatory 'expression' option not passed to makeJobset.\n";
my $expression = $opts{'expression'};
my $flake = $opts{'flake'};
if (not $expression and not $flake) {
die "One of 'expression' or 'flake' must be passed to makeJobset.\n";
}

my $jobsdir = $opts{'jobsdir'} // $self->jobsdir;

# Create a new user for this test
Expand All @@ -222,12 +238,20 @@ sub makeJobset {
});

# Create a new jobset for this test and set up the inputs
my $jobset = $project->jobsets->create({
my %args = (
name => rand_chars(),
nixexprinput => "jobs",
nixexprpath => $expression,
emailoverride => ""
});
);
if ($expression) {
$args{type} = 0;
$args{nixexprinput} = "jobs";
$args{nixexprpath} = $expression;
}
if ($flake) {
$args{type} = 1;
$args{flake} = $flake;
}
my $jobset = $project->jobsets->create(\%args);
my $jobsetinput = $jobset->jobsetinputs->create({name => "jobs", type => "path"});
$jobsetinput->jobsetinputalts->create({altnr => 0, value => $jobsdir});

Expand Down

0 comments on commit 8d78648

Please sign in to comment.