From 8d9b2e388b1990792bc057a8e93f6ab59a002b68 Mon Sep 17 00:00:00 2001 From: Harshit Gangal Date: Mon, 11 Mar 2024 16:36:50 +0530 Subject: [PATCH] test: add failing test on views in sharded keyspace Signed-off-by: Harshit Gangal --- go/test/endtoend/utils/utils.go | 16 +++++--- .../foreignkey/stress/fk_stress_test.go | 4 +- go/test/endtoend/vtgate/misc_test.go | 13 ------- .../endtoend/vtgate/queries/misc/main_test.go | 3 ++ .../endtoend/vtgate/queries/misc/misc_test.go | 38 +++++++++++++++++++ 5 files changed, 54 insertions(+), 20 deletions(-) diff --git a/go/test/endtoend/utils/utils.go b/go/test/endtoend/utils/utils.go index 468841c23f6..d94da27377e 100644 --- a/go/test/endtoend/utils/utils.go +++ b/go/test/endtoend/utils/utils.go @@ -280,17 +280,23 @@ func WaitForKsError(t *testing.T, vtgateProcess cluster.VtgateProcess, ks string var ok bool errString, ok = ksErr.(string) return ok - }) + }, "Waiting for error") return errString } // WaitForVschemaCondition waits for the condition to be true -func WaitForVschemaCondition(t *testing.T, vtgateProcess cluster.VtgateProcess, ks string, conditionMet func(t *testing.T, keyspace map[string]interface{}) bool) { +func WaitForVschemaCondition( + t *testing.T, + vtgateProcess cluster.VtgateProcess, + ks string, + conditionMet func(t *testing.T, keyspace map[string]interface{}) bool, + message string, +) { timeout := time.After(60 * time.Second) for { select { case <-timeout: - t.Fatalf("schema tracking did not met the condition within the time for keyspace: %s", ks) + t.Fatalf("schema tracking did not met the condition within the time for keyspace: %s\n%s", ks, message) default: res, err := vtgateProcess.ReadVSchema() require.NoError(t, err, res) @@ -305,12 +311,12 @@ func WaitForVschemaCondition(t *testing.T, vtgateProcess cluster.VtgateProcess, } // WaitForTableDeletions waits for a table to be deleted -func WaitForTableDeletions(ctx context.Context, t *testing.T, vtgateProcess cluster.VtgateProcess, ks, tbl string) { +func WaitForTableDeletions(t *testing.T, vtgateProcess cluster.VtgateProcess, ks, tbl string) { WaitForVschemaCondition(t, vtgateProcess, ks, func(t *testing.T, keyspace map[string]interface{}) bool { tablesMap := keyspace["tables"] _, isPresent := convertToMap(tablesMap)[tbl] return !isPresent - }) + }, "Waiting for table to be deleted") } // WaitForColumn waits for a table's column to be present diff --git a/go/test/endtoend/vtgate/foreignkey/stress/fk_stress_test.go b/go/test/endtoend/vtgate/foreignkey/stress/fk_stress_test.go index 341db836fd8..13882802f40 100644 --- a/go/test/endtoend/vtgate/foreignkey/stress/fk_stress_test.go +++ b/go/test/endtoend/vtgate/foreignkey/stress/fk_stress_test.go @@ -786,10 +786,10 @@ func createInitialSchema(t *testing.T, tcase *testCase) { } }) t.Run("waiting for vschema deletions to apply", func(t *testing.T) { - timeoutCtx, cancel := context.WithTimeout(ctx, 1*time.Minute) + _, cancel := context.WithTimeout(ctx, 1*time.Minute) defer cancel() for _, tableName := range tableNames { - utils.WaitForTableDeletions(timeoutCtx, t, clusterInstance.VtgateProcess, keyspaceName, tableName) + utils.WaitForTableDeletions(t, clusterInstance.VtgateProcess, keyspaceName, tableName) } }) t.Run("creating tables", func(t *testing.T) { diff --git a/go/test/endtoend/vtgate/misc_test.go b/go/test/endtoend/vtgate/misc_test.go index 4d529da5d17..128d930718c 100644 --- a/go/test/endtoend/vtgate/misc_test.go +++ b/go/test/endtoend/vtgate/misc_test.go @@ -321,19 +321,6 @@ func TestCreateIndex(t *testing.T) { utils.Exec(t, conn, `create index i2 on ks.t1000 (id1)`) } -func TestCreateView(t *testing.T) { - // The test wont work since we cant change the vschema without reloading the vtgate. - t.Skip() - conn, closer := start(t) - defer closer() - // Test that create view works and the output is as expected - utils.Exec(t, conn, `create view v1 as select * from t1`) - utils.Exec(t, conn, `insert into t1(id1, id2) values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)`) - // This wont work, since ALTER VSCHEMA ADD TABLE is only supported for unsharded keyspaces - utils.Exec(t, conn, "alter vschema add table v1") - utils.AssertMatches(t, conn, "select * from v1", `[[INT64(1) INT64(1)] [INT64(2) INT64(2)] [INT64(3) INT64(3)] [INT64(4) INT64(4)] [INT64(5) INT64(5)]]`) -} - func TestVersions(t *testing.T) { conn, closer := start(t) defer closer() diff --git a/go/test/endtoend/vtgate/queries/misc/main_test.go b/go/test/endtoend/vtgate/queries/misc/main_test.go index a3858284884..f20072031a8 100644 --- a/go/test/endtoend/vtgate/queries/misc/main_test.go +++ b/go/test/endtoend/vtgate/queries/misc/main_test.go @@ -73,6 +73,9 @@ func TestMain(m *testing.M) { return 1 } + clusterInstance.VtGateExtraArgs = append(clusterInstance.VtGateExtraArgs, "--enable-views") + clusterInstance.VtTabletExtraArgs = append(clusterInstance.VtTabletExtraArgs, "--queryserver-enable-views") + // Start keyspace keyspace := &cluster.Keyspace{ Name: keyspaceName, diff --git a/go/test/endtoend/vtgate/queries/misc/misc_test.go b/go/test/endtoend/vtgate/queries/misc/misc_test.go index ce7f83916ab..0d95bcfa1ac 100644 --- a/go/test/endtoend/vtgate/queries/misc/misc_test.go +++ b/go/test/endtoend/vtgate/queries/misc/misc_test.go @@ -371,3 +371,41 @@ func TestAliasesInOuterJoinQueries(t *testing.T) { mcmp.AssertMatches("select t1.id1 as t0, t1.id1 as t1, tbl.unq_col as col from t1 left outer join tbl on t1.id2 = tbl.nonunq_col order by t1.id2 limit 2", `[[INT64(1) INT64(1) INT64(42)] [INT64(42) INT64(42) NULL]]`) mcmp.ExecWithColumnCompare("select t1.id1 as t0, t1.id1 as t1, tbl.unq_col as col from t1 left outer join tbl on t1.id2 = tbl.nonunq_col order by t1.id2 limit 2") } + +func TestAlterTableWithView(t *testing.T) { + mcmp, closer := start(t) + defer closer() + + // Test that create view works and the output is as expected + mcmp.Exec(`use ks_misc`) + mcmp.Exec(`create view v1 as select * from t1`) + var viewDef string + utils.WaitForVschemaCondition(t, clusterInstance.VtgateProcess, keyspaceName, func(t *testing.T, ksMap map[string]interface{}) bool { + views, ok := ksMap["views"] + if !ok { + return false + } + viewsMap := views.(map[string]interface{}) + view, ok := viewsMap["v1"] + if ok { + viewDef = view.(string) + } + return ok + }, "Waiting for view creation") + mcmp.Exec(`insert into t1(id1, id2) values (1, 1)`) + mcmp.AssertMatches("select * from v1", `[[INT64(1) INT64(1)]]`) + + // alter table + mcmp.Exec(`alter table t1 add column test bigint`) + mcmp.Exec(`alter view v1 as select * from t1`) + + utils.WaitForVschemaCondition(t, clusterInstance.VtgateProcess, keyspaceName, func(t *testing.T, ksMap map[string]interface{}) bool { + // wait for the view definition to change + views := ksMap["views"] + viewsMap := views.(map[string]interface{}) + view := viewsMap["v1"] + return view.(string) != viewDef + }, "Waiting for alter view") + + mcmp.AssertMatches("select * from v1", `[[INT64(1) INT64(1) NULL]]`) +}