github.com/kiali/kiali@v1.84.0/handlers/utils_test.go (about) 1 package handlers 2 3 import ( 4 "errors" 5 "net/http" 6 "net/http/httptest" 7 "net/url" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 core_v1 "k8s.io/api/core/v1" 12 meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 13 "k8s.io/apimachinery/pkg/runtime" 14 "k8s.io/client-go/tools/clientcmd/api" 15 16 "github.com/kiali/kiali/business" 17 "github.com/kiali/kiali/business/authentication" 18 "github.com/kiali/kiali/config" 19 "github.com/kiali/kiali/kubernetes" 20 "github.com/kiali/kiali/kubernetes/kubetest" 21 "github.com/kiali/kiali/models" 22 "github.com/kiali/kiali/prometheus" 23 "github.com/kiali/kiali/prometheus/prometheustest" 24 ) 25 26 type nsForbidden struct { 27 kubernetes.ClientInterface 28 forbiddenNamespace string 29 } 30 31 func (n *nsForbidden) GetNamespace(name string) (*core_v1.Namespace, error) { 32 if name == n.forbiddenNamespace { 33 return nil, errors.New("no privileges") 34 } 35 return n.ClientInterface.GetNamespace(name) 36 } 37 38 // Setup mock 39 func utilSetupMocks(t *testing.T, additionalObjs ...runtime.Object) promClientSupplier { 40 t.Helper() 41 conf := config.NewConfig() 42 // TODO: Find a way to mock out the istio endpoints so that the most used case can be tested by default. 43 conf.ExternalServices.Istio.IstioAPIEnabled = false 44 config.Set(conf) 45 objs := []runtime.Object{ 46 &core_v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: "ns1"}}, 47 &core_v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: "ns2"}}, 48 } 49 objs = append(objs, additionalObjs...) 50 k := kubetest.NewFakeK8sClient(objs...) 51 k8s := &nsForbidden{k, "nsNil"} 52 53 promAPI := new(prometheustest.PromAPIMock) 54 prom, err := prometheus.NewClient() 55 if err != nil { 56 t.Fatal(err) 57 return nil 58 } 59 prom.Inject(promAPI) 60 business.WithProm(prom) 61 62 mockClientFactory := kubetest.NewK8SClientFactoryMock(k8s) 63 business.SetWithBackends(mockClientFactory, nil) 64 return func() (*prometheus.Client, error) { return prom, nil } 65 } 66 67 func TestCreateMetricsServiceForNamespace(t *testing.T) { 68 assert := assert.New(t) 69 prom := utilSetupMocks(t) 70 71 req := httptest.NewRequest("GET", "/foo", nil) 72 req = req.WithContext(authentication.SetAuthInfoContext(req.Context(), &api.AuthInfo{Token: "test"})) 73 74 w := httptest.NewRecorder() 75 srv, info := createMetricsServiceForNamespace(w, req, prom, models.Namespace{Name: "ns1", Cluster: config.Get().KubernetesConfig.ClusterName}) 76 77 assert.NotNil(srv) 78 assert.NotNil(info) 79 assert.Equal("ns1", info.Name) 80 assert.Equal(http.StatusOK, w.Code) 81 } 82 83 func TestCreateMetricsServiceForNamespaceForbidden(t *testing.T) { 84 assert := assert.New(t) 85 prom := utilSetupMocks(t) 86 87 req := httptest.NewRequest("GET", "/foo", nil) 88 req = req.WithContext(authentication.SetAuthInfoContext(req.Context(), &api.AuthInfo{Token: "test"})) 89 90 w := httptest.NewRecorder() 91 srv, info := createMetricsServiceForNamespace(w, req, prom, models.Namespace{Name: "nsNil", Cluster: config.Get().KubernetesConfig.ClusterName}) 92 93 assert.Nil(srv) 94 assert.Nil(info) 95 assert.Equal(http.StatusForbidden, w.Code) 96 } 97 98 func TestCreateMetricsServiceForSeveralNamespaces(t *testing.T) { 99 assert := assert.New(t) 100 prom := utilSetupMocks(t) 101 102 req := httptest.NewRequest("GET", "/foo", nil) 103 req = req.WithContext(authentication.SetAuthInfoContext(req.Context(), &api.AuthInfo{Token: "test"})) 104 105 w := httptest.NewRecorder() 106 srv, info := createMetricsServiceForNamespaces(w, req, prom, []models.Namespace{{Name: "ns1"}, {Name: "ns2"}, {Name: "nsNil"}}) 107 108 assert.NotNil(srv) 109 assert.Len(info, 3) 110 assert.Equal(http.StatusOK, w.Code) 111 assert.Equal("ns1", info["ns1"].info.Name) 112 assert.Nil(info["ns1"].err) 113 assert.Equal("ns2", info["ns2"].info.Name) 114 assert.Nil(info["ns2"].err) 115 assert.Nil(info["nsNil"].info) 116 assert.Equal("no privileges", info["nsNil"].err.Error()) 117 } 118 119 func TestClusterNameFromQuery(t *testing.T) { 120 assert := assert.New(t) 121 conf := config.Get() 122 123 query := url.Values{"clusterName": []string{"east"}} 124 assert.Equal("east", clusterNameFromQuery(query)) 125 126 query = url.Values{} 127 assert.Equal(conf.KubernetesConfig.ClusterName, clusterNameFromQuery(query)) 128 129 query = url.Values{"notcluster": []string{"east"}} 130 assert.Equal(conf.KubernetesConfig.ClusterName, clusterNameFromQuery(query)) 131 }