vitess.io/vitess@v0.16.2/go/vt/vtgate/endtoend/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 endtoend
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os"
    23  	"testing"
    24  
    25  	_flag "vitess.io/vitess/go/internal/flag"
    26  	"vitess.io/vitess/go/mysql"
    27  	"vitess.io/vitess/go/sqltypes"
    28  	"vitess.io/vitess/go/vt/vttest"
    29  
    30  	vschemapb "vitess.io/vitess/go/vt/proto/vschema"
    31  	vttestpb "vitess.io/vitess/go/vt/proto/vttest"
    32  )
    33  
    34  var (
    35  	cluster     *vttest.LocalCluster
    36  	vtParams    mysql.ConnParams
    37  	mysqlParams mysql.ConnParams
    38  	grpcAddress string
    39  
    40  	schema = `
    41  create table t1(
    42  	id1 bigint,
    43  	id2 bigint,
    44  	primary key(id1)
    45  ) Engine=InnoDB;
    46  
    47  create table t1_copy_basic(
    48  	id1 bigint,
    49  	id2 bigint,
    50  	primary key(id1)
    51  ) Engine=InnoDB;
    52  
    53  create table t1_copy_resume(
    54  	id1 bigint,
    55  	id2 bigint,
    56  	primary key(id1)
    57  ) Engine=InnoDB;
    58  
    59  create table t1_id2_idx(
    60  	id2 bigint,
    61  	keyspace_id varbinary(10),
    62  	primary key(id2)
    63  ) Engine=InnoDB;
    64  
    65  create table vstream_test(
    66  	id bigint,
    67  	val bigint,
    68  	primary key(id)
    69  ) Engine=InnoDB;
    70  
    71  create table aggr_test(
    72  	id bigint,
    73  	val1 varchar(16),
    74  	val2 bigint,
    75  	primary key(id)
    76  ) Engine=InnoDB;
    77  
    78  create table t2(
    79  	id3 bigint,
    80  	id4 bigint,
    81  	primary key(id3)
    82  ) Engine=InnoDB;
    83  
    84  create table t2_id4_idx(
    85  	id bigint not null auto_increment,
    86  	id4 bigint,
    87  	id3 bigint,
    88  	primary key(id),
    89  	key idx_id4(id4)
    90  ) Engine=InnoDB;
    91  
    92  create table t1_last_insert_id(
    93  	id bigint not null auto_increment,
    94  	id1 bigint,
    95  	primary key(id)
    96  ) Engine=InnoDB;
    97  
    98  create table t1_row_count(
    99  	id bigint not null,
   100  	id1 bigint,
   101  	primary key(id)
   102  ) Engine=InnoDB;
   103  
   104  create table t1_sharded(
   105  	id1 bigint,
   106  	id2 bigint,
   107  	primary key(id1)
   108  ) Engine=InnoDB;
   109  `
   110  
   111  	vschema = &vschemapb.Keyspace{
   112  		Sharded: true,
   113  		Vindexes: map[string]*vschemapb.Vindex{
   114  			"hash": {
   115  				Type: "hash",
   116  			},
   117  			"t1_id2_vdx": {
   118  				Type: "consistent_lookup_unique",
   119  				Params: map[string]string{
   120  					"table": "t1_id2_idx",
   121  					"from":  "id2",
   122  					"to":    "keyspace_id",
   123  				},
   124  				Owner: "t1",
   125  			},
   126  			"t2_id4_idx": {
   127  				Type: "lookup_hash",
   128  				Params: map[string]string{
   129  					"table":      "t2_id4_idx",
   130  					"from":       "id4",
   131  					"to":         "id3",
   132  					"autocommit": "true",
   133  				},
   134  				Owner: "t2",
   135  			},
   136  		},
   137  		Tables: map[string]*vschemapb.Table{
   138  			"t1": {
   139  				ColumnVindexes: []*vschemapb.ColumnVindex{{
   140  					Column: "id1",
   141  					Name:   "hash",
   142  				}, {
   143  					Column: "id2",
   144  					Name:   "t1_id2_vdx",
   145  				}},
   146  			},
   147  			"t1_copy_basic": {
   148  				ColumnVindexes: []*vschemapb.ColumnVindex{{
   149  					Column: "id1",
   150  					Name:   "hash",
   151  				}},
   152  			},
   153  			"t1_copy_resume": {
   154  				ColumnVindexes: []*vschemapb.ColumnVindex{{
   155  					Column: "id1",
   156  					Name:   "hash",
   157  				}},
   158  			},
   159  			"t1_sharded": {
   160  				ColumnVindexes: []*vschemapb.ColumnVindex{{
   161  					Column: "id1",
   162  					Name:   "hash",
   163  				}},
   164  			},
   165  			"t1_id2_idx": {
   166  				ColumnVindexes: []*vschemapb.ColumnVindex{{
   167  					Column: "id2",
   168  					Name:   "hash",
   169  				}},
   170  			},
   171  			"t2": {
   172  				ColumnVindexes: []*vschemapb.ColumnVindex{{
   173  					Column: "id3",
   174  					Name:   "hash",
   175  				}, {
   176  					Column: "id4",
   177  					Name:   "t2_id4_idx",
   178  				}},
   179  			},
   180  			"t2_id4_idx": {
   181  				ColumnVindexes: []*vschemapb.ColumnVindex{{
   182  					Column: "id4",
   183  					Name:   "hash",
   184  				}},
   185  			},
   186  			"vstream_test": {
   187  				ColumnVindexes: []*vschemapb.ColumnVindex{{
   188  					Column: "id",
   189  					Name:   "hash",
   190  				}},
   191  			},
   192  			"aggr_test": {
   193  				ColumnVindexes: []*vschemapb.ColumnVindex{{
   194  					Column: "id",
   195  					Name:   "hash",
   196  				}},
   197  				Columns: []*vschemapb.Column{{
   198  					Name: "val1",
   199  					Type: sqltypes.VarChar,
   200  				}},
   201  			},
   202  			"t1_last_insert_id": {
   203  				ColumnVindexes: []*vschemapb.ColumnVindex{{
   204  					Column: "id1",
   205  					Name:   "hash",
   206  				}},
   207  				Columns: []*vschemapb.Column{{
   208  					Name: "id1",
   209  					Type: sqltypes.Int64,
   210  				}},
   211  			},
   212  			"t1_row_count": {
   213  				ColumnVindexes: []*vschemapb.ColumnVindex{{
   214  					Column: "id",
   215  					Name:   "hash",
   216  				}},
   217  			},
   218  		},
   219  	}
   220  )
   221  
   222  func TestMain(m *testing.M) {
   223  	_flag.ParseFlagsForTest()
   224  
   225  	exitCode := func() int {
   226  		var cfg vttest.Config
   227  		cfg.Topology = &vttestpb.VTTestTopology{
   228  			Keyspaces: []*vttestpb.Keyspace{{
   229  				Name: "ks",
   230  				Shards: []*vttestpb.Shard{{
   231  					Name: "-80",
   232  				}, {
   233  					Name: "80-",
   234  				}},
   235  			}},
   236  		}
   237  		if err := cfg.InitSchemas("ks", schema, vschema); err != nil {
   238  			fmt.Fprintf(os.Stderr, "%v\n", err)
   239  			os.RemoveAll(cfg.SchemaDir)
   240  			return 1
   241  		}
   242  		defer os.RemoveAll(cfg.SchemaDir)
   243  
   244  		cluster = &vttest.LocalCluster{
   245  			Config: cfg,
   246  		}
   247  		if err := cluster.Setup(); err != nil {
   248  			fmt.Fprintf(os.Stderr, "%v\n", err)
   249  			cluster.TearDown()
   250  			return 1
   251  		}
   252  		defer cluster.TearDown()
   253  
   254  		vtParams = mysql.ConnParams{
   255  			Host: "localhost",
   256  			Port: cluster.Env.PortForProtocol("vtcombo_mysql_port", ""),
   257  		}
   258  		mysqlParams = cluster.MySQLConnParams()
   259  		grpcAddress = fmt.Sprintf("localhost:%d", cluster.Env.PortForProtocol("vtcombo", "grpc"))
   260  
   261  		insertStartValue()
   262  
   263  		return m.Run()
   264  	}()
   265  	os.Exit(exitCode)
   266  }
   267  
   268  func insertStartValue() {
   269  	ctx := context.Background()
   270  	conn, err := mysql.Connect(ctx, &vtParams)
   271  	if err != nil {
   272  		panic(err)
   273  	}
   274  	defer conn.Close()
   275  
   276  	// lets insert a single starting value for tests
   277  	_, err = conn.ExecuteFetch("insert into t1_last_insert_id(id1) values(42)", 1000, true)
   278  	if err != nil {
   279  		panic(err)
   280  	}
   281  }