-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andres Taylor <[email protected]>
- Loading branch information
Showing
5 changed files
with
314 additions
and
36 deletions.
There are no files selected for viewing
162 changes: 162 additions & 0 deletions
162
go/test/endtoend/vtgate/queries/plan_tests/plan_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package plan_tests | ||
|
||
import ( | ||
_ "embed" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"vitess.io/vitess/go/mysql" | ||
"vitess.io/vitess/go/test/endtoend/cluster" | ||
"vitess.io/vitess/go/test/endtoend/utils" | ||
"vitess.io/vitess/go/vt/vtgate/planbuilder" | ||
) | ||
|
||
func readJSONTests(filename string) []planbuilder.PlanTest { | ||
var output []planbuilder.PlanTest | ||
file, err := os.Open(locateFile(filename)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
dec := json.NewDecoder(file) | ||
err = dec.Decode(&output) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return output | ||
} | ||
|
||
func locateFile(name string) string { | ||
return "../../../../../vt/vtgate/planbuilder/testdata/" + name | ||
} | ||
|
||
func TestPlan(t *testing.T) { | ||
mcmp, closer := start(t) | ||
defer closer() | ||
tests := readJSONTests("select_cases.json") | ||
for _, test := range tests { | ||
mcmp.Run(test.Query, func(mcmp *utils.MySQLCompare) { | ||
mcmp.Exec(test.Query) | ||
}) | ||
} | ||
} | ||
|
||
var ( | ||
clusterInstance *cluster.LocalProcessCluster | ||
vtParams mysql.ConnParams | ||
mysqlParams mysql.ConnParams | ||
keyspaceName = "user" | ||
cell = "test_aggr" | ||
) | ||
|
||
func readFile(filename string) string { | ||
schema, err := os.ReadFile(locateFile(filename)) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
return string(schema) | ||
} | ||
|
||
func start(t *testing.T) (utils.MySQLCompare, func()) { | ||
mcmp, err := utils.NewMySQLCompare(t, vtParams, mysqlParams) | ||
require.NoError(t, err) | ||
return mcmp, func() { | ||
mcmp.Close() | ||
} | ||
} | ||
|
||
func extractUserKS(jsonString string) string { | ||
var result map[string]any | ||
if err := json.Unmarshal([]byte(jsonString), &result); err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
keyspaces, ok := result["keyspaces"].(map[string]any) | ||
if !ok { | ||
panic("Keyspaces not found") | ||
} | ||
|
||
user, ok := keyspaces["user"].(map[string]any) | ||
if !ok { | ||
panic("User keyspaces not found") | ||
} | ||
|
||
tables, ok := user["tables"].(map[string]any) | ||
if !ok { | ||
panic("Tables not found") | ||
} | ||
|
||
userTbl, ok := tables["user"].(map[string]any) | ||
if !ok { | ||
panic("User table not found") | ||
} | ||
|
||
delete(userTbl, "auto_increment") // TODO: we should have an unsharded keyspace where this could live | ||
|
||
// Marshal the inner part back to JSON string | ||
userJson, err := json.Marshal(user) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
return string(userJson) | ||
} | ||
|
||
func TestMain(m *testing.M) { | ||
defer cluster.PanicHandler(nil) | ||
|
||
schemaSQL := readFile("vschemas/schema.sql") | ||
vschema := extractUserKS(readFile("vschemas/schema.json")) | ||
|
||
exitCode := func() int { | ||
clusterInstance = cluster.NewCluster(cell, "localhost") | ||
defer clusterInstance.Teardown() | ||
|
||
// Start topo server | ||
err := clusterInstance.StartTopo() | ||
if err != nil { | ||
return 1 | ||
} | ||
|
||
// Start keyspace | ||
keyspace := &cluster.Keyspace{ | ||
Name: keyspaceName, | ||
SchemaSQL: schemaSQL, | ||
VSchema: vschema, | ||
} | ||
err = clusterInstance.StartKeyspace(*keyspace, []string{"-80", "80-"}, 0, false) | ||
if err != nil { | ||
return 1 | ||
} | ||
|
||
// TODO: (@GuptaManan100/@systay): Also run the tests with normalizer on. | ||
clusterInstance.VtGateExtraArgs = append(clusterInstance.VtGateExtraArgs, | ||
"--normalize_queries=false", | ||
"--schema_change_signal=false", | ||
) | ||
|
||
// Start vtgate | ||
err = clusterInstance.StartVtgate() | ||
if err != nil { | ||
return 1 | ||
} | ||
|
||
vtParams = clusterInstance.GetVTParams(keyspaceName) | ||
|
||
// create mysql instance and connection parameters | ||
conn, closer, err := utils.NewMySQL(clusterInstance, keyspaceName, schemaSQL) | ||
if err != nil { | ||
fmt.Println(err) | ||
return 1 | ||
} | ||
defer closer() | ||
mysqlParams = conn | ||
|
||
return m.Run() | ||
}() | ||
os.Exit(exitCode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package planbuilder | ||
|
||
import "encoding/json" | ||
|
||
type PlanTest struct { | ||
Comment string `json:"comment,omitempty"` | ||
Query string `json:"query,omitempty"` | ||
Plan json.RawMessage `json:"plan,omitempty"` | ||
Skip bool `json:"skip,omitempty"` | ||
SkipE2E bool `json:"skip_e2e,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.