vitess.io/vitess@v0.16.2/go/test/endtoend/vtctldclient/cli_test.go (about) 1 /* 2 Copyright 2022 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 vtctldclient 18 19 import ( 20 "context" 21 "os" 22 "testing" 23 "time" 24 25 "github.com/stretchr/testify/assert" 26 "github.com/stretchr/testify/require" 27 "google.golang.org/protobuf/proto" 28 29 "vitess.io/vitess/go/cmd/vtctldclient/command" 30 "vitess.io/vitess/go/protoutil" 31 "vitess.io/vitess/go/vt/vtctl/localvtctldclient" 32 33 vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata" 34 vtctlservicepb "vitess.io/vitess/go/vt/proto/vtctlservice" 35 ) 36 37 type fakeServer struct { 38 vtctlservicepb.UnimplementedVtctldServer 39 t testing.TB 40 41 applySchemaRequests []*vtctldatapb.ApplySchemaRequest 42 } 43 44 func (s *fakeServer) ApplySchema(ctx context.Context, req *vtctldatapb.ApplySchemaRequest) (*vtctldatapb.ApplySchemaResponse, error) { 45 s.applySchemaRequests = append(s.applySchemaRequests, req) 46 return &vtctldatapb.ApplySchemaResponse{}, nil 47 } 48 49 func TestApplySchema(t *testing.T) { 50 server := &fakeServer{t: t} 51 52 command.VtctldClientProtocol = "local" 53 localvtctldclient.SetServer(server) 54 55 defer func(argv []string) { 56 os.Args = argv 57 }(append([]string{}, os.Args...)) 58 59 os.Args = []string{ 60 "vtctldclient", 61 "--server='doesnotmatter'", 62 "ApplySchema", 63 "--sql", 64 `"CREATE TABLE foo(id int not null primary key, name varchar(255)); CREATE TABLE bar (id int not null primary key, foo_id int not null);`, 65 "test", 66 } 67 68 require.NoError(t, command.Root.Execute()) 69 expected := &vtctldatapb.ApplySchemaRequest{ 70 Keyspace: "test", 71 Sql: []string{ 72 `"CREATE TABLE foo(id int not null primary key, name varchar(255)); CREATE TABLE bar (id int not null primary key, foo_id int not null);`, 73 }, 74 DdlStrategy: "direct", 75 WaitReplicasTimeout: protoutil.DurationToProto(10 * time.Second), 76 } 77 actual := server.applySchemaRequests[0] 78 assert.True(t, proto.Equal(actual, expected), "ApplySchema received unexpected request (got %v want %v)", actual, expected) 79 }