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

     1  package endtoend
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"vitess.io/vitess/go/test/utils"
     8  
     9  	"github.com/google/uuid"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"vitess.io/vitess/go/json2"
    14  	"vitess.io/vitess/go/vt/logutil"
    15  	"vitess.io/vitess/go/vt/topo/memorytopo"
    16  	"vitess.io/vitess/go/vt/topo/topoproto"
    17  	"vitess.io/vitess/go/vt/vtctl"
    18  	"vitess.io/vitess/go/vt/vtctl/grpcvtctldserver/testutil"
    19  	"vitess.io/vitess/go/vt/vttablet/tmclient"
    20  	"vitess.io/vitess/go/vt/vttablet/tmclienttest"
    21  	"vitess.io/vitess/go/vt/wrangler"
    22  
    23  	querypb "vitess.io/vitess/go/vt/proto/query"
    24  	tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
    25  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    26  )
    27  
    28  func TestGetSchema(t *testing.T) {
    29  	ctx := context.Background()
    30  
    31  	topo := memorytopo.NewServer("zone1", "zone2", "zone3")
    32  
    33  	tablet := &topodatapb.Tablet{
    34  		Alias: &topodatapb.TabletAlias{
    35  			Cell: "zone1",
    36  			Uid:  uuid.New().ID(),
    37  		},
    38  		Hostname: "abcd",
    39  		Keyspace: "testkeyspace",
    40  		Shard:    "-",
    41  		Type:     topodatapb.TabletType_PRIMARY,
    42  	}
    43  	require.NoError(t, topo.CreateTablet(ctx, tablet))
    44  
    45  	sd := &tabletmanagerdatapb.SchemaDefinition{
    46  		TableDefinitions: []*tabletmanagerdatapb.TableDefinition{
    47  			{
    48  				Name:       "foo",
    49  				RowCount:   1000,
    50  				DataLength: 1000000,
    51  				Schema: `CREATE TABLE foo (
    52  	id INT(11) NOT NULL,
    53  	name VARCHAR(255) NOT NULL,
    54  	PRIMARY KEY(id)
    55  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
    56  				Columns: []string{
    57  					"id",
    58  					"name",
    59  				},
    60  				PrimaryKeyColumns: []string{
    61  					"id",
    62  				},
    63  				Fields: []*querypb.Field{
    64  					{
    65  						Name:         "id",
    66  						Type:         querypb.Type_INT32,
    67  						Table:        "foo",
    68  						OrgTable:     "foo",
    69  						Database:     "vt_testkeyspace",
    70  						OrgName:      "id",
    71  						ColumnLength: 11,
    72  						Charset:      63,
    73  						Decimals:     0,
    74  					},
    75  					{
    76  						Name:         "name",
    77  						Type:         querypb.Type_VARCHAR,
    78  						Table:        "foo",
    79  						OrgTable:     "foo",
    80  						Database:     "vt_testkeyspace",
    81  						OrgName:      "name",
    82  						ColumnLength: 1020,
    83  						Charset:      45,
    84  						Decimals:     0,
    85  					},
    86  				},
    87  			},
    88  			{
    89  				Name:       "bar",
    90  				RowCount:   1,
    91  				DataLength: 10,
    92  				Schema: `CREATE TABLE bar (
    93  	id INT(11) NOT NULL
    94  	foo_id INT(11) NOT NULL
    95  	is_active TINYINT(1) NOT NULL DEFAULT 1
    96  ) ENGINE=InnoDB`,
    97  				Columns: []string{
    98  					"id",
    99  					"foo_id",
   100  					"is_active",
   101  				},
   102  				PrimaryKeyColumns: []string{
   103  					"id",
   104  				},
   105  				Fields: []*querypb.Field{
   106  					{
   107  						Name:         "id",
   108  						Type:         querypb.Type_INT32,
   109  						Table:        "bar",
   110  						OrgTable:     "bar",
   111  						Database:     "vt_testkeyspace",
   112  						OrgName:      "id",
   113  						ColumnLength: 11,
   114  						Charset:      63,
   115  						Decimals:     0,
   116  					},
   117  					{
   118  						Name:         "foo_id",
   119  						Type:         querypb.Type_INT32,
   120  						Table:        "bar",
   121  						OrgTable:     "bar",
   122  						Database:     "vt_testkeyspace",
   123  						OrgName:      "foo_id",
   124  						ColumnLength: 11,
   125  						Charset:      63,
   126  						Decimals:     0,
   127  					},
   128  					{
   129  						Name:         "is_active",
   130  						Type:         querypb.Type_INT8,
   131  						Table:        "bar",
   132  						OrgTable:     "bar",
   133  						Database:     "vt_testkeyspace",
   134  						OrgName:      "is_active",
   135  						ColumnLength: 1,
   136  						Charset:      63,
   137  						Decimals:     0,
   138  					},
   139  				},
   140  			},
   141  		},
   142  	}
   143  
   144  	tmc := testutil.TabletManagerClient{
   145  		GetSchemaResults: map[string]struct {
   146  			Schema *tabletmanagerdatapb.SchemaDefinition
   147  			Error  error
   148  		}{
   149  			topoproto.TabletAliasString(tablet.Alias): {
   150  				Schema: sd,
   151  				Error:  nil,
   152  			},
   153  		},
   154  	}
   155  
   156  	tmclient.RegisterTabletManagerClientFactory(t.Name(), func() tmclient.TabletManagerClient {
   157  		return &tmc
   158  	})
   159  	tmclienttest.SetProtocol("go.vt.vtctl.endtoend", t.Name())
   160  
   161  	logger := logutil.NewMemoryLogger()
   162  
   163  	err := vtctl.RunCommand(ctx, wrangler.New(logger, topo, &tmc), []string{
   164  		"GetSchema",
   165  		topoproto.TabletAliasString(tablet.Alias),
   166  	})
   167  	require.NoError(t, err)
   168  
   169  	events := logger.Events
   170  	assert.Equal(t, 1, len(events), "expected 1 event from GetSchema")
   171  	val := events[0].Value
   172  
   173  	actual := &tabletmanagerdatapb.SchemaDefinition{}
   174  	err = json2.Unmarshal([]byte(val), actual)
   175  	require.NoError(t, err)
   176  
   177  	utils.MustMatch(t, sd, actual)
   178  
   179  	// reset for the next invocation, where we verify that passing
   180  	// --table_sizes_only does not include the create table statement or columns.
   181  	logger.Events = nil
   182  	sd = &tabletmanagerdatapb.SchemaDefinition{
   183  		TableDefinitions: []*tabletmanagerdatapb.TableDefinition{
   184  			{
   185  				Name:              "foo",
   186  				RowCount:          1000,
   187  				DataLength:        1000000,
   188  				Columns:           []string{},
   189  				PrimaryKeyColumns: []string{},
   190  				Fields:            []*querypb.Field{},
   191  			},
   192  			{
   193  				Name:              "bar",
   194  				RowCount:          1,
   195  				DataLength:        10,
   196  				Columns:           []string{},
   197  				PrimaryKeyColumns: []string{},
   198  				Fields:            []*querypb.Field{},
   199  			},
   200  		},
   201  	}
   202  
   203  	err = vtctl.RunCommand(ctx, wrangler.New(logger, topo, &tmc), []string{
   204  		"GetSchema",
   205  		"--table_sizes_only",
   206  		topoproto.TabletAliasString(tablet.Alias),
   207  	})
   208  	require.NoError(t, err)
   209  
   210  	events = logger.Events
   211  	assert.Equal(t, 1, len(events), "expected 1 event from GetSchema")
   212  	val = events[0].Value
   213  
   214  	actual = &tabletmanagerdatapb.SchemaDefinition{}
   215  	err = json2.Unmarshal([]byte(val), actual)
   216  	require.NoError(t, err)
   217  
   218  	utils.MustMatch(t, sd, actual)
   219  }