vitess.io/vitess@v0.16.2/go/vt/vtctl/grpcvtctlclient/client_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package grpcvtctlclient 18 19 import ( 20 "fmt" 21 "io" 22 "net" 23 "os" 24 "testing" 25 26 "github.com/spf13/pflag" 27 "github.com/stretchr/testify/require" 28 "google.golang.org/grpc" 29 30 "vitess.io/vitess/go/vt/grpcclient" 31 "vitess.io/vitess/go/vt/servenv" 32 "vitess.io/vitess/go/vt/vtctl/grpcvtctlserver" 33 "vitess.io/vitess/go/vt/vtctl/vtctlclienttest" 34 35 vtctlservicepb "vitess.io/vitess/go/vt/proto/vtctlservice" 36 ) 37 38 // the test here creates a fake server implementation, a fake client 39 // implementation, and runs the test suite against the setup. 40 func TestVtctlServer(t *testing.T) { 41 ts := vtctlclienttest.CreateTopoServer(t) 42 43 // Listen on a random port 44 listener, err := net.Listen("tcp", "127.0.0.1:0") 45 if err != nil { 46 t.Fatalf("Cannot listen: %v", err) 47 } 48 port := listener.Addr().(*net.TCPAddr).Port 49 50 // Create a gRPC server and listen on the port 51 server := grpc.NewServer() 52 vtctlservicepb.RegisterVtctlServer(server, grpcvtctlserver.NewVtctlServer(ts)) 53 go server.Serve(listener) 54 55 // Create a VtctlClient gRPC client to talk to the fake server 56 client, err := gRPCVtctlClientFactory(fmt.Sprintf("localhost:%v", port)) 57 if err != nil { 58 t.Fatalf("Cannot create client: %v", err) 59 } 60 defer client.Close() 61 62 vtctlclienttest.TestSuite(t, ts, client) 63 } 64 65 // the test here creates a fake server implementation, a fake client with auth 66 // implementation, and runs the test suite against the setup. 67 func TestVtctlAuthClient(t *testing.T) { 68 ts := vtctlclienttest.CreateTopoServer(t) 69 70 // Listen on a random port 71 listener, err := net.Listen("tcp", "127.0.0.1:0") 72 if err != nil { 73 t.Fatalf("Cannot listen: %v", err) 74 } 75 port := listener.Addr().(*net.TCPAddr).Port 76 77 // Create a gRPC server and listen on the port 78 // add auth interceptors 79 var opts []grpc.ServerOption 80 opts = append(opts, grpc.StreamInterceptor(servenv.FakeAuthStreamInterceptor)) 81 opts = append(opts, grpc.UnaryInterceptor(servenv.FakeAuthUnaryInterceptor)) 82 server := grpc.NewServer(opts...) 83 84 vtctlservicepb.RegisterVtctlServer(server, grpcvtctlserver.NewVtctlServer(ts)) 85 go server.Serve(listener) 86 87 authJSON := `{ 88 "Username": "valid", 89 "Password": "valid" 90 }` 91 92 f, err := os.CreateTemp("", "static_auth_creds.json") 93 if err != nil { 94 t.Fatal(err) 95 } 96 defer os.Remove(f.Name()) 97 if _, err := io.WriteString(f, authJSON); err != nil { 98 t.Fatal(err) 99 } 100 if err := f.Close(); err != nil { 101 t.Fatal(err) 102 } 103 104 fs := pflag.NewFlagSet("", pflag.ContinueOnError) 105 grpcclient.RegisterFlags(fs) 106 107 err = fs.Parse([]string{ 108 "--grpc_auth_static_client_creds", 109 f.Name(), 110 }) 111 require.NoError(t, err, "failed to set `--grpc_auth_static_client_creds=%s`", f.Name()) 112 113 // Create a VtctlClient gRPC client to talk to the fake server 114 client, err := gRPCVtctlClientFactory(fmt.Sprintf("localhost:%v", port)) 115 if err != nil { 116 t.Fatalf("Cannot create client: %v", err) 117 } 118 defer client.Close() 119 120 vtctlclienttest.TestSuite(t, ts, client) 121 }