From d084b0a0d6b850a6d4022a97f9b0359070e7c0aa Mon Sep 17 00:00:00 2001 From: Sascha Schwarze Date: Thu, 21 Jan 2021 13:31:03 +0100 Subject: [PATCH] Print operator log for failed integration test case --- pkg/ctxlog/log.go | 13 +++++++ test/integration/integration_suite_test.go | 6 +++ test/integration/utils/environment.go | 44 ++++++++++++++-------- test/integration/utils/operator.go | 4 +- 4 files changed, 48 insertions(+), 19 deletions(-) diff --git a/pkg/ctxlog/log.go b/pkg/ctxlog/log.go index 72cd4a7f59..ebfd78e0d8 100644 --- a/pkg/ctxlog/log.go +++ b/pkg/ctxlog/log.go @@ -6,6 +6,7 @@ package ctxlog import ( "context" + "io" "github.com/go-logr/logr" "github.com/operator-framework/operator-sdk/pkg/log/zap" @@ -26,6 +27,18 @@ func NewLogger(name string) logr.Logger { return l } +// NewLoggerTo returns a new Logger which logs +// to a given destination. +func NewLoggerTo(destWriter io.Writer, name string) logr.Logger { + l := zap.LoggerTo(destWriter) + + logf.SetLogger(l) + + l = l.WithName(name) + + return l +} + // Error returns an ERROR level log from an specified context func Error(ctx context.Context, err error, msg string, v ...interface{}) { l := ExtractLogger(ctx) diff --git a/test/integration/integration_suite_test.go b/test/integration/integration_suite_test.go index 98ad2bd270..2195b018fb 100644 --- a/test/integration/integration_suite_test.go +++ b/test/integration/integration_suite_test.go @@ -55,6 +55,12 @@ var _ = AfterEach(func() { if tb.StopBuildOperator != nil { close(tb.StopBuildOperator) } + + if CurrentGinkgoTestDescription().Failed && tb.BuildOperatorLogBuffer != nil { + // print operator logs + fmt.Println("\nLogs of the operator:") + fmt.Printf("%v\n", tb.BuildOperatorLogBuffer) + } }) var _ = AfterSuite(func() { diff --git a/test/integration/utils/environment.go b/test/integration/utils/environment.go index fa226a9c2f..64a79cefbd 100644 --- a/test/integration/utils/environment.go +++ b/test/integration/utils/environment.go @@ -5,6 +5,8 @@ package utils import ( + "bytes" + "context" "os" "path/filepath" "strconv" @@ -20,6 +22,7 @@ import ( "k8s.io/client-go/tools/clientcmd" buildClient "github.com/shipwright-io/build/pkg/client/build/clientset/versioned" + "github.com/shipwright-io/build/pkg/ctxlog" "github.com/shipwright-io/build/test" // from https://github.com/kubernetes/client-go/issues/345 ) @@ -35,15 +38,17 @@ type TestBuild struct { // TODO: Adding specific field for polling here, interval and timeout // but I think we need a small refactoring to make them global for all // tests under /test dir - Interval time.Duration - TimeOut time.Duration - KubeConfig *rest.Config - Clientset *kubernetes.Clientset - Namespace string - StopBuildOperator chan struct{} - BuildClientSet *buildClient.Clientset - PipelineClientSet *tektonClient.Clientset - Catalog test.Catalog + Interval time.Duration + TimeOut time.Duration + KubeConfig *rest.Config + Clientset *kubernetes.Clientset + Namespace string + StopBuildOperator chan struct{} + BuildClientSet *buildClient.Clientset + PipelineClientSet *tektonClient.Clientset + Catalog test.Catalog + Context context.Context + BuildOperatorLogBuffer *bytes.Buffer } // NewTestBuild returns an initialized instance of TestBuild @@ -51,6 +56,11 @@ func NewTestBuild() (*TestBuild, error) { namespaceID := gomegaConfig.GinkgoConfig.ParallelNode*200 + int(atomic.AddInt32(&namespaceCounter, 1)) testNamespace := "test-build-" + strconv.Itoa(int(namespaceID)) + logBuffer := &bytes.Buffer{} + l := ctxlog.NewLoggerTo(logBuffer, testNamespace) + + ctx := ctxlog.NewParentContext(l) + kubeConfig, restConfig, err := KubeConfig() if err != nil { return nil, err @@ -70,13 +80,15 @@ func NewTestBuild() (*TestBuild, error) { return &TestBuild{ // TODO: interval and timeout can be configured via ENV vars - Interval: time.Second * 3, - TimeOut: time.Second * 180, - KubeConfig: restConfig, - Clientset: kubeConfig, - Namespace: testNamespace, - BuildClientSet: buildClientSet, - PipelineClientSet: pipelineClientSet, + Interval: time.Second * 3, + TimeOut: time.Second * 180, + KubeConfig: restConfig, + Clientset: kubeConfig, + Namespace: testNamespace, + BuildClientSet: buildClientSet, + PipelineClientSet: pipelineClientSet, + Context: ctx, + BuildOperatorLogBuffer: logBuffer, }, nil } diff --git a/test/integration/utils/operator.go b/test/integration/utils/operator.go index d1adea0bdd..357e3bc575 100644 --- a/test/integration/utils/operator.go +++ b/test/integration/utils/operator.go @@ -5,8 +5,6 @@ package utils import ( - "context" - "sigs.k8s.io/controller-runtime/pkg/manager" buildconfig "github.com/shipwright-io/build/pkg/config" @@ -19,7 +17,7 @@ import ( func (t *TestBuild) StartBuildOperator() (chan struct{}, error) { c := buildconfig.NewDefaultConfig() - mgr, err := controller.NewManager(context.Background(), c, t.KubeConfig, manager.Options{ + mgr, err := controller.NewManager(t.Context, c, t.KubeConfig, manager.Options{ Namespace: t.Namespace, LeaderElection: false, MetricsBindAddress: "0",