Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small refactoring of Wait* commands 🐏 #2253

Merged
merged 1 commit into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 4 additions & 51 deletions test/cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ limitations under the License.
package test

import (
"errors"
"fmt"
"sync"
"testing"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
tb "github.com/tektoncd/pipeline/test/builder"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
knativetest "knative.dev/pkg/test"
)

Expand Down Expand Up @@ -91,17 +87,7 @@ func TestTaskRunPipelineRunCancel(t *testing.T) {
}

t.Logf("Waiting for Pipelinerun %s in namespace %s to be started", "pear", namespace)
if err := WaitForPipelineRunState(c, "pear", pipelineRunTimeout, func(pr *v1alpha1.PipelineRun) (bool, error) {
c := pr.Status.GetCondition(apis.ConditionSucceeded)
if c != nil {
if c.Status == corev1.ConditionTrue || c.Status == corev1.ConditionFalse {
return true, errors.New(`pipelineRun "pear" already finished`)
} else if c.Status == corev1.ConditionUnknown && (c.Reason == "Running" || c.Reason == "Pending") {
return true, nil
}
}
return false, nil
}, "PipelineRunRunning"); err != nil {
if err := WaitForPipelineRunState(c, "pear", pipelineRunTimeout, Running("pear"), "PipelineRunRunning"); err != nil {
t.Fatalf("Error waiting for PipelineRun %s to be running: %s", "pear", err)
}

Expand All @@ -118,16 +104,7 @@ func TestTaskRunPipelineRunCancel(t *testing.T) {
wg.Add(1)
go func(name string) {
defer wg.Done()
err := WaitForTaskRunState(c, name, func(tr *v1alpha1.TaskRun) (bool, error) {
if c := tr.Status.GetCondition(apis.ConditionSucceeded); c != nil {
if c.IsTrue() || c.IsFalse() {
return true, fmt.Errorf("taskRun %q already finished", name)
} else if c.IsUnknown() && (c.Reason == "Running" || c.Reason == "Pending") {
return true, nil
}
}
return false, nil
}, "TaskRunRunning")
err := WaitForTaskRunState(c, name, Running(name), "TaskRunRunning")
if err != nil {
t.Errorf("Error waiting for TaskRun %s to be running: %v", name, err)
}
Expand All @@ -146,19 +123,7 @@ func TestTaskRunPipelineRunCancel(t *testing.T) {
}

t.Logf("Waiting for PipelineRun %s in namespace %s to be cancelled", "pear", namespace)
if err := WaitForPipelineRunState(c, "pear", pipelineRunTimeout, func(pr *v1alpha1.PipelineRun) (bool, error) {
if c := pr.Status.GetCondition(apis.ConditionSucceeded); c != nil {
if c.IsFalse() {
if c.Reason == "PipelineRunCancelled" {
return true, nil
}
return true, fmt.Errorf(`pipelineRun "pear" completed with the wrong reason: %s`, c.Reason)
} else if c.IsTrue() {
return true, errors.New(`pipelineRun "pear" completed successfully, should have been cancelled`)
}
}
return false, nil
}, "PipelineRunCancelled"); err != nil {
if err := WaitForPipelineRunState(c, "pear", pipelineRunTimeout, FailedWithReason("PipelineRunCancelled", "pear"), "PipelineRunCancelled"); err != nil {
t.Errorf("Error waiting for PipelineRun `pear` to finished: %s", err)
}

Expand All @@ -167,19 +132,7 @@ func TestTaskRunPipelineRunCancel(t *testing.T) {
wg.Add(1)
go func(name string) {
defer wg.Done()
err := WaitForTaskRunState(c, name, func(tr *v1alpha1.TaskRun) (bool, error) {
if c := tr.Status.GetCondition(apis.ConditionSucceeded); c != nil {
if c.IsFalse() {
if c.Reason == "TaskRunCancelled" {
return true, nil
}
return true, fmt.Errorf("taskRun %q completed with the wrong reason: %s", name, c.Reason)
} else if c.IsTrue() {
return true, fmt.Errorf("taskRun %q completed successfully, should have been cancelled", name)
}
}
return false, nil
}, "TaskRunCancelled")
err := WaitForTaskRunState(c, name, FailedWithReason("TaskRunCancelled", name), "TaskRunCancelled")
if err != nil {
t.Errorf("Error waiting for TaskRun %s to be finished: %v", name, err)
}
Expand Down
4 changes: 1 addition & 3 deletions test/kaniko_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ func TestKanikoTaskRun(t *testing.T) {

// Verify status of TaskRun (wait for it)

if err := WaitForTaskRunState(c, kanikoTaskRunName, func(tr *v1alpha1.TaskRun) (bool, error) {
return TaskRunSucceed(kanikoTaskRunName)(tr)
}, "TaskRunCompleted"); err != nil {
if err := WaitForTaskRunState(c, kanikoTaskRunName, Succeed(kanikoTaskRunName), "TaskRunCompleted"); err != nil {
t.Errorf("Error waiting for TaskRun %s to finish: %s", kanikoTaskRunName, err)
}

Expand Down
13 changes: 7 additions & 6 deletions test/sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"testing"
"time"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
tb "github.com/tektoncd/pipeline/test/builder"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -91,14 +90,16 @@ func TestSidecarTaskSupport(t *testing.T) {
t.Errorf("Failed to create TaskRun %q: %v", sidecarTaskRunName, err)
}

var podName string
if err := WaitForTaskRunState(clients, sidecarTaskRunName, func(tr *v1alpha1.TaskRun) (bool, error) {
podName = tr.Status.PodName
return TaskRunSucceed(sidecarTaskRunName)(tr)
}, "TaskRunSucceed"); err != nil {
if err := WaitForTaskRunState(clients, sidecarTaskRunName, Succeed(sidecarTaskRunName), "TaskRunSucceed"); err != nil {
t.Errorf("Error waiting for TaskRun %q to finish: %v", sidecarTaskRunName, err)
}

tr, err := clients.TaskRunClient.Get(sidecarTaskRunName, metav1.GetOptions{})
if err != nil {
t.Errorf("Error getting Taskrun: %v", err)
}
podName := tr.Status.PodName

if err := WaitForPodState(clients, podName, namespace, func(pod *corev1.Pod) (bool, error) {
terminatedCount := 0
for _, c := range pod.Status.ContainerStatuses {
Expand Down
118 changes: 13 additions & 105 deletions test/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,7 @@ func TestPipelineRunTimeout(t *testing.T) {
}

t.Logf("Waiting for Pipelinerun %s in namespace %s to be started", pipelineRun.Name, namespace)
if err := WaitForPipelineRunState(c, pipelineRun.Name, timeout, func(pr *v1alpha1.PipelineRun) (bool, error) {
c := pr.Status.GetCondition(apis.ConditionSucceeded)
if c != nil {
if c.Status == corev1.ConditionTrue || c.Status == corev1.ConditionFalse {
return true, fmt.Errorf("pipelineRun %q already finished", pipelineRun.Name)
} else if c.Status == corev1.ConditionUnknown && (c.Reason == "Running" || c.Reason == "Pending") {
return true, nil
}
}
return false, nil
}, "PipelineRunRunning"); err != nil {
if err := WaitForPipelineRunState(c, pipelineRun.Name, timeout, Running(pipelineRun.Name), "PipelineRunRunning"); err != nil {
t.Fatalf("Error waiting for PipelineRun %s to be running: %s", pipelineRun.Name, err)
}

Expand All @@ -89,17 +79,7 @@ func TestPipelineRunTimeout(t *testing.T) {

for _, taskrunItem := range taskrunList.Items {
go func(name string) {
err := WaitForTaskRunState(c, name, func(tr *v1alpha1.TaskRun) (bool, error) {
c := tr.Status.GetCondition(apis.ConditionSucceeded)
if c != nil {
if c.Status == corev1.ConditionTrue || c.Status == corev1.ConditionFalse {
return true, fmt.Errorf("taskRun %q already finished", name)
} else if c.Status == corev1.ConditionUnknown && (c.Reason == "Running" || c.Reason == "Pending") {
return true, nil
}
}
return false, nil
}, "TaskRunRunning")
err := WaitForTaskRunState(c, name, Running(name), "TaskRunRunning")
errChan <- err
}(taskrunItem.Name)
}
Expand All @@ -115,20 +95,7 @@ func TestPipelineRunTimeout(t *testing.T) {
}

t.Logf("Waiting for PipelineRun %s in namespace %s to be timed out", pipelineRun.Name, namespace)
if err := WaitForPipelineRunState(c, pipelineRun.Name, timeout, func(pr *v1alpha1.PipelineRun) (bool, error) {
c := pr.Status.GetCondition(apis.ConditionSucceeded)
if c != nil {
if c.Status == corev1.ConditionFalse {
if c.Reason == resources.ReasonTimedOut {
return true, nil
}
return true, fmt.Errorf("pipelineRun %q completed with the wrong reason: %s", pipelineRun.Name, c.Reason)
} else if c.Status == corev1.ConditionTrue {
return true, fmt.Errorf("pipelineRun %q completed successfully, should have been timed out", pipelineRun.Name)
}
}
return false, nil
}, "PipelineRunTimedOut"); err != nil {
if err := WaitForPipelineRunState(c, pipelineRun.Name, timeout, FailedWithReason(resources.ReasonTimedOut, pipelineRun.Name), "PipelineRunTimedOut"); err != nil {
t.Errorf("Error waiting for PipelineRun %s to finish: %s", pipelineRun.Name, err)
}

Expand All @@ -138,20 +105,7 @@ func TestPipelineRunTimeout(t *testing.T) {
wg.Add(1)
go func(name string) {
defer wg.Done()
err := WaitForTaskRunState(c, name, func(tr *v1alpha1.TaskRun) (bool, error) {
cond := tr.Status.GetCondition(apis.ConditionSucceeded)
if cond != nil {
if cond.Status == corev1.ConditionFalse {
if cond.Reason == "TaskRunTimeout" {
return true, nil
}
return true, fmt.Errorf("taskRun %q completed with the wrong reason: %s", task.Name, cond.Reason)
} else if cond.Status == corev1.ConditionTrue {
return true, fmt.Errorf("taskRun %q completed successfully, should have been timed out", name)
}
}
return false, nil
}, "TaskRunTimeout")
err := WaitForTaskRunState(c, name, FailedWithReason("TaskRunTimeout", name), "TaskRunTimeout")
if err != nil {
t.Errorf("Error waiting for TaskRun %s to timeout: %s", name, err)
}
Expand Down Expand Up @@ -202,21 +156,7 @@ func TestTaskRunTimeout(t *testing.T) {
}

t.Logf("Waiting for TaskRun %s in namespace %s to complete", "run-giraffe", namespace)
if err := WaitForTaskRunState(c, "run-giraffe", func(tr *v1alpha1.TaskRun) (bool, error) {
cond := tr.Status.GetCondition(apis.ConditionSucceeded)
if cond != nil {
if cond.Status == corev1.ConditionFalse {
if cond.Reason == "TaskRunTimeout" {
return true, nil
}
return true, fmt.Errorf("taskRun %q completed with the wrong reason: %s", "run-giraffe", cond.Reason)
} else if cond.Status == corev1.ConditionTrue {
return true, fmt.Errorf("taskRun %q completed successfully, should have been timed out", "run-giraffe")
}
}

return false, nil
}, "TaskRunTimeout"); err != nil {
if err := WaitForTaskRunState(c, "run-giraffe", FailedWithReason("TaskRunTimeout", "run-giraffe"), "TaskRunTimeout"); err != nil {
t.Errorf("Error waiting for TaskRun %s to finish: %s", "run-giraffe", err)
}
}
Expand Down Expand Up @@ -259,17 +199,7 @@ func TestPipelineTaskTimeout(t *testing.T) {
}

t.Logf("Waiting for Pipelinerun %s in namespace %s to be started", pipelineRun.Name, namespace)
if err := WaitForPipelineRunState(c, pipelineRun.Name, timeout, func(pr *v1alpha1.PipelineRun) (bool, error) {
c := pr.Status.GetCondition(apis.ConditionSucceeded)
if c != nil {
if c.Status == corev1.ConditionTrue || c.Status == corev1.ConditionFalse {
return true, fmt.Errorf("pipelineRun %q already finished", pipelineRun.Name)
} else if c.Status == corev1.ConditionUnknown && (c.Reason == "Running" || c.Reason == "Pending") {
return true, nil
}
}
return false, nil
}, "PipelineRunRunning"); err != nil {
if err := WaitForPipelineRunState(c, pipelineRun.Name, timeout, Running(pipelineRun.Name), "PipelineRunRunning"); err != nil {
t.Fatalf("Error waiting for PipelineRun %s to be running: %s", pipelineRun.Name, err)
}

Expand All @@ -284,17 +214,7 @@ func TestPipelineTaskTimeout(t *testing.T) {

for _, taskrunItem := range taskrunList.Items {
go func(name string) {
err := WaitForTaskRunState(c, name, func(tr *v1alpha1.TaskRun) (bool, error) {
c := tr.Status.GetCondition(apis.ConditionSucceeded)
if c != nil {
if c.Status == corev1.ConditionTrue || c.Status == corev1.ConditionFalse {
return true, fmt.Errorf("taskRun %q already finished", name)
} else if c.Status == corev1.ConditionUnknown && (c.Reason == "Running" || c.Reason == "Pending") {
return true, nil
}
}
return false, nil
}, "TaskRunRunning")
err := WaitForTaskRunState(c, name, Running(name), "TaskRunRunning")
errChan <- err
}(taskrunItem.Name)
}
Expand All @@ -310,31 +230,19 @@ func TestPipelineTaskTimeout(t *testing.T) {
}

t.Logf("Waiting for PipelineRun %s with PipelineTask timeout in namespace %s to fail", pipelineRun.Name, namespace)
if err := WaitForPipelineRunState(c, pipelineRun.Name, timeout, func(pr *v1alpha1.PipelineRun) (bool, error) {
c := pr.Status.GetCondition(apis.ConditionSucceeded)
if c != nil {
if c.Status == corev1.ConditionFalse {
if c.Reason == resources.ReasonFailed {
return true, nil
}
return true, fmt.Errorf("pipelineRun %q completed with the wrong reason: %s", pipelineRun.Name, c.Reason)
} else if c.Status == corev1.ConditionTrue {
return true, fmt.Errorf("pipelineRun %q completed successfully, but should have been Failed", pipelineRun.Name)
}
}
return false, nil
}, "PipelineRunTimedOut"); err != nil {
if err := WaitForPipelineRunState(c, pipelineRun.Name, timeout, FailedWithReason(resources.ReasonFailed, pipelineRun.Name), "PipelineRunTimedOut"); err != nil {
t.Errorf("Error waiting for PipelineRun %s to finish: %s", pipelineRun.Name, err)
}

t.Logf("Waiting for TaskRun from PipelineRun %s in namespace %s to be timed out", pipelineRun.Name, namespace)
var wg sync.WaitGroup
for _, taskrunItem := range taskrunList.Items {
wg.Add(1)
go func(name string) {
go func(tr v1alpha1.TaskRun) {
defer wg.Done()
err := WaitForTaskRunState(c, name, func(tr *v1alpha1.TaskRun) (bool, error) {
cond := tr.Status.GetCondition(apis.ConditionSucceeded)
name := tr.Name
err := WaitForTaskRunState(c, name, func(ca apis.ConditionAccessor) (bool, error) {
cond := ca.GetCondition(apis.ConditionSucceeded)
if cond != nil {
if tr.Spec.TaskRef.Name == task1.Name && cond.Status == corev1.ConditionTrue {
if cond.Reason == "Succeeded" {
Expand All @@ -359,7 +267,7 @@ func TestPipelineTaskTimeout(t *testing.T) {
if err != nil {
t.Errorf("Error waiting for TaskRun %s to timeout: %s", name, err)
}
}(taskrunItem.Name)
}(taskrunItem)
}
wg.Wait()
}
Loading