...
1
2
3
4
5
6
7 package mtest
8
9 import (
10 "errors"
11 "fmt"
12
13 "go.mongodb.org/mongo-driver/bson"
14 "go.mongodb.org/mongo-driver/mongo/options"
15 )
16
17
18 type TopologyKind string
19
20
21 const (
22 ReplicaSet TopologyKind = "replicaset"
23 Sharded TopologyKind = "sharded"
24 Single TopologyKind = "single"
25 LoadBalanced TopologyKind = "load-balanced"
26
27
28 ShardedReplicaSet TopologyKind = "sharded-replicaset"
29 )
30
31
32 type ClientType int
33
34
35 const (
36
37
38 Default ClientType = iota
39
40 Pinned
41
42 Mock
43
44
45 Proxy
46 )
47
48 var (
49 falseBool = false
50 )
51
52
53 type RunOnBlock struct {
54 MinServerVersion string `bson:"minServerVersion"`
55 MaxServerVersion string `bson:"maxServerVersion"`
56 Topology []TopologyKind `bson:"topology"`
57 Serverless string `bson:"serverless"`
58 ServerParameters map[string]bson.RawValue `bson:"serverParameters"`
59 Auth *bool `bson:"auth"`
60 AuthEnabled *bool `bson:"authEnabled"`
61 CSFLE *bool `bson:"csfle"`
62 }
63
64
65
66 func (r *RunOnBlock) UnmarshalBSON(data []byte) error {
67 var temp struct {
68 MinServerVersion string `bson:"minServerVersion"`
69 MaxServerVersion string `bson:"maxServerVersion"`
70 Topology []TopologyKind `bson:"topology"`
71 Topologies []TopologyKind `bson:"topologies"`
72 Serverless string `bson:"serverless"`
73 ServerParameters map[string]bson.RawValue `bson:"serverParameters"`
74 Auth *bool `bson:"auth"`
75 AuthEnabled *bool `bson:"authEnabled"`
76 CSFLE *bool `bson:"csfle"`
77 Extra map[string]interface{} `bson:",inline"`
78 }
79 if err := bson.Unmarshal(data, &temp); err != nil {
80 return fmt.Errorf("error unmarshalling to temporary RunOnBlock object: %w", err)
81 }
82 if len(temp.Extra) > 0 {
83 return fmt.Errorf("unrecognized fields for RunOnBlock: %v", temp.Extra)
84 }
85
86 r.MinServerVersion = temp.MinServerVersion
87 r.MaxServerVersion = temp.MaxServerVersion
88 r.Serverless = temp.Serverless
89 r.ServerParameters = temp.ServerParameters
90 r.Auth = temp.Auth
91 r.AuthEnabled = temp.AuthEnabled
92 r.CSFLE = temp.CSFLE
93
94 if temp.Topology != nil {
95 r.Topology = temp.Topology
96 }
97 if temp.Topologies != nil {
98 if r.Topology != nil {
99 return errors.New("both 'topology' and 'topologies' keys cannot be specified for a RunOnBlock")
100 }
101
102 r.Topology = temp.Topologies
103 }
104 return nil
105 }
106
107
108 type optionFunc func(*T)
109
110
111 type Options struct {
112 optFuncs []optionFunc
113 }
114
115
116 func NewOptions() *Options {
117 return &Options{}
118 }
119
120
121 func (op *Options) CollectionCreateOptions(opts *options.CreateCollectionOptions) *Options {
122 op.optFuncs = append(op.optFuncs, func(t *T) {
123 t.collCreateOpts = opts
124 })
125 return op
126 }
127
128
129 func (op *Options) CollectionOptions(opts *options.CollectionOptions) *Options {
130 op.optFuncs = append(op.optFuncs, func(t *T) {
131 t.collOpts = opts
132 })
133 return op
134 }
135
136
137 func (op *Options) ClientOptions(opts *options.ClientOptions) *Options {
138 op.optFuncs = append(op.optFuncs, func(t *T) {
139 t.clientOpts = opts
140 })
141 return op
142 }
143
144
145
146 func (op *Options) CreateClient(create bool) *Options {
147 op.optFuncs = append(op.optFuncs, func(t *T) {
148 t.createClient = &create
149 })
150 return op
151 }
152
153
154 func (op *Options) CreateCollection(create bool) *Options {
155 op.optFuncs = append(op.optFuncs, func(t *T) {
156 t.createCollection = &create
157 })
158 return op
159 }
160
161
162
163
164 func (op *Options) ShareClient(share bool) *Options {
165 op.optFuncs = append(op.optFuncs, func(t *T) {
166 t.shareClient = &share
167 })
168 return op
169 }
170
171
172 func (op *Options) CollectionName(collName string) *Options {
173 op.optFuncs = append(op.optFuncs, func(t *T) {
174 t.collName = collName
175 })
176 return op
177 }
178
179
180 func (op *Options) DatabaseName(dbName string) *Options {
181 op.optFuncs = append(op.optFuncs, func(t *T) {
182 t.dbName = dbName
183 })
184 return op
185 }
186
187
188
189
190 func (op *Options) ClientType(ct ClientType) *Options {
191 op.optFuncs = append(op.optFuncs, func(t *T) {
192 t.clientType = ct
193
194 if ct == Proxy {
195 t.ssl = &falseBool
196 }
197 })
198 return op
199 }
200
201
202
203 func (op *Options) MockResponses(responses ...bson.D) *Options {
204 op.optFuncs = append(op.optFuncs, func(t *T) {
205 t.mockResponses = responses
206 })
207 return op
208 }
209
210
211
212 func (op *Options) RunOn(blocks ...RunOnBlock) *Options {
213 op.optFuncs = append(op.optFuncs, func(t *T) {
214 t.runOn = append(t.runOn, blocks...)
215 })
216 return op
217 }
218
219
220 func (op *Options) MinServerVersion(version string) *Options {
221 op.optFuncs = append(op.optFuncs, func(t *T) {
222 t.minServerVersion = version
223 })
224 return op
225 }
226
227
228 func (op *Options) MaxServerVersion(version string) *Options {
229 op.optFuncs = append(op.optFuncs, func(t *T) {
230 t.maxServerVersion = version
231 })
232 return op
233 }
234
235
236 func (op *Options) Topologies(topos ...TopologyKind) *Options {
237 op.optFuncs = append(op.optFuncs, func(t *T) {
238 t.validTopologies = topos
239 })
240 return op
241 }
242
243
244
245 func (op *Options) Auth(auth bool) *Options {
246 op.optFuncs = append(op.optFuncs, func(t *T) {
247 t.auth = &auth
248 })
249 return op
250 }
251
252
253
254 func (op *Options) SSL(ssl bool) *Options {
255 op.optFuncs = append(op.optFuncs, func(t *T) {
256 t.ssl = &ssl
257 })
258 return op
259 }
260
261
262 func (op *Options) Enterprise(ent bool) *Options {
263 op.optFuncs = append(op.optFuncs, func(t *T) {
264 t.enterprise = &ent
265 })
266 return op
267 }
268
269
270 func (op *Options) AtlasDataLake(adl bool) *Options {
271 op.optFuncs = append(op.optFuncs, func(t *T) {
272 t.dataLake = &adl
273 })
274 return op
275 }
276
277
278 func (op *Options) RequireAPIVersion(rav bool) *Options {
279 op.optFuncs = append(op.optFuncs, func(t *T) {
280 t.requireAPIVersion = &rav
281 })
282 return op
283 }
284
View as plain text