RunCommandLogs: add a did_succeed helper

This commit is contained in:
Graham Christensen 2021-11-18 14:27:00 -05:00
parent 5bb3e2be78
commit d003fec8a5
4 changed files with 29 additions and 0 deletions

View file

@ -228,5 +228,27 @@ sub completed_with_child_error {
}); });
} }
=head2 did_succeed
Return:
* true if the task ran and finished successfully,
* false if the task did not run successfully but is completed
* undef if the task has not yet run
=cut
sub did_succeed {
my ($self) = @_;
if (!defined($self->end_time)) {
return undef;
}
if (!defined($self->exit_code)) {
return 0;
}
return $self->exit_code == 0;
}
1; 1;

View file

@ -53,6 +53,7 @@ subtest "Completing a process before it is started is invalid" => sub {
subtest "Starting a process" => sub { subtest "Starting a process" => sub {
my $runlog = new_run_log(); my $runlog = new_run_log();
$runlog->started(); $runlog->started();
is($runlog->did_succeed(), undef, "The process has not yet succeeded.");
is($runlog->start_time, within(time() - 1, 2), "The start time is recent."); is($runlog->start_time, within(time() - 1, 2), "The start time is recent.");
is($runlog->end_time, undef, "The end time is undefined."); is($runlog->end_time, undef, "The end time is undefined.");
is($runlog->exit_code, undef, "The exit code is undefined."); is($runlog->exit_code, undef, "The exit code is undefined.");
@ -64,6 +65,7 @@ subtest "The process completed (success)" => sub {
my $runlog = new_run_log(); my $runlog = new_run_log();
$runlog->started(); $runlog->started();
$runlog->completed_with_child_error(0, 123); $runlog->completed_with_child_error(0, 123);
ok($runlog->did_succeed(), "The process did succeed.");
is($runlog->start_time, within(time() - 1, 2), "The start time is recent."); 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 recent."); is($runlog->end_time, within(time() - 1, 2), "The end time is recent.");
is($runlog->error_number, undef, "The error number is undefined"); is($runlog->error_number, undef, "The error number is undefined");
@ -76,6 +78,7 @@ subtest "The process completed (errored)" => sub {
my $runlog = new_run_log(); my $runlog = new_run_log();
$runlog->started(); $runlog->started();
$runlog->completed_with_child_error(21760, 123); $runlog->completed_with_child_error(21760, 123);
ok(!$runlog->did_succeed(), "The process did not succeed.");
is($runlog->start_time, within(time() - 1, 2), "The start time is recent."); 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 recent."); is($runlog->end_time, within(time() - 1, 2), "The end time is recent.");
is($runlog->error_number, undef, "The error number is undefined"); is($runlog->error_number, undef, "The error number is undefined");
@ -88,6 +91,7 @@ subtest "The process completed (signaled)" => sub {
my $runlog = new_run_log(); my $runlog = new_run_log();
$runlog->started(); $runlog->started();
$runlog->completed_with_child_error(393, 234); $runlog->completed_with_child_error(393, 234);
ok(!$runlog->did_succeed(), "The process did not succeed.");
is($runlog->start_time, within(time() - 1, 2), "The start time is recent."); 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 recent."); is($runlog->end_time, within(time() - 1, 2), "The end time is recent.");
is($runlog->error_number, undef, "The error number is undefined"); is($runlog->error_number, undef, "The error number is undefined");
@ -100,6 +104,7 @@ subtest "The process failed to start" => sub {
my $runlog = new_run_log(); my $runlog = new_run_log();
$runlog->started(); $runlog->started();
$runlog->completed_with_child_error(-1, 2); $runlog->completed_with_child_error(-1, 2);
ok(!$runlog->did_succeed(), "The process did not succeed.");
is($runlog->start_time, within(time() - 1, 2), "The start time is recent."); 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 recent."); is($runlog->end_time, within(time() - 1, 2), "The end time is recent.");
is($runlog->error_number, 2, "The error number is saved"); is($runlog->error_number, 2, "The error number is saved");

View file

@ -52,6 +52,7 @@ subtest "Validate the file parsed and at least one field matches" => sub {
subtest "Validate a run log was created" => sub { subtest "Validate a run log was created" => sub {
my $runlog = $build->runcommandlogs->find({}); 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->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->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->start_time, within(time() - 1, 2), "The start time is recent.");

View file

@ -39,6 +39,7 @@ ok(sendNotifications(), "Notifications execute successfully.");
subtest "Validate a run log was created" => sub { subtest "Validate a run log was created" => sub {
my $runlog = $build->runcommandlogs->find({}); my $runlog = $build->runcommandlogs->find({});
ok(!$runlog->did_succeed(), "The process did not succeed.");
is($runlog->job_matcher, "*:*:*", "An unspecified job matcher is defaulted to *:*:*"); 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->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->start_time, within(time() - 1, 2), "The start time is recent.");