dial_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. "github.com/coreos/etcd/clientv3"
  22. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  23. "github.com/coreos/etcd/integration"
  24. "github.com/coreos/etcd/pkg/testutil"
  25. "github.com/coreos/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 c.Close()
  75. // TODO: this should not be required when we set grpc.WithBlock()
  76. if c != nil {
  77. ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
  78. _, err = c.KV.Get(ctx, "/")
  79. cancel()
  80. }
  81. if !isClientTimeout(err) {
  82. t.Fatalf("expected dial timeout error, got %v", err)
  83. }
  84. }
  85. // TestDialSetEndpointsBeforeFail ensures SetEndpoints can replace unavailable
  86. // endpoints with available ones.
  87. func TestDialSetEndpointsBeforeFail(t *testing.T) {
  88. testDialSetEndpoints(t, true)
  89. }
  90. func TestDialSetEndpointsAfterFail(t *testing.T) {
  91. testDialSetEndpoints(t, false)
  92. }
  93. // testDialSetEndpoints ensures SetEndpoints can replace unavailable endpoints with available ones.
  94. func testDialSetEndpoints(t *testing.T, setBefore bool) {
  95. defer testutil.AfterTest(t)
  96. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3, SkipCreatingClient: true})
  97. defer clus.Terminate(t)
  98. // get endpoint list
  99. eps := make([]string, 3)
  100. for i := range eps {
  101. eps[i] = clus.Members[i].GRPCAddr()
  102. }
  103. toKill := rand.Intn(len(eps))
  104. cfg := clientv3.Config{
  105. Endpoints: []string{eps[toKill]},
  106. DialTimeout: 1 * time.Second,
  107. DialOptions: []grpc.DialOption{grpc.WithBlock()},
  108. }
  109. cli, err := clientv3.New(cfg)
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. defer cli.Close()
  114. if setBefore {
  115. cli.SetEndpoints(eps[toKill%3], eps[(toKill+1)%3])
  116. }
  117. // make a dead node
  118. clus.Members[toKill].Stop(t)
  119. clus.WaitLeader(t)
  120. if !setBefore {
  121. cli.SetEndpoints(eps[toKill%3], eps[(toKill+1)%3])
  122. }
  123. time.Sleep(time.Second * 2)
  124. ctx, cancel := context.WithTimeout(context.Background(), integration.RequestWaitTimeout)
  125. if _, err = cli.Get(ctx, "foo", clientv3.WithSerializable()); err != nil {
  126. t.Fatal(err)
  127. }
  128. cancel()
  129. }
  130. // TestSwitchSetEndpoints ensures SetEndpoints can switch one endpoint
  131. // with a new one that doesn't include original endpoint.
  132. func TestSwitchSetEndpoints(t *testing.T) {
  133. defer testutil.AfterTest(t)
  134. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  135. defer clus.Terminate(t)
  136. // get non partitioned members endpoints
  137. eps := []string{clus.Members[1].GRPCAddr(), clus.Members[2].GRPCAddr()}
  138. cli := clus.Client(0)
  139. clus.Members[0].InjectPartition(t, clus.Members[1:]...)
  140. cli.SetEndpoints(eps...)
  141. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  142. defer cancel()
  143. if _, err := cli.Get(ctx, "foo"); err != nil {
  144. t.Fatal(err)
  145. }
  146. }
  147. func TestRejectOldCluster(t *testing.T) {
  148. defer testutil.AfterTest(t)
  149. // 2 endpoints to test multi-endpoint Status
  150. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2, SkipCreatingClient: true})
  151. defer clus.Terminate(t)
  152. cfg := clientv3.Config{
  153. Endpoints: []string{clus.Members[0].GRPCAddr(), clus.Members[1].GRPCAddr()},
  154. DialTimeout: 5 * time.Second,
  155. DialOptions: []grpc.DialOption{grpc.WithBlock()},
  156. RejectOldCluster: true,
  157. }
  158. cli, err := clientv3.New(cfg)
  159. if err != nil {
  160. t.Fatal(err)
  161. }
  162. cli.Close()
  163. }
  164. // TestDialForeignEndpoint checks an endpoint that is not registered
  165. // with the balancer can be dialed.
  166. func TestDialForeignEndpoint(t *testing.T) {
  167. defer testutil.AfterTest(t)
  168. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
  169. defer clus.Terminate(t)
  170. conn, err := clus.Client(0).Dial(clus.Client(1).Endpoints()[0])
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. defer conn.Close()
  175. // grpc can return a lazy connection that's not connected yet; confirm
  176. // that it can communicate with the cluster.
  177. kvc := clientv3.NewKVFromKVClient(pb.NewKVClient(conn), clus.Client(0))
  178. ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
  179. defer cancel()
  180. if _, gerr := kvc.Get(ctx, "abc"); gerr != nil {
  181. t.Fatal(err)
  182. }
  183. }
  184. // TestSetEndpointAndPut checks that a Put following a SetEndpoints
  185. // to a working endpoint will always succeed.
  186. func TestSetEndpointAndPut(t *testing.T) {
  187. defer testutil.AfterTest(t)
  188. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
  189. defer clus.Terminate(t)
  190. clus.Client(1).SetEndpoints(clus.Members[0].GRPCAddr())
  191. _, err := clus.Client(1).Put(context.TODO(), "foo", "bar")
  192. if err != nil && !strings.Contains(err.Error(), "closing") {
  193. t.Fatal(err)
  194. }
  195. }