internal_test.go 3.0 KB

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