Tests: restructure to more closely mirror the sources

t/ had lots of directories and files mirroring src/lib/Hydra. This moves
those files under t/Hydra
This commit is contained in:
Graham Christensen
2022-01-10 15:34:37 -05:00
parent 98c88a4dbf
commit a5d1d36fa6
41 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,65 @@
use feature 'unicode_strings';
use strict;
use warnings;
use Setup;
use IO::Uncompress::Bunzip2 qw(bunzip2);
use Archive::Tar;
use JSON::MaybeXS qw(decode_json);
use Data::Dumper;
my %ctx = test_init(
use_external_destination_store => 0
);
require Hydra::Schema;
require Hydra::Model::DB;
require Hydra::Helper::Nix;
use Test2::V0;
require Catalyst::Test;
Catalyst::Test->import('Hydra');
my $db = Hydra::Model::DB->new;
hydra_setup($db);
my $project = $db->resultset('Projects')->create({name => "tests", displayname => "", owner => "root"});
# Most basic test case, no parameters
my $jobset = createBaseJobset("nested-attributes", "nested-attributes.nix", $ctx{jobsdir});
ok(evalSucceeds($jobset));
is(nrQueuedBuildsForJobset($jobset), 4);
for my $build (queuedBuildsForJobset($jobset)) {
ok(runBuild($build), "Build '".$build->job."' should exit with return code 0");
my $newbuild = $db->resultset('Builds')->find($build->id);
is($newbuild->finished, 1, "Build '".$build->job."' should be finished.");
is($newbuild->buildstatus, 0, "Build '".$build->job."' should have buildstatus 0.");
}
my $compressed = get('/jobset/tests/nested-attributes/channel/latest/nixexprs.tar.bz2');
my $tarcontent;
bunzip2(\$compressed => \$tarcontent);
open(my $tarfh, "<", \$tarcontent);
my $tar = Archive::Tar->new($tarfh);
my $defaultnix = $ctx{"tmpdir"} . "/channel-default.nix";
$tar->extract_file("channel/default.nix", $defaultnix);
print STDERR $tar->get_content("channel/default.nix");
(my $status, my $stdout, my $stderr) = Hydra::Helper::Nix::captureStdoutStderr(5, "nix-env", "--json", "--query", "--available", "--attr-path", "--file", $defaultnix);
is($stderr, "", "Stderr should be empty");
is($status, 0, "Querying the packages should succeed");
my $packages = decode_json($stdout);
my $keys = [sort keys %$packages];
is($keys, [
"packageset-nested",
"packageset.deeper.deeper.nested",
"packageset.nested",
"packageset.nested2",
]);
is($packages->{"packageset-nested"}->{"name"}, "actually-top-level");
is($packages->{"packageset.nested"}->{"name"}, "actually-nested");
done_testing;

View File

@ -0,0 +1,35 @@
use strict;
use warnings;
use Setup;
use Data::Dumper;
my %ctx = test_init();
require Hydra::Schema;
require Hydra::Model::DB;
require Hydra::Helper::Nix;
use Test2::V0;
require Catalyst::Test;
use HTTP::Request::Common;
Catalyst::Test->import('Hydra');
my $db = Hydra::Model::DB->new;
hydra_setup($db);
my $project = $db->resultset('Projects')->create({name => "tests", displayname => "", owner => "root"});
my $jobset = createBaseJobset("basic", "basic.nix", $ctx{jobsdir});
ok(evalSucceeds($jobset), "Evaluating jobs/basic.nix should exit with return code 0");
subtest "/jobset/PROJECT/JOBSET" => sub {
my $jobset = request(GET '/jobset/' . $project->name . '/' . $jobset->name);
ok($jobset->is_success, "The page showing the jobset returns 200.");
};
subtest "/jobset/PROJECT/JOBSET/evals" => sub {
my $jobsetevals = request(GET '/jobset/' . $project->name . '/' . $jobset->name . '/evals');
ok($jobsetevals->is_success, "The page showing the jobset evals returns 200.");
};
done_testing;

View File

@ -0,0 +1,205 @@
use feature 'unicode_strings';
use strict;
use warnings;
use Setup;
use JSON::MaybeXS qw(decode_json encode_json);
my %ctx = test_init();
require Hydra::Schema;
require Hydra::Model::DB;
require Hydra::Helper::Nix;
use Test2::V0;
require Catalyst::Test;
Catalyst::Test->import('Hydra');
use HTTP::Request::Common qw(POST PUT GET DELETE);
# This test verifies that creating, reading, updating, and deleting a jobset via
# the HTTP API works as expected.
my $db = Hydra::Model::DB->new;
hydra_setup($db);
# Create a user to log in to
my $user = $db->resultset('Users')->create({ username => 'alice', emailaddress => 'root@invalid.org', password => '!' });
$user->setPassword('foobar');
$user->userroles->update_or_create({ role => 'admin' });
my $project = $db->resultset('Projects')->create({name => 'tests', displayname => 'Tests', owner => 'alice'});
# Login and save cookie for future requests
my $req = request(POST '/login',
Referer => 'http://localhost/',
Content => {
username => 'alice',
password => 'foobar'
}
);
is($req->code, 302);
my $cookie = $req->header("set-cookie");
subtest 'Create new jobset "job" as flake type' => sub {
my $jobsetcreate = request(PUT '/jobset/tests/job',
Accept => 'application/json',
Content_Type => 'application/json',
Cookie => $cookie,
Content => encode_json({
enabled => 2,
visible => JSON::MaybeXS::true,
name => "job",
type => 1,
description => "test jobset",
flake => "github:nixos/nix",
checkinterval => 0,
schedulingshares => 100,
keepnr => 3
})
);
ok($jobsetcreate->is_success);
is($jobsetcreate->header("location"), "http://localhost/jobset/tests/job");
};
subtest 'Read newly-created jobset "job"' => sub {
my $jobsetinfo = request(GET '/jobset/tests/job',
Accept => 'application/json',
);
ok($jobsetinfo->is_success);
is(decode_json($jobsetinfo->content), {
checkinterval => 0,
description => "test jobset",
emailoverride => "",
enabled => 2,
enableemail => JSON::MaybeXS::false,
errortime => undef,
errormsg => "",
fetcherrormsg => "",
flake => "github:nixos/nix",
visible => JSON::MaybeXS::true,
inputs => {},
keepnr => 3,
lastcheckedtime => undef,
name => "job",
nixexprinput => "",
nixexprpath => "",
project => "tests",
schedulingshares => 100,
starttime => undef,
triggertime => undef,
type => 1
});
};
subtest 'Update jobset "job" to legacy type' => sub {
my $jobsetupdate = request(PUT '/jobset/tests/job',
Accept => 'application/json',
Content_Type => 'application/json',
Cookie => $cookie,
Content => encode_json({
enabled => 3,
visible => JSON::MaybeXS::true,
name => "job",
type => 0,
nixexprinput => "ofborg",
nixexprpath => "release.nix",
inputs => {
ofborg => {
name => "ofborg",
type => "git",
value => "https://github.com/NixOS/ofborg.git released"
}
},
description => "test jobset",
checkinterval => 0,
schedulingshares => 50,
keepnr => 1
})
);
ok($jobsetupdate->is_success);
# Read newly-updated jobset "job"
my $jobsetinfo = request(GET '/jobset/tests/job',
Accept => 'application/json',
);
ok($jobsetinfo->is_success);
is(decode_json($jobsetinfo->content), {
checkinterval => 0,
description => "test jobset",
emailoverride => "",
enabled => 3,
enableemail => JSON::MaybeXS::false,
errortime => undef,
errormsg => "",
fetcherrormsg => "",
flake => "",
visible => JSON::MaybeXS::true,
inputs => {
ofborg => {
name => "ofborg",
type => "git",
emailresponsible => JSON::MaybeXS::false,
value => "https://github.com/NixOS/ofborg.git released"
}
},
keepnr => 1,
lastcheckedtime => undef,
name => "job",
nixexprinput => "ofborg",
nixexprpath => "release.nix",
project => "tests",
schedulingshares => 50,
starttime => undef,
triggertime => undef,
type => 0
});
};
subtest 'Update jobset "job" to have an invalid input type' => sub {
my $jobsetupdate = request(PUT '/jobset/tests/job',
Accept => 'application/json',
Content_Type => 'application/json',
Cookie => $cookie,
Content => encode_json({
enabled => 3,
visible => JSON::MaybeXS::true,
name => "job",
type => 0,
nixexprinput => "ofborg",
nixexprpath => "release.nix",
inputs => {
ofborg => {
name => "ofborg",
type => "123",
value => "https://github.com/NixOS/ofborg.git released"
}
},
description => "test jobset",
checkinterval => 0,
schedulingshares => 50,
keepnr => 1
})
);
ok(!$jobsetupdate->is_success);
ok($jobsetupdate->content =~ m/Invalid input type.*valid types:/);
};
subtest 'Delete jobset "job"' => sub {
my $jobsetinfo = request(DELETE '/jobset/tests/job',
Accept => 'application/json',
Cookie => $cookie
);
ok($jobsetinfo->is_success);
# Jobset "job" should no longer exist.
$jobsetinfo = request(GET '/jobset/tests/job',
Accept => 'application/json',
);
ok(!$jobsetinfo->is_success);
};
done_testing;

View File

@ -0,0 +1,123 @@
use strict;
use warnings;
use Setup;
my %ctx = test_init();
require Hydra::Schema;
require Hydra::Model::DB;
require Hydra::Helper::Nix;
use Data::Dumper;
use Test2::V0;
use Test2::Compare qw(compare strict_convert);
my $db = Hydra::Model::DB->new;
hydra_setup($db);
# This test checks a matrix of jobset configuration options for constraint violations.
my @types = ( 0, 1, 2 );
my @nixexprinputs = ( undef, "input" );
my @nixexprpaths = ( undef, "path" );
my @flakes = ( undef, "flake" );
my @expected_failing;
my @expected_succeeding = (
{
"name" => "test",
"emailoverride" => "",
"type" => 0,
"nixexprinput" => "input",
"nixexprpath" => "path",
"flake" => undef,
},
{
"name" => "test",
"emailoverride" => "",
"type" => 1,
"nixexprinput" => undef,
"nixexprpath" => undef,
"flake" => "flake",
},
);
# Checks if two Perl hashes (in scalar context) contain the same data.
# Returns 0 if they are different and 1 if they are the same.
sub test_scenario_matches {
my ($first, $second) = @_;
my $ret = compare($first, $second, \&strict_convert);
if (defined $ret == 1) {
return 0;
} else {
return 1;
}
}
# Construct a matrix of parameters that should violate the Jobsets table's constraints.
foreach my $type (@types) {
foreach my $nixexprinput (@nixexprinputs) {
foreach my $nixexprpath (@nixexprpaths) {
foreach my $flake (@flakes) {
my $hash = {
"name" => "test",
"emailoverride" => "",
"type" => $type,
"nixexprinput" => $nixexprinput,
"nixexprpath" => $nixexprpath,
"flake" => $flake,
};
push(@expected_failing, $hash) if (!grep { test_scenario_matches($_, $hash) } @expected_succeeding);
};
};
};
};
my $project = $db->resultset('Projects')->create({name => "tests", displayname => "", owner => "root"});
# Validate that the list of parameters that should fail the constraints do indeed fail.
subtest "Expected constraint failures" => sub {
my $count = 1;
foreach my $case (@expected_failing) {
subtest "Case $count: " . Dumper ($case) => sub {
dies {
# Necessary, otherwise cases will fail because the `->create`
# will throw an exception due to an expected constraint failure
# (which will cause the `ok()` to be skipped, leading to no
# assertions in the subtest).
is(1, 1);
ok(
!$project->jobsets->create($case),
"Expected jobset to violate constraints"
);
};
};
$count++;
};
};
# Validate that the list of parameters that should not fail the constraints do indeed succeed.
subtest "Expected constraint successes" => sub {
my $count = 1;
foreach my $case (@expected_succeeding) {
subtest "Case $count: " . Dumper ($case) => sub {
my $jobset = $project->jobsets->create($case);
ok(
$jobset,
"Expected jobset to not violate constraints"
);
# Delete the jobset so the next jobset won't violate the name constraint.
$jobset->delete;
};
$count++;
};
};
done_testing;