Skip to content

Commit

Permalink
Cleanup integration test for setting up user (#4160)
Browse files Browse the repository at this point in the history
Closes #4100.
  • Loading branch information
chilagrow committed Mar 14, 2024
1 parent 58ff7b0 commit 1bfc549
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 91 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Expand Up @@ -317,7 +317,7 @@ tasks:
-coverpkg=../...
-coverprofile=integration-mongodb.txt
./...
-target-url='mongodb://127.0.0.1:47017/'
-target-url='mongodb://username:password@127.0.0.1:47018/?tls=true&tlsCertificateKeyFile=../build/certs/client.pem&tlsCaFile=../build/certs/rootCA-cert.pem&replicaSet=rs0'
-target-backend=mongodb
bench-unit:
Expand Down
24 changes: 10 additions & 14 deletions integration/cursors/getmore_test.go
Expand Up @@ -606,17 +606,6 @@ func TestCursorsGetMoreCommandConnection(t *testing.T) {
t := setup.FailsForFerretDB(tt, "https://github.com/FerretDB/FerretDB/issues/153")

// do not run subtest in parallel to avoid breaking another parallel subtest

u, err := url.Parse(s.MongoDBURI)
require.NoError(t, err)

client2, err := mongo.Connect(ctx, options.Client().ApplyURI(u.String()))
require.NoError(t, err)

defer client2.Disconnect(ctx)

collection2 := client2.Database(databaseName).Collection(collectionName)

var res bson.D
err = collection1.Database().RunCommand(
ctx,
Expand All @@ -638,15 +627,22 @@ func TestCursorsGetMoreCommandConnection(t *testing.T) {
cursorID, _ := cursor.Get("id")
assert.NotNil(t, cursorID)

err = collection2.Database().RunCommand(
client2, err := mongo.Connect(ctx, options.Client().ApplyURI(s.MongoDBURI))
require.NoError(t, err)

t.Cleanup(func() {
require.NoError(t, client2.Disconnect(ctx))
})

err = client2.Database(databaseName).RunCommand(
ctx,
bson.D{
{"getMore", cursorID},
{"collection", collection2.Name()},
{"collection", client2.Database(databaseName).Collection(collectionName).Name()},
},
).Decode(&res)

integration.AssertMatchesCommandError(t, mongo.CommandError{Code: 50738, Name: "Location50738"}, err)
integration.AssertMatchesCommandError(t, mongo.CommandError{Code: 13, Name: "Unauthorized"}, err)
})
}

Expand Down
7 changes: 1 addition & 6 deletions integration/cursors/tailable_sessions_test.go
Expand Up @@ -15,7 +15,6 @@
package cursors

import (
"errors"
"sync/atomic"
"testing"

Expand Down Expand Up @@ -88,11 +87,7 @@ func TestTailableCursorsBetweenSessions(tt *testing.T) {
for i := 1; i < 50; i++ {
err = db.RunCommand(ctx, getMoreCmd).Err()
if err != nil {
var ce mongo.CommandError

require.True(t, errors.As(err, &ce))
require.Equal(t, int32(50738), ce.Code)
require.Equal(t, "Location50738", ce.Name)
integration.AssertMatchesCommandError(t, mongo.CommandError{Code: 13, Name: "Unauthorized"}, err)

passed.Store(true)
return
Expand Down
8 changes: 5 additions & 3 deletions integration/distinct_test.go
Expand Up @@ -36,7 +36,8 @@ func TestDistinctCommandErrors(t *testing.T) {
collName any // optional, defaults to coll.Name()
filter any // required

err *mongo.CommandError
err *mongo.CommandError
altMessage string
}{
"StringFilter": {
command: "a",
Expand Down Expand Up @@ -64,8 +65,9 @@ func TestDistinctCommandErrors(t *testing.T) {
err: &mongo.CommandError{
Code: 73,
Name: "InvalidNamespace",
Message: "collection name has invalid type object",
Message: "Failed to parse namespace element",
},
altMessage: "collection name has invalid type object",
},
"WrongTypeObject": {
command: bson.D{},
Expand Down Expand Up @@ -114,7 +116,7 @@ func TestDistinctCommandErrors(t *testing.T) {
err := collection.Database().RunCommand(ctx, command).Decode(res)

assert.Nil(t, res)
AssertEqualCommandError(t, *tc.err, err)
AssertEqualAltCommandError(t, *tc.err, tc.altMessage, err)
})
}
}
Expand Down
91 changes: 52 additions & 39 deletions integration/query_test.go
Expand Up @@ -39,104 +39,117 @@ func TestQueryBadFindType(t *testing.T) {
ctx, collection := s.Ctx, s.Collection

for name, tc := range map[string]struct {
value any
err *mongo.CommandError
value any
err *mongo.CommandError
altMessage string
}{
"Document": {
value: bson.D{},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type object",
Code: 73,
Name: "InvalidNamespace",
Message: "Failed to parse namespace element",
},
altMessage: "collection name has invalid type object",
},
"Array": {
value: primitive.A{},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type array",
Code: 73,
Name: "InvalidNamespace",
Message: "Failed to parse namespace element",
},
altMessage: "collection name has invalid type array",
},
"Double": {
value: 3.14,
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type double",
Code: 73,
Name: "InvalidNamespace",
Message: "Failed to parse namespace element",
},
altMessage: "collection name has invalid type double",
},
"Binary": {
value: primitive.Binary{},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type binData",
Code: 73,
Name: "InvalidNamespace",
Message: "Failed to parse namespace element",
},
altMessage: "collection name has invalid type binData",
},
"ObjectID": {
value: primitive.ObjectID{},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type objectId",
Code: 73,
Name: "InvalidNamespace",
Message: "Failed to parse namespace element",
},
altMessage: "collection name has invalid type objectId",
},
"Bool": {
value: true,
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type bool",
Code: 73,
Name: "InvalidNamespace",
Message: "Failed to parse namespace element",
},
altMessage: "collection name has invalid type bool",
},
"Date": {
value: time.Now(),
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type date",
Code: 73,
Name: "InvalidNamespace",
Message: "Failed to parse namespace element",
},
altMessage: "collection name has invalid type date",
},
"Null": {
value: nil,
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type null",
Code: 73,
Name: "InvalidNamespace",
Message: "Failed to parse namespace element",
},
altMessage: "collection name has invalid type null",
},
"Regex": {
value: primitive.Regex{Pattern: "/foo/"},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type regex",
Code: 73,
Name: "InvalidNamespace",
Message: "Failed to parse namespace element",
},
altMessage: "collection name has invalid type regex",
},
"Int": {
value: int32(42),
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type int",
Code: 73,
Name: "InvalidNamespace",
Message: "Failed to parse namespace element",
},
altMessage: "collection name has invalid type int",
},
"Timestamp": {
value: primitive.Timestamp{},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type timestamp",
Code: 73,
Name: "InvalidNamespace",
Message: "Failed to parse namespace element",
},
altMessage: "collection name has invalid type timestamp",
},
"Long": {
value: int64(42),
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type long",
Code: 73,
Name: "InvalidNamespace",
Message: "Failed to parse namespace element",
},
altMessage: "collection name has invalid type long",
},
} {
name, tc := name, tc
Expand All @@ -153,7 +166,7 @@ func TestQueryBadFindType(t *testing.T) {
err := collection.Database().RunCommand(ctx, cmd).Decode(&res)

require.Nil(t, res)
AssertEqualCommandError(t, *tc.err, err)
AssertEqualAltCommandError(t, *tc.err, tc.altMessage, err)
})
}
}
Expand Down
41 changes: 41 additions & 0 deletions integration/setup/client.go
Expand Up @@ -16,6 +16,9 @@ package setup

import (
"context"
"net/url"
"os"
"path/filepath"

"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -76,3 +79,41 @@ func setupClient(tb testtb.TB, ctx context.Context, uri string) *mongo.Client {

return client
}

// toAbsolutePathURI replaces the relative path of tlsCertificateKeyFile and tlsCaFile path
// to an absolute path. If the file is not found because the test is in subdirectory
// such as`integration/user/`, the parent directory is looked.
func toAbsolutePathURI(tb testtb.TB, uri string) string {
u, err := url.Parse(uri)
require.NoError(tb, err)

values := url.Values{}

for k, v := range u.Query() {
require.Len(tb, v, 1)

switch k {
case "tlsCertificateKeyFile", "tlsCaFile":
file := v[0]

if filepath.IsAbs(file) {
values[k] = []string{file}

continue
}

file = filepath.Join(Dir(tb), v[0])
if _, err = os.Stat(file); os.IsNotExist(err) {
file = filepath.Join(Dir(tb), "..", v[0])
}

values[k] = []string{file}
default:
values[k] = v
}
}

u.RawQuery = values.Encode()

return u.String()
}

0 comments on commit 1bfc549

Please sign in to comment.