dial_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package integration
  15. import (
  16. "math/rand"
  17. "strings"
  18. "testing"
  19. "time"
  20. "github.com/coreos/etcd/clientv3"
  21. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  22. "github.com/coreos/etcd/integration"
  23. "github.com/coreos/etcd/pkg/testutil"
  24. "github.com/coreos/etcd/pkg/transport"
  25. "golang.org/x/net/context"
  26. )
  27. var (
  28. testTLSInfo = transport.TLSInfo{
  29. KeyFile: "../../integration/fixtures/server.key.insecure",
  30. CertFile: "../../integration/fixtures/server.crt",
  31. TrustedCAFile: "../../integration/fixtures/ca.crt",
  32. ClientCertAuth: true,
  33. }
  34. testTLSInfoExpired = transport.TLSInfo{
  35. KeyFile: "../../integration/fixtures-expired/server-key.pem",
  36. CertFile: "../../integration/fixtures-expired/server.pem",
  37. TrustedCAFile: "../../integration/fixtures-expired/etcd-root-ca.pem",
  38. ClientCertAuth: true,
  39. }
  40. )
  41. // TestDialTLSExpired tests client with expired certs fails to dial.
  42. func TestDialTLSExpired(t *testing.T) {
  43. defer testutil.AfterTest(t)
  44. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1, PeerTLS: &testTLSInfo, ClientTLS: &testTLSInfo, SkipCreatingClient: true})
  45. defer clus.Terminate(t)
  46. tls, err := testTLSInfoExpired.ClientConfig()
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. // expect remote errors "tls: bad certificate"
  51. _, err = clientv3.New(clientv3.Config{
  52. Endpoints: []string{clus.Members[0].GRPCAddr()},
  53. DialTimeout: 3 * time.Second,
  54. TLS: tls,
  55. })
  56. if err != context.DeadlineExceeded {
  57. t.Fatalf("expected %v, got %v", context.DeadlineExceeded, err)
  58. }
  59. }
  60. // TestDialTLSNoConfig ensures the client fails to dial / times out
  61. // when TLS endpoints (https, unixs) are given but no tls config.
  62. func TestDialTLSNoConfig(t *testing.T) {
  63. defer testutil.AfterTest(t)
  64. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1, ClientTLS: &testTLSInfo, SkipCreatingClient: true})
  65. defer clus.Terminate(t)
  66. // expect "signed by unknown authority"
  67. _, err := clientv3.New(clientv3.Config{
  68. Endpoints: []string{clus.Members[0].GRPCAddr()},
  69. DialTimeout: time.Second,
  70. })
  71. if err != context.DeadlineExceeded {
  72. t.Fatalf("expected %v, got %v", context.DeadlineExceeded, err)
  73. }
  74. }
  75. // TestDialSetEndpointsBeforeFail ensures SetEndpoints can replace unavailable
  76. // endpoints with available ones.
  77. func TestDialSetEndpointsBeforeFail(t *testing.T) {
  78. testDialSetEndpoints(t, true)
  79. }
  80. func TestDialSetEndpointsAfterFail(t *testing.T) {
  81. testDialSetEndpoints(t, false)
  82. }
  83. // testDialSetEndpoints ensures SetEndpoints can replace unavailable endpoints with available ones.
  84. func testDialSetEndpoints(t *testing.T, setBefore bool) {
  85. defer testutil.AfterTest(t)
  86. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3, SkipCreatingClient: true})
  87. defer clus.Terminate(t)
  88. // get endpoint list
  89. eps := make([]string, 3)
  90. for i := range eps {
  91. eps[i] = clus.Members[i].GRPCAddr()
  92. }
  93. toKill := rand.Intn(len(eps))
  94. cfg := clientv3.Config{Endpoints: []string{eps[toKill]}, DialTimeout: 1 * time.Second}
  95. cli, err := clientv3.New(cfg)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. defer cli.Close()
  100. if setBefore {
  101. cli.SetEndpoints(eps[toKill%3], eps[(toKill+1)%3])
  102. }
  103. // make a dead node
  104. clus.Members[toKill].Stop(t)
  105. clus.WaitLeader(t)
  106. if !setBefore {
  107. cli.SetEndpoints(eps[toKill%3], eps[(toKill+1)%3])
  108. }
  109. ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
  110. if _, err = cli.Get(ctx, "foo", clientv3.WithSerializable()); err != nil {
  111. t.Fatal(err)
  112. }
  113. cancel()
  114. }
  115. // TestSwitchSetEndpoints ensures SetEndpoints can switch one endpoint
  116. // with a new one that doesn't include original endpoint.
  117. func TestSwitchSetEndpoints(t *testing.T) {
  118. defer testutil.AfterTest(t)
  119. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  120. defer clus.Terminate(t)
  121. // get non partitioned members endpoints
  122. eps := []string{clus.Members[1].GRPCAddr(), clus.Members[2].GRPCAddr()}
  123. cli := clus.Client(0)
  124. clus.Members[0].InjectPartition(t, clus.Members[1:]...)
  125. cli.SetEndpoints(eps...)
  126. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  127. defer cancel()
  128. if _, err := cli.Get(ctx, "foo"); err != nil {
  129. t.Fatal(err)
  130. }
  131. }
  132. func TestRejectOldCluster(t *testing.T) {
  133. defer testutil.AfterTest(t)
  134. // 2 endpoints to test multi-endpoint Status
  135. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2, SkipCreatingClient: true})
  136. defer clus.Terminate(t)
  137. cfg := clientv3.Config{
  138. Endpoints: []string{clus.Members[0].GRPCAddr(), clus.Members[1].GRPCAddr()},
  139. DialTimeout: 5 * time.Second,
  140. RejectOldCluster: true,
  141. }
  142. cli, err := clientv3.New(cfg)
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. cli.Close()
  147. }
  148. // TestDialForeignEndpoint checks an endpoint that is not registered
  149. // with the balancer can be dialed.
  150. func TestDialForeignEndpoint(t *testing.T) {
  151. defer testutil.AfterTest(t)
  152. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
  153. defer clus.Terminate(t)
  154. conn, err := clus.Client(0).Dial(clus.Client(1).Endpoints()[0])
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. defer conn.Close()
  159. // grpc can return a lazy connection that's not connected yet; confirm
  160. // that it can communicate with the cluster.
  161. kvc := clientv3.NewKVFromKVClient(pb.NewKVClient(conn), clus.Client(0))
  162. ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
  163. defer cancel()
  164. if _, gerr := kvc.Get(ctx, "abc"); gerr != nil {
  165. t.Fatal(err)
  166. }
  167. }
  168. // TestSetEndpointAndPut checks that a Put following a SetEndpoints
  169. // to a working endpoint will always succeed.
  170. func TestSetEndpointAndPut(t *testing.T) {
  171. defer testutil.AfterTest(t)
  172. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
  173. defer clus.Terminate(t)
  174. clus.Client(1).SetEndpoints(clus.Members[0].GRPCAddr())
  175. _, err := clus.Client(1).Put(context.TODO(), "foo", "bar")
  176. if err != nil && !strings.Contains(err.Error(), "closing") {
  177. t.Fatal(err)
  178. }
  179. }