dial_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "fmt"
  17. "io/ioutil"
  18. "math/rand"
  19. "net/url"
  20. "os"
  21. "sync"
  22. "testing"
  23. "time"
  24. "github.com/coreos/etcd/clientv3"
  25. "github.com/coreos/etcd/embed"
  26. "github.com/coreos/etcd/integration"
  27. "github.com/coreos/etcd/pkg/testutil"
  28. "golang.org/x/net/context"
  29. )
  30. // TestDialSetEndpoints ensures SetEndpoints can replace unavailable endpoints with available ones.
  31. func TestDialSetEndpoints(t *testing.T) {
  32. defer testutil.AfterTest(t)
  33. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  34. defer clus.Terminate(t)
  35. // get endpoint list
  36. eps := make([]string, 3)
  37. for i := range eps {
  38. eps[i] = clus.Members[i].GRPCAddr()
  39. }
  40. toKill := rand.Intn(len(eps))
  41. cfg := clientv3.Config{Endpoints: []string{eps[toKill]}, DialTimeout: 1 * time.Second}
  42. cli, err := clientv3.New(cfg)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. defer cli.Close()
  47. // make a dead node
  48. clus.Members[toKill].Stop(t)
  49. clus.WaitLeader(t)
  50. // update client with available endpoints
  51. cli.SetEndpoints(eps[(toKill+1)%3])
  52. ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
  53. if _, err = cli.Get(ctx, "foo", clientv3.WithSerializable()); err != nil {
  54. t.Fatal(err)
  55. }
  56. cancel()
  57. }
  58. var (
  59. testMu sync.Mutex
  60. testPort = 31000
  61. )
  62. // TestDialWithHTTPS ensures that client can handle 'https' scheme in endpoints.
  63. func TestDialWithHTTPS(t *testing.T) {
  64. defer testutil.AfterTest(t)
  65. testMu.Lock()
  66. port := testPort
  67. testPort += 10 // to avoid port conflicts
  68. testMu.Unlock()
  69. dir, err := ioutil.TempDir(os.TempDir(), "dial-test")
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. defer os.RemoveAll(dir)
  74. // set up single-node cluster with client auto TLS
  75. cfg := embed.NewConfig()
  76. cfg.Dir = dir
  77. cfg.ClientAutoTLS = true
  78. clientURL := url.URL{Scheme: "https", Host: fmt.Sprintf("localhost:%d", port)}
  79. cfg.LCUrls, cfg.ACUrls = []url.URL{clientURL}, []url.URL{clientURL}
  80. peerURL := url.URL{Scheme: "http", Host: fmt.Sprintf("localhost:%d", port+1)}
  81. cfg.LPUrls, cfg.APUrls = []url.URL{peerURL}, []url.URL{peerURL}
  82. cfg.InitialCluster = cfg.Name + "=" + peerURL.String()
  83. srv, err := embed.StartEtcd(cfg)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. nc := srv.Config() // overwrite config after processing ClientTLSInfo
  88. cfg = &nc
  89. <-srv.Server.ReadyNotify()
  90. defer func() {
  91. srv.Close()
  92. <-srv.Err()
  93. }()
  94. // wait for leader election to finish
  95. time.Sleep(500 * time.Millisecond)
  96. ccfg := clientv3.Config{Endpoints: []string{clientURL.String()}}
  97. tcfg, err := cfg.ClientTLSInfo.ClientConfig()
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. ccfg.TLS = tcfg
  102. cli, err := clientv3.New(ccfg)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. defer cli.Close()
  107. if _, err = cli.Get(context.Background(), "foo"); err != nil {
  108. t.Fatal(err)
  109. }
  110. }