vitess.io/vitess@v0.16.2/go/test/endtoend/recovery/recovery_util.go (about)

     1  /*
     2  Copyright 2020 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 recovery
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  
    28  	"vitess.io/vitess/go/test/endtoend/cluster"
    29  	"vitess.io/vitess/go/vt/vtgate/vtgateconn"
    30  )
    31  
    32  var (
    33  	dbPassword = "VtDbaPass"
    34  
    35  	// UseXb flag to use extra backup for recovery teseting.
    36  	UseXb = false
    37  	// XbArgs are the arguments for specifying xtrabackup.
    38  	XbArgs = []string{
    39  		"--backup_engine_implementation", "xtrabackup",
    40  		"--xtrabackup_stream_mode=xbstream",
    41  		"--xtrabackup_user=vt_dba",
    42  		"--xtrabackup_backup_flags", fmt.Sprintf("--password=%s", dbPassword),
    43  	}
    44  )
    45  
    46  // VerifyQueriesUsingVtgate verifies queries using vtgate.
    47  func VerifyQueriesUsingVtgate(t *testing.T, session *vtgateconn.VTGateSession, query string, value string) {
    48  	qr, err := session.Execute(context.Background(), query, nil)
    49  	require.Nil(t, err)
    50  	assert.Equal(t, value, fmt.Sprintf("%v", qr.Rows[0][0]))
    51  }
    52  
    53  // RestoreTablet performs a PITR restore.
    54  func RestoreTablet(t *testing.T, localCluster *cluster.LocalProcessCluster, tablet *cluster.Vttablet, restoreKSName string, shardName string, keyspaceName string, commonTabletArg []string) {
    55  	tablet.ValidateTabletRestart(t)
    56  	replicaTabletArgs := commonTabletArg
    57  
    58  	_, err := localCluster.VtctlProcess.ExecuteCommandWithOutput("GetKeyspace", restoreKSName)
    59  
    60  	if err != nil {
    61  		tm := time.Now().UTC()
    62  		_, err := localCluster.VtctlProcess.ExecuteCommandWithOutput("CreateKeyspace", "--",
    63  			"--keyspace_type=SNAPSHOT", "--base_keyspace="+keyspaceName,
    64  			"--snapshot_time", tm.Format(time.RFC3339), restoreKSName)
    65  		require.Nil(t, err)
    66  	}
    67  
    68  	if UseXb {
    69  		replicaTabletArgs = append(replicaTabletArgs, XbArgs...)
    70  	}
    71  	replicaTabletArgs = append(replicaTabletArgs, "--disable_active_reparents",
    72  		"--enable_replication_reporter=false",
    73  		"--init_tablet_type", "replica",
    74  		"--init_keyspace", restoreKSName,
    75  		"--init_shard", shardName,
    76  		"--init_db_name_override", "vt_"+keyspaceName,
    77  	)
    78  	tablet.VttabletProcess.SupportsBackup = true
    79  	tablet.VttabletProcess.ExtraArgs = replicaTabletArgs
    80  
    81  	tablet.VttabletProcess.ServingStatus = ""
    82  	err = tablet.VttabletProcess.Setup()
    83  	require.Nil(t, err)
    84  
    85  	err = tablet.VttabletProcess.WaitForTabletStatusesForTimeout([]string{"SERVING"}, 20*time.Second)
    86  	require.Nil(t, err)
    87  }
    88  
    89  // InsertData inserts data.
    90  func InsertData(t *testing.T, tablet *cluster.Vttablet, index int, keyspaceName string) {
    91  	_, err := tablet.VttabletProcess.QueryTablet(fmt.Sprintf("insert into vt_insert_test (id, msg) values (%d, 'test %d')", index, index), keyspaceName, true)
    92  	require.Nil(t, err)
    93  }