gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/xds/internal/internal_test.go (about)

     1  /*
     2   *
     3   * Copyright 2019 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 internal
    19  
    20  import (
    21  	"reflect"
    22  	"strings"
    23  	"testing"
    24  	"unicode"
    25  
    26  	corepb "gitee.com/ks-custle/core-gm/go-control-plane/envoy/api/v2/core"
    27  	"gitee.com/ks-custle/core-gm/grpc/internal/grpctest"
    28  	"github.com/google/go-cmp/cmp"
    29  )
    30  
    31  const ignorePrefix = "XXX_"
    32  
    33  type s struct {
    34  	grpctest.Tester
    35  }
    36  
    37  func Test(t *testing.T) {
    38  	grpctest.RunSubTests(t, s{})
    39  }
    40  
    41  func ignore(name string) bool {
    42  	if !unicode.IsUpper([]rune(name)[0]) {
    43  		return true
    44  	}
    45  	return strings.HasPrefix(name, ignorePrefix)
    46  }
    47  
    48  // A reflection based test to make sure internal.Locality contains all the
    49  // fields (expect for XXX_) from the proto message.
    50  func (s) TestLocalityMatchProtoMessage(t *testing.T) {
    51  	want1 := make(map[string]string)
    52  	for ty, i := reflect.TypeOf(LocalityID{}), 0; i < ty.NumField(); i++ {
    53  		f := ty.Field(i)
    54  		if ignore(f.Name) {
    55  			continue
    56  		}
    57  		want1[f.Name] = f.Type.Name()
    58  	}
    59  
    60  	want2 := make(map[string]string)
    61  	for ty, i := reflect.TypeOf(corepb.Locality{}), 0; i < ty.NumField(); i++ {
    62  		f := ty.Field(i)
    63  		if ignore(f.Name) {
    64  			continue
    65  		}
    66  		want2[f.Name] = f.Type.Name()
    67  	}
    68  
    69  	if diff := cmp.Diff(want1, want2); diff != "" {
    70  		t.Fatalf("internal type and proto message have different fields: (-got +want):\n%+v", diff)
    71  	}
    72  }
    73  
    74  func TestLocalityToAndFromJSON(t *testing.T) {
    75  	tests := []struct {
    76  		name       string
    77  		localityID LocalityID
    78  		str        string
    79  		wantErr    bool
    80  	}{
    81  		{
    82  			name:       "3 fields",
    83  			localityID: LocalityID{Region: "r:r", Zone: "z#z", SubZone: "s^s"},
    84  			str:        `{"region":"r:r","zone":"z#z","subZone":"s^s"}`,
    85  		},
    86  		{
    87  			name:       "2 fields",
    88  			localityID: LocalityID{Region: "r:r", Zone: "z#z"},
    89  			str:        `{"region":"r:r","zone":"z#z"}`,
    90  		},
    91  		{
    92  			name:       "1 field",
    93  			localityID: LocalityID{Region: "r:r"},
    94  			str:        `{"region":"r:r"}`,
    95  		},
    96  	}
    97  	for _, tt := range tests {
    98  		t.Run(tt.name, func(t *testing.T) {
    99  			gotStr, err := tt.localityID.ToString()
   100  			if err != nil {
   101  				t.Errorf("failed to marshal LocalityID: %#v", tt.localityID)
   102  			}
   103  			if gotStr != tt.str {
   104  				t.Errorf("%#v.String() = %q, want %q", tt.localityID, gotStr, tt.str)
   105  			}
   106  
   107  			gotID, err := LocalityIDFromString(tt.str)
   108  			if (err != nil) != tt.wantErr {
   109  				t.Errorf("LocalityIDFromString(%q) error = %v, wantErr %v", tt.str, err, tt.wantErr)
   110  				return
   111  			}
   112  			if diff := cmp.Diff(gotID, tt.localityID); diff != "" {
   113  				t.Errorf("LocalityIDFromString() got = %v, want %v, diff: %s", gotID, tt.localityID, diff)
   114  			}
   115  		})
   116  	}
   117  }