vitess.io/vitess@v0.16.2/go/test/endtoend/tabletmanager/tablet_test.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 tabletmanager
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  
    27  	"vitess.io/vitess/go/test/endtoend/cluster"
    28  	"vitess.io/vitess/go/vt/log"
    29  )
    30  
    31  // TestEnsureDB tests that vttablet creates the db as needed
    32  func TestEnsureDB(t *testing.T) {
    33  	defer cluster.PanicHandler(t)
    34  
    35  	// Create new tablet
    36  	tablet := clusterInstance.NewVttabletInstance("replica", 0, "")
    37  	tablet.MysqlctlProcess = *cluster.MysqlCtlProcessInstance(tablet.TabletUID, tablet.MySQLPort, clusterInstance.TmpDirectory)
    38  	err := tablet.MysqlctlProcess.Start()
    39  	require.NoError(t, err)
    40  
    41  	log.Info(fmt.Sprintf("Started vttablet %v", tablet))
    42  	// Start vttablet process as replica. It won't be able to serve because there's no db.
    43  	err = clusterInstance.StartVttablet(tablet, "NOT_SERVING", false, cell, "dbtest", hostname, "0")
    44  	require.NoError(t, err)
    45  
    46  	// Make it the primary.
    47  	err = clusterInstance.VtctlclientProcess.ExecuteCommand("TabletExternallyReparented", tablet.Alias)
    48  	require.EqualError(t, err, "exit status 1")
    49  
    50  	// It is still NOT_SERVING because the db is read-only.
    51  	assert.Equal(t, "NOT_SERVING", tablet.VttabletProcess.GetTabletStatus())
    52  	status := tablet.VttabletProcess.GetStatusDetails()
    53  	assert.Contains(t, status, "read-only")
    54  
    55  	// Switch to read-write and verify that we go serving.
    56  	// Note: for TabletExternallyReparented, we expect SetReadWrite to be called by the user
    57  	err = clusterInstance.VtctlclientProcess.ExecuteCommand("SetReadWrite", tablet.Alias)
    58  	require.NoError(t, err)
    59  	err = tablet.VttabletProcess.WaitForTabletStatus("SERVING")
    60  	require.NoError(t, err)
    61  	killTablets(t, tablet)
    62  }
    63  
    64  // TestResetReplicationParameters tests that the RPC ResetReplicationParameters works as intended.
    65  func TestResetReplicationParameters(t *testing.T) {
    66  	defer cluster.PanicHandler(t)
    67  
    68  	// Create new tablet
    69  	tablet := clusterInstance.NewVttabletInstance("replica", 0, "")
    70  	tablet.MysqlctlProcess = *cluster.MysqlCtlProcessInstance(tablet.TabletUID, tablet.MySQLPort, clusterInstance.TmpDirectory)
    71  	err := tablet.MysqlctlProcess.Start()
    72  	require.NoError(t, err)
    73  
    74  	log.Info(fmt.Sprintf("Started vttablet %v", tablet))
    75  	// Start vttablet process as replica. It won't be able to serve because there's no db.
    76  	err = clusterInstance.StartVttablet(tablet, "NOT_SERVING", false, cell, "dbtest", hostname, "0")
    77  	require.NoError(t, err)
    78  
    79  	// Set a replication source on the tablet and start replication
    80  	_, err = tablet.VttabletProcess.QueryTablet("stop slave;change master to master_host = 'localhost', master_port = 123;start slave;", keyspaceName, false)
    81  	require.NoError(t, err)
    82  
    83  	// Check the replica status.
    84  	res, err := tablet.VttabletProcess.QueryTablet("show slave status", keyspaceName, false)
    85  	require.NoError(t, err)
    86  	// This is expected to return 1 row result
    87  	require.Len(t, res.Rows, 1)
    88  
    89  	// Reset the replication parameters on the tablet
    90  	err = tmcResetReplicationParameters(context.Background(), tablet.GrpcPort)
    91  	require.NoError(t, err)
    92  
    93  	// Recheck the replica status and this time is should be empty
    94  	res, err = tablet.VttabletProcess.QueryTablet("show slave status", keyspaceName, false)
    95  	require.NoError(t, err)
    96  	require.Len(t, res.Rows, 0)
    97  }