hydra-notify: move BuildStarted processing to an Event

This commit is contained in:
Graham Christensen 2021-08-12 09:56:42 -04:00 committed by Your Name
parent 10e85e3422
commit 4fdb20d3bd
4 changed files with 102 additions and 42 deletions

View file

@ -19,7 +19,29 @@ sub parse :prototype(@) {
sub new {
my ($self, $id) = @_;
return bless { "build_id" => $id }, $self;
return bless {
"build_id" => $id,
"build" => undef
}, $self;
}
sub load {
my ($self, $db) = @_;
if (!defined($self->{"build"})) {
$self->{"build"} = $db->resultset('Builds')->find($self->{"build_id"})
or die "build $self->{'build_id'} does not exist\n";
}
}
sub execute {
my ($self, $db, $plugin) = @_;
$self->load($db);
$plugin->buildStarted($self->{"build"});
return 1;
}
1;

View file

@ -44,22 +44,6 @@ sub runPluginsForEvent {
}
}
sub buildStarted {
my ($buildId) = @_;
my $build = $db->resultset('Builds')->find($buildId)
or die "build $buildId does not exist\n";
foreach my $plugin (@plugins) {
eval {
$plugin->buildStarted($build);
1;
} or do {
print STDERR "error with $plugin->buildStarted: $@\n";
}
}
}
sub buildFinished {
my ($buildId, @deps) = @_;
@ -136,7 +120,8 @@ while (!$queued_only) {
eval {
if ($channelName eq "build_started") {
buildStarted(int($payload[0]));
my $event = Hydra::Event::new_event($channelName, $message->{"payload"});
runPluginsForEvent($event);
} elsif ($channelName eq "build_finished") {
my $buildId = int($payload[0]);
buildFinished($buildId, @payload[1..$#payload]);

View file

@ -1,7 +1,6 @@
use strict;
use Hydra::Event;
use Hydra::Event::BuildFinished;
use Hydra::Event::BuildStarted;
use Hydra::Event::StepFinished;
use Test2::V0;
@ -14,29 +13,6 @@ subtest "Event: new event" => sub {
is($event->{'event'}->{'build_id'}, 19);
};
subtest "Payload type: build_started" => sub {
like(
dies { Hydra::Event::parse_payload("build_started", "") },
qr/one argument/,
"empty payload"
);
like(
dies { Hydra::Event::parse_payload("build_started", "abc123\tabc123") },
qr/only one argument/,
"two arguments"
);
like(
dies { Hydra::Event::parse_payload("build_started", "abc123") },
qr/should be an integer/,
"not an integer"
);
is(
Hydra::Event::parse_payload("build_started", "19"),
Hydra::Event::BuildStarted->new(19),
"Valid parse"
);
};
subtest "Payload type: step_finished" => sub {
like(

77
t/Event/BuildStarted.t Normal file
View file

@ -0,0 +1,77 @@
use strict;
use Setup;
my %ctx = test_init();
require Hydra::Schema;
require Hydra::Model::DB;
use Hydra::Event;
use Hydra::Event::BuildStarted;
use Test2::V0;
use Test2::Tools::Exception;
use Test2::Tools::Mock qw(mock_obj);
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");
is(nrQueuedBuildsForJobset($jobset), 3, "Evaluating jobs/basic.nix should result in 3 builds");
subtest "Parsing build_started" => sub {
like(
dies { Hydra::Event::parse_payload("build_started", "") },
qr/one argument/,
"empty payload"
);
like(
dies { Hydra::Event::parse_payload("build_started", "abc123\tabc123") },
qr/only one argument/,
"two arguments"
);
like(
dies { Hydra::Event::parse_payload("build_started", "abc123") },
qr/should be an integer/,
"not an integer"
);
is(
Hydra::Event::parse_payload("build_started", "19"),
Hydra::Event::BuildStarted->new(19),
"Valid parse"
);
};
subtest "load" => sub {
my $build = $db->resultset('Builds')->search(
{ },
{ limit => 1 }
)->next;
my $event = Hydra::Event::BuildStarted->new($build->id);
$event->load($db);
is($event->{"build"}->id, $build->id, "The build record matches.");
# Create a fake "plugin" with a buildStarted sub, the sub sets this
# global passedBuild variable.
my $passedBuild;
my $plugin = {};
my $mock = mock_obj $plugin => (
add => [
"buildStarted" => sub {
my ($self, $build) = @_;
$passedBuild = $build;
}
]
);
$event->execute($db, $plugin);
is($passedBuild->id, $build->id, "The plugin's buildStarted hook is called with the proper build");
};
done_testing;