dial_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "testing"
  18. "time"
  19. "github.com/coreos/etcd/clientv3"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "github.com/coreos/etcd/integration"
  22. "github.com/coreos/etcd/pkg/testutil"
  23. "github.com/coreos/etcd/pkg/transport"
  24. "golang.org/x/net/context"
  25. "google.golang.org/grpc"
  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})
  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 != grpc.ErrClientConnTimeout {
  57. t.Fatalf("expected %v, got %v", grpc.ErrClientConnTimeout, err)
  58. }
  59. }
  60. // TestDialSetEndpoints ensures SetEndpoints can replace unavailable endpoints with available ones.
  61. func TestDialSetEndpointsBeforeFail(t *testing.T) {
  62. testDialSetEndpoints(t, true)
  63. }
  64. func TestDialSetEndpointsAfterFail(t *testing.T) {
  65. testDialSetEndpoints(t, false)
  66. }
  67. // testDialSetEndpoints ensures SetEndpoints can replace unavailable endpoints with available ones.
  68. func testDialSetEndpoints(t *testing.T, setBefore bool) {
  69. defer testutil.AfterTest(t)
  70. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  71. defer clus.Terminate(t)
  72. // get endpoint list
  73. eps := make([]string, 3)
  74. for i := range eps {
  75. eps[i] = clus.Members[i].GRPCAddr()
  76. }
  77. toKill := rand.Intn(len(eps))
  78. cfg := clientv3.Config{Endpoints: []string{eps[toKill]}, DialTimeout: 1 * time.Second}
  79. cli, err := clientv3.New(cfg)
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. defer cli.Close()
  84. if setBefore {
  85. cli.SetEndpoints(eps[toKill%3], eps[(toKill+1)%3])
  86. }
  87. // make a dead node
  88. clus.Members[toKill].Stop(t)
  89. clus.WaitLeader(t)
  90. if !setBefore {
  91. cli.SetEndpoints(eps[toKill%3], eps[(toKill+1)%3])
  92. }
  93. ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
  94. if _, err = cli.Get(ctx, "foo", clientv3.WithSerializable()); err != nil {
  95. t.Fatal(err)
  96. }
  97. cancel()
  98. }
  99. // TestSwitchSetEndpoints ensures SetEndpoints can switch one endpoint
  100. // with a new one that doesn't include original endpoint.
  101. func TestSwitchSetEndpoints(t *testing.T) {
  102. defer testutil.AfterTest(t)
  103. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  104. defer clus.Terminate(t)
  105. // get non partitioned members endpoints
  106. eps := []string{clus.Members[1].GRPCAddr(), clus.Members[2].GRPCAddr()}
  107. cli := clus.Client(0)
  108. clus.Members[0].InjectPartition(t, clus.Members[1:])
  109. cli.SetEndpoints(eps...)
  110. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  111. defer cancel()
  112. if _, err := cli.Get(ctx, "foo"); err != nil {
  113. t.Fatal(err)
  114. }
  115. }
  116. func TestRejectOldCluster(t *testing.T) {
  117. defer testutil.AfterTest(t)
  118. // 2 endpoints to test multi-endpoint Status
  119. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
  120. defer clus.Terminate(t)
  121. cfg := clientv3.Config{
  122. Endpoints: []string{clus.Members[0].GRPCAddr(), clus.Members[1].GRPCAddr()},
  123. DialTimeout: 5 * time.Second,
  124. RejectOldCluster: true,
  125. }
  126. cli, err := clientv3.New(cfg)
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. cli.Close()
  131. }
  132. // TestDialForeignEndpoint checks an endpoint that is not registered
  133. // with the balancer can be dialed.
  134. func TestDialForeignEndpoint(t *testing.T) {
  135. defer testutil.AfterTest(t)
  136. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
  137. defer clus.Terminate(t)
  138. conn, err := clus.Client(0).Dial(clus.Client(1).Endpoints()[0])
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. defer conn.Close()
  143. // grpc can return a lazy connection that's not connected yet; confirm
  144. // that it can communicate with the cluster.
  145. kvc := clientv3.NewKVFromKVClient(pb.NewKVClient(conn))
  146. ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
  147. defer cancel()
  148. if _, gerr := kvc.Get(ctx, "abc"); gerr != nil {
  149. t.Fatal(err)
  150. }
  151. }