vitess.io/vitess@v0.16.2/go/test/endtoend/utils/mysql_test.go (about)

     1  /*
     2  Copyright 2022 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 utils
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/require"
    26  
    27  	"vitess.io/vitess/go/mysql"
    28  	"vitess.io/vitess/go/test/endtoend/cluster"
    29  )
    30  
    31  var (
    32  	clusterInstance *cluster.LocalProcessCluster
    33  	mysqlParams     mysql.ConnParams
    34  	keyspaceName    = "ks"
    35  	cell            = "test"
    36  	schemaSQL       = `create table t1(
    37  		id1 bigint,
    38  		id2 bigint,
    39  		id3 bigint,
    40  		primary key(id1)
    41  	) Engine=InnoDB;`
    42  )
    43  
    44  func TestMain(m *testing.M) {
    45  	defer cluster.PanicHandler(nil)
    46  
    47  	exitCode := func() int {
    48  		clusterInstance = cluster.NewCluster(cell, "localhost")
    49  		defer clusterInstance.Teardown()
    50  
    51  		conn, closer, err := NewMySQL(clusterInstance, keyspaceName, schemaSQL)
    52  		if err != nil {
    53  			fmt.Println(err)
    54  			return 1
    55  		}
    56  		defer closer()
    57  		mysqlParams = conn
    58  		return m.Run()
    59  	}()
    60  	os.Exit(exitCode)
    61  }
    62  
    63  func TestCreateMySQL(t *testing.T) {
    64  	ctx := context.Background()
    65  	conn, err := mysql.Connect(ctx, &mysqlParams)
    66  	require.NoError(t, err)
    67  
    68  	AssertMatches(t, conn, "show databases;", `[[VARCHAR("information_schema")] [VARCHAR("ks")] [VARCHAR("mysql")] [VARCHAR("performance_schema")] [VARCHAR("sys")]]`)
    69  	AssertMatches(t, conn, "show tables;", `[[VARCHAR("t1")]]`)
    70  	Exec(t, conn, "insert into t1(id1, id2, id3) values (1, 1, 1), (2, 2, 2), (3, 3, 3)")
    71  	AssertMatches(t, conn, "select * from t1;", `[[INT64(1) INT64(1) INT64(1)] [INT64(2) INT64(2) INT64(2)] [INT64(3) INT64(3) INT64(3)]]`)
    72  }