vitess.io/vitess@v0.16.2/go/test/endtoend/vtgate/main_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 vtgate
    18  
    19  import (
    20  	"context"
    21  	_ "embed"
    22  	"flag"
    23  	"os"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  
    28  	"vitess.io/vitess/go/test/endtoend/utils"
    29  
    30  	"vitess.io/vitess/go/mysql"
    31  	"vitess.io/vitess/go/test/endtoend/cluster"
    32  )
    33  
    34  var (
    35  	clusterInstance *cluster.LocalProcessCluster
    36  	vtParams        mysql.ConnParams
    37  	KeyspaceName    = "ks"
    38  	Cell            = "test"
    39  
    40  	//go:embed schema.sql
    41  	SchemaSQL string
    42  
    43  	//go:embed vschema.json
    44  	VSchema string
    45  
    46  	routingRules = `
    47  {"rules": [
    48    {
    49      "from_table": "ks.t1000",
    50  	"to_tables": ["ks.t1"]
    51    }
    52  ]}
    53  `
    54  )
    55  
    56  func TestMain(m *testing.M) {
    57  	defer cluster.PanicHandler(nil)
    58  	flag.Parse()
    59  	exitCode := func() int {
    60  		clusterInstance = cluster.NewCluster(Cell, "localhost")
    61  		defer clusterInstance.Teardown()
    62  
    63  		// Start topo server
    64  		err := clusterInstance.StartTopo()
    65  		if err != nil {
    66  			return 1
    67  		}
    68  
    69  		// Start keyspace
    70  		keyspace := &cluster.Keyspace{
    71  			Name:      KeyspaceName,
    72  			SchemaSQL: SchemaSQL,
    73  			VSchema:   VSchema,
    74  		}
    75  		clusterInstance.VtGateExtraArgs = []string{"--schema_change_signal"}
    76  		clusterInstance.VtTabletExtraArgs = []string{"--queryserver-config-schema-change-signal", "--queryserver-config-schema-change-signal-interval", "0.1", "--queryserver-config-max-result-size", "100", "--queryserver-config-terse-errors"}
    77  		err = clusterInstance.StartKeyspace(*keyspace, []string{"-80", "80-"}, 0, false)
    78  		if err != nil {
    79  			return 1
    80  		}
    81  
    82  		err = clusterInstance.VtctlclientProcess.ApplyRoutingRules(routingRules)
    83  		if err != nil {
    84  			return 1
    85  		}
    86  
    87  		_, err = clusterInstance.VtctlclientProcess.ExecuteCommandWithOutput("RebuildVSchemaGraph")
    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 = clusterInstance.GetVTParams(KeyspaceName)
   100  
   101  		return m.Run()
   102  	}()
   103  	os.Exit(exitCode)
   104  }
   105  
   106  func start(t *testing.T) (*mysql.Conn, func()) {
   107  	ctx := context.Background()
   108  	conn, err := mysql.Connect(ctx, &vtParams)
   109  	require.NoError(t, err)
   110  
   111  	deleteAll := func() {
   112  		utils.Exec(t, conn, "use ks")
   113  		tables := []string{"t1", "t2", "vstream_test", "t3", "t4", "t6", "t7_xxhash", "t7_xxhash_idx", "t7_fk", "t8", "t9", "t9_id_to_keyspace_id_idx", "t10", "t10_id_to_keyspace_id_idx", "t1_id2_idx", "t2_id4_idx", "t3_id7_idx", "t4_id2_idx", "t5_null_vindex", "t6_id2_idx"}
   114  		for _, table := range tables {
   115  			_, _ = utils.ExecAllowError(t, conn, "delete from "+table)
   116  		}
   117  		utils.Exec(t, conn, "set workload = oltp")
   118  	}
   119  
   120  	deleteAll()
   121  
   122  	return conn, func() {
   123  		deleteAll()
   124  		conn.Close()
   125  		cluster.PanicHandler(t)
   126  	}
   127  }