-
Notifications
You must be signed in to change notification settings - Fork 32
/
config_test.go
268 lines (250 loc) · 6.39 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Copyright (c) 2016-2020 by MemSQL. All rights reserved.
*
* 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 main
import (
"path/filepath"
"reflect"
"strconv"
"strings"
"testing"
"time"
"github.com/awreece/goini"
)
func TestExamplesParse(t *testing.T) {
examples, err := filepath.Glob("examples/*.ini")
if err != nil {
t.Fatalf("Error finding example files: %v", err)
}
for _, example := range examples {
if _, e := parseConfig(supportedDatabaseFlavors["mysql"], example, "examples/"); e != nil {
t.Errorf("Error parsing %s: %v", example, e)
}
}
}
func TestReadQueries(t *testing.T) {
var cases = []struct {
in string
out []string
}{
{"select * from t; select * from t",
[]string{"select * from t", " select * from t"},
},
{" select * \n from t; select * \nfrom t",
[]string{" select * \n from t", " select * \nfrom t"},
},
{";;;;", []string{}},
}
df := supportedDatabaseFlavors["mysql"]
for _, c := range cases {
qs, err := readQueriesFromReader(df, strings.NewReader(c.in))
if err != nil {
t.Errorf("Error reading queries from %s: %v", strconv.Quote(c.in), err)
} else if !reflect.DeepEqual(qs, c.out) {
t.Errorf("Failure reading queries from %s:\ngot\t\t%v\nbut expected\t%v",
strconv.Quote(c.in), quotedValue(qs), quotedValue(c.out))
}
}
}
func TestParseIniConfig(t *testing.T) {
var goodCases = []struct {
in string
out *Config
}{
{"[test]\nquery=select 1",
&Config{
Flavor: supportedDatabaseFlavors["mysql"],
Jobs: map[string]*Job{
"test": &Job{
Name: "test", QueueDepth: 1,
Queries: []string{"select 1"},
},
},
},
},
{"[test1]\nquery=select 1\n[test2]\nquery=select 2",
&Config{
Flavor: supportedDatabaseFlavors["mysql"],
Jobs: map[string]*Job{
"test1": &Job{
Name: "test1", QueueDepth: 1,
Queries: []string{"select 1"},
},
"test2": &Job{
Name: "test2", QueueDepth: 1,
Queries: []string{"select 2"},
},
},
},
},
{`
[test1]
query=select 1
rate=1
`,
&Config{
Flavor: supportedDatabaseFlavors["mysql"],
Jobs: map[string]*Job{
"test1": &Job{
Name: "test1", Rate: 1.0,
Queries: []string{"select 1"},
BatchSize: 1,
},
},
},
},
{`
[test job]
query=show databases
count=1
`,
&Config{
Flavor: supportedDatabaseFlavors["mysql"],
Jobs: map[string]*Job{
"test job": &Job{
Name: "test job", QueueDepth: 1, Count: 1,
Queries: []string{"show databases"},
},
},
},
},
{`
[setup]
query=insert into t select RAND(), RAND()
query=insert into t select RAND(), RAND() from t
query=insert into t select RAND(), RAND() from t
[teardown]
query=drop table t
[count]
query=count(*) from t where a < b
count=30
`,
&Config{
Flavor: supportedDatabaseFlavors["mysql"],
Setup: []string{
"insert into t select RAND(), RAND()",
"insert into t select RAND(), RAND() from t",
"insert into t select RAND(), RAND() from t",
},
Teardown: []string{
"drop table t",
},
Jobs: map[string]*Job{
"count": &Job{
Name: "count", QueueDepth: 1, Count: 30,
Queries: []string{"count(*) from t where a < b"},
},
},
},
},
{
`
[run 2 queries at a time for 10 seconds, starting at 5s]
query=select count(*) from mytable
queue-depth=2
start=5s
stop=15s
`,
&Config{
Flavor: supportedDatabaseFlavors["mysql"],
Jobs: map[string]*Job{
"run 2 queries at a time for 10 seconds, starting at 5s": &Job{
Name: "run 2 queries at a time for 10 seconds, starting at 5s",
QueueDepth: 2, Start: 5 * time.Second, Stop: 15 * time.Second,
Queries: []string{"select count(*) from mytable"},
},
},
},
},
{
`
duration=10s
[test job]
query=select 1+1
`,
&Config{
Flavor: supportedDatabaseFlavors["mysql"],
Duration: 10 * time.Second,
Jobs: map[string]*Job{
"test job": &Job{
Name: "test job", QueueDepth: 1,
Queries: []string{"select 1+1"},
},
},
},
},
{
// error(s) can be any string, so long as there is a corresponding parser that converts the
// error returned by the database driver into this string. For example mySQLErrorCodeParser or
// postgresErrorCodeParser. Below are a few examples. The first is MySQL and MemSQL
// ER_LOCK_WAIT_TIMEOUT and the second is Postgres deadlock_detected. (In a real workload, of
// course, you wouldn't have errors for two different types of databases.)
`
error=1205
error=40P01
[test job]
query=select 1+1
`,
&Config{
Flavor: supportedDatabaseFlavors["mysql"],
Jobs: map[string]*Job{
"test job": &Job{
Name: "test job", QueueDepth: 1,
Queries: []string{"select 1+1"},
},
},
AcceptedErrors: Set{
"1205": struct{}{},
"40P01": struct{}{},
},
},
},
}
var badCases = []string{
"[test]\nrate=1",
}
df := supportedDatabaseFlavors["mysql"]
for _, c := range goodCases {
cp := goini.NewRawConfigParser()
cp.Parse(strings.NewReader(c.in))
iniConfig, err := cp.Finish()
if err != nil {
t.Errorf("Error parsing config %s: %v", strconv.Quote(c.in), err)
continue
}
config, err := parseIniConfig(df, iniConfig, ".")
if err != nil {
t.Errorf("Error parsing ini config %s: %v", strconv.Quote(c.in), err)
continue
}
if !reflect.DeepEqual(config, c.out) {
t.Errorf("Failure parsing config %s:\ngot\t\t%v\nbut expected\t%v",
strconv.Quote(c.in), config, c.out)
}
}
for _, c := range badCases {
cp := goini.NewRawConfigParser()
cp.Parse(strings.NewReader(c))
iniConfig, err := cp.Finish()
if err != nil {
t.Errorf("Error parsing config %s: %v", strconv.Quote(c), err)
continue
}
_, err = parseIniConfig(df, iniConfig, ".")
if err == nil {
t.Errorf("Unexpected succesful parse of iniConfig for %s", strconv.Quote(c))
}
}
}