vitess.io/vitess@v0.16.2/go/test/endtoend/tabletgateway/main_test.go (about)

     1  /*
     2  Copyright 2020 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 healthcheck
    18  
    19  import (
    20  	"flag"
    21  	"os"
    22  	"testing"
    23  
    24  	"vitess.io/vitess/go/mysql"
    25  	"vitess.io/vitess/go/test/endtoend/cluster"
    26  )
    27  
    28  var (
    29  	clusterInstance *cluster.LocalProcessCluster
    30  	vtParams        mysql.ConnParams
    31  	keyspaceName    = "commerce"
    32  	cell            = "zone1"
    33  	sqlSchema       = `create table product( 
    34  		sku varbinary(128),
    35  			description varbinary(128),
    36  			price bigint,
    37  			primary key(sku)
    38  		) ENGINE=InnoDB;
    39  		create table customer(
    40  			id bigint not null auto_increment,
    41  			email varchar(128),
    42  			primary key(id)
    43  		) ENGINE=InnoDB;
    44  		create table corder(
    45  			order_id bigint not null auto_increment,
    46  			customer_id bigint,
    47  			sku varbinary(128),
    48  			price bigint,
    49  			primary key(order_id)
    50  		) ENGINE=InnoDB;`
    51  
    52  	vSchema = `{
    53  						"tables": {
    54  							"product": {},
    55  							"customer": {},
    56  							"corder": {}
    57  						}
    58  					}`
    59  )
    60  
    61  func TestMain(m *testing.M) {
    62  	defer cluster.PanicHandler(nil)
    63  	flag.Parse()
    64  
    65  	exitCode := func() int {
    66  		clusterInstance = cluster.NewCluster(cell, "localhost")
    67  		clusterInstance.VtTabletExtraArgs = []string{"--health_check_interval", "1s"}
    68  		defer clusterInstance.Teardown()
    69  
    70  		// Start topo server
    71  		err := clusterInstance.StartTopo()
    72  		if err != nil {
    73  			return 1
    74  		}
    75  
    76  		// Start keyspace
    77  		keyspace := &cluster.Keyspace{
    78  			Name:      keyspaceName,
    79  			SchemaSQL: sqlSchema,
    80  			VSchema:   vSchema,
    81  		}
    82  		err = clusterInstance.StartUnshardedKeyspace(*keyspace, 1, true)
    83  		if err != nil {
    84  			return 1
    85  		}
    86  
    87  		vtgateInstance := clusterInstance.NewVtgateInstance()
    88  		// Start vtgate
    89  		err = vtgateInstance.Setup()
    90  		if err != nil {
    91  			return 1
    92  		}
    93  		// ensure it is torn down during cluster TearDown
    94  		clusterInstance.VtgateProcess = *vtgateInstance
    95  		vtParams = mysql.ConnParams{
    96  			Host: clusterInstance.Hostname,
    97  			Port: clusterInstance.VtgateMySQLPort,
    98  		}
    99  		return m.Run()
   100  	}()
   101  	os.Exit(exitCode)
   102  }