dial_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. "context"
  17. "math/rand"
  18. "strings"
  19. "testing"
  20. "time"
  21. "go.etcd.io/etcd/clientv3"
  22. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  23. "go.etcd.io/etcd/integration"
  24. "go.etcd.io/etcd/pkg/testutil"
  25. "go.etcd.io/etcd/pkg/transport"
  26. "google.golang.org/grpc"
  27. )
  28. var (
  29. testTLSInfo = transport.TLSInfo{
  30. KeyFile: "../../integration/fixtures/server.key.insecure",
  31. CertFile: "../../integration/fixtures/server.crt",
  32. TrustedCAFile: "../../integration/fixtures/ca.crt",
  33. ClientCertAuth: true,
  34. }
  35. testTLSInfoExpired = transport.TLSInfo{
  36. KeyFile: "../../integration/fixtures-expired/server.key.insecure",
  37. CertFile: "../../integration/fixtures-expired/server.crt",
  38. TrustedCAFile: "../../integration/fixtures-expired/ca.crt",
  39. ClientCertAuth: true,
  40. }
  41. )
  42. // TestDialTLSExpired tests client with expired certs fails to dial.
  43. func TestDialTLSExpired(t *testing.T) {
  44. defer testutil.AfterTest(t)
  45. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1, PeerTLS: &testTLSInfo, ClientTLS: &testTLSInfo, SkipCreatingClient: true})
  46. defer clus.Terminate(t)
  47. tls, err := testTLSInfoExpired.ClientConfig()
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. // expect remote errors "tls: bad certificate"
  52. _, err = clientv3.New(clientv3.Config{
  53. Endpoints: []string{clus.Members[0].GRPCAddr()},
  54. DialTimeout: 3 * time.Second,
  55. DialOptions: []grpc.DialOption{grpc.WithBlock()},
  56. TLS: tls,
  57. })
  58. if !isClientTimeout(err) {
  59. t.Fatalf("expected dial timeout error, got %v", err)
  60. }
  61. }
  62. // TestDialTLSNoConfig ensures the client fails to dial / times out
  63. // when TLS endpoints (https, unixs) are given but no tls config.
  64. func TestDialTLSNoConfig(t *testing.T) {
  65. defer testutil.AfterTest(t)
  66. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1, ClientTLS: &testTLSInfo, SkipCreatingClient: true})
  67. defer clus.Terminate(t)
  68. // expect "signed by unknown authority"
  69. c, err := clientv3.New(clientv3.Config{
  70. Endpoints: []string{clus.Members[0].GRPCAddr()},
  71. DialTimeout: time.Second,
  72. DialOptions: []grpc.DialOption{grpc.WithBlock()},
  73. })
  74. defer func() {
  75. if c != nil {
  76. c.Close()
  77. }
  78. }()
  79. if !isClientTimeout(err) {
  80. t.Fatalf("expected dial timeout error, got %v", err)
  81. }
  82. }
  83. // TestDialSetEndpointsBeforeFail ensures SetEndpoints can replace unavailable
  84. // endpoints with available ones.
  85. func TestDialSetEndpointsBeforeFail(t *testing.T) {
  86. testDialSetEndpoints(t, true)
  87. }
  88. func TestDialSetEndpointsAfterFail(t *testing.T) {
  89. testDialSetEndpoints(t, false)
  90. }
  91. // testDialSetEndpoints ensures SetEndpoints can replace unavailable endpoints with available ones.
  92. func testDialSetEndpoints(t *testing.T, setBefore bool) {
  93. defer testutil.AfterTest(t)
  94. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3, SkipCreatingClient: true})
  95. defer clus.Terminate(t)
  96. // get endpoint list
  97. eps := make([]string, 3)
  98. for i := range eps {
  99. eps[i] = clus.Members[i].GRPCAddr()
  100. }
  101. toKill := rand.Intn(len(eps))
  102. cfg := clientv3.Config{
  103. Endpoints: []string{eps[toKill]},
  104. DialTimeout: 1 * time.Second,
  105. DialOptions: []grpc.DialOption{grpc.WithBlock()},
  106. }
  107. cli, err := clientv3.New(cfg)
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. defer cli.Close()
  112. if setBefore {
  113. cli.SetEndpoints(eps[toKill%3], eps[(toKill+1)%3])
  114. }
  115. // make a dead node
  116. clus.Members[toKill].Stop(t)
  117. clus.WaitLeader(t)
  118. if !setBefore {
  119. cli.SetEndpoints(eps[toKill%3], eps[(toKill+1)%3])
  120. }
  121. time.Sleep(time.Second * 2)
  122. ctx, cancel := context.WithTimeout(context.Background(), integration.RequestWaitTimeout)
  123. if _, err = cli.Get(ctx, "foo", clientv3.WithSerializable()); err != nil {
  124. t.Fatal(err)
  125. }
  126. cancel()
  127. }
  128. // TestSwitchSetEndpoints ensures SetEndpoints can switch one endpoint
  129. // with a new one that doesn't include original endpoint.
  130. func TestSwitchSetEndpoints(t *testing.T) {
  131. defer testutil.AfterTest(t)
  132. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  133. defer clus.Terminate(t)
  134. // get non partitioned members endpoints
  135. eps := []string{clus.Members[1].GRPCAddr(), clus.Members[2].GRPCAddr()}
  136. cli := clus.Client(0)
  137. clus.Members[0].InjectPartition(t, clus.Members[1:]...)
  138. cli.SetEndpoints(eps...)
  139. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  140. defer cancel()
  141. if _, err := cli.Get(ctx, "foo"); err != nil {
  142. t.Fatal(err)
  143. }
  144. }
  145. func TestRejectOldCluster(t *testing.T) {
  146. defer testutil.AfterTest(t)
  147. // 2 endpoints to test multi-endpoint Status
  148. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2, SkipCreatingClient: true})
  149. defer clus.Terminate(t)
  150. cfg := clientv3.Config{
  151. Endpoints: []string{clus.Members[0].GRPCAddr(), clus.Members[1].GRPCAddr()},
  152. DialTimeout: 5 * time.Second,
  153. DialOptions: []grpc.DialOption{grpc.WithBlock()},
  154. RejectOldCluster: true,
  155. }
  156. cli, err := clientv3.New(cfg)
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. cli.Close()
  161. }
  162. // TestDialForeignEndpoint checks an endpoint that is not registered
  163. // with the balancer can be dialed.
  164. func TestDialForeignEndpoint(t *testing.T) {
  165. defer testutil.AfterTest(t)
  166. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
  167. defer clus.Terminate(t)
  168. conn, err := clus.Client(0).Dial(clus.Client(1).Endpoints()[0])
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. defer conn.Close()
  173. // grpc can return a lazy connection that's not connected yet; confirm
  174. // that it can communicate with the cluster.
  175. kvc := clientv3.NewKVFromKVClient(pb.NewKVClient(conn), clus.Client(0))
  176. ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
  177. defer cancel()
  178. if _, gerr := kvc.Get(ctx, "abc"); gerr != nil {
  179. t.Fatal(err)
  180. }
  181. }
  182. // TestSetEndpointAndPut checks that a Put following a SetEndpoints
  183. // to a working endpoint will always succeed.
  184. func TestSetEndpointAndPut(t *testing.T) {
  185. defer testutil.AfterTest(t)
  186. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
  187. defer clus.Terminate(t)
  188. clus.Client(1).SetEndpoints(clus.Members[0].GRPCAddr())
  189. _, err := clus.Client(1).Put(context.TODO(), "foo", "bar")
  190. if err != nil && !strings.Contains(err.Error(), "closing") {
  191. t.Fatal(err)
  192. }
  193. }