github.com/kiali/kiali@v1.84.0/business/tls_perf_test.go (about)

     1  package business
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"strconv"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	networking_v1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
    12  	security_v1beta1 "istio.io/client-go/pkg/apis/security/v1beta1"
    13  	core_v1 "k8s.io/api/core/v1"
    14  	"k8s.io/apimachinery/pkg/runtime"
    15  
    16  	"github.com/kiali/kiali/config"
    17  	"github.com/kiali/kiali/kubernetes"
    18  	"github.com/kiali/kiali/kubernetes/kubetest"
    19  	"github.com/kiali/kiali/tests/data"
    20  )
    21  
    22  func TestTlsPerfNsDr(t *testing.T) {
    23  	sNumNs := os.Getenv("NUMNS")
    24  	sNumDr := os.Getenv("NUMDR")
    25  	numNs := 10
    26  	numDr := 10
    27  	if sNumNs != "" {
    28  		if n, err := strconv.Atoi(sNumNs); err == nil {
    29  			numNs = n
    30  		}
    31  		if n, err := strconv.Atoi(sNumDr); err == nil {
    32  			numDr = n
    33  		}
    34  	}
    35  	// Iterate on namespaces
    36  	nss, pss, drs := preparePerfScenario(numNs, numDr)
    37  
    38  	testPerfScenario(MTLSPartiallyEnabled, nss, drs, pss, false, t)
    39  	testPerfScenario(MTLSEnabled, nss, drs, pss, true, t)
    40  	testPerfScenario(MTLSEnabled, nss, []*networking_v1beta1.DestinationRule{}, pss, true, t)
    41  }
    42  
    43  func preparePerfScenario(numNs, numDr int) ([]core_v1.Namespace, []*security_v1beta1.PeerAuthentication, []*networking_v1beta1.DestinationRule) {
    44  	nss := []core_v1.Namespace{}
    45  	pss := []*security_v1beta1.PeerAuthentication{}
    46  	drs := []*networking_v1beta1.DestinationRule{}
    47  
    48  	fmt.Printf("TLS perf test. Num NS: %d DR per NS: %d\n", numNs, numDr)
    49  	i := 0
    50  	for i < numNs {
    51  		ns := core_v1.Namespace{}
    52  		ns.Name = fmt.Sprintf("bookinfo-%d", i)
    53  		nss = append(nss, ns)
    54  		ps := *data.CreateEmptyPeerAuthentication(fmt.Sprintf("pa-%d", i), ns.Name, data.CreateMTLS("STRICT"))
    55  		pss = append(pss, &ps)
    56  		j := 0
    57  		for j < numDr {
    58  			dr := *data.CreateEmptyDestinationRule(ns.Name, fmt.Sprintf("dr-%d-%d", i, j), fmt.Sprintf("*.%s.svc.cluster.local", ns.Name))
    59  			drs = append(drs, &dr)
    60  			j++
    61  		}
    62  		i++
    63  	}
    64  	return nss, pss, drs
    65  }
    66  
    67  func testPerfScenario(exStatus string, namespaces []core_v1.Namespace, drs []*networking_v1beta1.DestinationRule, ps []*security_v1beta1.PeerAuthentication, autoMtls bool, t *testing.T) {
    68  	assert := assert.New(t)
    69  
    70  	conf := config.NewConfig()
    71  	conf.Deployment.ClusterWideAccess = true
    72  	kubernetes.SetConfig(t, *conf)
    73  
    74  	var objs []runtime.Object
    75  	for _, obj := range namespaces {
    76  		o := obj
    77  		objs = append(objs, &o)
    78  	}
    79  	objs = append(objs, kubernetes.ToRuntimeObjects(ps)...)
    80  	objs = append(objs, kubernetes.ToRuntimeObjects(drs)...)
    81  
    82  	k8s := kubetest.NewFakeK8sClient(objs...)
    83  	SetupBusinessLayer(t, k8s, *conf)
    84  
    85  	k8sclients := make(map[string]kubernetes.ClientInterface)
    86  	k8sclients[conf.KubernetesConfig.ClusterName] = k8s
    87  
    88  	tlsService := NewWithBackends(k8sclients, k8sclients, nil, nil).TLS
    89  	tlsService.enabledAutoMtls = &autoMtls
    90  
    91  	nss := []string{}
    92  	for _, ns := range namespaces {
    93  		nss = append(nss, ns.Name)
    94  	}
    95  	statuses, err := tlsService.ClusterWideNSmTLSStatus(context.TODO(), nss, conf.KubernetesConfig.ClusterName)
    96  	assert.NoError(err)
    97  	assert.NotEmpty(statuses)
    98  	for _, status := range statuses {
    99  		assert.Equal(exStatus, status.Status)
   100  	}
   101  }