vitess.io/vitess@v0.16.2/go/test/endtoend/clustertest/add_keyspace_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  This adds sharded keyspace dynamically in this test only and test sql insert, select
    17  */
    18  
    19  package clustertest
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"testing"
    25  
    26  	"vitess.io/vitess/go/test/endtoend/utils"
    27  
    28  	"vitess.io/vitess/go/vt/log"
    29  
    30  	"vitess.io/vitess/go/mysql"
    31  	"vitess.io/vitess/go/test/endtoend/cluster"
    32  )
    33  
    34  var (
    35  	testKeyspace = &cluster.Keyspace{
    36  		Name: "kstest",
    37  		SchemaSQL: `create table vt_user (
    38  id bigint,
    39  name varchar(64),
    40  primary key (id)
    41  ) Engine=InnoDB`,
    42  		VSchema: `{
    43   "sharded": true,
    44   "vindexes": {
    45     "hash_index": {
    46       "type": "hash"
    47     }
    48   },
    49   "tables": {
    50     "vt_user": {
    51       "column_vindexes": [
    52         {
    53           "column": "id",
    54           "name": "hash_index"
    55         }
    56       ]
    57     }
    58   }
    59  }`,
    60  	}
    61  )
    62  
    63  func TestAddKeyspace(t *testing.T) {
    64  	defer cluster.PanicHandler(t)
    65  	if err := clusterInstance.StartKeyspace(*testKeyspace, []string{"-80", "80-"}, 0, false); err != nil {
    66  		log.Errorf("failed to AddKeyspace %v: %v", *testKeyspace, err)
    67  		t.Fatal(err)
    68  	}
    69  	// Restart vtgate process
    70  	_ = clusterInstance.VtgateProcess.TearDown()
    71  	_ = clusterInstance.VtgateProcess.Setup()
    72  	clusterInstance.WaitForTabletsToHealthyInVtgate()
    73  
    74  	ctx := context.Background()
    75  	vtParams := mysql.ConnParams{
    76  		Host: clusterInstance.Hostname,
    77  		Port: clusterInstance.VtgateMySQLPort,
    78  	}
    79  	conn, err := mysql.Connect(ctx, &vtParams)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  	defer conn.Close()
    84  
    85  	utils.Exec(t, conn, "insert into vt_user(id, name) values(1,'name1')")
    86  
    87  	qr := utils.Exec(t, conn, "select id, name from vt_user")
    88  	if got, want := fmt.Sprintf("%v", qr.Rows), `[[INT64(1) VARCHAR("name1")]]`; got != want {
    89  		t.Errorf("select:\n%v want\n%v", got, want)
    90  	}
    91  }