Skip to content
This repository has been archived by the owner on Oct 29, 2023. It is now read-only.

Commit

Permalink
Improve test command run_sql_in_sandbox
Browse files Browse the repository at this point in the history
Add ability of doing comparisons other than "equals"
  • Loading branch information
datacharmer committed Aug 24, 2022
1 parent 08677d2 commit 9021288
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/go-sql-driver/mysql v1.4.1
github.com/nightlyone/lockfile v0.0.0-20180618180623-0ad87eef1443
github.com/pkg/errors v0.9.1
github.com/rogpeppe/go-internal v1.8.2-0.20220804145408-77fe68fd64d5
github.com/rogpeppe/go-internal v1.9.0
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.2-0.20220804145408-77fe68fd64d5 h1:I8mM3rz9nUXXYKXPREmt5X+BpgN4RqGBDKVAY52sMEg=
github.com/rogpeppe/go-internal v1.8.2-0.20220804145408-77fe68fd64d5/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
Expand Down
27 changes: 22 additions & 5 deletions ts/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func customCommands() map[string]func(ts *testscript.TestScript, neg bool, args
"cleanup_at_end": cleanupAtEnd,

// run_sql_in_sandbox runs a SQL query in a sandbox, and compares the result with an expected value
// invoke as "run_sql_in_sandbox $sb_dir 'SQL query' value_to_compare"
// invoke as "run_sql_in_sandbox $sb_dir 'SQL query' {eq|lt|le|gt|ge} value_to_compare "
// Notice that the query must return a single value
"run_sql_in_sandbox": runSqlInSandbox,
}
Expand All @@ -136,6 +136,9 @@ func cleanupAtEnd(ts *testscript.TestScript, neg bool, args []string) {
sandboxName := path.Base(sandboxDir)
// testscript.Defer runs at the end of the current test
ts.Defer(func() {
if os.Getenv("ts_preserve") != "" {
return
}
if !common.DirExists(sandboxDir) {
return
}
Expand All @@ -152,12 +155,13 @@ func cleanupAtEnd(ts *testscript.TestScript, neg bool, args []string) {

// runSqlInSandbox is a testscript command that runs a SQL query in a sandbox
// use as:
// run_sql_in_sandbox "query" wanted
// run_sql_in_sandbox "query" {eq|lt|le|gt|ge} wanted
func runSqlInSandbox(ts *testscript.TestScript, neg bool, args []string) {
assertEqual[int](ts, len(args), 3, "syntax: run_sql_in_sandbox sandbox_dir 'query' wanted_value")
assertEqual[int](ts, len(args), 4, "syntax: run_sql_in_sandbox sandbox_dir 'query' {eq|lt|le|gt|ge} wanted_value")
sbDir := args[0]
query := args[1]
wanted := args[2]
operation := args[2]
wanted := args[3]
assertDirExists(ts, sbDir, globals.ErrDirectoryNotFound, sbDir)

var strResult string
Expand All @@ -171,5 +175,18 @@ func runSqlInSandbox(ts *testscript.TestScript, neg bool, args []string) {
strResult = result.(string)
}

assertEqual[string](ts, strResult, wanted, "got %s - want: %s", strResult, wanted)
switch strings.ToLower(operation) {
case "eq", "=", "==":
assertEqual[string](ts, strResult, wanted, "got %s - want: %s", strResult, wanted)
case "ge", ">=":
assertGreaterEqual[string](ts, strResult, wanted, "got %s - want: >= %s", strResult, wanted)
case "gt", ">":
assertGreater[string](ts, strResult, wanted, "got %s - want: > %s", strResult, wanted)
case "le", "<=":
assertGreaterEqual[string](ts, wanted, strResult, "got %s - want: <= %s", strResult, wanted)
case "lt", "<":
assertGreater[string](ts, wanted, strResult, "got %s - want: < %s", strResult, wanted)
default:
ts.Fatalf("unrecognized operation %s", operation)
}
}
8 changes: 4 additions & 4 deletions ts/templates/feature/dd-expose-tables.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ stdout '# fail : 0'

# check dictionary tables

run_sql_in_sandbox $sb_dir 'select VERSION()' {{.DbVersion}}-debug
run_sql_in_sandbox $sb_dir 'select @@debug is not null' 1
run_sql_in_sandbox $sb_dir 'select count(*) from mysql.tables where name =''tables'' and schema_id=1' 1
run_sql_in_sandbox $sb_dir 'select count(*) from information_schema.tables where table_name =''tables'' and table_schema=''mysql''' 1
run_sql_in_sandbox $sb_dir 'select VERSION()' eq {{.DbVersion}}-debug
run_sql_in_sandbox $sb_dir 'select @@debug is not null' eq 1
run_sql_in_sandbox $sb_dir 'select count(*) from mysql.tables where name =''tables'' and schema_id=1' gt 0
run_sql_in_sandbox $sb_dir 'select count(*) from information_schema.tables where table_name =''tables'' and table_schema=''mysql''' gt 0

# sandbox cleanup
exec dbdeployer delete msb_{{.DbPathVer}}_dd
Expand Down
6 changes: 3 additions & 3 deletions ts/templates/feature/multiple.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ stdout -count=3 'sandbox server started'
exec $sb_dir/use_all 'select @@max_connections'
stdout -count=3 77

run_sql_in_sandbox $sb_dir/node1 'select @@max_connections' 77
run_sql_in_sandbox $sb_dir/node2 'select @@max_connections' 77
run_sql_in_sandbox $sb_dir/node3 'select @@max_connections' 77
run_sql_in_sandbox $sb_dir/node1 'select @@max_connections' eq 77
run_sql_in_sandbox $sb_dir/node2 'select @@max_connections' eq 77
run_sql_in_sandbox $sb_dir/node3 'select @@max_connections' eq 77
! find_errors $sb_dir/node1
! find_errors $sb_dir/node2
! find_errors $sb_dir/node3
Expand Down
2 changes: 1 addition & 1 deletion ts/templates/feature/single.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ stdout 'stop .*/msb_{{.DbPathVer}}'
stdout 'sandbox server started'
! stderr .

run_sql_in_sandbox $sb_dir 'select @@max_connections' 88
run_sql_in_sandbox $sb_dir 'select @@max_connections' eq 88
! find_errors $sb_dir

# sandbox cleanup
Expand Down
6 changes: 3 additions & 3 deletions ts/templates/replication/replication-gtid.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ stdout -count=3 'sandbox server started'
exec $sb_dir/use_all 'select @@max_connections'
stdout -count=3 66

run_sql_in_sandbox $sb_dir/master 'select @@max_connections' 66
run_sql_in_sandbox $sb_dir/node1 'select @@max_connections' 66
run_sql_in_sandbox $sb_dir/node2 'select @@max_connections' 66
run_sql_in_sandbox $sb_dir/master 'select @@max_connections' eq 66
run_sql_in_sandbox $sb_dir/node1 'select @@max_connections' eq 66
run_sql_in_sandbox $sb_dir/node2 'select @@max_connections' eq 66
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/master
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/node1
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/node2
Expand Down
6 changes: 3 additions & 3 deletions ts/templates/replication/replication.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ stdout -count=3 'sandbox server started'
exec $sb_dir/use_all 'select @@max_connections'
stdout -count=3 66

run_sql_in_sandbox $sb_dir/master 'select @@max_connections' 66
run_sql_in_sandbox $sb_dir/node1 'select @@max_connections' 66
run_sql_in_sandbox $sb_dir/node2 'select @@max_connections' 66
run_sql_in_sandbox $sb_dir/master 'select @@max_connections' eq 66
run_sql_in_sandbox $sb_dir/node1 'select @@max_connections' eq 66
run_sql_in_sandbox $sb_dir/node2 'select @@max_connections' eq 66
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/master
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/node1
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/node2
Expand Down
3 changes: 3 additions & 0 deletions ts/templates/replication/semisync.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ check_ports $sb_dir $required_ports

# test replication

[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/master
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/node1
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/node2
exec $sb_dir/test_replication
stdout '# failed: 0'
! stderr .
Expand Down
6 changes: 6 additions & 0 deletions ts/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,9 @@ func assertGreater[T constraints.Ordered](ts *testscript.TestScript, a, b T, msg
ts.Fatalf(msg, args...)
}
}

func assertGreaterEqual[T constraints.Ordered](ts *testscript.TestScript, a, b T, msg string, args ...interface{}) {
if a < b {
ts.Fatalf(msg, args...)
}
}
1 change: 1 addition & 0 deletions ts/ts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func testDbDeployer(t *testing.T, name string, parallel bool) {
Condition: customConditions,
Setup: dbdeployerSetup(t, dir),
RequireExplicitExec: true,
TestWork: os.Getenv("ts_preserve") != "",
})
})
}
Expand Down

0 comments on commit 9021288

Please sign in to comment.