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

Commit 9021288

Browse files
committed
Improve test command run_sql_in_sandbox
Add ability of doing comparisons other than "equals"
1 parent 08677d2 commit 9021288

File tree

11 files changed

+49
-22
lines changed

11 files changed

+49
-22
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/go-sql-driver/mysql v1.4.1
1010
github.com/nightlyone/lockfile v0.0.0-20180618180623-0ad87eef1443
1111
github.com/pkg/errors v0.9.1
12-
github.com/rogpeppe/go-internal v1.8.2-0.20220804145408-77fe68fd64d5
12+
github.com/rogpeppe/go-internal v1.9.0
1313
github.com/spf13/cobra v1.4.0
1414
github.com/spf13/pflag v1.0.5
1515
github.com/stretchr/testify v1.8.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
3535
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3636
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3737
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
38-
github.com/rogpeppe/go-internal v1.8.2-0.20220804145408-77fe68fd64d5 h1:I8mM3rz9nUXXYKXPREmt5X+BpgN4RqGBDKVAY52sMEg=
39-
github.com/rogpeppe/go-internal v1.8.2-0.20220804145408-77fe68fd64d5/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
38+
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
39+
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
4040
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
4141
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
4242
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=

ts/commands.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func customCommands() map[string]func(ts *testscript.TestScript, neg bool, args
122122
"cleanup_at_end": cleanupAtEnd,
123123

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

153156
// runSqlInSandbox is a testscript command that runs a SQL query in a sandbox
154157
// use as:
155-
// run_sql_in_sandbox "query" wanted
158+
// run_sql_in_sandbox "query" {eq|lt|le|gt|ge} wanted
156159
func runSqlInSandbox(ts *testscript.TestScript, neg bool, args []string) {
157-
assertEqual[int](ts, len(args), 3, "syntax: run_sql_in_sandbox sandbox_dir 'query' wanted_value")
160+
assertEqual[int](ts, len(args), 4, "syntax: run_sql_in_sandbox sandbox_dir 'query' {eq|lt|le|gt|ge} wanted_value")
158161
sbDir := args[0]
159162
query := args[1]
160-
wanted := args[2]
163+
operation := args[2]
164+
wanted := args[3]
161165
assertDirExists(ts, sbDir, globals.ErrDirectoryNotFound, sbDir)
162166

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

174-
assertEqual[string](ts, strResult, wanted, "got %s - want: %s", strResult, wanted)
178+
switch strings.ToLower(operation) {
179+
case "eq", "=", "==":
180+
assertEqual[string](ts, strResult, wanted, "got %s - want: %s", strResult, wanted)
181+
case "ge", ">=":
182+
assertGreaterEqual[string](ts, strResult, wanted, "got %s - want: >= %s", strResult, wanted)
183+
case "gt", ">":
184+
assertGreater[string](ts, strResult, wanted, "got %s - want: > %s", strResult, wanted)
185+
case "le", "<=":
186+
assertGreaterEqual[string](ts, wanted, strResult, "got %s - want: <= %s", strResult, wanted)
187+
case "lt", "<":
188+
assertGreater[string](ts, wanted, strResult, "got %s - want: < %s", strResult, wanted)
189+
default:
190+
ts.Fatalf("unrecognized operation %s", operation)
191+
}
175192
}

ts/templates/feature/dd-expose-tables.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ stdout '# fail : 0'
4141

4242
# check dictionary tables
4343

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

4949
# sandbox cleanup
5050
exec dbdeployer delete msb_{{.DbPathVer}}_dd

ts/templates/feature/multiple.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ stdout -count=3 'sandbox server started'
6363
exec $sb_dir/use_all 'select @@max_connections'
6464
stdout -count=3 77
6565

66-
run_sql_in_sandbox $sb_dir/node1 'select @@max_connections' 77
67-
run_sql_in_sandbox $sb_dir/node2 'select @@max_connections' 77
68-
run_sql_in_sandbox $sb_dir/node3 'select @@max_connections' 77
66+
run_sql_in_sandbox $sb_dir/node1 'select @@max_connections' eq 77
67+
run_sql_in_sandbox $sb_dir/node2 'select @@max_connections' eq 77
68+
run_sql_in_sandbox $sb_dir/node3 'select @@max_connections' eq 77
6969
! find_errors $sb_dir/node1
7070
! find_errors $sb_dir/node2
7171
! find_errors $sb_dir/node3

ts/templates/feature/single.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ stdout 'stop .*/msb_{{.DbPathVer}}'
4848
stdout 'sandbox server started'
4949
! stderr .
5050

51-
run_sql_in_sandbox $sb_dir 'select @@max_connections' 88
51+
run_sql_in_sandbox $sb_dir 'select @@max_connections' eq 88
5252
! find_errors $sb_dir
5353

5454
# sandbox cleanup

ts/templates/replication/replication-gtid.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ stdout -count=3 'sandbox server started'
7878
exec $sb_dir/use_all 'select @@max_connections'
7979
stdout -count=3 66
8080

81-
run_sql_in_sandbox $sb_dir/master 'select @@max_connections' 66
82-
run_sql_in_sandbox $sb_dir/node1 'select @@max_connections' 66
83-
run_sql_in_sandbox $sb_dir/node2 'select @@max_connections' 66
81+
run_sql_in_sandbox $sb_dir/master 'select @@max_connections' eq 66
82+
run_sql_in_sandbox $sb_dir/node1 'select @@max_connections' eq 66
83+
run_sql_in_sandbox $sb_dir/node2 'select @@max_connections' eq 66
8484
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/master
8585
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/node1
8686
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/node2

ts/templates/replication/replication.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ stdout -count=3 'sandbox server started'
7272
exec $sb_dir/use_all 'select @@max_connections'
7373
stdout -count=3 66
7474

75-
run_sql_in_sandbox $sb_dir/master 'select @@max_connections' 66
76-
run_sql_in_sandbox $sb_dir/node1 'select @@max_connections' 66
77-
run_sql_in_sandbox $sb_dir/node2 'select @@max_connections' 66
75+
run_sql_in_sandbox $sb_dir/master 'select @@max_connections' eq 66
76+
run_sql_in_sandbox $sb_dir/node1 'select @@max_connections' eq 66
77+
run_sql_in_sandbox $sb_dir/node2 'select @@max_connections' eq 66
7878
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/master
7979
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/node1
8080
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/node2

ts/templates/replication/semisync.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ check_ports $sb_dir $required_ports
5555

5656
# test replication
5757

58+
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/master
59+
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/node1
60+
[version_is_at_least:$db_version:5.6.0] ! find_errors $sb_dir/node2
5861
exec $sb_dir/test_replication
5962
stdout '# failed: 0'
6063
! stderr .

ts/test_helpers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,9 @@ func assertGreater[T constraints.Ordered](ts *testscript.TestScript, a, b T, msg
6262
ts.Fatalf(msg, args...)
6363
}
6464
}
65+
66+
func assertGreaterEqual[T constraints.Ordered](ts *testscript.TestScript, a, b T, msg string, args ...interface{}) {
67+
if a < b {
68+
ts.Fatalf(msg, args...)
69+
}
70+
}

ts/ts_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func testDbDeployer(t *testing.T, name string, parallel bool) {
7474
Condition: customConditions,
7575
Setup: dbdeployerSetup(t, dir),
7676
RequireExplicitExec: true,
77+
TestWork: os.Getenv("ts_preserve") != "",
7778
})
7879
})
7980
}

0 commit comments

Comments
 (0)