Skip to content

Commit

Permalink
Fixes basic job success checks (#78)
Browse files Browse the repository at this point in the history
* Adds support for new job status string

* Fixes job status checking, and adds basic test
  • Loading branch information
Eagerod authored Feb 22, 2025
1 parent c7cf633 commit 70eb713
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
20 changes: 12 additions & 8 deletions pkg/hope/kubectl_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,23 @@ const (

// Check to see if the provided job has completed, or is still running.
func GetJobStatus(log *logrus.Entry, kubectl *kubeutil.Kubectl, namespace, job string) (JobStatus, error) {
output, err := kubeutil.GetKubectl(kubectl, "get", "-n", namespace, "job", job, "-o", "template={{range .status.conditions}}{{.type}}{{end}}")
template := "template={{range .status.conditions}}{{.type}}\n{{end}}"
output, err := kubeutil.GetKubectl(kubectl, "get", "-n", namespace, "job", job, "-o", template)
if err != nil {
return JobStatusUnknown, err
}

switch output {
case "Complete":
return JobStatusComplete, nil
case "Failed":
return JobStatusFailed, nil
default:
return JobStatusRunning, nil
// Search for terminal statuses, and with none, assume running.
for _, status := range strings.Split(output, "\n") {
switch status {
case "Complete":
return JobStatusComplete, nil
case "Failed":
return JobStatusFailed, nil
}
}

return JobStatusRunning, nil
}

func FollowLogsIfContainersRunning(kubectl *kubeutil.Kubectl, namespace, job string) error {
Expand Down
49 changes: 49 additions & 0 deletions pkg/hope/kubectl_jobs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package hope

import (
"testing"
)

import (
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

import (
"github.com/Eagerod/hope/pkg/kubeutil"
)

// Implemented as a suite to allow manipulating kubeutil.kubectl funcs
type KubectlJobsTestSuite struct {
suite.Suite

originalGetKubectl func(kubectl *kubeutil.Kubectl, args ...string) (string, error)
}

func (s *KubectlJobsTestSuite) SetupTest() {
s.originalGetKubectl = kubeutil.GetKubectl

}

func (s *KubectlJobsTestSuite) TeardownTest() {
kubeutil.GetKubectl = s.originalGetKubectl
}

// Actual test method to run the suite
func TestKubectlJobs(t *testing.T) {
suite.Run(t, new(KubectlJobsTestSuite))
}

func (s *KubectlJobsTestSuite) TestGetJobStatus() {
t := s.T()

kubeutil.GetKubectl = func(kubectl *kubeutil.Kubectl, args ...string) (string, error) {
return "SuccessCriteriaMet\nComplete", nil
}

kubectl := kubeutil.Kubectl{}
status, err := GetJobStatus(log.WithFields(log.Fields{}), &kubectl, "default", "imaginary-job")
assert.Nil(t, err)
assert.Equal(t, status, JobStatusComplete)
}

0 comments on commit 70eb713

Please sign in to comment.