Skip to content

Commit

Permalink
Clean up dag_test.go
Browse files Browse the repository at this point in the history
- don't use test builders
- use script mode instead of command+args
- sort start times using sort.Slice
- compare parallel task start times using math.Abs in case s1 starts >5s before s2 -- only the other case was checked before
- then copy test/dag_test.go -> test/v1alpha1/dag_test.go and fix incompatibilities
  • Loading branch information
imjasonh authored and tekton-robot committed Sep 15, 2020
1 parent acf34f9 commit 723fca9
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 171 deletions.
120 changes: 49 additions & 71 deletions test/dag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ limitations under the License.
package test

import (
"math"
"sort"
"strings"
"testing"
"time"

tb "github.com/tektoncd/pipeline/internal/builder/v1beta1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
resources "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
resource "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
clientset "github.com/tektoncd/pipeline/pkg/client/clientset/versioned/typed/pipeline/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -53,7 +52,7 @@ func TestDAGPipelineRun(t *testing.T) {

// Create the Task that echoes text
repoTaskResource := v1beta1.TaskResource{ResourceDeclaration: v1beta1.ResourceDeclaration{
Name: "repo", Type: resources.PipelineResourceTypeGit,
Name: "repo", Type: resource.PipelineResourceTypeGit,
}}
echoTask := &v1beta1.Task{
ObjectMeta: metav1.ObjectMeta{Name: "echo-task", Namespace: namespace},
Expand All @@ -66,26 +65,30 @@ func TestDAGPipelineRun(t *testing.T) {
Name: "text", Type: v1beta1.ParamTypeString,
Description: "The text that should be echoed",
}},
Steps: []v1beta1.Step{{Container: corev1.Container{
Image: "busybox",
Command: []string{"echo"},
Args: []string{"$(params.text)"},
}}, {Container: corev1.Container{
Image: "busybox",
Command: []string{"ln"},
Args: []string{"-s", "$(resources.inputs.repo.path)", "$(resources.outputs.repo.path)"},
}}},
Steps: []v1beta1.Step{{
Container: corev1.Container{Image: "busybox"},
Script: "echo $(params.text)",
}, {
Container: corev1.Container{Image: "busybox"},
Script: "ln -s $(resources.inputs.repo.path) $(resources.outputs.repo.path)",
}},
},
}
if _, err := c.TaskClient.Create(echoTask); err != nil {
t.Fatalf("Failed to create echo Task: %s", err)
}

// Create the repo PipelineResource (doesn't really matter which repo we use)
repoResource := tb.PipelineResource("repo", tb.PipelineResourceSpec(
v1alpha1.PipelineResourceTypeGit,
tb.PipelineResourceSpecParam("Url", "https://github.com/githubtraining/example-basic"),
))
repoResource := &resource.PipelineResource{
ObjectMeta: metav1.ObjectMeta{Name: "repo"},
Spec: resource.PipelineResourceSpec{
Type: resource.PipelineResourceTypeGit,
Params: []resource.ResourceParam{{
Name: "Url",
Value: "https://github.com/githubtraining/example-basic",
}},
},
}
if _, err := c.PipelineResourceClient.Create(repoResource); err != nil {
t.Fatalf("Failed to create simple repo PipelineResource: %s", err)
}
Expand All @@ -96,7 +99,7 @@ func TestDAGPipelineRun(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{Name: "dag-pipeline", Namespace: namespace},
Spec: v1beta1.PipelineSpec{
Resources: []v1beta1.PipelineDeclaredResource{{
Name: "repo", Type: resources.PipelineResourceTypeGit,
Name: "repo", Type: resource.PipelineResourceTypeGit,
}},
Tasks: []v1beta1.PipelineTask{{
Name: "pipeline-task-3",
Expand Down Expand Up @@ -196,71 +199,46 @@ func TestDAGPipelineRun(t *testing.T) {
t.Fatalf("Error waiting for PipelineRun to finish: %s", err)
}

t.Logf("Verifying order of execution")
times := getTaskStartTimes(t, c.TaskRunClient)
vefifyExpectedOrder(t, times)
}

type runTime struct {
name string
t time.Time
}

type runTimes []runTime

func (f runTimes) Len() int {
return len(f)
verifyExpectedOrder(t, c.TaskRunClient)
}

func (f runTimes) Less(i, j int) bool {
return f[i].t.Before(f[j].t)
}

func (f runTimes) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}
func verifyExpectedOrder(t *testing.T, c clientset.TaskRunInterface) {
t.Logf("Verifying order of execution")

func getTaskStartTimes(t *testing.T, c clientset.TaskRunInterface) runTimes {
taskRuns, err := c.List(metav1.ListOptions{})
taskRunsResp, err := c.List(metav1.ListOptions{})
if err != nil {
t.Fatalf("Couldn't get TaskRuns (so that we could check when they executed): %v", err)
}
times := runTimes{}
for _, t := range taskRuns.Items {
times = append(times, runTime{
name: t.Name,
t: t.Status.StartTime.Time,
})
}
return times
}

func vefifyExpectedOrder(t *testing.T, times runTimes) {
if len(times) != 5 {
t.Fatalf("Expected 5 Taskruns to have executed but only got start times for %d Taskruns", len(times))
taskRuns := taskRunsResp.Items
if len(taskRuns) != 5 {
t.Fatalf("Expected 5 TaskRuns to have executed but got start times for %d TaskRuns", len(taskRuns))
}

sort.Sort(times)
sort.Slice(taskRuns, func(i, j int) bool {
it := taskRuns[i].Status.StartTime.Time
jt := taskRuns[j].Status.StartTime.Time
return it.Before(jt)
})

if !strings.HasPrefix(times[0].name, "dag-pipeline-run-pipeline-task-1") {
t.Errorf("Expected first task to execute first, but %q was first", times[0].name)
}
if !strings.HasPrefix(times[1].name, "dag-pipeline-run-pipeline-task-2") {
t.Errorf("Expected parallel tasks to run second & third, but %q was second", times[1].name)
}
if !strings.HasPrefix(times[2].name, "dag-pipeline-run-pipeline-task-2") {
t.Errorf("Expected parallel tasks to run second & third, but %q was third", times[2].name)
}
if !strings.HasPrefix(times[3].name, "dag-pipeline-run-pipeline-task-3") {
t.Errorf("Expected third task to execute third, but %q was third", times[3].name)
wantPrefixes := []string{
"dag-pipeline-run-pipeline-task-1",
// Could be task-2-parallel-1 or task-2-parallel-2
"dag-pipeline-run-pipeline-task-2-parallel",
"dag-pipeline-run-pipeline-task-2-parallel",
"dag-pipeline-run-pipeline-task-3",
"dag-pipeline-run-pipeline-task-4",
}
if !strings.HasPrefix(times[4].name, "dag-pipeline-run-pipeline-task-4") {
t.Errorf("Expected fourth task to execute fourth, but %q was fourth", times[4].name)
for i, wp := range wantPrefixes {
if !strings.HasPrefix(taskRuns[i].Name, wp) {
t.Errorf("Expected task %q to execute first, but %q was first", wp, taskRuns[0].Name)
}
}

// Check that the two tasks that can run in parallel did
parallelDiff := times[2].t.Sub(times[1].t)
if parallelDiff > (time.Second * 5) {
t.Errorf("Expected parallel tasks to execute more or less at the same time, but they were %v apart", parallelDiff)
s1 := taskRuns[1].Status.StartTime.Time
s2 := taskRuns[2].Status.StartTime.Time
absDiff := time.Duration(math.Abs(float64(s2.Sub(s1))))
if absDiff > (time.Second * 5) {
t.Errorf("Expected parallel tasks to execute more or less at the same time, but they were %v apart", absDiff)
}
}
Loading

0 comments on commit 723fca9

Please sign in to comment.