diff --git a/pkg/controller/test/BUILD.bazel b/pkg/controller/test/BUILD.bazel index 0ff8b28b0..1fd2bfefc 100644 --- a/pkg/controller/test/BUILD.bazel +++ b/pkg/controller/test/BUILD.bazel @@ -26,6 +26,7 @@ go_library( "//vendor/k8s.io/client-go/informers:go_default_library", "//vendor/k8s.io/client-go/kubernetes/fake:go_default_library", "//vendor/k8s.io/client-go/testing:go_default_library", + "//vendor/k8s.io/client-go/tools/cache:go_default_library", "//vendor/k8s.io/utils/clock:go_default_library", "//vendor/k8s.io/utils/clock/testing:go_default_library", ], diff --git a/pkg/controller/test/context_builder.go b/pkg/controller/test/context_builder.go index df2bea84c..3ff5ff08b 100644 --- a/pkg/controller/test/context_builder.go +++ b/pkg/controller/test/context_builder.go @@ -30,6 +30,7 @@ import ( kubeinformers "k8s.io/client-go/informers" kubefake "k8s.io/client-go/kubernetes/fake" coretesting "k8s.io/client-go/testing" + "k8s.io/client-go/tools/cache" "k8s.io/utils/clock" fakeclock "k8s.io/utils/clock/testing" @@ -72,8 +73,9 @@ type Builder struct { // test). CheckFn func(*Builder, ...interface{}) - stopCh chan struct{} - requiredReactors map[string]bool + stopCh chan struct{} + requiredReactors map[string]bool + additionalSyncFuncs []cache.InformerSynced *controller.Context } @@ -285,6 +287,18 @@ func (b *Builder) Sync() { if err := mustAllSync(b.SharedInformerFactory.WaitForCacheSync(b.stopCh)); err != nil { panic("Error waiting for SharedInformerFactory to sync: " + err.Error()) } + if b.additionalSyncFuncs != nil { + cache.WaitForCacheSync(b.stopCh, b.additionalSyncFuncs...) + } +} + +// RegisterAdditionalSyncFuncs registers an additional InformerSynced function +// with the builder. +// When the Sync method is called, the builder will also wait for the given +// listers to be synced as well as the listers that were registered with the +// informer factories that the builder provides. +func (b *Builder) RegisterAdditionalSyncFuncs(fns ...cache.InformerSynced) { + b.additionalSyncFuncs = append(b.additionalSyncFuncs, fns...) } func (b *Builder) Events() []string {