github.com/polarismesh/polaris@v1.17.8/apiserver/xdsserverv3/resource/node_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 resource 19 20 import "testing" 21 22 func Test_parseNodeID(t *testing.T) { 23 type args struct { 24 nodeID string 25 } 26 tests := []struct { 27 name string 28 args args 29 wantRunType string 30 wantPolarisNamespace string 31 wantUuid string 32 wantHostIP string 33 }{ 34 { 35 name: "test-1", 36 args: args{ 37 nodeID: "default/12345~127.0.0.1", 38 }, 39 wantRunType: "sidecar", 40 wantPolarisNamespace: "default", 41 wantUuid: "12345", 42 wantHostIP: "127.0.0.1", 43 }, 44 { 45 name: "test-1", 46 args: args{ 47 nodeID: "sidecar~bookinfo/productpage-v1-7bc8c4cb99-c69gh~10.0.0.208", 48 }, 49 wantRunType: string(RunTypeSidecar), 50 wantPolarisNamespace: "bookinfo", 51 wantUuid: "productpage-v1-7bc8c4cb99-c69gh", 52 wantHostIP: "10.0.0.208", 53 }, 54 { 55 name: "test-1", 56 args: args{ 57 nodeID: "gateway~default/12345~127.0.0.1", 58 }, 59 wantRunType: "gateway", 60 wantPolarisNamespace: "default", 61 wantUuid: "12345", 62 wantHostIP: "127.0.0.1", 63 }, 64 { 65 name: "test-1", 66 args: args{ 67 nodeID: "12345~127.0.0.1", 68 }, 69 wantRunType: "", 70 wantPolarisNamespace: "", 71 wantUuid: "", 72 wantHostIP: "", 73 }, 74 { 75 name: "test-1", 76 args: args{ 77 nodeID: "default/127.0.0.1", 78 }, 79 wantRunType: "", 80 wantPolarisNamespace: "", 81 wantUuid: "", 82 wantHostIP: "", 83 }, 84 { 85 name: "test-1", 86 args: args{ 87 nodeID: "sidecar~default/127.0.0.1", 88 }, 89 wantRunType: "", 90 wantPolarisNamespace: "", 91 wantUuid: "", 92 wantHostIP: "", 93 }, 94 } 95 for _, tt := range tests { 96 t.Run(tt.name, func(t *testing.T) { 97 gotRunType, gotPolarisNamespace, gotUuid, gotHostIP := ParseNodeID(tt.args.nodeID) 98 if gotRunType != tt.wantRunType { 99 t.Errorf("parseNodeID() gotRunType = %v, want %v", gotRunType, tt.wantRunType) 100 } 101 if gotPolarisNamespace != tt.wantPolarisNamespace { 102 t.Errorf("parseNodeID() gotPolarisNamespace = %v, want %v", gotPolarisNamespace, tt.wantPolarisNamespace) 103 } 104 if gotUuid != tt.wantUuid { 105 t.Errorf("parseNodeID() gotUuid = %v, want %v", gotUuid, tt.wantUuid) 106 } 107 if gotHostIP != tt.wantHostIP { 108 t.Errorf("parseNodeID() gotHostIP = %v, want %v", gotHostIP, tt.wantHostIP) 109 } 110 }) 111 } 112 }