cluster_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2017 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 grpcproxy
  15. import (
  16. "net"
  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. "golang.org/x/net/context"
  24. "google.golang.org/grpc"
  25. )
  26. func TestClusterProxyMemberList(t *testing.T) {
  27. defer testutil.AfterTest(t)
  28. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  29. defer clus.Terminate(t)
  30. cts := newClusterProxyServer([]string{clus.Members[0].GRPCAddr()}, t)
  31. defer cts.close(t)
  32. cfg := clientv3.Config{
  33. Endpoints: []string{cts.caddr},
  34. DialTimeout: 5 * time.Second,
  35. }
  36. client, err := clientv3.New(cfg)
  37. if err != nil {
  38. t.Fatalf("err %v, want nil", err)
  39. }
  40. defer client.Close()
  41. // wait some time for register-loop to write keys
  42. time.Sleep(time.Second)
  43. var mresp *clientv3.MemberListResponse
  44. mresp, err = client.Cluster.MemberList(context.Background())
  45. if err != nil {
  46. t.Fatalf("err %v, want nil", err)
  47. }
  48. if len(mresp.Members) != 1 {
  49. t.Fatalf("len(mresp.Members) expected 1, got %d (%+v)", len(mresp.Members), mresp.Members)
  50. }
  51. if len(mresp.Members[0].ClientURLs) != 1 {
  52. t.Fatalf("len(mresp.Members[0].ClientURLs) expected 1, got %d (%+v)", len(mresp.Members[0].ClientURLs), mresp.Members[0].ClientURLs[0])
  53. }
  54. if mresp.Members[0].ClientURLs[0] != cts.caddr {
  55. t.Fatalf("mresp.Members[0].ClientURLs[0] expected %q, got %q", cts.caddr, mresp.Members[0].ClientURLs[0])
  56. }
  57. }
  58. type clusterproxyTestServer struct {
  59. cp pb.ClusterServer
  60. c *clientv3.Client
  61. server *grpc.Server
  62. l net.Listener
  63. donec <-chan struct{}
  64. caddr string
  65. }
  66. func (cts *clusterproxyTestServer) close(t *testing.T) {
  67. cts.server.Stop()
  68. cts.l.Close()
  69. cts.c.Close()
  70. select {
  71. case <-cts.donec:
  72. return
  73. case <-time.After(5 * time.Second):
  74. t.Fatalf("register-loop took too long to return")
  75. }
  76. }
  77. func newClusterProxyServer(endpoints []string, t *testing.T) *clusterproxyTestServer {
  78. cfg := clientv3.Config{
  79. Endpoints: endpoints,
  80. DialTimeout: 5 * time.Second,
  81. }
  82. client, err := clientv3.New(cfg)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. cts := &clusterproxyTestServer{
  87. c: client,
  88. }
  89. cts.l, err = net.Listen("tcp", "127.0.0.1:0")
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. var opts []grpc.ServerOption
  94. cts.server = grpc.NewServer(opts...)
  95. servec := make(chan struct{})
  96. go func() {
  97. <-servec
  98. cts.server.Serve(cts.l)
  99. }()
  100. Register(client, "test-prefix", cts.l.Addr().String(), 7)
  101. cts.cp, cts.donec = NewClusterProxy(client, cts.l.Addr().String(), "test-prefix")
  102. cts.caddr = cts.l.Addr().String()
  103. pb.RegisterClusterServer(cts.server, cts.cp)
  104. close(servec)
  105. // wait some time for free port 0 to be resolved
  106. time.Sleep(500 * time.Millisecond)
  107. return cts
  108. }