github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/internal/xdsclient/xdsresource/name_test.go (about) 1 /* 2 * 3 * Copyright 2021 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package xdsresource 19 20 import ( 21 "reflect" 22 "testing" 23 24 "github.com/hxx258456/ccgo/grpc/internal/envconfig" 25 ) 26 27 func TestParseName(t *testing.T) { 28 tests := []struct { 29 name string 30 env bool // Whether federation env is set to true. 31 in string 32 want *Name 33 }{ 34 { 35 name: "env off", 36 env: false, 37 in: "xdstp://auth/type/id", 38 want: &Name{ID: "xdstp://auth/type/id"}, 39 }, 40 { 41 name: "old style name", 42 env: true, 43 in: "test-resource", 44 want: &Name{ID: "test-resource"}, 45 }, 46 { 47 name: "invalid not url", 48 env: true, 49 in: "a:/b/c", 50 want: &Name{ID: "a:/b/c"}, 51 }, 52 { 53 name: "invalid no resource type", 54 env: true, 55 in: "xdstp://auth/id", 56 want: &Name{ID: "xdstp://auth/id"}, 57 }, 58 { 59 name: "valid no ctx params", 60 env: true, 61 in: "xdstp://auth/type/id", 62 want: &Name{Scheme: "xdstp", Authority: "auth", Type: "type", ID: "id"}, 63 }, 64 { 65 name: "valid with ctx params", 66 env: true, 67 in: "xdstp://auth/type/id?a=1&b=2", 68 want: &Name{Scheme: "xdstp", Authority: "auth", Type: "type", ID: "id", ContextParams: map[string]string{"a": "1", "b": "2"}}, 69 }, 70 } 71 for _, tt := range tests { 72 t.Run(tt.name, func(t *testing.T) { 73 if tt.env { 74 defer func() func() { 75 oldEnv := envconfig.XDSFederation 76 envconfig.XDSFederation = true 77 return func() { envconfig.XDSFederation = oldEnv } 78 }()() 79 } 80 if got := ParseName(tt.in); !reflect.DeepEqual(got, tt.want) { 81 t.Errorf("ParseName() = %#v, want %#v", got, tt.want) 82 } 83 }) 84 } 85 } 86 87 // TestNameStringCtxParamsOrder covers the case that if two names differ only in 88 // context parameter __order__, the parsed name.String() has the same value. 89 func TestNameStringCtxParamsOrder(t *testing.T) { 90 oldEnv := envconfig.XDSFederation 91 envconfig.XDSFederation = true 92 defer func() { envconfig.XDSFederation = oldEnv }() 93 94 const ( 95 a = "xdstp://auth/type/id?a=1&b=2" 96 b = "xdstp://auth/type/id?b=2&a=1" 97 ) 98 aParsed := ParseName(a).String() 99 bParsed := ParseName(b).String() 100 101 if aParsed != bParsed { 102 t.Fatalf("aParsed.String() = %q, bParsed.String() = %q, want them to be the same", aParsed, bParsed) 103 } 104 }