vitess.io/vitess@v0.16.2/go/mysql/collations/integration/main_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 integration
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os"
    23  	"os/signal"
    24  	"strings"
    25  	"testing"
    26  
    27  	"github.com/spf13/pflag"
    28  
    29  	_flag "vitess.io/vitess/go/internal/flag"
    30  	"vitess.io/vitess/go/mysql"
    31  	"vitess.io/vitess/go/vt/vttest"
    32  
    33  	vttestpb "vitess.io/vitess/go/vt/proto/vttest"
    34  )
    35  
    36  var (
    37  	connParams mysql.ConnParams
    38  	waitmysql  bool
    39  )
    40  
    41  func init() {
    42  	pflag.BoolVar(&waitmysql, "waitmysql", waitmysql, "")
    43  }
    44  
    45  func mysqlconn(t *testing.T) *mysql.Conn {
    46  	conn, err := mysql.Connect(context.Background(), &connParams)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	if !strings.HasPrefix(conn.ServerVersion, "8.0.") {
    51  		conn.Close()
    52  		t.Skipf("collation integration tests are only supported in MySQL 8.0+")
    53  	}
    54  	if strings.HasPrefix(conn.ServerVersion, "8.0.27") {
    55  		conn.Close()
    56  		t.Fatalf("MySQL 8.0.27 is UNSUPPORTED for integration testing because of a behavior regression; " +
    57  			"please update to 8.0.28, or rollback to a previous 8.0 version. See: MySQL bug #33117410.")
    58  	}
    59  	return conn
    60  }
    61  
    62  func TestMain(m *testing.M) {
    63  	_flag.TrickGlog()
    64  	_flag.ParseFlagsForTest()
    65  	pflag.Parse()
    66  
    67  	exitCode := func() int {
    68  		// Launch MySQL.
    69  		// We need a Keyspace in the topology, so the DbName is set.
    70  		// We need a Shard too, so the database 'vttest' is created.
    71  		cfg := vttest.Config{
    72  			Topology: &vttestpb.VTTestTopology{
    73  				Keyspaces: []*vttestpb.Keyspace{
    74  					{
    75  						Name: "vttest",
    76  						Shards: []*vttestpb.Shard{
    77  							{
    78  								Name:           "0",
    79  								DbNameOverride: "vttest",
    80  							},
    81  						},
    82  					},
    83  				},
    84  			},
    85  			OnlyMySQL: true,
    86  			Charset:   "utf8mb4",
    87  		}
    88  		cluster := vttest.LocalCluster{
    89  			Config: cfg,
    90  		}
    91  		if err := cluster.Setup(); err != nil {
    92  			fmt.Fprintf(os.Stderr, "could not launch mysql: %v\n", err)
    93  			return 1
    94  		}
    95  		defer cluster.TearDown()
    96  
    97  		connParams = cluster.MySQLConnParams()
    98  
    99  		if waitmysql {
   100  			debugMysql()
   101  		}
   102  		return m.Run()
   103  	}()
   104  	os.Exit(exitCode)
   105  }
   106  
   107  func debugMysql() {
   108  	fmt.Fprintf(os.Stderr, "Connect to MySQL using parameters: mysql -u %s -D %s -S %s\n",
   109  		connParams.Uname, connParams.DbName, connParams.UnixSocket)
   110  	fmt.Fprintf(os.Stderr, "Press ^C to resume testing...\n")
   111  
   112  	sigchan := make(chan os.Signal, 1)
   113  	signal.Notify(sigchan, os.Interrupt)
   114  	<-sigchan
   115  	signal.Stop(sigchan)
   116  
   117  	fmt.Fprintf(os.Stderr, "Resuming!\n")
   118  }