github.com/kiali/kiali@v1.84.0/graph/telemetry/istio/appender/labeler_test.go (about)

     1  package appender
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	apps_v1 "k8s.io/api/apps/v1"
     8  	core_v1 "k8s.io/api/core/v1"
     9  	meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  
    11  	"github.com/kiali/kiali/business"
    12  	"github.com/kiali/kiali/config"
    13  	"github.com/kiali/kiali/graph"
    14  	"github.com/kiali/kiali/kubernetes"
    15  	"github.com/kiali/kiali/kubernetes/kubetest"
    16  )
    17  
    18  func setupLabelerTrafficMap() (map[string]*graph.Node, string, string, string, string, string) {
    19  	trafficMap := graph.NewTrafficMap()
    20  
    21  	appNode, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "test", "testNamespace", graph.Unknown, "test", "", graph.GraphTypeVersionedApp)
    22  	trafficMap[appNode.ID] = appNode
    23  
    24  	appNodeV1, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
    25  	trafficMap[appNodeV1.ID] = appNodeV1
    26  
    27  	appNodeV2, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "test", "testNamespace", "test-v2", "test", "v2", graph.GraphTypeVersionedApp)
    28  	trafficMap[appNodeV2.ID] = appNodeV2
    29  
    30  	serviceNode, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "test", "testNamespace", graph.Unknown, graph.Unknown, graph.Unknown, graph.GraphTypeVersionedApp)
    31  	trafficMap[serviceNode.ID] = serviceNode
    32  
    33  	workloadNode, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "test", "testNamespace", "test-v1", graph.Unknown, graph.Unknown, graph.GraphTypeWorkload)
    34  	trafficMap[workloadNode.ID] = workloadNode
    35  
    36  	return trafficMap, appNode.ID, appNodeV1.ID, appNodeV2.ID, serviceNode.ID, workloadNode.ID
    37  }
    38  
    39  func setupLabelerK8S(t *testing.T) *business.Layer {
    40  	conf := config.NewConfig()
    41  	conf.KubernetesConfig.ClusterName = config.DefaultClusterID
    42  	conf.ExternalServices.Istio.IstioAPIEnabled = false
    43  	config.Set(conf)
    44  
    45  	k8s := kubetest.NewFakeK8sClient(
    46  		&core_v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: "testNamespace"}},
    47  		&apps_v1.Deployment{
    48  			ObjectMeta: meta_v1.ObjectMeta{
    49  				Name:      "test-v1",
    50  				Namespace: "testNamespace",
    51  			},
    52  			Spec: apps_v1.DeploymentSpec{
    53  				Template: core_v1.PodTemplateSpec{
    54  					ObjectMeta: meta_v1.ObjectMeta{
    55  						Labels: graph.LabelsMetadata{"app": "test", "version": "v1", "datacenter": "east"},
    56  					},
    57  				},
    58  			},
    59  		},
    60  		&apps_v1.Deployment{
    61  			ObjectMeta: meta_v1.ObjectMeta{
    62  				Name:      "test-v2",
    63  				Namespace: "testNamespace",
    64  			},
    65  			Spec: apps_v1.DeploymentSpec{
    66  				Template: core_v1.PodTemplateSpec{
    67  					ObjectMeta: meta_v1.ObjectMeta{
    68  						Labels: graph.LabelsMetadata{"app": "test", "version": "v2", "datacenter": "west"},
    69  					},
    70  				},
    71  			},
    72  		},
    73  		&core_v1.Pod{
    74  			ObjectMeta: meta_v1.ObjectMeta{
    75  				Name:      "test-v1-1234",
    76  				Namespace: "testNamespace",
    77  				Labels:    graph.LabelsMetadata{"app": "test", "version": "v1", "datacenter": "east"},
    78  			},
    79  			Status: core_v1.PodStatus{
    80  				Message: "foo",
    81  			},
    82  		},
    83  		&core_v1.Pod{
    84  			ObjectMeta: meta_v1.ObjectMeta{
    85  				Name:      "test-v2-1234",
    86  				Namespace: "testNamespace",
    87  				Labels:    graph.LabelsMetadata{"app": "test", "version": "v2", "datacenter": "west"},
    88  			},
    89  			Status: core_v1.PodStatus{
    90  				Message: "foo",
    91  			},
    92  		},
    93  		&core_v1.Service{
    94  			ObjectMeta: meta_v1.ObjectMeta{
    95  				Name:      "test",
    96  				Namespace: "testNamespace",
    97  				Labels:    graph.LabelsMetadata{"app": "test", "datacenter": "east"},
    98  			},
    99  		},
   100  	)
   101  
   102  	business.SetupBusinessLayer(t, k8s, *conf)
   103  	k8sclients := map[string]kubernetes.ClientInterface{
   104  		config.DefaultClusterID: kubetest.NewFakeK8sClient(
   105  			&core_v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: "testNamespace"}},
   106  		),
   107  	}
   108  	businessLayer := business.NewWithBackends(k8sclients, k8sclients, nil, nil)
   109  	return businessLayer
   110  }
   111  
   112  func TestLabeler(t *testing.T) {
   113  	assert := assert.New(t)
   114  
   115  	businessLayer := setupLabelerK8S(t)
   116  	trafficMap, appNodeId, appNodeV1Id, appNodeV2Id, svcNodeId, wlNodeId := setupLabelerTrafficMap()
   117  
   118  	assert.Equal(5, len(trafficMap))
   119  	assert.Equal(nil, trafficMap[appNodeId].Metadata[graph.Labels])
   120  	assert.Equal(nil, trafficMap[appNodeV1Id].Metadata[graph.Labels])
   121  	assert.Equal(nil, trafficMap[appNodeV2Id].Metadata[graph.Labels])
   122  	assert.Equal(nil, trafficMap[svcNodeId].Metadata[graph.Labels])
   123  	assert.Equal(nil, trafficMap[wlNodeId].Metadata[graph.Labels])
   124  
   125  	globalInfo := graph.NewAppenderGlobalInfo()
   126  	globalInfo.Business = businessLayer
   127  
   128  	a := LabelerAppender{}
   129  	a.AppendGraph(trafficMap, globalInfo, nil)
   130  
   131  	assert.Equal(5, len(trafficMap))
   132  	assert.Equal(1, len(trafficMap[appNodeId].Metadata[graph.Labels].(graph.LabelsMetadata)))
   133  	assert.Equal("east,west", trafficMap[appNodeId].Metadata[graph.Labels].(graph.LabelsMetadata)["datacenter"])
   134  	assert.Equal(1, len(trafficMap[appNodeV1Id].Metadata[graph.Labels].(graph.LabelsMetadata)))
   135  	assert.Equal("east", trafficMap[appNodeV1Id].Metadata[graph.Labels].(graph.LabelsMetadata)["datacenter"])
   136  	assert.Equal(1, len(trafficMap[appNodeV2Id].Metadata[graph.Labels].(graph.LabelsMetadata)))
   137  	assert.Equal("west", trafficMap[appNodeV2Id].Metadata[graph.Labels].(graph.LabelsMetadata)["datacenter"])
   138  	assert.Equal(1, len(trafficMap[svcNodeId].Metadata[graph.Labels].(graph.LabelsMetadata)))
   139  	assert.Equal("east", trafficMap[svcNodeId].Metadata[graph.Labels].(graph.LabelsMetadata)["datacenter"])
   140  	assert.Equal(1, len(trafficMap[wlNodeId].Metadata[graph.Labels].(graph.LabelsMetadata)))
   141  	assert.Equal("east", trafficMap[wlNodeId].Metadata[graph.Labels].(graph.LabelsMetadata)["datacenter"])
   142  }