vitess.io/vitess@v0.16.2/go/test/endtoend/onlineddl/vttablet_util.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 onlineddl
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"vitess.io/vitess/go/mysql"
    24  	"vitess.io/vitess/go/sqltypes"
    25  	"vitess.io/vitess/go/vt/sqlparser"
    26  
    27  	"vitess.io/vitess/go/test/endtoend/cluster"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  // WaitForVReplicationStatus waits for a vreplication stream to be in one of given states, or timeout
    34  func WaitForVReplicationStatus(t *testing.T, vtParams *mysql.ConnParams, tablet *cluster.Vttablet, uuid string, timeout time.Duration, expectStatuses ...string) (status string) {
    35  
    36  	query, err := sqlparser.ParseAndBind("select state from _vt.vreplication where workflow=%a",
    37  		sqltypes.StringBindVariable(uuid),
    38  	)
    39  	require.NoError(t, err)
    40  
    41  	statusesMap := map[string]bool{}
    42  	for _, status := range expectStatuses {
    43  		statusesMap[status] = true
    44  	}
    45  	startTime := time.Now()
    46  	lastKnownStatus := ""
    47  	for time.Since(startTime) < timeout {
    48  		r, err := tablet.VttabletProcess.QueryTablet(query, "", true)
    49  		require.NoError(t, err)
    50  
    51  		if row := r.Named().Row(); row != nil {
    52  			lastKnownStatus, err = row.ToString("state")
    53  			assert.NoError(t, err)
    54  			if statusesMap[lastKnownStatus] {
    55  				return lastKnownStatus
    56  			}
    57  		}
    58  		time.Sleep(1 * time.Second)
    59  	}
    60  	return lastKnownStatus
    61  }
    62  
    63  func GetMySQLVersion(t *testing.T, tablet *cluster.Vttablet) string {
    64  	query := `select @@version as version`
    65  	rs, err := tablet.VttabletProcess.QueryTablet(query, "", true)
    66  	assert.NoError(t, err)
    67  	row := rs.Named().Row()
    68  	assert.NotNil(t, row)
    69  	version := row["version"].ToString()
    70  	assert.NotEmpty(t, row)
    71  	return version
    72  }