From c97ed13fa432df2b5b1225f29f38170fea158ef7 Mon Sep 17 00:00:00 2001 From: nbn01 Date: Fri, 1 Apr 2022 12:33:54 +0530 Subject: [PATCH] Add unit test Signed-off-by: nbn01 --- admiral/pkg/clusters/serviceentry_test.go | 77 +++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/admiral/pkg/clusters/serviceentry_test.go b/admiral/pkg/clusters/serviceentry_test.go index cd7ace3d..9a8387a0 100644 --- a/admiral/pkg/clusters/serviceentry_test.go +++ b/admiral/pkg/clusters/serviceentry_test.go @@ -1161,6 +1161,83 @@ func TestCreateServiceEntryForBlueGreenRolloutsUsecase(t *testing.T) { } } +func TestUpdateEndpointsForBlueGreen(t *testing.T) { + const CLUSTER_INGRESS_1 = "ingress1.com" + const ACTIVE_SERVICE = "activeService" + const PREVIEW_SERVICE = "previewService" + const NAMESPACE = "namespace" + const ACTIVE_MESH_HOST = "qal.example.mesh" + const PREVIEW_MESH_HOST = "preview.qal.example.mesh" + + rollout := argo.Rollout{} + rollout.Spec.Strategy = argo.RolloutStrategy{ + BlueGreen: &argo.BlueGreenStrategy{ + ActiveService: ACTIVE_SERVICE, + PreviewService: PREVIEW_SERVICE, + }, + } + rollout.Spec.Template.Annotations = map[string]string{} + rollout.Spec.Template.Annotations[common.SidecarEnabledPorts] = "8080" + + endpoint := istionetworkingv1alpha3.ServiceEntry_Endpoint{ + Labels: map[string]string{}, Address: CLUSTER_INGRESS_1, Ports: map[string]uint32{"http": 15443}, + } + + meshPorts := map[string]uint32{"http": 8080} + + weightedServices := map[string]*WeightedService{ + ACTIVE_SERVICE: {Service: &v1.Service{ObjectMeta: v12.ObjectMeta{Name: ACTIVE_SERVICE, Namespace: NAMESPACE}}}, + PREVIEW_SERVICE: {Service: &v1.Service{ObjectMeta: v12.ObjectMeta{Name: PREVIEW_SERVICE, Namespace: NAMESPACE}}}, + } + + activeWantedEndpoints := istionetworkingv1alpha3.ServiceEntry_Endpoint{ + Address: ACTIVE_SERVICE + common.Sep + NAMESPACE + common.DotLocalDomainSuffix, Ports: meshPorts, + } + + previewWantedEndpoints := istionetworkingv1alpha3.ServiceEntry_Endpoint{ + Address: PREVIEW_SERVICE + common.Sep + NAMESPACE + common.DotLocalDomainSuffix, Ports: meshPorts, + } + + testCases := []struct { + name string + rollout argo.Rollout + inputEndpoint istionetworkingv1alpha3.ServiceEntry_Endpoint + weightedServices map[string]*WeightedService + clusterIngress string + meshPorts map[string]uint32 + meshHost string + wantedEndpoints istionetworkingv1alpha3.ServiceEntry_Endpoint + }{ + { + name: "should return endpoint with active service address", + rollout: rollout, + inputEndpoint: endpoint, + weightedServices: weightedServices, + meshPorts: meshPorts, + meshHost: ACTIVE_MESH_HOST, + wantedEndpoints: activeWantedEndpoints, + }, + { + name: "should return endpoint with preview service address", + rollout: rollout, + inputEndpoint: endpoint, + weightedServices: weightedServices, + meshPorts: meshPorts, + meshHost: PREVIEW_MESH_HOST, + wantedEndpoints: previewWantedEndpoints, + }, + } + + for _, c := range testCases { + t.Run(c.name, func(t *testing.T) { + updateEndpointsForBlueGreen(&c.rollout, c.weightedServices, map[string]string{}, &c.inputEndpoint, "test", c.meshHost) + if c.inputEndpoint.Address != c.wantedEndpoints.Address { + t.Errorf("Wanted %s endpoint, got: %s", c.wantedEndpoints.Address, c.inputEndpoint.Address) + } + }) + } +} + func TestUpdateEndpointsForWeightedServices(t *testing.T) { t.Parallel()