github.com/lfch/etcd-io/tests/v3@v3.0.0-20221004140520-eac99acd3e9d/integration/proxy/grpcproxy/kv_test.go (about)

     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  
    15  package grpcproxy
    16  
    17  import (
    18  	"context"
    19  	"net"
    20  	"testing"
    21  	"time"
    22  
    23  	pb "github.com/lfch/etcd-io/api/v3/etcdserverpb"
    24  	"github.com/lfch/etcd-io/client/v3"
    25  	"github.com/lfch/etcd-io/server/v3/proxy/grpcproxy"
    26  	integration2 "github.com/lfch/etcd-io/tests/v3/framework/integration"
    27  	"google.golang.org/grpc"
    28  )
    29  
    30  func TestKVProxyRange(t *testing.T) {
    31  	integration2.BeforeTest(t)
    32  
    33  	clus := integration2.NewCluster(t, &integration2.ClusterConfig{Size: 1})
    34  	defer clus.Terminate(t)
    35  
    36  	kvts := newKVProxyServer([]string{clus.Members[0].GRPCURL()}, t)
    37  	defer kvts.close()
    38  
    39  	// create a client and try to get key from proxy.
    40  	cfg := clientv3.Config{
    41  		Endpoints:   []string{kvts.l.Addr().String()},
    42  		DialTimeout: 5 * time.Second,
    43  	}
    44  	client, err := integration2.NewClient(t, cfg)
    45  	if err != nil {
    46  		t.Fatalf("err = %v, want nil", err)
    47  	}
    48  	_, err = client.Get(context.Background(), "foo")
    49  	if err != nil {
    50  		t.Fatalf("err = %v, want nil", err)
    51  	}
    52  	client.Close()
    53  }
    54  
    55  type kvproxyTestServer struct {
    56  	kp     pb.KVServer
    57  	c      *clientv3.Client
    58  	server *grpc.Server
    59  	l      net.Listener
    60  }
    61  
    62  func (kts *kvproxyTestServer) close() {
    63  	kts.server.Stop()
    64  	kts.l.Close()
    65  	kts.c.Close()
    66  }
    67  
    68  func newKVProxyServer(endpoints []string, t *testing.T) *kvproxyTestServer {
    69  	cfg := clientv3.Config{
    70  		Endpoints:   endpoints,
    71  		DialTimeout: 5 * time.Second,
    72  	}
    73  	client, err := integration2.NewClient(t, cfg)
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  
    78  	kvp, _ := grpcproxy.NewKvProxy(client)
    79  
    80  	kvts := &kvproxyTestServer{
    81  		kp: kvp,
    82  		c:  client,
    83  	}
    84  
    85  	var opts []grpc.ServerOption
    86  	kvts.server = grpc.NewServer(opts...)
    87  	pb.RegisterKVServer(kvts.server, kvts.kp)
    88  
    89  	kvts.l, err = net.Listen("tcp", "127.0.0.1:0")
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  
    94  	go kvts.server.Serve(kvts.l)
    95  
    96  	return kvts
    97  }