Skip to content

Commit

Permalink
Merge pull request #540 from SaschaSchwarze0/sascha-integration-test-…
Browse files Browse the repository at this point in the history
…operator-logs

Print operator log for failed integration test case
  • Loading branch information
openshift-merge-robot authored Jan 21, 2021
2 parents 05343e9 + d084b0a commit 54ef91b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
13 changes: 13 additions & 0 deletions pkg/ctxlog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package ctxlog

import (
"context"
"io"

"github.com/go-logr/logr"
"github.com/operator-framework/operator-sdk/pkg/log/zap"
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions test/integration/integration_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
44 changes: 28 additions & 16 deletions test/integration/utils/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package utils

import (
"bytes"
"context"
"os"
"path/filepath"
"strconv"
Expand All @@ -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
)
Expand All @@ -35,22 +38,29 @@ 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
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
Expand All @@ -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
}

Expand Down
4 changes: 1 addition & 3 deletions test/integration/utils/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package utils

import (
"context"

"sigs.k8s.io/controller-runtime/pkg/manager"

buildconfig "github.com/shipwright-io/build/pkg/config"
Expand All @@ -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",
Expand Down

0 comments on commit 54ef91b

Please sign in to comment.