vitess.io/vitess@v0.16.2/go/test/endtoend/vtgate/queries/dml/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 dml 18 19 import ( 20 _ "embed" 21 "flag" 22 "fmt" 23 "os" 24 "testing" 25 26 "github.com/stretchr/testify/require" 27 28 "vitess.io/vitess/go/mysql" 29 "vitess.io/vitess/go/test/endtoend/cluster" 30 "vitess.io/vitess/go/test/endtoend/utils" 31 "vitess.io/vitess/go/vt/vtgate/planbuilder" 32 ) 33 34 var ( 35 clusterInstance *cluster.LocalProcessCluster 36 vtParams mysql.ConnParams 37 mysqlParams mysql.ConnParams 38 sKs = "sks" 39 uKs = "uks" 40 cell = "test" 41 42 //go:embed sharded_schema.sql 43 sSchemaSQL string 44 45 //go:embed vschema.json 46 sVSchema string 47 48 //go:embed unsharded_schema.sql 49 uSchemaSQL string 50 51 uVSchema = ` 52 { 53 "tables": { 54 "u_tbl": {}, 55 "user_seq": { 56 "type": "sequence" 57 }, 58 "auto_seq": { 59 "type": "sequence" 60 } 61 } 62 }` 63 ) 64 65 func TestMain(m *testing.M) { 66 defer cluster.PanicHandler(nil) 67 flag.Parse() 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 unsharded keyspace 80 uKeyspace := &cluster.Keyspace{ 81 Name: uKs, 82 SchemaSQL: uSchemaSQL, 83 VSchema: uVSchema, 84 } 85 err = clusterInstance.StartUnshardedKeyspace(*uKeyspace, 0, false) 86 if err != nil { 87 return 1 88 } 89 90 // Start sharded keyspace 91 sKeyspace := &cluster.Keyspace{ 92 Name: sKs, 93 SchemaSQL: sSchemaSQL, 94 VSchema: sVSchema, 95 } 96 err = clusterInstance.StartKeyspace(*sKeyspace, []string{"-80", "80-"}, 0, false) 97 if err != nil { 98 return 1 99 } 100 101 clusterInstance.VtGateExtraArgs = append(clusterInstance.VtGateExtraArgs, "--vtgate-config-terse-errors") 102 103 // Start vtgate 104 clusterInstance.VtGatePlannerVersion = planbuilder.Gen4 105 err = clusterInstance.StartVtgate() 106 if err != nil { 107 return 1 108 } 109 110 vtParams = clusterInstance.GetVTParams(sKs) 111 // create mysql instance and connection parameters 112 conn, closer, err := utils.NewMySQL(clusterInstance, sKs, sSchemaSQL, uSchemaSQL) 113 if err != nil { 114 fmt.Println(err) 115 return 1 116 } 117 defer closer() 118 mysqlParams = conn 119 return m.Run() 120 }() 121 os.Exit(exitCode) 122 } 123 124 func start(t *testing.T) (utils.MySQLCompare, func()) { 125 mcmp, err := utils.NewMySQLCompare(t, vtParams, mysqlParams) 126 require.NoError(t, err) 127 128 deleteAll := func() { 129 _, _ = utils.ExecAllowError(t, mcmp.VtConn, "set workload = oltp") 130 131 tables := []string{ 132 "s_tbl", "num_vdx_tbl", "user_tbl", "order_tbl", "oevent_tbl", "oextra_tbl", 133 "auto_tbl", "oid_vdx_tbl", "unq_idx", "nonunq_idx", "u_tbl", 134 } 135 for _, table := range tables { 136 // TODO (@frouioui): following assertions produce different results between MySQL and Vitess 137 // their differences are ignored for now. Fix it. 138 // delete from returns different RowsAffected and Flag values 139 _, _ = mcmp.ExecAndIgnore("delete from " + table) 140 } 141 } 142 143 deleteAll() 144 145 return mcmp, func() { 146 deleteAll() 147 mcmp.Close() 148 cluster.PanicHandler(t) 149 } 150 }