This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.go
467 lines (440 loc) · 11.4 KB
/
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// 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 kiviktest
import (
"context"
"errors"
"flag"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"sync"
"sync/atomic"
"testing"
"github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kiviktest/v4/kt"
// Tests
_ "github.com/go-kivik/kiviktest/v4/client"
_ "github.com/go-kivik/kiviktest/v4/db"
)
// The available test suites
const (
SuiteAuto = "auto"
SuitePouchLocal = "pouch"
SuitePouchRemote = "pouchRemote"
SuiteCouch16 = "couch16"
SuiteCouch17 = "couch17"
SuiteCouch20 = "couch20"
SuiteCouch21 = "couch21"
SuiteCouch22 = "couch22"
SuiteCouch23 = "couch23"
SuiteCouch30 = "couch30"
SuiteCouch31 = "couch31"
SuiteCouch32 = "couch32"
SuiteCouch33 = "couch33"
SuiteCloudant = "cloudant"
SuiteKivikServer = "kivikServer"
SuiteKivikMemory = "kivikMemory"
SuiteKivikFS = "kivikFilesystem"
)
// AllSuites is a list of all defined suites.
var AllSuites = []string{
SuitePouchLocal,
SuitePouchRemote,
SuiteCouch16,
SuiteCouch17,
SuiteCouch20,
SuiteCouch21,
SuiteCouch22,
SuiteCouch30,
SuiteCouch31,
SuiteCouch32,
SuiteCouch33,
SuiteKivikMemory,
SuiteKivikFS,
SuiteCloudant,
SuiteKivikServer,
}
var driverMap = map[string]string{
SuitePouchLocal: "pouch",
SuitePouchRemote: "pouch",
SuiteCouch16: "couch",
SuiteCouch17: "couch",
SuiteCouch20: "couch",
SuiteCouch21: "couch",
SuiteCouch22: "couch",
SuiteCouch23: "couch",
SuiteCouch30: "couch",
SuiteCouch31: "couch",
SuiteCouch32: "couch",
SuiteCouch33: "couch",
SuiteCloudant: "couch",
SuiteKivikServer: "couch",
SuiteKivikMemory: "memory",
SuiteKivikFS: "fs",
}
// ListTests prints a list of available test suites to stdout.
func ListTests() {
fmt.Printf("Available test suites:\n\tauto\n")
for _, suite := range AllSuites {
fmt.Printf("\t%s\n", suite)
}
}
// Options are the options to run a test from the command line tool.
type Options struct {
Driver string
DSN string
Verbose bool
RW bool
Match string
Suites []string
Cleanup bool
}
// CleanupTests attempts to clean up any stray test databases created by a
// previous test run.
func CleanupTests(driver, dsn string, verbose bool) error {
client, err := kivik.New(driver, dsn)
if err != nil {
return err
}
count, err := doCleanup(client, verbose)
if verbose {
fmt.Printf("Deleted %d test databases\n", count)
}
return err
}
func doCleanup(client *kivik.Client, verbose bool) (int, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errCh := make(chan error, 3)
var count int32
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
c, err := cleanupDatabases(ctx, client, verbose)
if err != nil {
cancel()
}
atomic.AddInt32(&count, int32(c))
errCh <- err
}()
wg.Add(1)
go func() {
defer wg.Done()
c, err := cleanupUsers(ctx, client, verbose)
if err != nil {
cancel()
}
atomic.AddInt32(&count, int32(c))
errCh <- err
}()
wg.Add(1)
go func() {
defer wg.Done()
c, err := cleanupReplications(ctx, client, verbose)
if err != nil {
cancel()
}
atomic.AddInt32(&count, int32(c))
errCh <- err
}()
wg.Wait()
err := <-errCh
for len(errCh) > 0 {
<-errCh
}
return int(count), err
}
func cleanupDatabases(ctx context.Context, client *kivik.Client, verbose bool) (int, error) {
if verbose {
fmt.Printf("Cleaning up stale databases\n")
}
allDBs, err := client.AllDBs(ctx)
if err != nil {
return 0, err
}
var count int
for _, dbName := range allDBs {
// FIXME: This filtering should be possible in AllDBs(), but all the
// backends need to support it first.
if strings.HasPrefix(dbName, kt.TestDBPrefix) {
if verbose {
fmt.Printf("\t--- Deleting %s\n", dbName)
}
if e := client.DestroyDB(ctx, dbName); e != nil && kivik.HTTPStatus(e) != http.StatusNotFound {
return count, e
}
count++
}
}
replicator := client.DB("_replicator")
if e := replicator.Err(); e != nil {
if kivik.HTTPStatus(e) != http.StatusNotFound && kivik.HTTPStatus(e) != http.StatusNotImplemented {
return count, e
}
return count, nil
}
docs := replicator.AllDocs(context.Background(), map[string]interface{}{"include_docs": true})
if err := docs.Err(); err != nil {
if kivik.HTTPStatus(err) == http.StatusNotImplemented || kivik.HTTPStatus(err) == http.StatusNotFound {
return count, nil
}
return count, err
}
var replDoc struct {
Rev string `json:"_rev"`
}
for docs.Next() {
id, _ := docs.ID()
if strings.HasPrefix(id, "kivik$") {
if err := docs.ScanDoc(&replDoc); err != nil {
return count, err
}
if _, err := replicator.Delete(context.Background(), id, replDoc.Rev); err != nil {
return count, err
}
count++
}
}
return count, nil
}
func cleanupUsers(ctx context.Context, client *kivik.Client, verbose bool) (int, error) {
if verbose {
fmt.Printf("Cleaning up stale users\n")
}
db := client.DB("_users")
if err := db.Err(); err != nil {
switch kivik.HTTPStatus(err) {
case http.StatusNotFound, http.StatusNotImplemented:
return 0, nil
}
return 0, err
}
users := db.AllDocs(ctx, map[string]interface{}{"include_docs": true})
if err := users.Err(); err != nil {
switch kivik.HTTPStatus(err) {
case http.StatusNotFound, http.StatusNotImplemented:
return 0, nil
}
return 0, err
}
var count int
for users.Next() {
id, _ := users.ID()
if strings.HasPrefix(id, "org.couchdb.user:kivik$") {
if verbose {
fmt.Printf("\t--- Deleting user %s\n", id)
}
var doc struct {
Rev string `json:"_rev"`
}
if err := users.ScanDoc(&doc); err != nil {
return count, err
}
if _, err := db.Delete(ctx, id, doc.Rev); err != nil {
return count, err
}
count++
}
}
return count, users.Err()
}
func cleanupReplications(ctx context.Context, client *kivik.Client, verbose bool) (int, error) {
if verbose {
fmt.Printf("Cleaning up stale replications\n")
}
db := client.DB("_replicator")
if err := db.Err(); err != nil {
switch kivik.HTTPStatus(err) {
case http.StatusNotFound, http.StatusNotImplemented:
return 0, nil
}
return 0, err
}
reps := db.AllDocs(ctx, map[string]interface{}{"include_docs": true})
if err := reps.Err(); err != nil {
switch kivik.HTTPStatus(err) {
case http.StatusNotFound, http.StatusNotImplemented:
return 0, nil
}
return 0, err
}
var count int
for reps.Next() {
var doc struct {
Rev string `json:"_rev"`
Source string `json:"source"`
Target string `json:"target"`
}
if err := reps.ScanDoc(&doc); err != nil {
return count, err
}
id, _ := reps.ID()
if strings.HasPrefix(id, "kivik$") ||
strings.HasPrefix(doc.Source, "kivik$") ||
strings.HasPrefix(doc.Target, "kivik$") {
if verbose {
fmt.Printf("\t--- Deleting replication %s\n", id)
}
if _, err := db.Delete(ctx, id, doc.Rev); err != nil {
return count, err
}
count++
}
}
return count, reps.Err()
}
// RunTests runs the requested test suites against the requested driver and DSN.
func RunTests(opts Options) {
if opts.Cleanup {
err := CleanupTests(opts.Driver, opts.DSN, opts.Verbose)
if err != nil {
fmt.Printf("Cleanup failed: %s\n", err)
os.Exit(1)
}
os.Exit(0)
}
_ = flag.Set("test.run", opts.Match)
if opts.Verbose {
_ = flag.Set("test.v", "true")
}
tests := []testing.InternalTest{
{
Name: "MainTest",
F: func(t *testing.T) {
Test(t, opts.Driver, opts.DSN, opts.Suites, opts.RW)
},
},
}
mainStart(tests)
}
// Test is the main test entry point when running tests through the command line
// tool.
func Test(t *testing.T, driver, dsn string, testSuites []string, rw bool) {
clients, err := ConnectClients(t, driver, dsn, nil)
if err != nil {
t.Fatalf("Failed to connect to %s (%s driver): %s\n", dsn, driver, err)
}
clients.RW = rw
tests := make(map[string]struct{})
for _, test := range testSuites {
tests[test] = struct{}{}
}
if _, ok := tests[SuiteAuto]; ok {
t.Log("Detecting target service compatibility...")
suites, err := detectCompatibility(clients.Admin)
if err != nil {
t.Fatalf("Unable to determine server suite compatibility: %s\n", err)
}
tests = make(map[string]struct{})
for _, suite := range suites {
tests[suite] = struct{}{}
}
}
testSuites = make([]string, 0, len(tests))
for test := range tests {
testSuites = append(testSuites, test)
}
t.Logf("Running the following test suites: %s\n", strings.Join(testSuites, ", "))
for _, suite := range testSuites {
RunTestsInternal(clients, suite)
}
}
// RunTestsInternal is for internal use only.
func RunTestsInternal(ctx *kt.Context, suite string) {
conf, ok := suites[suite]
if !ok {
ctx.Skipf("No configuration found for suite '%s'", suite)
}
ctx.Config = conf
// This is run as a sub-test so configuration will work nicely.
ctx.Run("PreCleanup", func(ctx *kt.Context) {
ctx.RunAdmin(func(ctx *kt.Context) {
count, err := doCleanup(ctx.Admin, true)
if count > 0 {
ctx.Logf("Pre-cleanup removed %d databases from previous test runs", count)
}
if err != nil {
ctx.Fatalf("Pre-cleanup failed: %s", err)
}
})
})
kt.RunSubtests(ctx)
}
func detectCompatibility(client *kivik.Client) ([]string, error) {
info, err := client.Version(context.Background())
if err != nil {
return nil, err
}
switch info.Vendor {
case "PouchDB":
return []string{SuitePouchLocal}, nil
case "IBM Cloudant":
return []string{SuiteCloudant}, nil
case "The Apache Software Foundation":
if strings.HasPrefix(info.Version, "2.0") {
return []string{SuiteCouch20}, nil
}
if strings.HasPrefix(info.Version, "2.1") {
return []string{SuiteCouch21}, nil
}
return []string{SuiteCouch16}, nil
case "Kivik Memory Adaptor":
return []string{SuiteKivikMemory}, nil
}
return []string{}, errors.New("Unable to automatically determine the proper test suite")
}
// ConnectClients connects clients.
func ConnectClients(t *testing.T, driverName, dsn string, opts kivik.Options) (*kt.Context, error) {
var noAuthDSN string
if parsed, err := url.Parse(dsn); err == nil {
if parsed.User == nil {
return nil, errors.New("DSN does not contain authentication credentials")
}
parsed.User = nil
noAuthDSN = parsed.String()
}
clients := &kt.Context{
T: t,
}
t.Logf("Connecting to %s ...\n", dsn)
if client, err := kivik.New(driverName, dsn, opts); err == nil {
clients.Admin = client
} else {
return nil, err
}
t.Logf("Connecting to %s ...\n", noAuthDSN)
if client, err := kivik.New(driverName, noAuthDSN, opts); err == nil {
clients.NoAuth = client
} else {
return nil, err
}
return clients, nil
}
// DoTest runs a suite of tests.
func DoTest(t *testing.T, suite, envName string) {
opts, _ := suites[suite].Interface(t, "Options").(kivik.Options)
dsn := os.Getenv(envName)
if dsn == "" {
t.Skipf("%s: %s DSN not set; skipping tests", envName, suite)
}
clients, err := ConnectClients(t, driverMap[suite], dsn, opts)
if err != nil {
t.Errorf("Failed to connect to %s: %s\n", suite, err)
return
}
clients.RW = true
RunTestsInternal(clients, suite)
}