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,63 @@
use feature 'unicode_strings';
use strict;
use warnings;
use JSON::MaybeXS;
use Setup;
my %ctx = test_init(
hydra_config => q|
<runcommand>
command = cp "$HYDRA_JSON" "$HYDRA_DATA/joboutput.json"
</runcommand>
|);
require Hydra::Schema;
require Hydra::Model::DB;
use Test2::V0;
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("basic", "runcommand.nix", $ctx{jobsdir});
ok(evalSucceeds($jobset), "Evaluating jobs/runcommand.nix should exit with return code 0");
is(nrQueuedBuildsForJobset($jobset), 1, "Evaluating jobs/runcommand.nix should result in 1 build1");
(my $build) = queuedBuildsForJobset($jobset);
is($build->job, "metrics", "The only job should be metrics");
ok(runBuild($build), "Build should exit with return code 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.");
ok(sendNotifications(), "Notifications execute successfully.");
my $dat = do {
my $filename = $ENV{'HYDRA_DATA'} . "/joboutput.json";
open(my $json_fh, "<", $filename)
or die("Can't open \"$filename\": $!\n");
local $/;
my $json = JSON::MaybeXS->new;
$json->decode(<$json_fh>)
};
subtest "Validate the file parsed and at least one field matches" => sub {
is($dat->{build}, $newbuild->id, "The build event matches our expected ID.");
};
subtest "Validate a run log was created" => sub {
my $runlog = $build->runcommandlogs->find({});
ok($runlog->did_succeed(), "The process did succeed.");
is($runlog->job_matcher, "*:*:*", "An unspecified job matcher is defaulted to *:*:*");
is($runlog->command, 'cp "$HYDRA_JSON" "$HYDRA_DATA/joboutput.json"', "The executed command is saved.");
is($runlog->start_time, within(time() - 1, 2), "The start time is recent.");
is($runlog->end_time, within(time() - 1, 2), "The end time is also recent.");
is($runlog->exit_code, 0, "This command should have succeeded.");
};
done_testing;

View File

@ -0,0 +1,52 @@
use feature 'unicode_strings';
use strict;
use warnings;
use JSON::MaybeXS;
use Setup;
my %ctx = test_init(
hydra_config => q|
<runcommand>
command = invalid-command-this-does-not-exist
</runcommand>
|);
require Hydra::Schema;
require Hydra::Model::DB;
use Test2::V0;
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("basic", "runcommand.nix", $ctx{jobsdir});
ok(evalSucceeds($jobset), "Evaluating jobs/runcommand.nix should exit with return code 0");
is(nrQueuedBuildsForJobset($jobset), 1, "Evaluating jobs/runcommand.nix should result in 1 build1");
(my $build) = queuedBuildsForJobset($jobset);
is($build->job, "metrics", "The only job should be metrics");
ok(runBuild($build), "Build should exit with return code 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.");
ok(sendNotifications(), "Notifications execute successfully.");
subtest "Validate a run log was created" => sub {
my $runlog = $build->runcommandlogs->find({});
ok(!$runlog->did_succeed(), "The process did not succeed.");
ok($runlog->did_fail_with_exec_error(), "The process failed to start due to an exec error.");
is($runlog->job_matcher, "*:*:*", "An unspecified job matcher is defaulted to *:*:*");
is($runlog->command, 'invalid-command-this-does-not-exist', "The executed command is saved.");
is($runlog->start_time, within(time() - 1, 2), "The start time is recent.");
is($runlog->end_time, within(time() - 1, 2), "The end time is also recent.");
is($runlog->exit_code, undef, "This command should not have executed.");
is($runlog->error_number, 2, "This command failed to exec.");
};
done_testing;

View File

@ -0,0 +1,133 @@
use strict;
use warnings;
use JSON::MaybeXS;
use Setup;
my %ctx = test_init(
hydra_config => q|
<runcommand>
command = cp "$HYDRA_JSON" "$HYDRA_DATA/joboutput.json"
</runcommand>
|);
use Test2::V0;
use Hydra::Plugin::RunCommand;
require Hydra::Schema;
require Hydra::Model::DB;
use Test2::V0;
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("basic", "runcommand.nix", $ctx{jobsdir});
ok(evalSucceeds($jobset), "Evaluating jobs/runcommand.nix should exit with return code 0");
is(nrQueuedBuildsForJobset($jobset), 1, "Evaluating jobs/runcommand.nix should result in 1 build1");
(my $build) = queuedBuildsForJobset($jobset);
is($build->job, "metrics", "The only job should be metrics");
ok(runBuild($build), "Build should exit with return code 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.");
$build = $newbuild;
my $dat = Hydra::Plugin::RunCommand::makeJsonPayload("buildFinished", $build);
subtest "Validate the top level fields match" => sub {
is($dat->{build}, $build->id, "The build event matches our expected ID.");
is($dat->{buildStatus}, 0, "The build status matches.");
is($dat->{event}, "buildFinished", "The build event matches.");
is($dat->{finished}, JSON::MaybeXS::true, "The build finished.");
is($dat->{project}, "tests", "The project matches.");
is($dat->{jobset}, "basic", "The jobset matches.");
is($dat->{job}, "metrics", "The job matches.");
is($dat->{nixName}, "my-build-product", "The nixName matches.");
is($dat->{system}, $build->system, "The system matches.");
is($dat->{drvPath}, $build->drvpath, "The derivation path matches.");
is($dat->{timestamp}, $build->timestamp, "The result has a timestamp field.");
is($dat->{startTime}, $build->starttime, "The result has a startTime field.");
is($dat->{stopTime}, $build->stoptime, "The result has a stopTime field.");
is($dat->{homepage}, "https://github.com/NixOS/hydra", "The homepage is passed.");
is($dat->{description}, "An example meta property.", "The description is passed.");
is($dat->{license}, "GPL", "The license is passed.");
};
subtest "Validate the outputs match" => sub {
is(scalar(@{$dat->{outputs}}), 2, "There are exactly two outputs");
subtest "output: out" => sub {
my ($output) = grep { $_->{name} eq "out" } @{$dat->{outputs}};
my $expectedoutput = $build->buildoutputs->find({name => "out"});
is($output->{name}, "out", "Output is named corrrectly");
is($output->{path}, $expectedoutput->path, "The output path matches the database's path.");
};
subtest "output: bin" => sub {
my ($output) = grep { $_->{name} eq "bin" } @{$dat->{outputs}};
my $expectedoutput = $build->buildoutputs->find({name => "bin"});
is($output->{name}, "bin", "Output is named corrrectly");
is($output->{path}, $expectedoutput->path, "The output path matches the database's path.");
};
};
subtest "Validate the metrics match" => sub {
is(scalar(@{$dat->{metrics}}), 2, "There are exactly two metrics");
my ($lineCoverage) = grep { $_->{name} eq "lineCoverage" } @{$dat->{metrics}};
my ($maxResident) = grep { $_->{name} eq "maxResident" } @{$dat->{metrics}};
subtest "verifying the lineCoverage metric" => sub {
is($lineCoverage->{name}, "lineCoverage", "The name matches.");
is($lineCoverage->{value}, 18, "The value matches.");
is($lineCoverage->{unit}, "%", "The unit matches.");
};
subtest "verifying the maxResident metric" => sub {
is($maxResident->{name}, "maxResident", "The name matches.");
is($maxResident->{value}, 27, "The value matches.");
is($maxResident->{unit}, "KiB", "The unit matches.");
};
};
subtest "Validate the products match" => sub {
is(scalar(@{$dat->{outputs}}), 2, "There are exactly two outputs");
subtest "product: out" => sub {
my ($product) = grep { $_->{name} eq "my-build-product" } @{$dat->{products}};
my $expectedproduct = $build->buildproducts->find({name => "my-build-product"});
is($product->{name}, "my-build-product", "The build product is named correctly.");
is($product->{subtype}, "", "The subtype is empty.");
is($product->{productNr}, $expectedproduct->productnr, "The product number matches.");
is($product->{defaultPath}, "", "The default path matches.");
is($product->{path}, $expectedproduct->path, "The path matches the output.");
is($product->{fileSize}, undef, "The fileSize is undefined for the nix-build output type.");
is($product->{sha256hash}, undef, "The sha256hash is undefined for the nix-build output type.");
};
subtest "output: bin" => sub {
my ($product) = grep { $_->{name} eq "my-build-product-bin" } @{$dat->{products}};
my $expectedproduct = $build->buildproducts->find({name => "my-build-product-bin"});
is($product->{name}, "my-build-product-bin", "The build product is named correctly.");
is($product->{subtype}, "bin", "The subtype matches the output name");
is($product->{productNr}, $expectedproduct->productnr, "The product number matches.");
is($product->{defaultPath}, "", "The default path matches.");
is($product->{path}, $expectedproduct->path, "The path matches the output.");
is($product->{fileSize}, undef, "The fileSize is undefined for the nix-build output type.");
is($product->{sha256hash}, undef, "The sha256hash is undefined for the nix-build output type.");
};
};
done_testing;

View File

@ -0,0 +1,177 @@
use strict;
use warnings;
use Setup;
use Test2::V0;
use Hydra::Plugin::RunCommand;
subtest "isEnabled" => sub {
is(
Hydra::Plugin::RunCommand::isEnabled({}),
"",
"Disabled by default."
);
is(
Hydra::Plugin::RunCommand::isEnabled({ config => {}}),
"",
"Disabled by default."
);
is(
Hydra::Plugin::RunCommand::isEnabled({ config => { runcommand => {}}}),
1,
"Enabled if any runcommand blocks exist."
);
};
subtest "configSectionMatches" => sub {
subtest "Expected matches" => sub {
my @examples = (
# Exact match
["project:jobset:job", "project", "jobset", "job"],
# One wildcard
["project:jobset:*", "project", "jobset", "job"],
["project:*:job", "project", "jobset", "job"],
["*:jobset:job", "project", "jobset", "job"],
# Two wildcards
["project:*:*", "project", "jobset", "job"],
["*:*:job", "project", "jobset", "job"],
# Three wildcards
["*:*:*", "project", "jobset", "job"],
# Implicit wildcards
["", "project", "jobset", "job"],
["project", "project", "jobset", "job"],
["project:jobset", "project", "jobset", "job"],
);
for my $example (@examples) {
my ($matcher, $project, $jobset, $job) = @$example;
is(
Hydra::Plugin::RunCommand::configSectionMatches(
$matcher, $project, $jobset, $job
),
1,
"Expecting $matcher to match $project:$jobset:$job"
);
}
};
subtest "Fails to match" => sub {
my @examples = (
# Literal string non-matches
["project:jobset:job", "project", "jobset", "nonmatch"],
["project:jobset:job", "project", "nonmatch", "job"],
["project:jobset:job", "nonmatch", "jobset", "job"],
# Wildcard based non-matches
["*:*:job", "project", "jobset", "nonmatch"],
["*:jobset:*", "project", "nonmatch", "job"],
["project:*:*", "nonmatch", "jobset", "job"],
# These wildcards are NOT regular expressions
["*:*:j.*", "project", "jobset", "job"],
[".*:.*:.*", "project", "nonmatch", "job"],
);
for my $example (@examples) {
my ($matcher, $project, $jobset, $job) = @$example;
is(
Hydra::Plugin::RunCommand::configSectionMatches(
$matcher, $project, $jobset, $job
),
0,
"Expecting $matcher to not match $project:$jobset:$job"
);
}
like(
dies {
Hydra::Plugin::RunCommand::configSectionMatches(
"foo:bar:baz:tux", "foo", "bar", "baz"
),
},
qr/invalid section name/,
"A matcher must have no more than 3 sections"
);
};
};
subtest "eventMatches" => sub {
# This is probably a misfeature that isn't very useful but let's test
# it anyway. At best this lets you make a RunCommand event not work
# by specifying the "events" key. Note: By testing it I'm not promising
# it'll keep working. In fact, I wouldn't be surprised if we chose to
# delete this support since RunCommand never runs on any event other
# than buildFinished.
is(
Hydra::Plugin::RunCommand::eventMatches({}, "buildFinished"),
1,
"An unspecified events key matches"
);
is(
Hydra::Plugin::RunCommand::eventMatches({ events => ""}, "buildFinished"),
0,
"An empty events key does not match"
);
is(
Hydra::Plugin::RunCommand::eventMatches({ events => "foo bar buildFinished baz"}, "buildFinished"),
1,
"An events key with multiple events does match when buildFinished is present"
);
is(
Hydra::Plugin::RunCommand::eventMatches({ events => "foo bar baz"}, "buildFinished"),
0,
"An events key with multiple events does not match when buildFinished is missing"
);
};
subtest "fanoutToCommands" => sub {
my $config = {
runcommand => [
{
job => "",
command => "foo"
},
{
job => "project:*:*",
command => "bar"
},
{
job => "project:jobset:nomatch",
command => "baz"
}
]
};
is(
Hydra::Plugin::RunCommand::fanoutToCommands(
$config,
"buildFinished",
"project",
"jobset",
"job"
),
[
{
matcher => "",
command => "foo"
},
{
matcher => "project:*:*",
command => "bar"
}
],
"fanoutToCommands returns a command per matching job"
);
};
done_testing;

76
t/Hydra/Plugin/gitea.t Normal file
View File

@ -0,0 +1,76 @@
use feature 'unicode_strings';
use strict;
use warnings;
use JSON::MaybeXS;
use Setup;
my %ctx = test_init(
hydra_config => q|
<gitea_authorization>
root=d7f16a3412e01a43a414535b16007c6931d3a9c7
</gitea_authorization>
|);
require Hydra::Schema;
require Hydra::Model::DB;
use Test2::V0;
my $db = Hydra::Model::DB->new;
hydra_setup($db);
my $scratch = "$ctx{tmpdir}/scratch";
mkdir $scratch;
my $uri = "file://$scratch/git-repo";
my $jobset = createJobsetWithOneInput('gitea', 'git-input.nix', 'src', 'git', $uri, $ctx{jobsdir});
sub addStringInput {
my ($jobset, $name, $value) = @_;
my $input = $jobset->jobsetinputs->create({name => $name, type => "string"});
$input->jobsetinputalts->create({value => $value, altnr => 0});
}
addStringInput($jobset, "gitea_repo_owner", "root");
addStringInput($jobset, "gitea_repo_name", "foo");
addStringInput($jobset, "gitea_status_repo", "src");
addStringInput($jobset, "gitea_http_url", "http://localhost:8282/gitea");
updateRepository('gitea', "$ctx{testdir}/jobs/git-update.sh", $scratch);
ok(evalSucceeds($jobset), "Evaluating nix expression");
is(nrQueuedBuildsForJobset($jobset), 1, "Evaluating jobs/runcommand.nix should result in 1 build1");
(my $build) = queuedBuildsForJobset($jobset);
ok(runBuild($build), "Build should succeed with exit code 0");
my $filename = $ENV{'HYDRA_DATA'} . "/giteaout.json";
my $pid;
if (!defined($pid = fork())) {
die "Cannot fork(): $!";
} elsif ($pid == 0) {
exec("python3 $ctx{jobsdir}/server.py $filename");
} else {
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.");
ok(sendNotifications(), "Sent notifications");
kill('INT', $pid);
}
open(my $fh, "<", $filename) or die ("Can't open(): $!\n");
my $i = 0;
my $uri = <$fh>;
my $data = <$fh>;
ok(index($uri, "gitea/api/v1/repos/root/foo/statuses") != -1, "Correct URL");
my $json = JSON->new;
my $content;
$content = $json->decode($data);
is($content->{state}, "success", "Success notification");
done_testing;