vitess.io/vitess@v0.16.2/go/test/endtoend/utils/mysqlvsvitess/main_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 mysqlvsvitess 18 19 import ( 20 "context" 21 "fmt" 22 "os" 23 "testing" 24 25 "github.com/stretchr/testify/require" 26 27 "vitess.io/vitess/go/mysql" 28 "vitess.io/vitess/go/test/endtoend/cluster" 29 "vitess.io/vitess/go/test/endtoend/utils" 30 ) 31 32 var ( 33 clusterInstance *cluster.LocalProcessCluster 34 vtParams mysql.ConnParams 35 mysqlParams mysql.ConnParams 36 keyspaceName = "ks" 37 cell = "test" 38 schemaSQL = `create table t1( 39 id1 bigint, 40 id2 bigint, 41 id3 bigint, 42 primary key(id1) 43 ) Engine=InnoDB;` 44 45 vschema = ` 46 { 47 "sharded": true, 48 "vindexes": { 49 "hash": { 50 "type": "hash" 51 } 52 }, 53 "tables": { 54 "t1": { 55 "column_vindexes": [ 56 { 57 "column": "id1", 58 "name": "hash" 59 } 60 ] 61 } 62 } 63 }` 64 ) 65 66 func TestMain(m *testing.M) { 67 defer cluster.PanicHandler(nil) 68 69 exitCode := func() int { 70 clusterInstance = cluster.NewCluster(cell, "localhost") 71 defer clusterInstance.Teardown() 72 73 // Start topo server 74 err := clusterInstance.StartTopo() 75 if err != nil { 76 return 1 77 } 78 79 // Start keyspace 80 keyspace := &cluster.Keyspace{ 81 Name: keyspaceName, 82 SchemaSQL: schemaSQL, 83 VSchema: vschema, 84 } 85 clusterInstance.VtGateExtraArgs = []string{"--schema_change_signal"} 86 clusterInstance.VtTabletExtraArgs = []string{"--queryserver-config-schema-change-signal", "--queryserver-config-schema-change-signal-interval", "0.1"} 87 err = clusterInstance.StartKeyspace(*keyspace, []string{"-80", "80-"}, 0, false) 88 if err != nil { 89 return 1 90 } 91 92 clusterInstance.VtGateExtraArgs = append(clusterInstance.VtGateExtraArgs, "--enable_system_settings=true") 93 // Start vtgate 94 err = clusterInstance.StartVtgate() 95 if err != nil { 96 return 1 97 } 98 99 vtParams = mysql.ConnParams{ 100 Host: clusterInstance.Hostname, 101 Port: clusterInstance.VtgateMySQLPort, 102 } 103 104 // create mysql instance and connection parameters 105 conn, closer, err := utils.NewMySQL(clusterInstance, keyspaceName, schemaSQL) 106 if err != nil { 107 fmt.Println(err) 108 return 1 109 } 110 defer closer() 111 mysqlParams = conn 112 113 return m.Run() 114 }() 115 os.Exit(exitCode) 116 } 117 118 func TestCreateMySQL(t *testing.T) { 119 ctx := context.Background() 120 mysqlConn, err := mysql.Connect(ctx, &mysqlParams) 121 require.NoError(t, err) 122 defer mysqlConn.Close() 123 124 vtConn, err := mysql.Connect(ctx, &vtParams) 125 require.NoError(t, err) 126 defer vtConn.Close() 127 128 utils.ExecCompareMySQL(t, vtConn, mysqlConn, "insert into t1(id1, id2, id3) values (1, 1, 1), (2, 2, 2), (3, 3, 3)") 129 utils.AssertMatchesCompareMySQL(t, vtConn, mysqlConn, "select * from t1;", `[[INT64(1) INT64(1) INT64(1)] [INT64(2) INT64(2) INT64(2)] [INT64(3) INT64(3) INT64(3)]]`) 130 utils.AssertMatchesCompareMySQL(t, vtConn, mysqlConn, "select * from t1 order by id1 desc;", `[[INT64(3) INT64(3) INT64(3)] [INT64(2) INT64(2) INT64(2)] [INT64(1) INT64(1) INT64(1)]]`) 131 }