Skip to content

Commit

Permalink
Fix tests for MongoDB with enabled replica set (#3807)
Browse files Browse the repository at this point in the history
Closes #3310.

Co-authored-by: Alexey Palazhchenko <alexey.palazhchenko@ferretdb.io>
  • Loading branch information
Elena Grahovac and AlekSi authored Dec 7, 2023
1 parent fe268cb commit 514acee
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 40 deletions.
8 changes: 4 additions & 4 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ tasks:
-target-backend=ferretdb-postgresql
-target-tls
-postgresql-url='postgres://username@127.0.0.1:5432/ferretdb?search_path='
-compat-url='mongodb://username:password@127.0.0.1:47018/?tls=true&tlsCertificateKeyFile=../build/certs/client.pem&tlsCaFile=../build/certs/rootCA-cert.pem'
-compat-url='mongodb://username:password@127.0.0.1:47018/?tls=true&tlsCertificateKeyFile=../build/certs/client.pem&tlsCaFile=../build/certs/rootCA-cert.pem&replicaSet=mongodb-rs'
-disable-pushdown={{.DISABLE_PUSHDOWN}}
test-integration-sqlite:
Expand All @@ -240,7 +240,7 @@ tasks:
-target-backend=ferretdb-sqlite
-sqlite-url=file:../tmp/sqlite-tests/
-target-tls
-compat-url='mongodb://username:password@127.0.0.1:47018/?tls=true&tlsCertificateKeyFile=../build/certs/client.pem&tlsCaFile=../build/certs/rootCA-cert.pem'
-compat-url='mongodb://username:password@127.0.0.1:47018/?tls=true&tlsCertificateKeyFile=../build/certs/client.pem&tlsCaFile=../build/certs/rootCA-cert.pem&replicaSet=mongodb-rs'
-disable-pushdown={{.DISABLE_PUSHDOWN}}
test-integration-mysql:
Expand All @@ -265,7 +265,7 @@ tasks:
-target-backend=ferretdb-mysql
-target-tls
-mysql-url='mysql://username:password@127.0.0.1:3306/ferretdb'
-compat-url='mongodb://username:password@127.0.0.1:47018/?tls=true&tlsCertificateKeyFile=../build/certs/client.pem&tlsCaFile=../build/certs/rootCA-cert.pem'
-compat-url='mongodb://username:password@127.0.0.1:47018/?tls=true&tlsCertificateKeyFile=../build/certs/client.pem&tlsCaFile=../build/certs/rootCA-cert.pem&replicaSet=mongodb-rs'
-disable-pushdown={{.DISABLE_PUSHDOWN}}
test-integration-hana:
Expand All @@ -290,7 +290,7 @@ tasks:
-target-backend=ferretdb-hana
-target-tls
-hana-url=$FERRETDB_HANA_URL
-compat-url='mongodb://username:password@127.0.0.1:47018/?tls=true&tlsCertificateKeyFile=../build/certs/client.pem&tlsCaFile=../build/certs/rootCA-cert.pem'
-compat-url='mongodb://username:password@127.0.0.1:47018/?tls=true&tlsCertificateKeyFile=../build/certs/client.pem&tlsCaFile=../build/certs/rootCA-cert.pem&replicaSet=mongodb-rs'
-disable-pushdown={{.DISABLE_PUSHDOWN}}
test-integration-mongodb:
Expand Down
12 changes: 0 additions & 12 deletions build/deps/mongo.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
FROM mongo:6.0.9

# If you encounter an "unknown instruction" error there,
# please update Docker to the latest version.

# see https://github.com/docker-library/mongo/issues/475
RUN <<EOF
set -ex

echo 'topsecret' > /etc/mongod_keyfile.txt
chmod 0400 /etc/mongod_keyfile.txt
chown 999:999 /etc/mongod_keyfile.txt
EOF
10 changes: 5 additions & 5 deletions build/mongodb_secured.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ net:
mode: requireTLS
certificateKeyFile: /etc/certs/server.pem
CAFile: /etc/certs/rootCA-cert.pem
clusterFile: /etc/certs/client.pem

security:
authorization: enabled # that also enables authentication
keyFile: /etc/mongod_keyfile.txt
clusterAuthMode: x509

# TODO https://github.com/FerretDB/FerretDB/issues/3310
# replication:
# oplogSizeMB: 512
# replSetName: mongodb-rs
replication:
oplogSizeMB: 512
replSetName: mongodb-rs
28 changes: 26 additions & 2 deletions cmd/envtool/envtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,32 @@ func setupMongodb(ctx context.Context, logger *zap.SugaredLogger) error {

// setupMongodbSecured configures `mongodb_secured` container.
func setupMongodbSecured(ctx context.Context, logger *zap.SugaredLogger) error {
// TODO https://github.com/FerretDB/FerretDB/issues/3310
return waitForPort(ctx, logger.Named("mongodb_secured"), 47018)
if err := waitForPort(ctx, logger.Named("mongodb_secured"), 47018); err != nil {
return err
}

eval := `'rs.initiate({_id: "mongodb-rs", members: [{_id: 0, host: "localhost:47018" }]})'`
shell := `mongodb://username:password@127.0.0.1:47018/?tls=true&tlsCertificateKeyFile=/etc/certs/client.pem&tlsCaFile=/etc/certs/rootCA-cert.pem` //nolint:lll // for readability
args := []string{"compose", "exec", "-T", "mongodb_secured", "mongosh", "--eval", eval, "--shell", shell}

var buf bytes.Buffer
var retry int64

for ctx.Err() == nil {
buf.Reset()

err := runCommand("docker", args, &buf, logger)
if err == nil {
break
}

logger.Infof("%s:\n%s", err, buf.String())

retry++
ctxutil.SleepWithJitter(ctx, time.Second, retry)
}

return ctx.Err()
}

// setup runs all setup commands.
Expand Down
9 changes: 8 additions & 1 deletion integration/aggregate_documents_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/FerretDB/FerretDB/integration/setup"
"github.com/FerretDB/FerretDB/integration/shareddata"
"github.com/FerretDB/FerretDB/internal/util/testutil"
)

// aggregateStagesCompatTestCase describes aggregation stages compatibility test case.
Expand Down Expand Up @@ -227,7 +228,13 @@ func testAggregateCommandCompat(t *testing.T, testCases map[string]aggregateComm
return
}
require.NoError(t, compatErr, "compat error; target returned no error")
AssertEqualDocuments(t, compatRes, targetRes)

compat := ConvertDocument(t, compatRes)
compat.Remove("$clusterTime")
compat.Remove("operationTime")

target := ConvertDocument(t, targetRes)
testutil.AssertEqual(t, compat, target)

if len(targetRes) > 0 || len(compatRes) > 0 {
nonEmptyResults = true
Expand Down
8 changes: 7 additions & 1 deletion integration/count_command_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"

"github.com/FerretDB/FerretDB/integration/setup"
"github.com/FerretDB/FerretDB/internal/util/testutil"
)

type countCommandCompatTestCase struct {
Expand Down Expand Up @@ -99,7 +100,12 @@ func testCountCommandCompat(t *testing.T, testCases map[string]countCommandCompa
require.NoError(t, targetResult.Decode(&targetRes))
require.NoError(t, compatResult.Decode(&compatRes))

AssertEqualDocuments(t, compatRes, targetRes)
compatDoc := ConvertDocument(t, compatRes)
compatDoc.Remove("$clusterTime")
compatDoc.Remove("operationTime")

targetDoc := ConvertDocument(t, targetRes)
testutil.AssertEqual(t, compatDoc, targetDoc)

targetCount := targetRes.Map()["n"].(int32)
compatCount := compatRes.Map()["n"].(int32)
Expand Down
9 changes: 8 additions & 1 deletion integration/count_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/FerretDB/FerretDB/integration/setup"
"github.com/FerretDB/FerretDB/integration/shareddata"
"github.com/FerretDB/FerretDB/internal/util/testutil"
)

// countCompatTestCase describes count compatibility test case.
Expand Down Expand Up @@ -99,7 +100,13 @@ func testCountCompat(t *testing.T, testCases map[string]countCompatTestCase) {

t.Logf("Compat (expected) result: %v", compatRes)
t.Logf("Target (actual) result: %v", targetRes)
assert.Equal(t, compatRes, targetRes)

compatDoc := ConvertDocument(t, compatRes)
compatDoc.Remove("$clusterTime")
compatDoc.Remove("operationTime")

targetDoc := ConvertDocument(t, targetRes)
testutil.AssertEqual(t, compatDoc, targetDoc)

if targetRes != nil || compatRes != nil {
nonEmptyResults = true
Expand Down
10 changes: 9 additions & 1 deletion integration/delete_command_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/FerretDB/FerretDB/integration/setup"
"github.com/FerretDB/FerretDB/integration/shareddata"
"github.com/FerretDB/FerretDB/internal/util/testutil"
)

// deleteCommandCompatTestCase describes delete compatibility test case.
Expand Down Expand Up @@ -149,5 +150,12 @@ func TestDeleteCommandCompatNotExistingDatabase(t *testing.T) {
assert.NoError(t, targetErr)
assert.NoError(t, compatErr)

assert.Equal(t, compatRes, targetRes)
compat := ConvertDocument(t, compatRes)
compat.Remove("$clusterTime")
compat.Remove("operationTime")
compat.Remove("electionId")
compat.Remove("opTime")

target := ConvertDocument(t, targetRes)
testutil.AssertEqual(t, compat, target)
}
8 changes: 7 additions & 1 deletion integration/findandmodify_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"

"github.com/FerretDB/FerretDB/integration/setup"
"github.com/FerretDB/FerretDB/internal/util/testutil"
)

func TestFindAndModifyCompatSimple(t *testing.T) {
Expand Down Expand Up @@ -836,7 +837,12 @@ func testFindAndModifyCompat(t *testing.T, testCases map[string]findAndModifyCom
}
require.NoError(t, compatErr, "compat error; target returned no error")

AssertEqualDocuments(t, compatMod, targetMod)
compat := ConvertDocument(t, compatMod)
compat.Remove("$clusterTime")
compat.Remove("operationTime")

target := ConvertDocument(t, targetMod)
testutil.AssertEqual(t, compat, target)

// To make sure that the results of modification are equal,
// find all the documents in target and compat collections and compare that they are the same
Expand Down
37 changes: 32 additions & 5 deletions integration/indexes_command_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/FerretDB/FerretDB/integration/setup"
"github.com/FerretDB/FerretDB/integration/shareddata"
"github.com/FerretDB/FerretDB/internal/util/testutil"
"github.com/FerretDB/FerretDB/internal/util/testutil/testtb"
)

Expand Down Expand Up @@ -164,7 +165,13 @@ func TestCreateIndexesCommandCompat(t *testing.T) {
require.Nil(t, compatRes)
}

assert.Equal(t, compatRes, targetRes)
compatDoc := ConvertDocument(t, compatRes)
compatDoc.Remove("$clusterTime")
compatDoc.Remove("operationTime")
compatDoc.Remove("commitQuorum")

targetDoc := ConvertDocument(t, targetRes)
testutil.AssertEqual(t, compatDoc, targetDoc)

targetCursor, targetErr := targetCollection.Indexes().List(ctx)
compatCursor, compatErr := compatCollection.Indexes().List(ctx)
Expand Down Expand Up @@ -229,7 +236,13 @@ func TestCreateIndexesCommandCompatCheckFields(t *testing.T) {
require.NoError(t, err)
require.True(t, createdCollectionAutomatically.(bool)) // must be true because a new collection was created

AssertEqualDocuments(t, compatRes, targetRes)
compat := ConvertDocument(t, compatRes)
compat.Remove("$clusterTime")
compat.Remove("operationTime")
compat.Remove("commitQuorum")

target := ConvertDocument(t, targetRes)
testutil.AssertEqual(t, compat, target)

// Now this collection exists, so we create another index and expect createdCollectionAutomatically to be false.
indexesDoc = bson.D{{"key", bson.D{{"foo", 1}}}, {"name", "foo_1"}}
Expand All @@ -252,7 +265,12 @@ func TestCreateIndexesCommandCompatCheckFields(t *testing.T) {
require.NoError(t, err)
require.False(t, createdCollectionAutomatically.(bool)) // must be false because the collection already exists

AssertEqualDocuments(t, compatRes, targetRes)
compatDoc.Remove("$clusterTime")
compatDoc.Remove("operationTime")
compatDoc.Remove("commitQuorum")

targetDoc := ConvertDocument(t, targetRes)
testutil.AssertEqual(t, compatDoc, targetDoc)

// Call index creation for the index that already exists, expect note to be set.
indexesDoc = bson.D{{"key", bson.D{{"foo", 1}}}, {"name", "foo_1"}}
Expand All @@ -277,7 +295,11 @@ func TestCreateIndexesCommandCompatCheckFields(t *testing.T) {
// note must be set because no new indexes were created:
require.Equal(t, "all indexes already exist", createdCollectionAutomatically.(string))

AssertEqualDocuments(t, compatRes, targetRes)
compatDoc.Remove("$clusterTime")
compatDoc.Remove("operationTime")

targetDoc = ConvertDocument(t, targetRes)
testutil.AssertEqual(t, compatDoc, targetDoc)
}

func TestDropIndexesCommandCompat(tt *testing.T) {
Expand Down Expand Up @@ -451,7 +473,12 @@ func TestDropIndexesCommandCompat(tt *testing.T) {
require.Nil(t, compatRes)
}

require.ElementsMatch(t, compatRes, targetRes)
compatDoc := ConvertDocument(t, compatRes)
compatDoc.Remove("$clusterTime")
compatDoc.Remove("operationTime")

targetDoc := ConvertDocument(t, targetRes)
testutil.AssertEqual(t, compatDoc, targetDoc)

if compatErr == nil {
nonEmptyResults = true
Expand Down
11 changes: 8 additions & 3 deletions integration/indexes_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,18 @@ func TestDropIndexesCompat(t *testing.T) {
compatRes, compatErr = compatCollection.Indexes().DropOne(ctx, tc.dropIndexName)
}

require.Equal(t, compatErr, targetErr)
require.Equal(t, compatRes, targetRes)
if targetErr != nil {
t.Logf("Target error: %v", targetErr)
t.Logf("Compat error: %v", compatErr)

if targetErr == nil {
AssertMatchesCommandError(t, compatErr, targetErr)
} else {
nonEmptyResults = true
require.NoError(t, compatErr, "compat error; target returned no error")
}

require.Equal(t, compatRes, targetRes)

// List indexes to see they are identical after drop.
targetCursor, targetErr := targetCollection.Indexes().List(ctx)
compatCursor, compatErr := compatCollection.Indexes().List(ctx)
Expand Down
12 changes: 10 additions & 2 deletions integration/insert_command_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package integration
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"

"github.com/FerretDB/FerretDB/integration/setup"
"github.com/FerretDB/FerretDB/internal/util/testutil"
)

type insertCommandCompatTestCase struct { //nolint:vet // for readability
Expand Down Expand Up @@ -85,7 +85,15 @@ func testInsertCommandCompat(t *testing.T, testCases map[string]insertCommandCom

t.Logf("Compat (expected) result: %v", compatRes)
t.Logf("Target (actual) result: %v", targetRes)
assert.Equal(t, compatRes, targetRes)

compatDoc := ConvertDocument(t, compatRes)
compatDoc.Remove("$clusterTime")
compatDoc.Remove("operationTime")
compatDoc.Remove("electionId")
compatDoc.Remove("opTime")

targetDoc := ConvertDocument(t, targetRes)
testutil.AssertEqual(t, compatDoc, targetDoc)
})
}
})
Expand Down
8 changes: 7 additions & 1 deletion integration/query_command_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"

"github.com/FerretDB/FerretDB/integration/setup"
"github.com/FerretDB/FerretDB/internal/util/testutil"
)

// queryCommandCompatTestCase describes query compatibility test case.
Expand Down Expand Up @@ -147,7 +148,12 @@ func testQueryCommandCompat(t *testing.T, testCases map[string]queryCommandCompa
require.NoError(t, targetResult.Decode(&targetRes))
require.NoError(t, compatResult.Decode(&compatRes))

AssertEqualDocuments(t, compatRes, targetRes)
compatDoc := ConvertDocument(t, compatRes)
compatDoc.Remove("$clusterTime")
compatDoc.Remove("operationTime")

targetDoc := ConvertDocument(t, targetRes)
testutil.AssertEqual(t, compatDoc, targetDoc)

targetDocs := targetRes.Map()["cursor"].(bson.D).Map()["firstBatch"].(primitive.A)
compatDocs := compatRes.Map()["cursor"].(bson.D).Map()["firstBatch"].(primitive.A)
Expand Down
9 changes: 8 additions & 1 deletion integration/update_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,14 @@ func testUpdateCommandCompat(t *testing.T, testCases map[string]updateCommandCom
nonEmptyResults = true
}

assert.Equal(t, compatUpdateRes, targetUpdateRes)
compat := ConvertDocument(t, compatUpdateRes)
compat.Remove("$clusterTime")
compat.Remove("operationTime")
compat.Remove("electionId")
compat.Remove("opTime")

target := ConvertDocument(t, targetUpdateRes)
testutil.AssertEqual(t, compat, target)

if isMulti, ok := multi.(bool); ok && isMulti {
// if multi == false, an item updated by compat and target are different.
Expand Down

0 comments on commit 514acee

Please sign in to comment.