Skip to content

Commit

Permalink
Use separate PostgreSQL databases in tests (#3622)
Browse files Browse the repository at this point in the history
Closes #3409.
  • Loading branch information
AlekSi committed Nov 10, 2023
1 parent c303194 commit 1f18df0
Show file tree
Hide file tree
Showing 28 changed files with 275 additions and 252 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Please use global .gitignore file for .vscode/, .idea/, etc.
# Please use global .gitignore file for .vscode/, .idea/, go.work, .envrc, etc.
# See https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files#configuring-ignored-files-for-all-repositories-on-your-computer

state.json
Expand Down
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ linters-settings:
desc: use `github.com/jackc/pgx/v5/pgproto3` package instead
- pkg: github.com/jackc/pgtype
desc: use `github.com/jackc/pgx/v5/pgtype` package instead
- pkg: github.com/jackc/pgx$
desc: use `github.com/jackc/pgx/v5` package instead
- pkg: github.com/jackc/pgx/v4
desc: use `github.com/jackc/pgx/v5` package instead
fjson:
Expand Down
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Supported Versions

We support all FerretDB versions since 1.0.0.
We support two of the latest minor versions of FerretDB.

## Reporting a Vulnerability

Expand Down
2 changes: 1 addition & 1 deletion integration/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ replace github.com/FerretDB/FerretDB => ../
require (
github.com/AlekSi/pointer v1.2.0
github.com/FerretDB/FerretDB v0.0.0-00010101000000-000000000000
github.com/jackc/pgx/v5 v5.5.0
github.com/jaswdr/faker v1.19.1
github.com/prometheus/client_golang v1.17.0
github.com/stretchr/testify v1.8.4
Expand Down Expand Up @@ -40,6 +39,7 @@ require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx-zap v0.0.0-20221202020421-94b1cb2f889f // indirect
github.com/jackc/pgx/v5 v5.5.0 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/klauspost/compress v1.13.6 // indirect
Expand Down
3 changes: 3 additions & 0 deletions integration/query_comparison_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import (
"github.com/FerretDB/FerretDB/internal/util/must"
)

// Replace `1<<XXX` with named constants.
// TODO https://github.com/FerretDB/FerretDB/issues/3626

func TestQueryComparisonCompatImplicit(t *testing.T) {
t.Parallel()

Expand Down
71 changes: 2 additions & 69 deletions integration/setup/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ package setup
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"path"
"path/filepath"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.uber.org/zap"
Expand Down Expand Up @@ -140,77 +136,14 @@ func setupListener(tb testtb.TB, ctx context.Context, logger *zap.Logger) string
// read schemas owned by concurrent tests
postgreSQLURLF := *postgreSQLURLF
if postgreSQLURLF != "" {
u, err := url.Parse(postgreSQLURLF)
require.NoError(tb, err)

require.True(tb, u.Path != "")
require.True(tb, u.Opaque == "")

// port logging, tracing; merge with openDB?
// TODO https://github.com/FerretDB/FerretDB/issues/3554

config, err := pgxpool.ParseConfig(postgreSQLURLF)
require.NoError(tb, err)

p, err := pgxpool.NewWithConfig(ctx, config)
require.NoError(tb, err)

name := testutil.DirectoryName(tb)
template := "template1"

q := "DROP DATABASE IF EXISTS " + pgx.Identifier{name}.Sanitize()
_, err = p.Exec(ctx, q)
require.NoError(tb, err)

q = fmt.Sprintf("CREATE DATABASE %s TEMPLATE %s", pgx.Identifier{name}.Sanitize(), pgx.Identifier{template}.Sanitize())
_, err = p.Exec(ctx, q)
require.NoError(tb, err)

p.Reset()

u.Path = name
postgreSQLURLF = u.String()

tb.Cleanup(func() {
defer p.Close()

if tb.Failed() {
tb.Logf("Keeping %s (%s) for debugging.", name, postgreSQLURLF)
return
}

q := "DROP DATABASE " + pgx.Identifier{name}.Sanitize()
_, err = p.Exec(context.Background(), q)
require.NoError(tb, err)
})
postgreSQLURLF = testutil.TestPostgreSQLURI(tb, ctx, postgreSQLURLF)
}

// use per-test directory to prevent handler's/backend's metadata registry
// read databases owned by concurrent tests
sqliteURL := *sqliteURLF
if sqliteURL != "" {
u, err := url.Parse(sqliteURL)
require.NoError(tb, err)

require.True(tb, u.Path == "")
require.True(tb, u.Opaque != "")

u.Opaque = path.Join(u.Opaque, testutil.DirectoryName(tb)) + "/"
sqliteURL = u.String()

dir, err := filepath.Abs(u.Opaque)
require.NoError(tb, err)
require.NoError(tb, os.RemoveAll(dir))
require.NoError(tb, os.MkdirAll(dir, 0o777))

tb.Cleanup(func() {
if tb.Failed() {
tb.Logf("Keeping %s (%s) for debugging.", dir, sqliteURL)
return
}

require.NoError(tb, os.RemoveAll(dir))
})
sqliteURL = testutil.TestSQLiteURI(tb, sqliteURL)
}

sp, err := state.NewProvider("")
Expand Down
2 changes: 1 addition & 1 deletion integration/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func SetupWithOpts(tb testtb.TB, opts *SetupOpts) *SetupResult {
}
}

// Setup setups a single collection for all providers, if the are present.
// Setup setups a single collection for all providers, if they are present.
func Setup(tb testtb.TB, providers ...shareddata.Provider) (context.Context, *mongo.Collection) {
tb.Helper()

Expand Down
4 changes: 2 additions & 2 deletions integration/update_field_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestUpdateFieldCompatIncComplex(t *testing.T) {
update: bson.D{{"$inc", bson.D{{"v", float64(-42.13)}}}},
},
"DoubleDoubleBigIncrement": {
update: bson.D{{"$inc", bson.D{{"v", float64(1 << 61)}}}},
update: bson.D{{"$inc", bson.D{{"v", float64(1 << 61)}}}}, // TODO https://github.com/FerretDB/FerretDB/issues/3626
},
"DoubleIncOnNullValue": {
update: bson.D{{"$inc", bson.D{{"v", float64(1)}}}},
Expand Down Expand Up @@ -1070,7 +1070,7 @@ func TestUpdateFieldCompatMul(t *testing.T) {
providers: providers,
},
"DoubleBig": {
update: bson.D{{"$mul", bson.D{{"v", float64(1 << 61)}}}},
update: bson.D{{"$mul", bson.D{{"v", float64(1 << 61)}}}}, // TODO https://github.com/FerretDB/FerretDB/issues/3626
providers: providers,
},
"Empty": {
Expand Down
5 changes: 1 addition & 4 deletions internal/backends/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ func TestVersion(t *testing.T) {
assert.Empty(t, s.BackendName)
assert.Empty(t, s.BackendVersion)

dbName := testutil.DatabaseName(t)
cleanupDatabase(t, ctx, b, dbName)

db, err := b.Database(dbName)
db, err := b.Database(testutil.DatabaseName(t))
require.NoError(t, err)

err = db.CreateCollection(ctx, &backends.CreateCollectionParams{
Expand Down
8 changes: 0 additions & 8 deletions internal/backends/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func TestCollectionInsertAllQueryExplain(t *testing.T) {

dbName := testutil.DatabaseName(t)
collName, cappedCollName := testutil.CollectionName(t), testutil.CollectionName(t)+"capped"
cleanupDatabase(t, ctx, b, dbName)

db, err := b.Database(dbName)
require.NoError(t, err)
Expand Down Expand Up @@ -212,7 +211,6 @@ func TestCollectionUpdateAll(t *testing.T) {
t.Parallel()

dbName, collName := testutil.DatabaseName(t), testutil.CollectionName(t)
cleanupDatabase(t, ctx, b, dbName)

db, err := b.Database(dbName)
require.NoError(t, err)
Expand Down Expand Up @@ -253,7 +251,6 @@ func TestCollectionUpdateAll(t *testing.T) {

dbName, collName := testutil.DatabaseName(t), testutil.CollectionName(t)
otherCollName := collName + "_other"
cleanupDatabase(t, ctx, b, dbName)

db, err := b.Database(dbName)
require.NoError(t, err)
Expand Down Expand Up @@ -317,7 +314,6 @@ func TestCollectionStats(t *testing.T) {
t.Parallel()

dbName, collName := testutil.DatabaseName(t), testutil.CollectionName(t)
cleanupDatabase(t, ctx, b, dbName)

db, err := b.Database(dbName)
require.NoError(t, err)
Expand All @@ -334,7 +330,6 @@ func TestCollectionStats(t *testing.T) {

dbName, collName := testutil.DatabaseName(t), testutil.CollectionName(t)
otherCollName := collName + "_other"
cleanupDatabase(t, ctx, b, dbName)

db, err := b.Database(dbName)
require.NoError(t, err)
Expand All @@ -354,7 +349,6 @@ func TestCollectionStats(t *testing.T) {

t.Run("Stats", func(t *testing.T) {
dbName := testutil.DatabaseName(t)
cleanupDatabase(t, ctx, b, dbName)

db, err := b.Database(dbName)
require.NoError(t, err)
Expand Down Expand Up @@ -407,7 +401,6 @@ func TestCollectionCompact(t *testing.T) {
t.Parallel()

dbName, collName := testutil.DatabaseName(t), testutil.CollectionName(t)
cleanupDatabase(t, ctx, b, dbName)

db, err := b.Database(dbName)
require.NoError(t, err)
Expand All @@ -424,7 +417,6 @@ func TestCollectionCompact(t *testing.T) {

dbName, collName := testutil.DatabaseName(t), testutil.CollectionName(t)
otherCollName := collName + "_other"
cleanupDatabase(t, ctx, b, dbName)

db, err := b.Database(dbName)
require.NoError(t, err)
Expand Down
16 changes: 2 additions & 14 deletions internal/backends/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func testBackends(t *testing.T) map[string]*testBackend {
require.NoError(t, err)

b, err := postgresql.NewBackend(&postgresql.NewBackendParams{
URI: "postgres://username:password@127.0.0.1:5432/ferretdb",
URI: testutil.TestPostgreSQLURI(t, context.TODO(), ""),
L: l.Named("postgresql"),
P: sp,
})
Expand All @@ -71,7 +71,7 @@ func testBackends(t *testing.T) map[string]*testBackend {
require.NoError(t, err)

b, err := sqlite.NewBackend(&sqlite.NewBackendParams{
URI: "file:" + t.TempDir() + "/",
URI: testutil.TestSQLiteURI(t, ""),
L: l.Named("sqlite"),
P: sp,
})
Expand Down Expand Up @@ -105,18 +105,6 @@ func testBackends(t *testing.T) map[string]*testBackend {
return res
}

// cleanupDatabase drops the database with the given name before and after the test.
func cleanupDatabase(t *testing.T, ctx context.Context, b backends.Backend, dbName string) {
t.Helper()

p := &backends.DropDatabaseParams{Name: dbName}
_ = b.DropDatabase(ctx, p)

t.Cleanup(func() {
_ = b.DropDatabase(ctx, p)
})
}

// assertErrorCode asserts that err is *Error with one of the given error codes.
func assertErrorCode(t *testing.T, err error, code backends.ErrorCode, codes ...backends.ErrorCode) {
assert.True(t, backends.ErrorCodeIs(err, code, codes...), "err = %v", err)
Expand Down
1 change: 1 addition & 0 deletions internal/backends/postgresql/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type NewBackendParams struct {
URI string
L *zap.Logger
P *state.Provider
_ struct{} // prevent unkeyed literals
}

// NewBackend creates a new backend.
Expand Down
7 changes: 1 addition & 6 deletions internal/backends/postgresql/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestDatabaseStats(t *testing.T) {
require.NoError(t, err)

params := NewBackendParams{
URI: "postgres://username:password@127.0.0.1:5432/ferretdb",
URI: testutil.TestPostgreSQLURI(t, ctx, ""),
L: testutil.Logger(t),
P: sp,
}
Expand All @@ -52,11 +52,6 @@ func TestDatabaseStats(t *testing.T) {
db, err := b.Database(dbName)
require.NoError(t, err)

t.Cleanup(func() {
err = b.DropDatabase(ctx, &backends.DropDatabaseParams{Name: dbName})
require.NoError(t, err)
})

cNames := []string{"collectionOne", "collectionTwo"}
for _, cName := range cNames {
err = db.CreateCollection(ctx, &backends.CreateCollectionParams{Name: cName})
Expand Down
2 changes: 1 addition & 1 deletion internal/backends/postgresql/metadata/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ func (r *Registry) indexesDrop(ctx context.Context, p *pgxpool.Pool, dbName, col
// Deprecated: Warning! Avoid using this function unless there is no other way.
// Ideally, use a placeholder and pass the value as a parameter instead of calling this function.
//
// This approach is used in github.com/jackc/pgx/v4@v4.18.1/internal/sanitize/sanitize.go.
// See https://github.com/jackc/pgx/blob/v5.5.0/internal/sanitize/sanitize.go#L90-L92
func quoteString(str string) string {
// We need "standard_conforming_strings=on" and "client_encoding=UTF8" (checked in checkConnection),
// otherwise we can't sanitize safely: https://github.com/jackc/pgx/issues/868#issuecomment-725544647
Expand Down
Loading

0 comments on commit 1f18df0

Please sign in to comment.