Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use authentication enabled docker for integration test #4160

Merged
merged 46 commits into from Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
8f0483c
wip
chilagrow Mar 6, 2024
1ebdda2
implement local host exception
chilagrow Mar 7, 2024
95c5d4e
fix logout
chilagrow Mar 7, 2024
ec0bc37
Add helper for checking local peers
AlekSi Mar 7, 2024
ba2da29
lint
chilagrow Mar 7, 2024
64376be
Merge branch 'main' into localhost-exception
chilagrow Mar 7, 2024
68d5cc0
update logout test
chilagrow Mar 7, 2024
2c24034
Merge branch 'local' into localhost-exception
chilagrow Mar 7, 2024
4e1388f
update local host exception
chilagrow Mar 7, 2024
baf646c
fmt
chilagrow Mar 7, 2024
a503adc
update
chilagrow Mar 7, 2024
7197ad1
make connectionStatus same as proxy
chilagrow Mar 7, 2024
5e838f7
add re-authenticate test and cleanup tests
chilagrow Mar 8, 2024
7c7c2e4
use authenticated user for supported mechanisms test
chilagrow Mar 8, 2024
205d7d0
fix test
chilagrow Mar 8, 2024
681855f
revert setup back
chilagrow Mar 8, 2024
dd2d240
add ipv6 test
chilagrow Mar 8, 2024
f3bbd12
remove mongodb specific condition on setup user
chilagrow Mar 8, 2024
e7a10f8
cleanup user
chilagrow Mar 8, 2024
b71bada
user is created per test and authenticated user is used as client
chilagrow Mar 11, 2024
f594641
check error
chilagrow Mar 11, 2024
0aa5c97
Merge branch 'main' into localhost-exception
AlekSi Mar 11, 2024
ae8c54d
merge conflict
chilagrow Mar 11, 2024
27c402c
handle empty mechanism for local host exception
chilagrow Mar 12, 2024
d84d716
use command error in case other driver can reach this path
chilagrow Mar 12, 2024
17ffa48
revert change on connectionStatus
chilagrow Mar 12, 2024
e1c6014
Merge branch 'localhost-exception' into cleanup-setupuser
chilagrow Mar 12, 2024
fcf8445
just check user
chilagrow Mar 12, 2024
8f662c7
Merge branch 'localhost-exception' into cleanup-setupuser
chilagrow Mar 12, 2024
004abe6
remove confusing error message
chilagrow Mar 12, 2024
8807bcb
Merge branch 'localhost-exception' into cleanup-setupuser
chilagrow Mar 12, 2024
2822e6c
use authenticated mongodb for test-integration-mongodb
chilagrow Mar 12, 2024
80767f7
merge conflict
chilagrow Mar 13, 2024
30cacc5
use absolute path for mongodb uri
chilagrow Mar 13, 2024
6904c53
assign authorization roles for mongodb users
chilagrow Mar 13, 2024
0603c84
fix changed error code upon authentication
chilagrow Mar 13, 2024
8865890
tablable session and getMore returns authorization error
chilagrow Mar 13, 2024
6b09aad
test cleanup
chilagrow Mar 13, 2024
41dcb5c
readability
chilagrow Mar 13, 2024
09a41f0
update finding certificates
chilagrow Mar 13, 2024
bb434c0
lint
chilagrow Mar 13, 2024
6ef0705
avoid conflict with other PR
chilagrow Mar 13, 2024
ee7a42d
idiomacy
chilagrow Mar 13, 2024
719728a
Merge branch 'main' into cleanup-setupuser
AlekSi Mar 13, 2024
90d2505
Merge branch 'main' into cleanup-setupuser
AlekSi Mar 14, 2024
b063d16
Merge branch 'main' into cleanup-setupuser
AlekSi Mar 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
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
39 changes: 39 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,39 @@ func setupClient(tb testtb.TB, ctx context.Context, uri string) *mongo.Client {

return client
}

// toAbsolutePathUri replaces tlsCertificateKeyFile and tlsCaFile path to an absolute path.
// If the test is run from subdirectory such as `integration/user/`, the absolute path
// is found by looking for the file in the parent directory.
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) {
file = filepath.Join(Dir(tb), v[0])
}

_, err := os.Stat(file)
if 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()
}