Skip to content

Commit d084b0a

Browse files
Print operator log for failed integration test case
1 parent 05343e9 commit d084b0a

File tree

4 files changed

+48
-19
lines changed

4 files changed

+48
-19
lines changed

pkg/ctxlog/log.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package ctxlog
66

77
import (
88
"context"
9+
"io"
910

1011
"github.com/go-logr/logr"
1112
"github.com/operator-framework/operator-sdk/pkg/log/zap"
@@ -26,6 +27,18 @@ func NewLogger(name string) logr.Logger {
2627
return l
2728
}
2829

30+
// NewLoggerTo returns a new Logger which logs
31+
// to a given destination.
32+
func NewLoggerTo(destWriter io.Writer, name string) logr.Logger {
33+
l := zap.LoggerTo(destWriter)
34+
35+
logf.SetLogger(l)
36+
37+
l = l.WithName(name)
38+
39+
return l
40+
}
41+
2942
// Error returns an ERROR level log from an specified context
3043
func Error(ctx context.Context, err error, msg string, v ...interface{}) {
3144
l := ExtractLogger(ctx)

test/integration/integration_suite_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ var _ = AfterEach(func() {
5555
if tb.StopBuildOperator != nil {
5656
close(tb.StopBuildOperator)
5757
}
58+
59+
if CurrentGinkgoTestDescription().Failed && tb.BuildOperatorLogBuffer != nil {
60+
// print operator logs
61+
fmt.Println("\nLogs of the operator:")
62+
fmt.Printf("%v\n", tb.BuildOperatorLogBuffer)
63+
}
5864
})
5965

6066
var _ = AfterSuite(func() {

test/integration/utils/environment.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package utils
66

77
import (
8+
"bytes"
9+
"context"
810
"os"
911
"path/filepath"
1012
"strconv"
@@ -20,6 +22,7 @@ import (
2022
"k8s.io/client-go/tools/clientcmd"
2123

2224
buildClient "github.com/shipwright-io/build/pkg/client/build/clientset/versioned"
25+
"github.com/shipwright-io/build/pkg/ctxlog"
2326
"github.com/shipwright-io/build/test"
2427
// from https://github.com/kubernetes/client-go/issues/345
2528
)
@@ -35,22 +38,29 @@ type TestBuild struct {
3538
// TODO: Adding specific field for polling here, interval and timeout
3639
// but I think we need a small refactoring to make them global for all
3740
// tests under /test dir
38-
Interval time.Duration
39-
TimeOut time.Duration
40-
KubeConfig *rest.Config
41-
Clientset *kubernetes.Clientset
42-
Namespace string
43-
StopBuildOperator chan struct{}
44-
BuildClientSet *buildClient.Clientset
45-
PipelineClientSet *tektonClient.Clientset
46-
Catalog test.Catalog
41+
Interval time.Duration
42+
TimeOut time.Duration
43+
KubeConfig *rest.Config
44+
Clientset *kubernetes.Clientset
45+
Namespace string
46+
StopBuildOperator chan struct{}
47+
BuildClientSet *buildClient.Clientset
48+
PipelineClientSet *tektonClient.Clientset
49+
Catalog test.Catalog
50+
Context context.Context
51+
BuildOperatorLogBuffer *bytes.Buffer
4752
}
4853

4954
// NewTestBuild returns an initialized instance of TestBuild
5055
func NewTestBuild() (*TestBuild, error) {
5156
namespaceID := gomegaConfig.GinkgoConfig.ParallelNode*200 + int(atomic.AddInt32(&namespaceCounter, 1))
5257
testNamespace := "test-build-" + strconv.Itoa(int(namespaceID))
5358

59+
logBuffer := &bytes.Buffer{}
60+
l := ctxlog.NewLoggerTo(logBuffer, testNamespace)
61+
62+
ctx := ctxlog.NewParentContext(l)
63+
5464
kubeConfig, restConfig, err := KubeConfig()
5565
if err != nil {
5666
return nil, err
@@ -70,13 +80,15 @@ func NewTestBuild() (*TestBuild, error) {
7080

7181
return &TestBuild{
7282
// TODO: interval and timeout can be configured via ENV vars
73-
Interval: time.Second * 3,
74-
TimeOut: time.Second * 180,
75-
KubeConfig: restConfig,
76-
Clientset: kubeConfig,
77-
Namespace: testNamespace,
78-
BuildClientSet: buildClientSet,
79-
PipelineClientSet: pipelineClientSet,
83+
Interval: time.Second * 3,
84+
TimeOut: time.Second * 180,
85+
KubeConfig: restConfig,
86+
Clientset: kubeConfig,
87+
Namespace: testNamespace,
88+
BuildClientSet: buildClientSet,
89+
PipelineClientSet: pipelineClientSet,
90+
Context: ctx,
91+
BuildOperatorLogBuffer: logBuffer,
8092
}, nil
8193
}
8294

test/integration/utils/operator.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
package utils
66

77
import (
8-
"context"
9-
108
"sigs.k8s.io/controller-runtime/pkg/manager"
119

1210
buildconfig "github.com/shipwright-io/build/pkg/config"
@@ -19,7 +17,7 @@ import (
1917
func (t *TestBuild) StartBuildOperator() (chan struct{}, error) {
2018
c := buildconfig.NewDefaultConfig()
2119

22-
mgr, err := controller.NewManager(context.Background(), c, t.KubeConfig, manager.Options{
20+
mgr, err := controller.NewManager(t.Context, c, t.KubeConfig, manager.Options{
2321
Namespace: t.Namespace,
2422
LeaderElection: false,
2523
MetricsBindAddress: "0",

0 commit comments

Comments
 (0)