Skip to content

Commit ab2482a

Browse files
fraenkeltekton-robot
authored andcommitted
Add logging to TimeoutHandler
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.
1 parent a6663fb commit ab2482a

File tree

9 files changed

+55
-17
lines changed

9 files changed

+55
-17
lines changed

pkg/reconciler/pipelinerun/cancel_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ import (
2222

2323
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
2424
"github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources"
25+
ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"
2526
"github.com/tektoncd/pipeline/test"
2627
tb "github.com/tektoncd/pipeline/test/builder"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2829
"knative.dev/pkg/apis"
29-
rtesting "knative.dev/pkg/reconciler/testing"
3030
)
3131

3232
func TestCancelPipelineRun(t *testing.T) {
@@ -74,7 +74,7 @@ func TestCancelPipelineRun(t *testing.T) {
7474
PipelineRuns: []*v1alpha1.PipelineRun{tc.pipelineRun},
7575
TaskRuns: tc.taskRuns,
7676
}
77-
ctx, _ := rtesting.SetupFakeContext(t)
77+
ctx, _ := ttesting.SetupFakeContext(t)
7878
ctx, cancel := context.WithCancel(ctx)
7979
defer cancel()
8080
c, _ := test.SeedTestData(t, ctx, d)

pkg/reconciler/pipelinerun/config/store_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ import (
2323
"github.com/google/go-cmp/cmp"
2424
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
2525
"github.com/tektoncd/pipeline/pkg/artifacts"
26-
logtesting "knative.dev/pkg/logging/testing"
26+
ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"
2727

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

3131
func TestStoreLoadWithContext(t *testing.T) {
32-
store := NewStore(pipeline.Images{}, logtesting.TestLogger(t))
32+
store := NewStore(pipeline.Images{}, ttesting.TestLogger(t))
3333
bucketConfig := test.ConfigMapFromTestFile(t, "config-artifact-bucket")
3434
store.OnConfigChanged(bucketConfig)
3535

@@ -41,7 +41,7 @@ func TestStoreLoadWithContext(t *testing.T) {
4141
}
4242
}
4343
func TestStoreImmutableConfig(t *testing.T) {
44-
store := NewStore(pipeline.Images{}, logtesting.TestLogger(t))
44+
store := NewStore(pipeline.Images{}, ttesting.TestLogger(t))
4545
store.OnConfigChanged(test.ConfigMapFromTestFile(t, "config-artifact-bucket"))
4646

4747
config := store.Load()

pkg/reconciler/pipelinerun/pipelinerun_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
3030
"github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources"
3131
taskrunresources "github.com/tektoncd/pipeline/pkg/reconciler/taskrun/resources"
32+
ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"
3233
"github.com/tektoncd/pipeline/pkg/system"
3334
"github.com/tektoncd/pipeline/test"
3435
tb "github.com/tektoncd/pipeline/test/builder"
@@ -39,7 +40,6 @@ import (
3940
"knative.dev/pkg/apis"
4041
duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1"
4142
"knative.dev/pkg/configmap"
42-
rtesting "knative.dev/pkg/reconciler/testing"
4343
)
4444

4545
var (
@@ -65,7 +65,7 @@ func getRunName(pr *v1alpha1.PipelineRun) string {
6565
// getPipelineRunController returns an instance of the PipelineRun controller/reconciler that has been seeded with
6666
// d, where d represents the state of the system (existing resources) needed for the test.
6767
func getPipelineRunController(t *testing.T, d test.Data) (test.TestAssets, func()) {
68-
ctx, _ := rtesting.SetupFakeContext(t)
68+
ctx, _ := ttesting.SetupFakeContext(t)
6969
c, _ := test.SeedTestData(t, ctx, d)
7070
configMapWatcher := configmap.NewInformedWatcher(c.Kube, system.GetNamespace())
7171
ctx, cancel := context.WithCancel(ctx)

pkg/reconciler/reconciler_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ import (
2525
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
2626
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
2727
"github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources"
28+
ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"
2829
"github.com/tektoncd/pipeline/test"
2930
tb "github.com/tektoncd/pipeline/test/builder"
3031
"go.uber.org/zap"
3132
"go.uber.org/zap/zaptest/observer"
3233
corev1 "k8s.io/api/core/v1"
3334
"k8s.io/client-go/tools/record"
3435
"knative.dev/pkg/apis"
35-
rtesting "knative.dev/pkg/reconciler/testing"
3636
)
3737

3838
// Test case for providing recorder in the option
@@ -56,7 +56,7 @@ func TestRecorderOptions(t *testing.T) {
5656
Pipelines: ps,
5757
Tasks: ts,
5858
}
59-
ctx, _ := rtesting.SetupFakeContext(t)
59+
ctx, _ := ttesting.SetupFakeContext(t)
6060
ctx, cancel := context.WithCancel(ctx)
6161
defer cancel()
6262
c, _ := test.SeedTestData(t, ctx, d)

pkg/reconciler/taskrun/cancel_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ import (
2222

2323
"github.com/google/go-cmp/cmp"
2424
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
25+
ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"
2526
"github.com/tektoncd/pipeline/test"
2627
tb "github.com/tektoncd/pipeline/test/builder"
2728
"go.uber.org/zap"
2829
"go.uber.org/zap/zaptest/observer"
2930
corev1 "k8s.io/api/core/v1"
3031
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132
"knative.dev/pkg/apis"
32-
rtesting "knative.dev/pkg/reconciler/testing"
3333
)
3434

3535
func TestCancelTaskRun(t *testing.T) {
@@ -83,7 +83,7 @@ func TestCancelTaskRun(t *testing.T) {
8383
d.Pods = []*corev1.Pod{tc.pod}
8484
}
8585

86-
ctx, _ := rtesting.SetupFakeContext(t)
86+
ctx, _ := ttesting.SetupFakeContext(t)
8787
ctx, cancel := context.WithCancel(ctx)
8888
defer cancel()
8989
c, _ := test.SeedTestData(t, ctx, d)

pkg/reconciler/taskrun/taskrun_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/tektoncd/pipeline/pkg/reconciler/taskrun/entrypoint"
3333
"github.com/tektoncd/pipeline/pkg/reconciler/taskrun/resources"
3434
"github.com/tektoncd/pipeline/pkg/reconciler/taskrun/resources/cloudevent"
35+
ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"
3536
"github.com/tektoncd/pipeline/pkg/status"
3637
"github.com/tektoncd/pipeline/pkg/system"
3738
"github.com/tektoncd/pipeline/test"
@@ -51,7 +52,6 @@ import (
5152
"k8s.io/client-go/tools/cache"
5253
"knative.dev/pkg/apis"
5354
"knative.dev/pkg/configmap"
54-
rtesting "knative.dev/pkg/reconciler/testing"
5555
)
5656

5757
const (
@@ -267,7 +267,7 @@ func getRunName(tr *v1alpha1.TaskRun) string {
267267
// getTaskRunController returns an instance of the TaskRun controller/reconciler that has been seeded with
268268
// d, where d represents the state of the system (existing resources) needed for the test.
269269
func getTaskRunController(t *testing.T, d test.Data) (test.TestAssets, func()) {
270-
ctx, _ := rtesting.SetupFakeContext(t)
270+
ctx, _ := ttesting.SetupFakeContext(t)
271271
ctx, cancel := context.WithCancel(ctx)
272272
cloudEventClientBehaviour := cloudevent.FakeClientBehaviour{
273273
SendSuccessfully: true,

pkg/reconciler/testing/logger.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package testing
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"go.uber.org/zap"
8+
"knative.dev/pkg/controller"
9+
"knative.dev/pkg/logging"
10+
rtesting "knative.dev/pkg/reconciler/testing"
11+
)
12+
13+
func SetupFakeContext(t *testing.T) (context.Context, []controller.Informer) {
14+
ctx, informer := rtesting.SetupFakeContext(t)
15+
return WithLogger(ctx, t), informer
16+
}
17+
18+
func WithLogger(ctx context.Context, t *testing.T) context.Context {
19+
return logging.WithLogger(ctx, TestLogger(t))
20+
}
21+
22+
func TestLogger(t *testing.T) *zap.SugaredLogger {
23+
logger, err := zap.NewDevelopment(zap.AddCaller())
24+
if err != nil {
25+
t.Fatalf("failed to create logger: %s", err)
26+
}
27+
return logger.Sugar().Named(t.Name())
28+
}

pkg/reconciler/timeout_handler.go

+10
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ func backoffDuration(count uint, jf jitterFunc) time.Duration {
170170
func (t *TimeoutSet) checkPipelineRunTimeouts(namespace string, pipelineclientset clientset.Interface) {
171171
pipelineRuns, err := pipelineclientset.TektonV1alpha1().PipelineRuns(namespace).List(metav1.ListOptions{})
172172
if err != nil {
173+
t.logger.Errorf("Can't get pipelinerun list in namespace %s: %s", namespace, err)
173174
return
174175
}
175176
for _, pipelineRun := range pipelineRuns.Items {
@@ -188,6 +189,7 @@ func (t *TimeoutSet) checkPipelineRunTimeouts(namespace string, pipelineclientse
188189
func (t *TimeoutSet) CheckTimeouts(kubeclientset kubernetes.Interface, pipelineclientset clientset.Interface) {
189190
namespaces, err := kubeclientset.CoreV1().Namespaces().List(metav1.ListOptions{})
190191
if err != nil {
192+
t.logger.Errorf("Can't get namespaces list: %s", err)
191193
return
192194
}
193195
for _, namespace := range namespaces.Items {
@@ -201,6 +203,7 @@ func (t *TimeoutSet) CheckTimeouts(kubeclientset kubernetes.Interface, pipelinec
201203
func (t *TimeoutSet) checkTaskRunTimeouts(namespace string, pipelineclientset clientset.Interface) {
202204
taskruns, err := pipelineclientset.TektonV1alpha1().TaskRuns(namespace).List(metav1.ListOptions{})
203205
if err != nil {
206+
t.logger.Errorf("Can't get taskrun list in namespace %s: %s", namespace, err)
204207
return
205208
}
206209
for _, taskrun := range taskruns.Items {
@@ -242,12 +245,14 @@ func (t *TimeoutSet) WaitPipelineRun(pr *v1alpha1.PipelineRun, startTime *metav1
242245

243246
func (t *TimeoutSet) waitRun(runObj StatusKey, timeout time.Duration, startTime *metav1.Time, callback func(interface{})) {
244247
if startTime == nil {
248+
t.logger.Errorf("startTime must be specified in order for a timeout to be calculated accurately for %s", runObj.GetRunKey())
245249
return
246250
}
247251
if callback == nil {
248252
callback = defaultFunc
249253
}
250254
runtime := time.Since(startTime.Time)
255+
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)
251256
defer t.Release(runObj)
252257
t.setTimer(runObj, timeout-runtime, callback)
253258
}
@@ -262,19 +267,24 @@ func (t *TimeoutSet) waitRun(runObj StatusKey, timeout time.Duration, startTime
262267
func (t *TimeoutSet) SetTaskRunTimer(tr *v1alpha1.TaskRun, d time.Duration) {
263268
callback := t.taskRunCallbackFunc
264269
if callback == nil {
270+
t.logger.Errorf("attempted to set a timer for %q but no task run callback has been assigned", tr.Name)
265271
return
266272
}
267273
t.setTimer(tr, d, callback)
268274
}
269275

270276
func (t *TimeoutSet) setTimer(runObj StatusKey, timeout time.Duration, callback func(interface{})) {
271277
finished := t.getOrCreateFinishedChan(runObj)
278+
started := time.Now()
272279
select {
273280
case <-t.stopCh:
281+
t.logger.Infof("stopping timer for %q", runObj.GetRunKey())
274282
return
275283
case <-finished:
284+
t.logger.Infof("%q finished, stopping timer", runObj.GetRunKey())
276285
return
277286
case <-time.After(timeout):
287+
t.logger.Infof("timer for %q has activated after %s", runObj.GetRunKey(), time.Since(started).String())
278288
callback(runObj)
279289
}
280290
}

pkg/reconciler/timeout_handler_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/tektoncd/pipeline/pkg/apis/config"
2525
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
26+
ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"
2627
"github.com/tektoncd/pipeline/test"
2728
tb "github.com/tektoncd/pipeline/test/builder"
2829
"go.uber.org/zap"
@@ -32,7 +33,6 @@ import (
3233
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3334
"k8s.io/apimachinery/pkg/util/wait"
3435
"knative.dev/pkg/apis"
35-
rtesting "knative.dev/pkg/reconciler/testing"
3636
)
3737

3838
var (
@@ -95,7 +95,7 @@ func TestTaskRunCheckTimeouts(t *testing.T) {
9595
},
9696
}},
9797
}
98-
ctx, _ := rtesting.SetupFakeContext(t)
98+
ctx, _ := ttesting.SetupFakeContext(t)
9999
c, _ := test.SeedTestData(t, ctx, d)
100100
stopCh := make(chan struct{})
101101
defer close(stopCh)
@@ -221,7 +221,7 @@ func TestPipelinRunCheckTimeouts(t *testing.T) {
221221
}},
222222
}
223223

224-
ctx, _ := rtesting.SetupFakeContext(t)
224+
ctx, _ := ttesting.SetupFakeContext(t)
225225
c, _ := test.SeedTestData(t, ctx, d)
226226
stopCh := make(chan struct{})
227227
defer close(stopCh)
@@ -301,7 +301,7 @@ func TestWithNoFunc(t *testing.T) {
301301
},
302302
}},
303303
}
304-
ctx, _ := rtesting.SetupFakeContext(t)
304+
ctx, _ := ttesting.SetupFakeContext(t)
305305
c, _ := test.SeedTestData(t, ctx, d)
306306
stopCh := make(chan struct{})
307307
observer, _ := observer.New(zap.InfoLevel)

0 commit comments

Comments
 (0)