Skip to content

Commit

Permalink
Add logging to TimeoutHandler
Browse files Browse the repository at this point in the history
The reconcile tests were causing panics or tripping race detection
because the timeout handler was using the *testing.T methods after the
test was already marked done.
The solution is to use loggers that are not tied to the testing
framework. The downside is that you may see logs from prior tests
intermixed but that is usually just the one associated with stopping the
timeout.
  • Loading branch information
fraenkel committed Sep 20, 2019
1 parent 8696190 commit 3a60493
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 17 deletions.
4 changes: 2 additions & 2 deletions pkg/reconciler/pipelinerun/cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources"
ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"
"github.com/tektoncd/pipeline/test"
tb "github.com/tektoncd/pipeline/test/builder"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
rtesting "knative.dev/pkg/reconciler/testing"
)

func TestCancelPipelineRun(t *testing.T) {
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestCancelPipelineRun(t *testing.T) {
PipelineRuns: []*v1alpha1.PipelineRun{tc.pipelineRun},
TaskRuns: tc.taskRuns,
}
ctx, _ := rtesting.SetupFakeContext(t)
ctx, _ := ttesting.SetupFakeContext(t)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
c, _ := test.SeedTestData(t, ctx, d)
Expand Down
6 changes: 3 additions & 3 deletions pkg/reconciler/pipelinerun/config/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/pipeline/pkg/artifacts"
logtesting "knative.dev/pkg/logging/testing"
ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"

test "github.com/tektoncd/pipeline/pkg/reconciler/testing"
)

func TestStoreLoadWithContext(t *testing.T) {
store := NewStore(logtesting.TestLogger(t))
store := NewStore(ttesting.TestLogger(t))
bucketConfig := test.ConfigMapFromTestFile(t, "config-artifact-bucket")
store.OnConfigChanged(bucketConfig)

Expand All @@ -40,7 +40,7 @@ func TestStoreLoadWithContext(t *testing.T) {
}
}
func TestStoreImmutableConfig(t *testing.T) {
store := NewStore(logtesting.TestLogger(t))
store := NewStore(ttesting.TestLogger(t))
store.OnConfigChanged(test.ConfigMapFromTestFile(t, "config-artifact-bucket"))

config := store.Load()
Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciler/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources"
taskrunresources "github.com/tektoncd/pipeline/pkg/reconciler/taskrun/resources"
ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"
"github.com/tektoncd/pipeline/pkg/system"
"github.com/tektoncd/pipeline/test"
tb "github.com/tektoncd/pipeline/test/builder"
Expand All @@ -39,7 +40,6 @@ import (
"knative.dev/pkg/apis"
duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1"
"knative.dev/pkg/configmap"
rtesting "knative.dev/pkg/reconciler/testing"
)

var (
Expand All @@ -53,7 +53,7 @@ func getRunName(pr *v1alpha1.PipelineRun) string {
// getPipelineRunController returns an instance of the PipelineRun controller/reconciler that has been seeded with
// d, where d represents the state of the system (existing resources) needed for the test.
func getPipelineRunController(t *testing.T, d test.Data) (test.TestAssets, func()) {
ctx, _ := rtesting.SetupFakeContext(t)
ctx, _ := ttesting.SetupFakeContext(t)
c, _ := test.SeedTestData(t, ctx, d)
configMapWatcher := configmap.NewInformedWatcher(c.Kube, system.GetNamespace())
ctx, cancel := context.WithCancel(ctx)
Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciler/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import (

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources"
ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"
"github.com/tektoncd/pipeline/test"
tb "github.com/tektoncd/pipeline/test/builder"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
"knative.dev/pkg/apis"
rtesting "knative.dev/pkg/reconciler/testing"
)

// Test case for providing recorder in the option
Expand All @@ -55,7 +55,7 @@ func TestRecorderOptions(t *testing.T) {
Pipelines: ps,
Tasks: ts,
}
ctx, _ := rtesting.SetupFakeContext(t)
ctx, _ := ttesting.SetupFakeContext(t)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
c, _ := test.SeedTestData(t, ctx, d)
Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciler/taskrun/cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"
"github.com/tektoncd/pipeline/test"
tb "github.com/tektoncd/pipeline/test/builder"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
rtesting "knative.dev/pkg/reconciler/testing"
)

func TestCancelTaskRun(t *testing.T) {
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestCancelTaskRun(t *testing.T) {
d.Pods = []*corev1.Pod{tc.pod}
}

ctx, _ := rtesting.SetupFakeContext(t)
ctx, _ := ttesting.SetupFakeContext(t)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
c, _ := test.SeedTestData(t, ctx, d)
Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciler/taskrun/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/tektoncd/pipeline/pkg/reconciler/taskrun/entrypoint"
"github.com/tektoncd/pipeline/pkg/reconciler/taskrun/resources"
"github.com/tektoncd/pipeline/pkg/reconciler/taskrun/resources/cloudevent"
ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"
"github.com/tektoncd/pipeline/pkg/status"
"github.com/tektoncd/pipeline/pkg/system"
"github.com/tektoncd/pipeline/test"
Expand All @@ -50,7 +51,6 @@ import (
"k8s.io/client-go/tools/cache"
"knative.dev/pkg/apis"
"knative.dev/pkg/configmap"
rtesting "knative.dev/pkg/reconciler/testing"
)

const (
Expand Down Expand Up @@ -254,7 +254,7 @@ func getRunName(tr *v1alpha1.TaskRun) string {
// getTaskRunController returns an instance of the TaskRun controller/reconciler that has been seeded with
// d, where d represents the state of the system (existing resources) needed for the test.
func getTaskRunController(t *testing.T, d test.Data) (test.TestAssets, func()) {
ctx, _ := rtesting.SetupFakeContext(t)
ctx, _ := ttesting.SetupFakeContext(t)
ctx, cancel := context.WithCancel(ctx)
cloudEventClientBehaviour := cloudevent.FakeClientBehaviour{
SendSuccessfully: true,
Expand Down
28 changes: 28 additions & 0 deletions pkg/reconciler/testing/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package testing

import (
"context"
"testing"

"go.uber.org/zap"
"knative.dev/pkg/controller"
"knative.dev/pkg/logging"
rtesting "knative.dev/pkg/reconciler/testing"
)

func SetupFakeContext(t *testing.T) (context.Context, []controller.Informer) {
ctx, informer := rtesting.SetupFakeContext(t)
return WithLogger(ctx, t), informer
}

func WithLogger(ctx context.Context, t *testing.T) context.Context {
return logging.WithLogger(ctx, TestLogger(t))
}

func TestLogger(t *testing.T) *zap.SugaredLogger {
logger, err := zap.NewDevelopment(zap.AddCaller())
if err != nil {
t.Fatalf("failed to create logger: %s", err)
}
return logger.Sugar().Named(t.Name())
}
10 changes: 10 additions & 0 deletions pkg/reconciler/timeout_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func backoffDuration(count uint, jf jitterFunc) time.Duration {
func (t *TimeoutSet) checkPipelineRunTimeouts(namespace string, pipelineclientset clientset.Interface) {
pipelineRuns, err := pipelineclientset.TektonV1alpha1().PipelineRuns(namespace).List(metav1.ListOptions{})
if err != nil {
t.logger.Errorf("Can't get pipelinerun list in namespace %s: %s", namespace, err)
return
}
for _, pipelineRun := range pipelineRuns.Items {
Expand All @@ -188,6 +189,7 @@ func (t *TimeoutSet) checkPipelineRunTimeouts(namespace string, pipelineclientse
func (t *TimeoutSet) CheckTimeouts(kubeclientset kubernetes.Interface, pipelineclientset clientset.Interface) {
namespaces, err := kubeclientset.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
t.logger.Errorf("Can't get namespaces list: %s", err)
return
}
for _, namespace := range namespaces.Items {
Expand All @@ -201,6 +203,7 @@ func (t *TimeoutSet) CheckTimeouts(kubeclientset kubernetes.Interface, pipelinec
func (t *TimeoutSet) checkTaskRunTimeouts(namespace string, pipelineclientset clientset.Interface) {
taskruns, err := pipelineclientset.TektonV1alpha1().TaskRuns(namespace).List(metav1.ListOptions{})
if err != nil {
t.logger.Errorf("Can't get taskrun list in namespace %s: %s", namespace, err)
return
}
for _, taskrun := range taskruns.Items {
Expand Down Expand Up @@ -242,12 +245,14 @@ func (t *TimeoutSet) WaitPipelineRun(pr *v1alpha1.PipelineRun, startTime *metav1

func (t *TimeoutSet) waitRun(runObj StatusKey, timeout time.Duration, startTime *metav1.Time, callback func(interface{})) {
if startTime == nil {
t.logger.Errorf("startTime must be specified in order for a timeout to be calculated accurately for %s", runObj.GetRunKey())
return
}
if callback == nil {
callback = defaultFunc
}
runtime := time.Since(startTime.Time)
t.logger.Infof("About to start timeout timer for %s. started at %s, timeout is %s, running for %s", runObj.GetRunKey(), startTime.Time, timeout, runtime)
defer t.Release(runObj)
t.setTimer(runObj, timeout-runtime, callback)
}
Expand All @@ -262,19 +267,24 @@ func (t *TimeoutSet) waitRun(runObj StatusKey, timeout time.Duration, startTime
func (t *TimeoutSet) SetTaskRunTimer(tr *v1alpha1.TaskRun, d time.Duration) {
callback := t.taskRunCallbackFunc
if callback == nil {
t.logger.Errorf("attempted to set a timer for %q but no task run callback has been assigned", tr.Name)
return
}
t.setTimer(tr, d, callback)
}

func (t *TimeoutSet) setTimer(runObj StatusKey, timeout time.Duration, callback func(interface{})) {
finished := t.getOrCreateFinishedChan(runObj)
started := time.Now()
select {
case <-t.stopCh:
t.logger.Infof("stopping timer for %q", runObj.GetRunKey())
return
case <-finished:
t.logger.Infof("%q finished, stopping timer", runObj.GetRunKey())
return
case <-time.After(timeout):
t.logger.Infof("timer for %q has activated after %s", runObj.GetRunKey(), time.Since(started).String())
callback(runObj)
}
}
8 changes: 4 additions & 4 deletions pkg/reconciler/timeout_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"
"github.com/tektoncd/pipeline/test"
tb "github.com/tektoncd/pipeline/test/builder"
"go.uber.org/zap"
Expand All @@ -32,7 +33,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"knative.dev/pkg/apis"
rtesting "knative.dev/pkg/reconciler/testing"
)

var (
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestTaskRunCheckTimeouts(t *testing.T) {
},
}},
}
ctx, _ := rtesting.SetupFakeContext(t)
ctx, _ := ttesting.SetupFakeContext(t)
c, _ := test.SeedTestData(t, ctx, d)
stopCh := make(chan struct{})
defer close(stopCh)
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestPipelinRunCheckTimeouts(t *testing.T) {
}},
}

ctx, _ := rtesting.SetupFakeContext(t)
ctx, _ := ttesting.SetupFakeContext(t)
c, _ := test.SeedTestData(t, ctx, d)
stopCh := make(chan struct{})
defer close(stopCh)
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestWithNoFunc(t *testing.T) {
},
}},
}
ctx, _ := rtesting.SetupFakeContext(t)
ctx, _ := ttesting.SetupFakeContext(t)
c, _ := test.SeedTestData(t, ctx, d)
stopCh := make(chan struct{})
observer, _ := observer.New(zap.InfoLevel)
Expand Down

0 comments on commit 3a60493

Please sign in to comment.