github.com/polarismesh/polaris@v1.17.8/apiserver/eurekaserver/vip_test.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package eurekaserver 19 20 import ( 21 "testing" 22 23 "github.com/google/uuid" 24 "github.com/stretchr/testify/assert" 25 26 "github.com/polarismesh/polaris/common/model" 27 "github.com/polarismesh/polaris/service" 28 ) 29 30 const ( 31 vipSvcCount = 20 32 vipInstanceCount = 6 33 svipInstanceCount = 10 34 vipAddress1 = "vip.address.one" 35 vipAddress2 = "vip.address.two" 36 vipOneCount = vipInstanceCount / 2 37 svipAddress1 = "svip.address.one" 38 svipAddress2 = "svip.address.two" 39 svipOneCount = svipInstanceCount / 2 40 ) 41 42 var ( 43 mockVipServices = map[svcName]*model.Service{} 44 mockVipInstances = map[string]map[string]*model.Instance{} 45 mockSvipServices = map[svcName]*model.Service{} 46 mockSvipInstances = map[string]map[string]*model.Instance{} 47 ) 48 49 func buildMockVipInstances() { 50 buildServices(vipSvcCount, namespaceDefault, mockVipServices) 51 idx := 0 52 for _, svc := range mockVipServices { 53 instances := make(map[string]*model.Instance, vipInstanceCount) 54 for i := 0; i < vipInstanceCount; i++ { 55 idx++ 56 var vipAddress string 57 if i < vipOneCount { 58 vipAddress = vipAddress1 59 } else if i == vipOneCount { 60 vipAddress = vipAddress1 + "," + vipAddress2 61 } else { 62 vipAddress = vipAddress2 63 } 64 instance := buildMockInstance(idx, svc, true, vipAddress, "") 65 instances[instance.ID()] = instance 66 } 67 mockVipInstances[svc.ID] = instances 68 } 69 } 70 71 func buildMockSvipInstances() { 72 buildServices(vipSvcCount, namespaceDefault, mockSvipServices) 73 idx := 0 74 for _, svc := range mockSvipServices { 75 instances := make(map[string]*model.Instance, svipInstanceCount) 76 for i := 0; i < svipInstanceCount; i++ { 77 idx++ 78 var vipAddress string 79 if i < svipOneCount { 80 vipAddress = svipAddress1 81 } else if i == svipOneCount { 82 vipAddress = svipAddress1 + "," + svipAddress2 83 } else { 84 vipAddress = svipAddress2 85 } 86 instance := buildMockInstance(idx, svc, true, "", vipAddress) 87 instances[instance.ID()] = instance 88 } 89 mockSvipInstances[svc.ID] = instances 90 } 91 } 92 93 func doVipFunctionMock() { 94 buildMockVipInstances() 95 getCacheServicesFunc = mockGetVipServices 96 getCacheInstancesFunc = mockGetVipInstances 97 } 98 99 func mockGetVipServices(namingServer service.DiscoverServer, namespace string) map[string]*model.Service { 100 var newServices = make(map[string]*model.Service) 101 for _, svc := range mockVipServices { 102 if namespace == svc.Namespace { 103 newServices[svc.ID] = svc 104 } 105 } 106 return newServices 107 } 108 109 func mockGetVipInstances(namingServer service.DiscoverServer, svcId string) ([]*model.Instance, string, error) { 110 instances := mockVipInstances[svcId] 111 var retValue = make([]*model.Instance, 0, len(instances)) 112 if len(instances) == 0 { 113 return retValue, uuid.NewString(), nil 114 } 115 for _, instance := range instances { 116 retValue = append(retValue, instance) 117 } 118 return retValue, uuid.NewString(), nil 119 } 120 121 // TestBuildApplicationsForVip test method for BuildApplicationsForVip 122 func TestBuildApplicationsForVip(t *testing.T) { 123 doVipFunctionMock() 124 builder := &ApplicationsBuilder{ 125 namespace: DefaultNamespace, 126 enableSelfPreservation: true, 127 } 128 appResCache := builder.BuildApplications(nil) 129 vipAddress1Apps := BuildApplicationsForVip(&VipCacheKey{ 130 entityType: entityTypeVip, 131 targetVipAddress: vipAddress1, 132 }, appResCache) 133 applications1 := vipAddress1Apps.AppsResp.Applications.Application 134 assert.Equal(t, vipSvcCount, len(applications1)) 135 for _, application := range applications1 { 136 assert.Equal(t, vipOneCount+1, len(application.Instance)) 137 } 138 vipAddress2Apps := BuildApplicationsForVip(&VipCacheKey{ 139 entityType: entityTypeVip, 140 targetVipAddress: vipAddress2, 141 }, appResCache) 142 applications2 := vipAddress2Apps.AppsResp.Applications.Application 143 assert.Equal(t, vipSvcCount, len(applications2)) 144 for _, application := range applications2 { 145 assert.Equal(t, vipInstanceCount-vipOneCount, len(application.Instance)) 146 } 147 } 148 149 func doSVipFunctionMock() { 150 buildMockSvipInstances() 151 getCacheServicesFunc = mockGetSvipServices 152 getCacheInstancesFunc = mockGetSvipInstances 153 } 154 155 func mockGetSvipServices(namingServer service.DiscoverServer, namespace string) map[string]*model.Service { 156 var newServices = make(map[string]*model.Service) 157 for _, svc := range mockSvipServices { 158 if namespace == svc.Namespace { 159 newServices[svc.ID] = svc 160 } 161 } 162 return newServices 163 } 164 165 func mockGetSvipInstances(namingServer service.DiscoverServer, svcId string) ([]*model.Instance, string, error) { 166 instances := mockSvipInstances[svcId] 167 var retValue = make([]*model.Instance, 0, len(instances)) 168 if len(instances) == 0 { 169 return retValue, uuid.NewString(), nil 170 } 171 for _, instance := range instances { 172 retValue = append(retValue, instance) 173 } 174 return retValue, uuid.NewString(), nil 175 } 176 177 // TestBuildApplicationsForSVip test method for BuildApplicationsForVip 178 func TestBuildApplicationsForSvip(t *testing.T) { 179 doSVipFunctionMock() 180 builder := &ApplicationsBuilder{ 181 namespace: DefaultNamespace, 182 enableSelfPreservation: true, 183 } 184 appResCache := builder.BuildApplications(nil) 185 vipAddress1Apps := BuildApplicationsForVip(&VipCacheKey{ 186 entityType: entityTypeSVip, 187 targetVipAddress: svipAddress1, 188 }, appResCache) 189 applications1 := vipAddress1Apps.AppsResp.Applications.Application 190 assert.Equal(t, vipSvcCount, len(applications1)) 191 for _, application := range applications1 { 192 assert.Equal(t, svipOneCount+1, len(application.Instance)) 193 } 194 vipAddress2Apps := BuildApplicationsForVip(&VipCacheKey{ 195 entityType: entityTypeSVip, 196 targetVipAddress: svipAddress2, 197 }, appResCache) 198 applications2 := vipAddress2Apps.AppsResp.Applications.Application 199 assert.Equal(t, vipSvcCount, len(applications2)) 200 for _, application := range applications2 { 201 assert.Equal(t, svipInstanceCount-svipOneCount, len(application.Instance)) 202 } 203 }