Skip to content

Commit

Permalink
more cleanups
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Oct 2, 2024
1 parent 4b9a20c commit 33030b3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 255 deletions.
29 changes: 15 additions & 14 deletions go/tools/data.go → go/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package tools
package data

import (
"bytes"
Expand All @@ -36,7 +36,7 @@ type (
}
)

func ReadData(url string) ([]byte, error) {
func readData(url string) ([]byte, error) {
if strings.HasPrefix(url, "http") {
client := http.Client{}
res, err := client.Get(url)
Expand All @@ -52,7 +52,11 @@ func ReadData(url string) ([]byte, error) {
return os.ReadFile(url)
}

func LoadQueries(data []byte) ([]Query, error) {
func LoadQueries(url string) ([]Query, error) {
data, err := readData(url)
if err != nil {
return nil, err
}
seps := bytes.Split(data, []byte("\n"))
queries := make([]Query, 0, len(seps))
newStmt := true
Expand Down Expand Up @@ -94,27 +98,27 @@ func ParseQueries(qs ...Query) ([]Query, error) {
realS := rs.Query
s := rs.Query
q := Query{}
q.Type = typ.Q_UNKNOWN
q.Type = typ.Unknown
q.Line = rs.Line
// a valid Query's length should be at least 3.
if len(s) < 3 {
continue
}
// we will skip #comment and line with zero characters here
if s[0] == '#' {
q.Type = typ.Q_COMMENT
q.Type = typ.Comment
} else if s[0:2] == "--" {
q.Type = typ.Q_COMMENT_WITH_COMMAND
q.Type = typ.CommentWithCommand
if s[2] == ' ' {
s = s[3:]
} else {
s = s[2:]
}
} else if s[0] == '\n' {
q.Type = typ.Q_EMPTY_LINE
q.Type = typ.EmptyLine
}

if q.Type != typ.Q_COMMENT {
if q.Type != typ.Comment {
// Calculate first word length(the command), terminated
// by 'space' , '(' or 'delimiter'
var i int
Expand All @@ -126,7 +130,7 @@ func ParseQueries(qs ...Query) ([]Query, error) {
s = s[i:]

q.Query = s
if q.Type == typ.Q_UNKNOWN || q.Type == typ.Q_COMMENT_WITH_COMMAND {
if q.Type == typ.Unknown || q.Type == typ.CommentWithCommand {
if err := q.getQueryType(realS); err != nil {
return nil, err
}
Expand All @@ -143,16 +147,13 @@ func ParseQueries(qs ...Query) ([]Query, error) {
func (q *Query) getQueryType(qu string) error {
tp := typ.FindType(q.FirstWord)
if tp > 0 {
if tp == typ.Q_ECHO || tp == typ.Q_SORTED_RESULT {
q.Query = strings.TrimSpace(q.Query)
}
q.Type = tp
} else {
// No mysqltest command matched
if q.Type != typ.Q_COMMENT_WITH_COMMAND {
if q.Type != typ.CommentWithCommand {
// A query that will sent to vitess
q.Query = qu
q.Type = typ.Q_QUERY
q.Type = typ.Query
} else {
log.WithFields(log.Fields{"line": q.Line, "command": q.FirstWord, "arguments": q.Query}).Error("invalid command")
return fmt.Errorf("invalid command %s", q.FirstWord)
Expand Down
7 changes: 3 additions & 4 deletions go/tester/comparing_query_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ package tester

import (
"fmt"
"github.com/vitessio/vitess-tester/go/tools"

"github.com/pingcap/errors"
log "github.com/sirupsen/logrus"
"github.com/vitessio/vitess-tester/go/data"
"vitess.io/vitess/go/test/endtoend/utils"
"vitess.io/vitess/go/vt/sqlparser"
)
Expand Down Expand Up @@ -55,11 +54,11 @@ func newComparingQueryRunner(
}
}

func (nqr ComparingQueryRunner) runQuery(q tools.Query, expectedErrs bool, ast sqlparser.Statement) error {
func (nqr ComparingQueryRunner) runQuery(q data.Query, expectedErrs bool, ast sqlparser.Statement) error {
return nqr.execute(q, expectedErrs, ast)
}

func (nqr *ComparingQueryRunner) execute(query tools.Query, expectedErrs bool, ast sqlparser.Statement) error {
func (nqr *ComparingQueryRunner) execute(query data.Query, expectedErrs bool, ast sqlparser.Statement) error {
if len(query.Query) == 0 {
return nil
}
Expand Down
44 changes: 12 additions & 32 deletions go/tester/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package tester
import (
"encoding/json"
"fmt"
"github.com/vitessio/vitess-tester/go/tools"
"github.com/vitessio/vitess-tester/go/data"
"github.com/vitessio/vitess-tester/go/typ"

"io"
Expand Down Expand Up @@ -66,7 +66,7 @@ type (
}

QueryRunner interface {
runQuery(q tools.Query, expectedErrs bool, ast sqlparser.Statement) error
runQuery(q data.Query, expectedErrs bool, ast sqlparser.Statement) error
}

QueryRunnerFactory interface {
Expand Down Expand Up @@ -154,37 +154,17 @@ func (t *Tester) Run() error {
if t.autoVSchema() {
defer t.postProcess()
}
data, err := tools.ReadData(t.name)
if err != nil {
return err
}

queries, err := tools.LoadQueries(data)
queries, err := data.LoadQueries(t.name)
if err != nil {
t.reporter.AddFailure(err)
return err
}

for _, q := range queries {
switch q.Type {
// no-ops
case typ.Q_ENABLE_QUERY_LOG,
typ.Q_DISABLE_QUERY_LOG,
typ.Q_ECHO,
typ.Q_DISABLE_WARNINGS,
typ.Q_ENABLE_WARNINGS,
typ.Q_ENABLE_INFO,
typ.Q_DISABLE_INFO,
typ.Q_ENABLE_RESULT_LOG,
typ.Q_DISABLE_RESULT_LOG,
typ.Q_SORTED_RESULT,
typ.Q_REPLACE_REGEX:
// do nothing
case typ.Q_SKIP:
case typ.Skip:
t.skipNext = true
case typ.Q_BEGIN_CONCURRENT, typ.Q_END_CONCURRENT, typ.Q_CONNECT, typ.Q_CONNECTION, typ.Q_DISCONNECT, typ.Q_LET, typ.Q_REPLACE_COLUMN:
t.reporter.AddFailure(fmt.Errorf("%s not supported", q.Type.String()))
case typ.Q_SKIP_IF_BELOW_VERSION:
case typ.SkipIfBelowVersion:
strs := strings.Split(q.Query, " ")
if len(strs) != 3 {
t.reporter.AddFailure(fmt.Errorf("incorrect syntax for typ.Q_SKIP_IF_BELOW_VERSION in: %v", q.Query))
Expand All @@ -197,19 +177,19 @@ func (t *Tester) Run() error {
t.reporter.AddFailure(err)
continue
}
case typ.Q_ERROR:
case typ.Error:
t.expectedErrs = true
case typ.Q_VEXPLAIN:
case typ.VExplain:
strs := strings.Split(q.Query, " ")
if len(strs) != 2 {
t.reporter.AddFailure(fmt.Errorf("incorrect syntax for typ.Q_VEXPLAIN in: %v", q.Query))
t.reporter.AddFailure(fmt.Errorf("incorrect syntax for typ.VExplain in: %v", q.Query))
continue
}

t.vexplain = strs[1]
case typ.Q_WAIT_FOR_AUTHORITATIVE:
case typ.WaitForAuthoritative:
t.waitAuthoritative(q.Query)
case typ.Q_QUERY:
case typ.Query:
if t.vexplain != "" {
result, err := t.curr.VtConn.ExecuteFetch(fmt.Sprintf("vexplain %s %s", t.vexplain, q.Query), -1, false)
t.vexplain = ""
Expand All @@ -221,7 +201,7 @@ func (t *Tester) Run() error {
}

t.runQuery(q)
case typ.Q_REMOVE_FILE:
case typ.RemoveFile:
err = os.Remove(strings.TrimSpace(q.Query))
if err != nil {
return errors.Annotate(err, "failed to remove file")
Expand All @@ -235,7 +215,7 @@ func (t *Tester) Run() error {
return nil
}

func (t *Tester) runQuery(q tools.Query) {
func (t *Tester) runQuery(q data.Query) {
if t.skipNext {
t.skipNext = false
return
Expand Down
6 changes: 3 additions & 3 deletions go/tester/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/vitessio/vitess-tester/go/tools"
"github.com/vitessio/vitess-tester/go/data"
"os"

"vitess.io/vitess/go/mysql"
Expand Down Expand Up @@ -67,7 +67,7 @@ func newTracer(traceFile *os.File,
}
}

func (t *Tracer) runQuery(q tools.Query, expectErr bool, ast sqlparser.Statement) error {
func (t *Tracer) runQuery(q data.Query, expectErr bool, ast sqlparser.Statement) error {
if sqlparser.IsDMLStatement(ast) && t.traceFile != nil && !expectErr {
// we don't want to run DMLs twice, so we just run them once while tracing
var errs []error
Expand Down Expand Up @@ -100,7 +100,7 @@ func (t *Tracer) runQuery(q tools.Query, expectErr bool, ast sqlparser.Statement
}

// trace writes the query and its trace (fetched from VtConn) as a JSON object into traceFile
func (t *Tracer) trace(query tools.Query) error {
func (t *Tracer) trace(query data.Query) error {
// Marshal the query into JSON format for safe embedding
queryJSON, err := json.Marshal(query.Query)
if err != nil {
Expand Down
Loading

0 comments on commit 33030b3

Please sign in to comment.