vitess.io/vitess@v0.16.2/go/test/endtoend/vtgate/schematracker/unsharded/st_unsharded_test.go (about)

     1  /*
     2  Copyright 2021 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 unsharded
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"os"
    23  	"testing"
    24  	"time"
    25  
    26  	"vitess.io/vitess/go/test/endtoend/utils"
    27  
    28  	"github.com/stretchr/testify/require"
    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            = "zone1"
    39  	sqlSchema       = `
    40  		create table main (
    41  			id bigint,
    42  			val varchar(128),
    43  			primary key(id)
    44  		) Engine=InnoDB;
    45  `
    46  )
    47  
    48  func TestMain(m *testing.M) {
    49  	defer cluster.PanicHandler(nil)
    50  	flag.Parse()
    51  
    52  	exitCode := func() int {
    53  		clusterInstance = cluster.NewCluster(cell, "localhost")
    54  		defer clusterInstance.Teardown()
    55  
    56  		// Start topo server
    57  		err := clusterInstance.StartTopo()
    58  		if err != nil {
    59  			return 1
    60  		}
    61  
    62  		// Start keyspace
    63  		keyspace := &cluster.Keyspace{
    64  			Name:      keyspaceName,
    65  			SchemaSQL: sqlSchema,
    66  		}
    67  		clusterInstance.VtTabletExtraArgs = []string{"--queryserver-config-schema-change-signal", "--queryserver-config-schema-change-signal-interval", "0.1"}
    68  		err = clusterInstance.StartUnshardedKeyspace(*keyspace, 0, false)
    69  		if err != nil {
    70  			return 1
    71  		}
    72  
    73  		// Start vtgate
    74  		clusterInstance.VtGateExtraArgs = []string{"--schema_change_signal", "--vschema_ddl_authorized_users", "%"}
    75  		err = clusterInstance.StartVtgate()
    76  		if err != nil {
    77  			return 1
    78  		}
    79  
    80  		vtParams = mysql.ConnParams{
    81  			Host: clusterInstance.Hostname,
    82  			Port: clusterInstance.VtgateMySQLPort,
    83  		}
    84  		return m.Run()
    85  	}()
    86  	os.Exit(exitCode)
    87  }
    88  
    89  func TestNewUnshardedTable(t *testing.T) {
    90  	defer cluster.PanicHandler(t)
    91  
    92  	// create a sql connection
    93  	ctx := context.Background()
    94  	conn, err := mysql.Connect(ctx, &vtParams)
    95  	require.NoError(t, err)
    96  	defer conn.Close()
    97  
    98  	vtgateVersion, err := cluster.GetMajorVersion("vtgate")
    99  	require.NoError(t, err)
   100  	expected := `[[VARCHAR("dual")] [VARCHAR("main")]]`
   101  	if vtgateVersion >= 17 {
   102  		expected = `[[VARCHAR("main")]]`
   103  	}
   104  
   105  	// ensuring our initial table "main" is in the schema
   106  	utils.AssertMatchesWithTimeout(t, conn,
   107  		"SHOW VSCHEMA TABLES",
   108  		expected,
   109  		100*time.Millisecond,
   110  		3*time.Second,
   111  		"initial table list not complete")
   112  
   113  	// create a new table which is not part of the VSchema
   114  	utils.Exec(t, conn, `create table new_table_tracked(id bigint, name varchar(100), primary key(id)) Engine=InnoDB`)
   115  
   116  	expected = `[[VARCHAR("dual")] [VARCHAR("main")] [VARCHAR("new_table_tracked")]]`
   117  	if vtgateVersion >= 17 {
   118  		expected = `[[VARCHAR("main")] [VARCHAR("new_table_tracked")]]`
   119  	}
   120  
   121  	// waiting for the vttablet's schema_reload interval to kick in
   122  	utils.AssertMatchesWithTimeout(t, conn,
   123  		"SHOW VSCHEMA TABLES",
   124  		expected,
   125  		100*time.Millisecond,
   126  		3*time.Second,
   127  		"new_table_tracked not in vschema tables")
   128  
   129  	utils.AssertMatches(t, conn, "select id from new_table_tracked", `[]`)              // select
   130  	utils.AssertMatches(t, conn, "select id from new_table_tracked where id = 5", `[]`) // select
   131  	// DML on new table
   132  	// insert initial data ,update and delete for the new table
   133  	utils.Exec(t, conn, `insert into new_table_tracked(id) values(0),(1)`)
   134  	utils.Exec(t, conn, `update new_table_tracked set name = "newName1"`)
   135  	utils.Exec(t, conn, "delete from new_table_tracked where id = 0")
   136  	utils.AssertMatches(t, conn, `select * from new_table_tracked`, `[[INT64(1) VARCHAR("newName1")]]`)
   137  
   138  	utils.Exec(t, conn, `drop table new_table_tracked`)
   139  
   140  	// waiting for the vttablet's schema_reload interval to kick in
   141  	expected = `[[VARCHAR("dual")] [VARCHAR("main")]]`
   142  	if vtgateVersion >= 17 {
   143  		expected = `[[VARCHAR("main")]]`
   144  	}
   145  	utils.AssertMatchesWithTimeout(t, conn,
   146  		"SHOW VSCHEMA TABLES",
   147  		expected,
   148  		100*time.Millisecond,
   149  		3*time.Second,
   150  		"new_table_tracked not in vschema tables")
   151  }