Skip to content

Commit 5913a99

Browse files
committed
tests: Filter log messages
This avoids a lot of logspam and lets us better see what is actually happening.
1 parent 7824d17 commit 5913a99

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

config/tests/samples/create/harness.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"testing"
2525
"time"
2626

27+
"github.com/go-logr/logr"
2728
"github.com/google/go-cmp/cmp"
2829
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
2930
"golang.org/x/oauth2"
@@ -33,6 +34,7 @@ import (
3334
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3435
"k8s.io/apimachinery/pkg/runtime/schema"
3536
"k8s.io/apimachinery/pkg/types"
37+
"k8s.io/apimachinery/pkg/util/sets"
3638
"k8s.io/apimachinery/pkg/util/wait"
3739
"k8s.io/client-go/rest"
3840
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -344,6 +346,8 @@ func NewHarness(t *testing.T, ctx context.Context) *Harness {
344346
ctrlManagerShutdown.Wait()
345347
t.Log("controller-runtime manager is shutdown")
346348
})
349+
kccConfig.ManagerOptions.Logger = filterLogs(log)
350+
347351
mgr, err := kccmanager.New(mgrContext, h.restConfig, kccConfig)
348352
if err != nil {
349353
t.Fatalf("error creating new manager: %v", err)
@@ -545,3 +549,53 @@ func (h *Harness) ReplaceString(from, to string) func(string) string {
545549
return strings.ReplaceAll(s, from, to)
546550
}
547551
}
552+
553+
func filterLogs(log logr.Logger) logr.Logger {
554+
f := &filterSink{sink: log.GetSink()}
555+
f.IgnoreMessages = sets.New[string]()
556+
f.IgnoreMessages.Insert("Registered controller")
557+
f.IgnoreMessages.Insert("Starting Controller")
558+
f.IgnoreMessages.Insert("Starting EventSource")
559+
f.IgnoreMessages.Insert("Starting workers")
560+
f.IgnoreMessages.Insert("Shutdown signal received, waiting for all workers to finish")
561+
f.IgnoreMessages.Insert("All workers finished")
562+
return log.WithSink(f)
563+
}
564+
565+
type filterSink struct {
566+
IgnoreMessages sets.Set[string]
567+
sink logr.LogSink
568+
}
569+
570+
// Init implements logr.LogSink
571+
func (s *filterSink) Init(info logr.RuntimeInfo) {
572+
s.sink.Init(info)
573+
}
574+
575+
// Enabled implements logr.LogSink
576+
func (s *filterSink) Enabled(level int) bool {
577+
return s.sink.Enabled(level)
578+
}
579+
580+
// Info implements logr.LogSink
581+
func (s *filterSink) Info(level int, msg string, args ...any) {
582+
if s.IgnoreMessages.Has(msg) {
583+
return
584+
}
585+
s.sink.Info(level, msg, args...)
586+
}
587+
588+
// WithValues implements logr.LogSink
589+
func (s *filterSink) WithValues(keysAndValues ...any) logr.LogSink {
590+
return &filterSink{IgnoreMessages: s.IgnoreMessages, sink: s.sink.WithValues(keysAndValues...)}
591+
}
592+
593+
// WithName implements logr.LogSink
594+
func (s *filterSink) WithName(name string) logr.LogSink {
595+
return &filterSink{IgnoreMessages: s.IgnoreMessages, sink: s.sink.WithName(name)}
596+
}
597+
598+
// Error implements logr.LogSink
599+
func (s *filterSink) Error(err error, msg string, args ...any) {
600+
s.sink.Error(err, msg, args...)
601+
}

0 commit comments

Comments
 (0)