github.com/kiali/kiali@v1.84.0/kubernetes/cache/testing.go (about)

     1  package cache
     2  
     3  /*
     4  	Contains utilities for unit testing with a cache.
     5  */
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/kiali/kiali/config"
    11  	"github.com/kiali/kiali/kubernetes"
    12  	"github.com/kiali/kiali/kubernetes/kubetest"
    13  )
    14  
    15  func newTestingCache(t *testing.T, cf kubernetes.ClientFactory, conf config.Config) KialiCache {
    16  	t.Helper()
    17  	// Disabling Istio API for tests. Otherwise the cache will try and poll the Istio endpoint
    18  	// when the cache is created.
    19  	conf.ExternalServices.Istio.IstioAPIEnabled = false
    20  
    21  	cache, err := NewKialiCache(cf, conf)
    22  	if err != nil {
    23  		t.Fatalf("Error creating KialiCache: %v", err)
    24  	}
    25  	t.Cleanup(cache.Stop)
    26  
    27  	return cache
    28  }
    29  
    30  // NewTestingCache will create a cache for you from the kube client and will cleanup the cache
    31  // when the test ends.
    32  func NewTestingCache(t *testing.T, k8s kubernetes.ClientInterface, conf config.Config) KialiCache {
    33  	t.Helper()
    34  	cf := kubetest.NewK8SClientFactoryMock(k8s)
    35  	return newTestingCache(t, cf, conf)
    36  }
    37  
    38  // NewTestingCacheWithFactory allows you to pass in a custom client factory. Good for testing multicluster.
    39  func NewTestingCacheWithFactory(t *testing.T, cf kubernetes.ClientFactory, conf config.Config) KialiCache {
    40  	t.Helper()
    41  	return newTestingCache(t, cf, conf)
    42  }