forked from tryvium-travels/memongo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemongo_test.go
More file actions
246 lines (206 loc) · 7.07 KB
/
memongo_test.go
File metadata and controls
246 lines (206 loc) · 7.07 KB
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
package memongo_test
import (
"context"
"fmt"
"strings"
"testing"
"github.com/100mslive/memongo/v2"
"github.com/100mslive/memongo/v2/memongolog"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
"go.mongodb.org/mongo-driver/v2/mongo/readpref"
)
func TestDefaultOptions(t *testing.T) {
versions := []string{"6.0.0", "7.0.0", "8.0.0"}
for _, version := range versions {
t.Run(version, func(t *testing.T) {
server, err := memongo.StartWithOptions(&memongo.Options{
MongoVersion: version,
LogLevel: memongolog.LogLevelDebug,
})
require.NoError(t, err)
defer server.Stop()
client, err := mongo.Connect(options.Client().ApplyURI(server.URI()))
require.NoError(t, err)
require.NoError(t, client.Ping(context.Background(), nil))
})
}
}
func TestWithReplica(t *testing.T) {
versions := []string{"6.0.0", "7.0.0", "8.0.0"}
for _, version := range versions {
t.Run(version, func(t *testing.T) {
server, err := memongo.StartWithOptions(&memongo.Options{
MongoVersion: version,
LogLevel: memongolog.LogLevelDebug,
ShouldUseReplica: true,
})
require.NoError(t, err)
defer server.Stop()
uri := fmt.Sprintf("%s%s", server.URI(), "/retryWrites=false")
client, err := mongo.Connect(options.Client().ApplyURI(uri))
if err != nil {
t.Logf("err Connect: %v", err)
}
require.NoError(t, err)
require.NoError(t, client.Ping(context.Background(), readpref.Primary()))
})
}
}
func TestWithAuth(t *testing.T) {
versions := []string{"6.0.0", "7.0.0", "8.0.0"}
for _, version := range versions {
t.Run(version, func(t *testing.T) {
server, err := memongo.StartWithOptions(&memongo.Options{
MongoVersion: version,
LogLevel: memongolog.LogLevelDebug,
Auth: true,
})
require.NoError(t, err)
defer server.Stop()
client, err := mongo.Connect(options.Client().ApplyURI(server.URI()))
require.NoError(t, err)
require.NoError(t, client.Ping(context.Background(), nil))
// Create a default user admin / 12345 to test auth.
admin := client.Database("admin")
res := admin.RunCommand(context.Background(), bson.D{
{Key: "createUser", Value: "admin"},
{Key: "pwd", Value: "12345"},
{Key: "roles", Value: []bson.M{
{"role": "userAdminAnyDatabase", "db": "admin"},
}},
})
require.NoError(t, res.Err())
// Verify we cannot connect without auth
client2, err := mongo.Connect(options.Client().ApplyURI(server.URI()))
require.NoError(t, err)
require.NoError(t, client2.Ping(context.Background(), nil))
_, err = client2.ListDatabaseNames(context.Background(), bson.D{})
if strings.HasPrefix(version, "7.") || strings.HasPrefix(version, "8.") {
require.EqualError(t, err, "(Unauthorized) Command listDatabases requires authentication")
} else {
require.EqualError(t, err, "(Unauthorized) command listDatabases requires authentication")
}
// Now connect again with auth
opts := options.Client().ApplyURI(server.URI())
opts.Auth = &options.Credential{
Username: "admin",
Password: "12345",
AuthSource: "admin",
}
client3, err := mongo.Connect(opts)
require.NoError(t, err)
require.NoError(t, client3.Ping(context.Background(), nil))
_, err = client3.ListDatabaseNames(context.Background(), bson.D{})
require.NoError(t, err)
})
}
}
func TestWithReplicaAndAuth(t *testing.T) {
versions := []string{"6.0.0", "7.0.0", "8.0.0"}
for _, version := range versions {
t.Run(version, func(t *testing.T) {
server, err := memongo.StartWithOptions(&memongo.Options{
MongoVersion: version,
LogLevel: memongolog.LogLevelDebug,
ShouldUseReplica: true,
Auth: true,
})
require.NoError(t, err)
defer server.Stop()
uri := fmt.Sprintf("%s%s", server.URI(), "/retryWrites=false")
client, err := mongo.Connect(options.Client().ApplyURI(uri))
if err != nil {
t.Logf("err Connect: %v", err)
}
require.NoError(t, err)
require.NoError(t, client.Ping(context.Background(), readpref.Primary()))
// Create a default user admin / 12345 to test auth.
admin := client.Database("admin")
res := admin.RunCommand(context.Background(), bson.D{
{Key: "createUser", Value: "admin"},
{Key: "pwd", Value: "12345"},
{Key: "roles", Value: []bson.M{
{"role": "userAdminAnyDatabase", "db": "admin"},
}},
})
require.NoError(t, res.Err())
// Verify we cannot connect without auth
client2, err := mongo.Connect(options.Client().ApplyURI(server.URI()))
require.NoError(t, err)
require.NoError(t, client2.Ping(context.Background(), nil))
_, err = client2.ListDatabaseNames(context.Background(), bson.D{})
if strings.HasPrefix(version, "7.") || strings.HasPrefix(version, "8.") {
require.EqualError(t, err, "(Unauthorized) Command listDatabases requires authentication")
} else {
require.EqualError(t, err, "(Unauthorized) command listDatabases requires authentication")
}
// Now connect again with auth
opts := options.Client().ApplyURI(server.URI())
opts.Auth = &options.Credential{
Username: "admin",
Password: "12345",
AuthSource: "admin",
}
client3, err := mongo.Connect(opts)
require.NoError(t, err)
require.NoError(t, client3.Ping(context.Background(), nil))
_, err = client3.ListDatabaseNames(context.Background(), bson.D{})
require.NoError(t, err)
})
}
}
func TestServerPing(t *testing.T) {
server, err := memongo.StartWithOptions(&memongo.Options{
MongoVersion: "8.0.0",
LogLevel: memongolog.LogLevelDebug,
})
require.NoError(t, err)
defer server.Stop()
// Test Ping method
err = server.Ping(context.Background())
require.NoError(t, err)
}
func TestServerHelperMethods(t *testing.T) {
server, err := memongo.StartWithOptions(&memongo.Options{
MongoVersion: "8.0.0",
LogLevel: memongolog.LogLevelWarn,
ShouldUseReplica: true,
ReplicaSetName: "customRS",
})
require.NoError(t, err)
defer server.Stop()
// Test IsReplicaSet
require.True(t, server.IsReplicaSet())
// Test ReplicaSetName
require.Equal(t, "customRS", server.ReplicaSetName())
// Test DBPath returns a non-empty path
require.NotEmpty(t, server.DBPath())
require.Contains(t, server.DBPath(), "memongo")
}
func TestServerNotReplicaSet(t *testing.T) {
server, err := memongo.StartWithOptions(&memongo.Options{
MongoVersion: "8.0.0",
LogLevel: memongolog.LogLevelWarn,
})
require.NoError(t, err)
defer server.Stop()
// Test IsReplicaSet returns false
require.False(t, server.IsReplicaSet())
// Test ReplicaSetName returns empty string
require.Empty(t, server.ReplicaSetName())
}
func TestWiredTigerCacheSize(t *testing.T) {
server, err := memongo.StartWithOptions(&memongo.Options{
MongoVersion: "8.0.0",
LogLevel: memongolog.LogLevelWarn,
WiredTigerCacheSizeGB: 0.25, // 256MB cache
})
require.NoError(t, err)
defer server.Stop()
// Verify server starts successfully with cache size limit
err = server.Ping(context.Background())
require.NoError(t, err)
}