-
Notifications
You must be signed in to change notification settings - Fork 378
/
setup_compat.go
221 lines (175 loc) · 6.75 KB
/
setup_compat.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
// Copyright 2021 FerretDB Inc.
//
// 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 setup
import (
"context"
"errors"
"fmt"
"runtime/trace"
"testing"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.uber.org/zap"
"golang.org/x/exp/slices"
"github.com/FerretDB/FerretDB/integration/shareddata"
"github.com/FerretDB/FerretDB/internal/util/testutil"
)
// SetupCompatOpts represents setup options for compatibility test.
//
// TODO Add option to use read-only user. https://github.com/FerretDB/FerretDB/issues/1025
type SetupCompatOpts struct {
// Data providers.
Providers []shareddata.Provider
// If true, a non-existent collection will be added to the list of collections.
// This is useful to test the behavior when a collection is not found.
// TODO This flag is not needed, always add a non-existent collection https://github.com/FerretDB/FerretDB/issues/1545
AddNonExistentCollection bool
databaseName string
baseCollectionName string
}
// SetupCompatResult represents compatibility test setup results.
type SetupCompatResult struct {
Ctx context.Context
TargetCollections []*mongo.Collection
CompatCollections []*mongo.Collection
}
// SetupCompatWithOpts setups the compatibility test according to given options.
func SetupCompatWithOpts(tb testing.TB, opts *SetupCompatOpts) *SetupCompatResult {
tb.Helper()
startup()
ctx, cancel := context.WithCancel(testutil.Ctx(tb))
defer trace.StartRegion(ctx, "SetupCompatWithOpts").End()
// skip tests for MongoDB as soon as possible
if *compatPortF == 0 {
tb.Skip("compatibility tests require second system")
}
if opts == nil {
opts = new(SetupCompatOpts)
}
// When we use `task all` to run `pg` and `tigris` compat tests in parallel,
// they both use the same MongoDB instance.
// Add the handler's name to prevent the usage of the same database.
opts.databaseName = testutil.DatabaseName(tb) + "_" + *handlerF
opts.baseCollectionName = testutil.CollectionName(tb)
level := zap.NewAtomicLevelAt(zap.ErrorLevel)
if *debugSetupF {
level = zap.NewAtomicLevelAt(zap.DebugLevel)
}
logger := testutil.Logger(tb, level)
var targetURI string
if *targetPortF == 0 {
targetURI = setupListener(tb, ctx, logger)
} else {
targetURI = buildMongoDBURI(tb, ctx, &buildMongoDBURIOpts{
hostPort: fmt.Sprintf("127.0.0.1:%d", *targetPortF),
tls: *targetTLSF,
})
}
// register cleanup function after setupListener registers its own to preserve full logs
tb.Cleanup(cancel)
compatURI := buildMongoDBURI(tb, ctx, &buildMongoDBURIOpts{
hostPort: fmt.Sprintf("127.0.0.1:%d", *compatPortF),
tls: *compatTLSF,
})
targetCollections := setupCompatCollections(tb, ctx, setupClient(tb, ctx, targetURI), opts)
compatCollections := setupCompatCollections(tb, ctx, setupClient(tb, ctx, compatURI), opts)
level.SetLevel(*logLevelF)
return &SetupCompatResult{
Ctx: ctx,
TargetCollections: targetCollections,
CompatCollections: compatCollections,
}
}
// SetupCompat setups compatibility test.
func SetupCompat(tb testing.TB) (context.Context, []*mongo.Collection, []*mongo.Collection) {
tb.Helper()
s := SetupCompatWithOpts(tb, &SetupCompatOpts{
Providers: shareddata.AllProviders(),
})
return s.Ctx, s.TargetCollections, s.CompatCollections
}
// setupCompatCollections setups a single database with one collection per provider for compatibility tests.
func setupCompatCollections(tb testing.TB, ctx context.Context, client *mongo.Client, opts *SetupCompatOpts) []*mongo.Collection {
tb.Helper()
defer trace.StartRegion(ctx, "setupCompatCollections").End()
database := client.Database(opts.databaseName)
// drop remnants of the previous failed run
_ = database.Drop(ctx)
// delete database unless test failed
tb.Cleanup(func() {
if tb.Failed() {
return
}
err := database.Drop(ctx)
require.NoError(tb, err)
})
collections := make([]*mongo.Collection, 0, len(opts.Providers))
for _, provider := range opts.Providers {
collectionName := opts.baseCollectionName + "_" + provider.Name()
fullName := opts.databaseName + "." + collectionName
if *targetPortF == 0 && !slices.Contains(provider.Handlers(), *handlerF) {
tb.Logf(
"Provider %q is not compatible with handler %q, skipping creating %q.",
provider.Name(), *handlerF, fullName,
)
continue
}
region := trace.StartRegion(ctx, fmt.Sprintf("setupCompatCollections/%s", collectionName))
collection := database.Collection(collectionName)
// drop remnants of the previous failed run
_ = collection.Drop(ctx)
// if validators are set, create collection with them (otherwise collection will be created on first insert)
if validators := provider.Validators(*handlerF, collectionName); len(validators) > 0 {
var opts options.CreateCollectionOptions
for key, value := range validators {
opts.SetValidator(bson.D{{key, value}})
}
err := database.CreateCollection(ctx, collectionName, &opts)
if err != nil {
var cmdErr *mongo.CommandError
if errors.As(err, &cmdErr) {
// If collection can't be created in MongoDB because MongoDB has a different validator format, it's ok:
require.Contains(tb, cmdErr.Message, `unknown top level operator: $tigrisSchemaString`)
}
}
}
docs := shareddata.Docs(provider)
require.NotEmpty(tb, docs)
res, err := collection.InsertMany(ctx, docs)
require.NoError(tb, err, "%s: handler %q, collection %s", provider.Name(), *handlerF, fullName)
require.Len(tb, res.InsertedIDs, len(docs))
// delete collection unless test failed
tb.Cleanup(func() {
if tb.Failed() {
tb.Logf("Keeping %s for debugging.", fullName)
return
}
err := collection.Drop(ctx)
require.NoError(tb, err)
})
collections = append(collections, collection)
region.End()
}
// TODO opts.AddNonExistentCollection is not needed, always add a non-existent collection
// https://github.com/FerretDB/FerretDB/issues/1545
if opts.AddNonExistentCollection {
nonExistedCollectionName := opts.baseCollectionName + "-non-existent"
collection := database.Collection(nonExistedCollectionName)
collections = append(collections, collection)
}
require.NotEmpty(tb, collections, "all providers were not compatible")
return collections
}