@@ -2,6 +2,7 @@ package controller
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "sync"
78 "testing"
@@ -12,7 +13,11 @@ import (
1213 "github.com/stretchr/testify/require"
1314 corev1 "k8s.io/api/core/v1"
1415 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16+ "k8s.io/apimachinery/pkg/runtime"
1517 amcache "k8s.io/apimachinery/pkg/util/cache"
18+ "k8s.io/client-go/kubernetes/fake"
19+ k8stesting "k8s.io/client-go/testing"
20+ "k8s.io/client-go/util/workqueue"
1621)
1722
1823// mockPoster records all PostOne calls and returns a configurable error.
@@ -533,3 +538,51 @@ func TestIsTerminalPhase(t *testing.T) {
533538 })
534539 }
535540}
541+
542+ func TestRun_InformerSyncTimeout (t * testing.T ) {
543+ t .Parallel ()
544+ fakeClient := fake .NewSimpleClientset ()
545+ blocker := make (chan struct {})
546+ fakeClient .PrependReactor ("list" , "*" , func (_ k8stesting.Action ) (bool , runtime.Object , error ) {
547+ // Block until the test completes.
548+ <- blocker
549+ return true , nil , errors .New ("fail" )
550+ })
551+ defer close (blocker )
552+
553+ factory := createInformerFactory (fakeClient , "" , "" )
554+
555+ ctrl := & Controller {
556+ clientset : fakeClient ,
557+ podInformer : factory .Core ().V1 ().Pods ().Informer (),
558+ deploymentInformer : factory .Apps ().V1 ().Deployments ().Informer (),
559+ daemonSetInformer : factory .Apps ().V1 ().DaemonSets ().Informer (),
560+ statefulSetInformer : factory .Apps ().V1 ().StatefulSets ().Informer (),
561+ jobInformer : factory .Batch ().V1 ().Jobs ().Informer (),
562+ cronJobInformer : factory .Batch ().V1 ().CronJobs ().Informer (),
563+ workqueue : workqueue .NewTypedRateLimitingQueue (
564+ workqueue .DefaultTypedControllerRateLimiter [PodEvent ](),
565+ ),
566+ apiClient : & mockPoster {},
567+ cfg : & Config {},
568+ observedDeployments : amcache .NewExpiring (),
569+ unknownArtifacts : amcache .NewExpiring (),
570+ informerSyncTimeout : 2 * time .Second ,
571+ }
572+
573+ ctx , cancel := context .WithCancel (context .Background ())
574+ defer cancel ()
575+
576+ errCh := make (chan error , 1 )
577+ go func () {
578+ errCh <- ctrl .Run (ctx , 1 )
579+ }()
580+
581+ select {
582+ case err := <- errCh :
583+ require .Error (t , err )
584+ assert .Contains (t , err .Error (), "timed out waiting for caches to sync" )
585+ case <- time .After (5 * time .Second ):
586+ t .Fatal ("Run did not return within 5 seconds — informer sync timeout was 2 seconds" )
587+ }
588+ }
0 commit comments