vitess.io/vitess@v0.16.2/go/vt/vtctl/endtoend/onlineddl_show_test.go (about)

     1  package endtoend
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/google/uuid"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"vitess.io/vitess/go/vt/logutil"
    13  	"vitess.io/vitess/go/vt/topo/memorytopo"
    14  	"vitess.io/vitess/go/vt/vtctl"
    15  	"vitess.io/vitess/go/vt/vtctl/grpcvtctldserver/testutil"
    16  	"vitess.io/vitess/go/vt/vttablet/tmclient"
    17  	"vitess.io/vitess/go/vt/vttablet/tmclienttest"
    18  	"vitess.io/vitess/go/vt/wrangler"
    19  
    20  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    21  )
    22  
    23  func TestShowOnlineDDL_All(t *testing.T) {
    24  	onlineDDLTest(t, []string{
    25  		"OnlineDDL", "testkeyspace", "show", "all",
    26  	}, "select\n\t\t\t\t*\n\t\t\t\tfrom _vt.schema_migrations where migration_uuid like '%'  order by `id` ASC ")
    27  }
    28  
    29  func TestShowOnlineDDL_Recent(t *testing.T) {
    30  	onlineDDLTest(t, []string{
    31  		"OnlineDDL", "testkeyspace", "show", "recent",
    32  	}, "select\n\t\t\t\t*\n\t\t\t\tfrom _vt.schema_migrations where requested_timestamp > now() - interval 1 week  order by `id` ASC ")
    33  }
    34  
    35  func TestShowOnlineDDL_ID(t *testing.T) {
    36  	onlineDDLTest(t, []string{
    37  		"OnlineDDL", "testkeyspace", "show", "82fa54ac_e83e_11ea_96b7_f875a4d24e90",
    38  	}, "select\n\t\t\t\t*\n\t\t\t\tfrom _vt.schema_migrations where migration_uuid='82fa54ac_e83e_11ea_96b7_f875a4d24e90'  order by `id` ASC ")
    39  }
    40  
    41  func TestShowOnlineDDL_Order_Descending(t *testing.T) {
    42  	onlineDDLTest(t, []string{
    43  		"OnlineDDL", "--order", "descending", "testkeyspace", "show", "all",
    44  	}, "select\n\t\t\t\t*\n\t\t\t\tfrom _vt.schema_migrations where migration_uuid like '%'  order by `id` DESC ")
    45  }
    46  
    47  func TestShowOnlineDDL_Order_Ascending(t *testing.T) {
    48  	onlineDDLTest(t, []string{
    49  		"OnlineDDL", "testkeyspace", "show", "all", "--order", "ascending",
    50  	}, "select\n\t\t\t\t*\n\t\t\t\tfrom _vt.schema_migrations where migration_uuid like '%'  order by `id` ASC ")
    51  }
    52  
    53  func TestShowOnlineDDL_Skip(t *testing.T) {
    54  	onlineDDLTest(t, []string{
    55  		"OnlineDDL", "--skip", "20", "--limit", "5", "testkeyspace", "show", "all",
    56  	}, "select\n\t\t\t\t*\n\t\t\t\tfrom _vt.schema_migrations where migration_uuid like '%'  order by `id` ASC LIMIT 20,5")
    57  }
    58  
    59  func TestShowOnlineDDL_Limit(t *testing.T) {
    60  	onlineDDLTest(t, []string{
    61  		"OnlineDDL", "--limit", "55", "testkeyspace", "show", "all",
    62  	}, "select\n\t\t\t\t*\n\t\t\t\tfrom _vt.schema_migrations where migration_uuid like '%'  order by `id` ASC LIMIT 0,55")
    63  }
    64  
    65  func TestShowOnlineDDL_Running(t *testing.T) {
    66  	onlineDDLTest(t, []string{
    67  		"OnlineDDL", "testkeyspace", "show", "running",
    68  	}, "select\n\t\t\t\t*\n\t\t\t\tfrom _vt.schema_migrations where migration_status='running'  order by `id` ASC ")
    69  }
    70  
    71  func TestShowOnlineDDL_Complete(t *testing.T) {
    72  	onlineDDLTest(t, []string{
    73  		"OnlineDDL", "testkeyspace", "show", "complete",
    74  	}, "select\n\t\t\t\t*\n\t\t\t\tfrom _vt.schema_migrations where migration_status='complete'  order by `id` ASC ")
    75  }
    76  
    77  func TestShowOnlineDDL_Failed(t *testing.T) {
    78  	onlineDDLTest(t, []string{
    79  		"OnlineDDL", "testkeyspace", "show", "failed",
    80  	}, "select\n\t\t\t\t*\n\t\t\t\tfrom _vt.schema_migrations where migration_status='failed'  order by `id` ASC ")
    81  }
    82  
    83  func TestShowOnlineDDL_Retry(t *testing.T) {
    84  	onlineDDLTest(t, []string{
    85  		"OnlineDDL", "testkeyspace", "retry", "82fa54ac_e83e_11ea_96b7_f875a4d24e90",
    86  	}, "update _vt.schema_migrations set migration_status='retry' where migration_uuid='82fa54ac_e83e_11ea_96b7_f875a4d24e90'")
    87  }
    88  
    89  func TestShowOnlineDDL_Cancel(t *testing.T) {
    90  	onlineDDLTest(t, []string{
    91  		"OnlineDDL", "testkeyspace", "cancel", "82fa54ac_e83e_11ea_96b7_f875a4d24e90",
    92  	}, "update _vt.schema_migrations set migration_status='cancel' where migration_uuid='82fa54ac_e83e_11ea_96b7_f875a4d24e90'")
    93  }
    94  
    95  func onlineDDLTest(t *testing.T, args []string, expectedQuery string) {
    96  	t.Helper()
    97  	ctx := context.Background()
    98  
    99  	fakeTopo := memorytopo.NewServer("zone1", "zone2", "zone3")
   100  
   101  	tablet := &topodatapb.Tablet{
   102  		Alias: &topodatapb.TabletAlias{
   103  			Cell: "zone1",
   104  			Uid:  uuid.New().ID(),
   105  		},
   106  		Hostname: "abcd",
   107  		Keyspace: "testkeyspace",
   108  		Shard:    "-",
   109  		Type:     topodatapb.TabletType_PRIMARY,
   110  	}
   111  	require.NoError(t, fakeTopo.CreateTablet(ctx, tablet))
   112  
   113  	tmc := testutil.TabletManagerClient{}
   114  	tmclient.RegisterTabletManagerClientFactory(t.Name(), func() tmclient.TabletManagerClient {
   115  		return &tmc
   116  	})
   117  	tmclienttest.SetProtocol("go.vt.vtctl.endtoend", t.Name())
   118  
   119  	logger := logutil.NewMemoryLogger()
   120  	wr := wrangler.New(logger, fakeTopo, &tmc)
   121  
   122  	err := vtctl.RunCommand(ctx, wr, args)
   123  	assert.Error(t, err)
   124  	assert.NotEmpty(t, err.Error())
   125  	containsExpectedError := false
   126  	expectedErrors := []string{
   127  		"unable to get shard names for keyspace",
   128  		"no ExecuteFetchAsDba results on fake TabletManagerClient",
   129  	}
   130  	for _, expect := range expectedErrors {
   131  		if strings.Contains(err.Error(), expect) {
   132  			containsExpectedError = true
   133  		}
   134  	}
   135  	assert.Truef(t, containsExpectedError, "expecting error <%v> to contain either of: %v", err.Error(), expectedErrors)
   136  }