resolver_conn_wrapper_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. *
  3. * Copyright 2017 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 grpc
  19. import (
  20. "fmt"
  21. "net"
  22. "testing"
  23. "time"
  24. "google.golang.org/grpc/resolver"
  25. )
  26. func TestParseTarget(t *testing.T) {
  27. for _, test := range []resolver.Target{
  28. {Scheme: "dns", Authority: "", Endpoint: "google.com"},
  29. {Scheme: "dns", Authority: "a.server.com", Endpoint: "google.com"},
  30. {Scheme: "dns", Authority: "a.server.com", Endpoint: "google.com/?a=b"},
  31. {Scheme: "passthrough", Authority: "", Endpoint: "/unix/socket/address"},
  32. } {
  33. str := test.Scheme + "://" + test.Authority + "/" + test.Endpoint
  34. got := parseTarget(str)
  35. if got != test {
  36. t.Errorf("parseTarget(%q) = %+v, want %+v", str, got, test)
  37. }
  38. }
  39. }
  40. func TestParseTargetString(t *testing.T) {
  41. for _, test := range []struct {
  42. targetStr string
  43. want resolver.Target
  44. }{
  45. {targetStr: "", want: resolver.Target{Scheme: "", Authority: "", Endpoint: ""}},
  46. {targetStr: ":///", want: resolver.Target{Scheme: "", Authority: "", Endpoint: ""}},
  47. {targetStr: "a:///", want: resolver.Target{Scheme: "a", Authority: "", Endpoint: ""}},
  48. {targetStr: "://a/", want: resolver.Target{Scheme: "", Authority: "a", Endpoint: ""}},
  49. {targetStr: ":///a", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "a"}},
  50. {targetStr: "a://b/", want: resolver.Target{Scheme: "a", Authority: "b", Endpoint: ""}},
  51. {targetStr: "a:///b", want: resolver.Target{Scheme: "a", Authority: "", Endpoint: "b"}},
  52. {targetStr: "://a/b", want: resolver.Target{Scheme: "", Authority: "a", Endpoint: "b"}},
  53. {targetStr: "a://b/c", want: resolver.Target{Scheme: "a", Authority: "b", Endpoint: "c"}},
  54. {targetStr: "dns:///google.com", want: resolver.Target{Scheme: "dns", Authority: "", Endpoint: "google.com"}},
  55. {targetStr: "dns://a.server.com/google.com", want: resolver.Target{Scheme: "dns", Authority: "a.server.com", Endpoint: "google.com"}},
  56. {targetStr: "dns://a.server.com/google.com/?a=b", want: resolver.Target{Scheme: "dns", Authority: "a.server.com", Endpoint: "google.com/?a=b"}},
  57. {targetStr: "/", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "/"}},
  58. {targetStr: "google.com", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "google.com"}},
  59. {targetStr: "google.com/?a=b", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "google.com/?a=b"}},
  60. {targetStr: "/unix/socket/address", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "/unix/socket/address"}},
  61. // If we can only parse part of the target.
  62. {targetStr: "://", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "://"}},
  63. {targetStr: "unix://domain", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "unix://domain"}},
  64. {targetStr: "a:b", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "a:b"}},
  65. {targetStr: "a/b", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "a/b"}},
  66. {targetStr: "a:/b", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "a:/b"}},
  67. {targetStr: "a//b", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "a//b"}},
  68. {targetStr: "a://b", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "a://b"}},
  69. } {
  70. got := parseTarget(test.targetStr)
  71. if got != test.want {
  72. t.Errorf("parseTarget(%q) = %+v, want %+v", test.targetStr, got, test.want)
  73. }
  74. }
  75. }
  76. // The target string with unknown scheme should be kept unchanged and passed to
  77. // the dialer.
  78. func TestDialParseTargetUnknownScheme(t *testing.T) {
  79. for _, test := range []struct {
  80. targetStr string
  81. want string
  82. }{
  83. {"/unix/socket/address", "/unix/socket/address"},
  84. // Special test for "unix:///".
  85. {"unix:///unix/socket/address", "unix:///unix/socket/address"},
  86. // For known scheme.
  87. {"passthrough://a.server.com/google.com", "google.com"},
  88. } {
  89. dialStrCh := make(chan string, 1)
  90. cc, err := Dial(test.targetStr, WithInsecure(), WithDialer(func(addr string, _ time.Duration) (net.Conn, error) {
  91. select {
  92. case dialStrCh <- addr:
  93. default:
  94. }
  95. return nil, fmt.Errorf("test dialer, always error")
  96. }))
  97. if err != nil {
  98. t.Fatalf("Failed to create ClientConn: %v", err)
  99. }
  100. got := <-dialStrCh
  101. cc.Close()
  102. if got != test.want {
  103. t.Errorf("Dial(%q), dialer got %q, want %q", test.targetStr, got, test.want)
  104. }
  105. }
  106. }