-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
client_test.go
318 lines (274 loc) · 8.29 KB
/
client_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package mongodbreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbreceiver"
import (
"context"
"errors"
"os"
"testing"
"github.com/hashicorp/go-version"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
"go.mongodb.org/mongo-driver/mongo/options"
"go.uber.org/zap"
)
// fakeClient is a mocked client of the scraper
// was not able to optimize scraping multiple databases via goroutines
// while also testing with exclusively mtest.
type fakeClient struct{ mock.Mock }
func (fc *fakeClient) ListDatabaseNames(ctx context.Context, filters any, opts ...*options.ListDatabasesOptions) ([]string, error) {
args := fc.Called(ctx, filters, opts)
return args.Get(0).([]string), args.Error(1)
}
func (fc *fakeClient) ListCollectionNames(ctx context.Context, dbName string) ([]string, error) {
args := fc.Called(ctx, dbName)
return args.Get(0).([]string), args.Error(1)
}
func (fc *fakeClient) Disconnect(ctx context.Context) error {
args := fc.Called(ctx)
return args.Error(0)
}
func (fc *fakeClient) Connect(ctx context.Context) error {
args := fc.Called(ctx)
return args.Error(0)
}
func (fc *fakeClient) GetVersion(ctx context.Context) (*version.Version, error) {
args := fc.Called(ctx)
return args.Get(0).(*version.Version), args.Error(1)
}
func (fc *fakeClient) ServerStatus(ctx context.Context, dbName string) (bson.M, error) {
args := fc.Called(ctx, dbName)
return args.Get(0).(bson.M), args.Error(1)
}
func (fc *fakeClient) DBStats(ctx context.Context, dbName string) (bson.M, error) {
args := fc.Called(ctx, dbName)
return args.Get(0).(bson.M), args.Error(1)
}
func (fc *fakeClient) TopStats(ctx context.Context) (bson.M, error) {
args := fc.Called(ctx)
return args.Get(0).(bson.M), args.Error(1)
}
func (fc *fakeClient) IndexStats(ctx context.Context, dbName, collectionName string) ([]bson.M, error) {
args := fc.Called(ctx, dbName, collectionName)
return args.Get(0).([]bson.M), args.Error(1)
}
func TestListDatabaseNames(t *testing.T) {
mont := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
mont.Run("list database names", func(mt *mtest.T) {
// mocking out a listdatabase call
mt.AddMockResponses(mtest.CreateSuccessResponse(
primitive.E{
Key: "databases",
Value: []struct {
Name string `bson:"name,omitempty"`
}{
{
Name: "admin",
},
},
}))
driver := mt.Client
client := &mongodbClient{
Client: driver,
}
dbNames, err := client.ListDatabaseNames(context.Background(), bson.D{})
require.NoError(t, err)
require.Equal(t, "admin", dbNames[0])
})
}
type commandString = string
const (
dbStatsType commandString = "dbStats"
serverStatusType commandString = "serverStatus"
topType commandString = "top"
)
func TestRunCommands(t *testing.T) {
mont := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
loadedDbStats, err := loadDBStats()
require.NoError(t, err)
loadedServerStatus, err := loadServerStatus()
require.NoError(t, err)
loadedTop, err := loadTop()
require.NoError(t, err)
testCases := []struct {
desc string
cmd commandString
response bson.D
validate func(*testing.T, bson.M)
}{
{
desc: "dbStats success",
cmd: dbStatsType,
response: loadedDbStats,
validate: func(t *testing.T, m bson.M) {
require.Equal(t, float64(16384), m["indexSize"])
},
},
{
desc: "serverStatus success",
cmd: serverStatusType,
response: loadedServerStatus,
validate: func(t *testing.T, m bson.M) {
require.Equal(t, int32(0), m["mem"].(bson.M)["mapped"])
},
},
{
desc: "top success",
cmd: topType,
response: loadedTop,
validate: func(t *testing.T, m bson.M) {
require.Equal(t, int32(540), m["totals"].(bson.M)["local.oplog.rs"].(bson.M)["commands"].(bson.M)["time"])
},
},
}
for _, tc := range testCases {
mont.Run(tc.desc, func(mt *mtest.T) {
mt.AddMockResponses(tc.response)
driver := mt.Client
client := mongodbClient{
Client: driver,
logger: zap.NewNop(),
}
var result bson.M
switch tc.cmd {
case serverStatusType:
result, err = client.ServerStatus(context.Background(), "test")
case dbStatsType:
result, err = client.DBStats(context.Background(), "test")
case topType:
result, err = client.TopStats(context.Background())
}
require.NoError(t, err)
if tc.validate != nil {
tc.validate(t, result)
}
})
}
}
func TestGetVersion(t *testing.T) {
mont := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
buildInfo, err := loadBuildInfo()
require.NoError(t, err)
mont.Run("test connection", func(mt *mtest.T) {
mt.AddMockResponses(
// retrieving build info
buildInfo,
)
driver := mt.Client
client := mongodbClient{
Client: driver,
logger: zap.NewNop(),
}
version, err := client.GetVersion(context.TODO())
require.NoError(t, err)
require.Equal(t, "4.4.10", version.String())
})
}
func TestGetVersionFailures(t *testing.T) {
mont := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
malformedBuildInfo := bson.D{
primitive.E{Key: "ok", Value: 1},
primitive.E{Key: "version", Value: 1},
}
testCases := []struct {
desc string
responses []primitive.D
partialError string
}{
{
desc: "Unable to run buildInfo",
responses: []primitive.D{mtest.CreateCommandErrorResponse(mtest.CommandError{})},
partialError: "unable to get build info",
},
{
desc: "unable to parse version",
responses: []primitive.D{mtest.CreateSuccessResponse(), malformedBuildInfo},
partialError: "unable to parse mongo version from server",
},
}
for _, tc := range testCases {
mont.Run(tc.desc, func(mt *mtest.T) {
mt.AddMockResponses(tc.responses...)
driver := mt.Client
client := mongodbClient{
Client: driver,
logger: zap.NewNop(),
}
_, err := client.GetVersion(context.TODO())
require.ErrorContains(t, err, tc.partialError)
})
}
}
func loadDBStats() (bson.D, error) {
return loadTestFile("./testdata/dbstats.json")
}
func loadDBStatsAsMap() (bson.M, error) {
return loadTestFileAsMap("./testdata/dbstats.json")
}
func loadServerStatus() (bson.D, error) {
return loadTestFile("./testdata/serverStatus.json")
}
func loadServerStatusAsMap() (bson.M, error) {
return loadTestFileAsMap("./testdata/serverStatus.json")
}
func loadTop() (bson.D, error) {
return loadTestFile("./testdata/top.json")
}
func loadTopAsMap() (bson.M, error) {
return loadTestFileAsMap("./testdata/top.json")
}
func loadIndexStatsAsMap(collectionName string) ([]bson.M, error) {
var indexStats []bson.M
switch collectionName {
case "products":
indexStats0, _ := loadTestFileAsMap("./testdata/productsIndexStats0.json")
indexStats = append(indexStats, indexStats0)
case "orders":
indexStats0, _ := loadTestFileAsMap("./testdata/ordersIndexStats0.json")
indexStats1, _ := loadTestFileAsMap("./testdata/ordersIndexStats1.json")
indexStats2, _ := loadTestFileAsMap("./testdata/ordersIndexStats2.json")
indexStats = append(indexStats, indexStats0, indexStats1, indexStats2)
case "error":
indexStatsError, _ := loadTestFileAsMap("./testdata/indexStatsError.json")
indexStats = append(indexStats, indexStatsError)
default:
return nil, errors.New("failed to load index stats from an unknown collection name")
}
return indexStats, nil
}
func loadBuildInfo() (bson.D, error) {
return loadTestFile("./testdata/buildInfo.json")
}
func loadAdminStatusAsMap() (bson.M, error) {
return loadTestFileAsMap("./testdata/admin.json")
}
func loadOnlyStorageEngineAsMap() (bson.M, error) {
return loadTestFileAsMap("./testdata/only_storage_engine.json")
}
func loadTestFile(filePath string) (bson.D, error) {
var doc bson.D
testFile, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
err = bson.UnmarshalExtJSON(testFile, true, &doc)
if err != nil {
return nil, err
}
return doc, nil
}
func loadTestFileAsMap(filePath string) (bson.M, error) {
var doc bson.M
testFile, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
err = bson.UnmarshalExtJSON(testFile, true, &doc)
if err != nil {
return nil, err
}
return doc, nil
}