Skip to content

Commit

Permalink
feat(cli): Add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
irvinlim committed Jun 5, 2022
1 parent 71e5cdc commit c4f761f
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
134 changes: 134 additions & 0 deletions pkg/cli/cmd/cmd_get_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@
package cmd_test

import (
"regexp"
"testing"

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"

execution "github.com/furiko-io/furiko/apis/execution/v1alpha1"
"github.com/furiko-io/furiko/pkg/cli/cmd"
"github.com/furiko-io/furiko/pkg/cli/formatter"
"github.com/furiko-io/furiko/pkg/execution/util/parallel"
runtimetesting "github.com/furiko-io/furiko/pkg/runtime/testing"
"github.com/furiko-io/furiko/pkg/utils/testutils"
)
Expand Down Expand Up @@ -117,6 +120,89 @@ var (
},
},
}

jobParallel = &execution.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "job-parallel",
Namespace: DefaultNamespace,
},
Spec: execution.JobSpec{
Template: &execution.JobTemplate{
Parallelism: &execution.ParallelismSpec{
WithCount: pointer.Int64(3),
CompletionStrategy: execution.AllSuccessful,
},
},
},
Status: execution.JobStatus{
Phase: execution.JobRunning,
Condition: execution.JobCondition{
Running: &execution.JobConditionRunning{
LatestCreationTimestamp: testutils.Mkmtime(taskCreateTime),
LatestRunningTimestamp: testutils.Mkmtime(taskLaunchTime),
},
},
StartTime: testutils.Mkmtimep(startTime),
CreatedTasks: 3,
ParallelStatus: &execution.ParallelStatus{
ParallelStatusSummary: execution.ParallelStatusSummary{
Complete: false,
},
Indexes: []execution.ParallelIndexStatus{
{
Index: execution.ParallelIndex{
IndexNumber: pointer.Int64(0),
},
Hash: makeHash(0),
CreatedTasks: 1,
State: execution.IndexRunning,
},
{
Index: execution.ParallelIndex{
IndexNumber: pointer.Int64(1),
},
Hash: makeHash(1),
CreatedTasks: 1,
State: execution.IndexRunning,
},
{
Index: execution.ParallelIndex{
IndexNumber: pointer.Int64(2),
},
Hash: makeHash(2),
CreatedTasks: 1,
State: execution.IndexRunning,
},
},
},
Tasks: []execution.TaskRef{
{
Name: "job-parallel-0",
CreationTimestamp: testutils.Mkmtime(taskCreateTime),
RunningTimestamp: testutils.Mkmtimep(taskLaunchTime),
Status: execution.TaskStatus{
State: execution.TaskRunning,
},
},
{
Name: "job-parallel-0",
CreationTimestamp: testutils.Mkmtime(taskCreateTime),
RunningTimestamp: testutils.Mkmtimep(taskLaunchTime),
Status: execution.TaskStatus{
State: execution.TaskRunning,
},
},
{
Name: "job-parallel-0",
CreationTimestamp: testutils.Mkmtime(taskCreateTime),
RunningTimestamp: testutils.Mkmtimep(taskLaunchTime),
Status: execution.TaskStatus{
State: execution.TaskRunning,
},
},
},
},
}
)

func TestGetJobCommand(t *testing.T) {
Expand Down Expand Up @@ -180,10 +266,58 @@ func TestGetJobCommand(t *testing.T) {
},
},
},
{
Name: "get a finished job, pretty print with detail",
Args: []string{"get", "job", "job-finished", "-o", "detail"},
Fixtures: []runtime.Object{jobFinished},
Stdout: runtimetesting.Output{
// Should print list of tasks and their details.
ContainsAll: []string{
jobFinished.Status.Tasks[0].Name,
jobFinished.Status.Tasks[0].Status.Message,
},
},
},
{
Name: "get a parallel job",
Args: []string{"get", "job", "job-parallel"},
Fixtures: []runtime.Object{jobParallel},
Stdout: runtimetesting.Output{
// Should print parallel task summary.
ContainsAll: []string{
string(jobParallel.Spec.Template.Parallelism.GetCompletionStrategy()),
"3 Running",
},
},
},
{
Name: "get a parallel job with detail",
Args: []string{"get", "job", "job-parallel", "-o", "detail"},
Fixtures: []runtime.Object{jobParallel},
Stdout: runtimetesting.Output{
// Should print parallel task groups.
ContainsAll: []string{
makeHash(0),
},
MatchesAll: []*regexp.Regexp{
regexp.MustCompile(`Index Number:\s+0`),
},
},
},
{
Name: "get job does not exist",
Args: []string{"get", "job", "job-running"},
WantError: runtimetesting.AssertErrorIsNotFound(),
},
})
}

func makeHash(index int64) string {
hash, err := parallel.HashIndex(execution.ParallelIndex{
IndexNumber: pointer.Int64(index),
})
if err != nil {
panic(err)
}
return hash
}
11 changes: 11 additions & 0 deletions pkg/runtime/testing/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ type Output struct {

// If specified, expects the output to match the specified regexp.
Matches *regexp.Regexp

// If specified, expects the output to match all of the given regular expressions.
MatchesAll []*regexp.Regexp
}

func (c *CommandTest) Run(t *testing.T) {
Expand Down Expand Up @@ -191,4 +194,12 @@ func (c *CommandTest) checkOutput(t *testing.T, name, s string, output Output) {
t.Errorf(`Output in %v did not match regex "%v", got: %v`, name, output.Matches, s)
}
}

if len(output.MatchesAll) > 0 {
for _, regex := range output.MatchesAll {
if !regex.MatchString(s) {
t.Errorf(`Output in %v did not match regex "%v", got: %v`, name, regex, s)
}
}
}
}

0 comments on commit c4f761f

Please sign in to comment.