From d003fec8a54980cfc2d96fa7f7c6fcbc87f2dca5 Mon Sep 17 00:00:00 2001
From: Graham Christensen <graham@grahamc.com>
Date: Thu, 18 Nov 2021 14:27:00 -0500
Subject: [PATCH] RunCommandLogs: add a did_succeed helper

---
 src/lib/Hydra/Schema/Result/RunCommandLogs.pm | 22 +++++++++++++++++++
 t/Schema/Result/RunCommandLogs.t              |  5 +++++
 t/plugins/RunCommand/basic.t                  |  1 +
 t/plugins/RunCommand/errno.t                  |  1 +
 4 files changed, 29 insertions(+)

diff --git a/src/lib/Hydra/Schema/Result/RunCommandLogs.pm b/src/lib/Hydra/Schema/Result/RunCommandLogs.pm
index 3a6ca9ae..329d3624 100644
--- a/src/lib/Hydra/Schema/Result/RunCommandLogs.pm
+++ b/src/lib/Hydra/Schema/Result/RunCommandLogs.pm
@@ -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;
diff --git a/t/Schema/Result/RunCommandLogs.t b/t/Schema/Result/RunCommandLogs.t
index ee65ef41..a84651de 100644
--- a/t/Schema/Result/RunCommandLogs.t
+++ b/t/Schema/Result/RunCommandLogs.t
@@ -53,6 +53,7 @@ subtest "Completing a process before it is started is invalid" => sub {
 subtest "Starting a process" => sub {
     my $runlog = new_run_log();
     $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->end_time, undef, "The end time 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();
     $runlog->started();
     $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->end_time, within(time() - 1, 2), "The end time is recent.");
     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();
     $runlog->started();
     $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->end_time, within(time() - 1, 2), "The end time is recent.");
     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();
     $runlog->started();
     $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->end_time, within(time() - 1, 2), "The end time is recent.");
     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();
     $runlog->started();
     $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->end_time, within(time() - 1, 2), "The end time is recent.");
     is($runlog->error_number, 2, "The error number is saved");
diff --git a/t/plugins/RunCommand/basic.t b/t/plugins/RunCommand/basic.t
index cb025095..e0cbd1bb 100644
--- a/t/plugins/RunCommand/basic.t
+++ b/t/plugins/RunCommand/basic.t
@@ -52,6 +52,7 @@ subtest "Validate the file parsed and at least one field matches" => sub {
 
 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.");
diff --git a/t/plugins/RunCommand/errno.t b/t/plugins/RunCommand/errno.t
index 35fea599..6cf6b9ea 100644
--- a/t/plugins/RunCommand/errno.t
+++ b/t/plugins/RunCommand/errno.t
@@ -39,6 +39,7 @@ 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.");
     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.");