Skip to content

Commit

Permalink
Add TestMain to each integration test package (#4366)
Browse files Browse the repository at this point in the history
Closes #4367.
  • Loading branch information
AlekSi committed Jun 21, 2024
1 parent 42b3dbe commit 0dfd9c5
Show file tree
Hide file tree
Showing 19 changed files with 277 additions and 118 deletions.
10 changes: 5 additions & 5 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,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&replicaSet=rs0'
-compat-url='mongodb://username:password@127.0.0.1:47018/?tls=true&tlsCertificateKeyFile=client.pem&tlsCaFile=rootCA-cert.pem&replicaSet=rs0'
-disable-pushdown={{.DISABLE_PUSHDOWN}}
test-integration-sqlite:
Expand All @@ -242,7 +242,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&replicaSet=rs0'
-compat-url='mongodb://username:password@127.0.0.1:47018/?tls=true&tlsCertificateKeyFile=client.pem&tlsCaFile=rootCA-cert.pem&replicaSet=rs0'
-disable-pushdown={{.DISABLE_PUSHDOWN}}
test-integration-mysql:
Expand All @@ -268,7 +268,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&replicaSet=rs0'
-compat-url='mongodb://username:password@127.0.0.1:47018/?tls=true&tlsCertificateKeyFile=client.pem&tlsCaFile=rootCA-cert.pem&replicaSet=rs0'
-disable-pushdown={{.DISABLE_PUSHDOWN}}
test-integration-hana:
Expand All @@ -294,7 +294,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&replicaSet=rs0'
-compat-url='mongodb://username:password@127.0.0.1:47018/?tls=true&tlsCertificateKeyFile=client.pem&tlsCaFile=rootCA-cert.pem&replicaSet=rs0'
-disable-pushdown={{.DISABLE_PUSHDOWN}}
test-integration-mongodb:
Expand All @@ -317,7 +317,7 @@ tasks:
-coverpkg=../...
-coverprofile=integration-mongodb.txt
./...
-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-url='mongodb://username:password@127.0.0.1:47018/?tls=true&tlsCertificateKeyFile=client.pem&tlsCaFile=rootCA-cert.pem&replicaSet=rs0'
-target-backend=mongodb
bench-unit:
Expand Down
7 changes: 4 additions & 3 deletions cmd/envtool/tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package main
import (
"context"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
Expand All @@ -27,6 +26,8 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

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

// makeTestLogger returns a logger that adds all messages to the given slice.
Expand Down Expand Up @@ -433,7 +434,7 @@ func TestFilterStringsByRegex(t *testing.T) {
func TestShardTestFuncs(t *testing.T) {
t.Parallel()

testFuncs, err := listTestFuncs(filepath.Join("..", "..", "integration"))
testFuncs, err := listTestFuncs(testutil.IntegrationDir)
require.NoError(t, err)
assert.Contains(t, testFuncs, "TestQueryCompatLimit")
assert.Contains(t, testFuncs, "TestCursorsGetMoreCommand")
Expand Down Expand Up @@ -484,7 +485,7 @@ func TestShardTestFuncs(t *testing.T) {
func TestListTestFuncsWithSkip(t *testing.T) {
t.Parallel()

testFuncs, err := listTestFuncsWithRegex(filepath.Join("testdata"), "", "Skip")
testFuncs, err := listTestFuncsWithRegex("testdata", "", "Skip")
require.NoError(t, err)

sort.Strings(testFuncs)
Expand Down
2 changes: 1 addition & 1 deletion cmd/ferretdb/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestVersion(t *testing.T) {
ctx, cancel := context.WithTimeout(testutil.Ctx(t), 5*time.Second)
t.Cleanup(cancel)

bin := filepath.Join("..", "..", "bin", "ferretdb")
bin := filepath.Join(testutil.BinDir, "ferretdb")

cmd := exec.CommandContext(ctx, bin, "--version")
b, err := cmd.Output()
Expand Down
10 changes: 3 additions & 7 deletions ferretdb/ferretdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,12 @@ func Example_unix() {
}

func Example_tls() {
certPath := filepath.Join("..", "build", "certs", "server-cert.pem")
keyPath := filepath.Join("..", "build", "certs", "server-key.pem")
caPath := filepath.Join("..", "build", "certs", "rootCA-cert.pem")

f, err := ferretdb.New(&ferretdb.Config{
Listener: ferretdb.ListenerConfig{
TLS: "127.0.0.1:17028",
TLSCertFile: certPath,
TLSKeyFile: keyPath,
TLSCAFile: caPath,
TLSCertFile: filepath.Join(testutil.BuildCertsDir, "server-cert.pem"),
TLSKeyFile: filepath.Join(testutil.BuildCertsDir, "server-key.pem"),
TLSCAFile: filepath.Join(testutil.BuildCertsDir, "rootCA-cert.pem"),
},
Handler: "postgresql",
PostgreSQLURL: "postgres://127.0.0.1:5432/ferretdb",
Expand Down
25 changes: 25 additions & 0 deletions integration/cursors/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cursors

import (
"testing"

"github.com/FerretDB/FerretDB/integration/setup"
)

func TestMain(m *testing.M) {
setup.Main(m)
}
20 changes: 1 addition & 19 deletions integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,11 @@
package integration

import (
"flag"
"os"
"testing"

"github.com/FerretDB/FerretDB/integration/setup"
)

// TestMain is the entry point for all integration tests.
func TestMain(m *testing.M) {
flag.Parse()

var code int

// ensure that Shutdown runs for any exit code or panic
func() {
// make `go test -list=.` work without side effects
if flag.Lookup("test.list").Value.String() == "" {
setup.Startup()
defer setup.Shutdown()
}

code = m.Run()
}()

os.Exit(code)
setup.Main(m)
}
25 changes: 25 additions & 0 deletions integration/oplog/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package oplog

import (
"testing"

"github.com/FerretDB/FerretDB/integration/setup"
)

func TestMain(m *testing.M) {
setup.Main(m)
}
71 changes: 32 additions & 39 deletions integration/setup/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package setup
import (
"context"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/mongo"
Expand All @@ -28,9 +28,40 @@ import (

"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/observability"
"github.com/FerretDB/FerretDB/internal/util/testutil"
"github.com/FerretDB/FerretDB/internal/util/testutil/testtb"
)

// setClientPaths replaces file names in query parameters with absolute paths.
func setClientPaths(uri string) (string, error) {
u, err := url.Parse(uri)
if err != nil {
return "", lazyerrors.Error(err)
}

q, err := url.ParseQuery(u.RawQuery)
if err != nil {
return "", lazyerrors.Error(err)
}

for k, vs := range q {
switch k {
case "tlsCertificateKeyFile", "tlsCaFile":
for i, v := range vs {
if strings.Contains(v, "/") {
return "", lazyerrors.Errorf("%q: %q should contain only a file name, got %q", uri, k, v)
}

vs[i] = filepath.Join(testutil.BuildCertsDir, v)
}
}
}

u.RawQuery = q.Encode()

return u.String(), nil
}

// makeClient returns new client for the given working MongoDB URI.
func makeClient(ctx context.Context, uri string) (*mongo.Client, error) {
clientOpts := options.Client().ApplyURI(uri)
Expand Down Expand Up @@ -79,41 +110,3 @@ 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()
}
29 changes: 21 additions & 8 deletions integration/setup/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
package setup

import (
"path/filepath"
"runtime"
"flag"
"os"
"strings"
"testing"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -105,12 +106,24 @@ func PushdownDisabled() bool {
return *disablePushdownF
}

// Dir returns the absolute directory of this package.
func Dir(tb testtb.TB) string {
tb.Helper()
// Main is the entry point for all integration test packages.
// It should be called from main_test.go in each package.
func Main(m *testing.M) {
flag.Parse()

var code int

// ensure that Shutdown runs for any exit code or panic
func() {
// make `go test -list=.` work without side effects
if flag.Lookup("test.list").Value.String() == "" {
Startup()

defer Shutdown()
}

_, file, _, ok := runtime.Caller(0)
require.True(tb, ok)
code = m.Run()
}()

return filepath.Dir(file)
os.Exit(code)
}
15 changes: 6 additions & 9 deletions integration/setup/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,11 @@ func listenerMongoDBURI(tb testtb.TB, hostPort, unixSocketPath string, tlsAndAut
if tlsAndAuth {
require.Empty(tb, unixSocketPath, "unixSocketPath cannot be used with TLS")

certsRoot := filepath.Join(Dir(tb), "..", "..", "build", "certs")

// we don't separate TLS and auth just for simplicity of our test configurations
q = url.Values{
"tls": []string{"true"},
"tlsCertificateKeyFile": []string{filepath.Join(certsRoot, "client.pem")},
"tlsCaFile": []string{filepath.Join(certsRoot, "rootCA-cert.pem")},
"tlsCertificateKeyFile": []string{filepath.Join(testutil.BuildCertsDir, "client.pem")},
"tlsCaFile": []string{filepath.Join(testutil.BuildCertsDir, "rootCA-cert.pem")},
"authMechanism": []string{"PLAIN"},
}
user = url.UserPassword("username", "password")
Expand Down Expand Up @@ -218,7 +216,7 @@ func setupListener(tb testtb.TB, ctx context.Context, logger *zap.Logger, opts *
Metrics: listenerMetrics,
Handler: h,
Logger: logger,
TestRecordsDir: filepath.Join(Dir(tb), "..", "..", "tmp", "records"),
TestRecordsDir: testutil.TmpRecordsDir,
}

if *targetProxyAddrF != "" {
Expand All @@ -231,11 +229,10 @@ func setupListener(tb testtb.TB, ctx context.Context, logger *zap.Logger, opts *

switch {
case *targetTLSF:
certsRoot := filepath.Join(Dir(tb), "..", "..", "build", "certs")
listenerOpts.TLS = "127.0.0.1:0"
listenerOpts.TLSCertFile = filepath.Join(certsRoot, "server-cert.pem")
listenerOpts.TLSKeyFile = filepath.Join(certsRoot, "server-key.pem")
listenerOpts.TLSCAFile = filepath.Join(certsRoot, "rootCA-cert.pem")
listenerOpts.TLSCertFile = filepath.Join(testutil.BuildCertsDir, "server-cert.pem")
listenerOpts.TLSKeyFile = filepath.Join(testutil.BuildCertsDir, "server-key.pem")
listenerOpts.TLSCAFile = filepath.Join(testutil.BuildCertsDir, "rootCA-cert.pem")
case *targetUnixSocketF:
listenerOpts.Unix = unixSocketPath(tb)
default:
Expand Down
2 changes: 0 additions & 2 deletions integration/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ func SetupWithOpts(tb testtb.TB, opts *SetupOpts) *SetupResult {
uri := *targetURLF
if uri == "" {
uri = setupListener(tb, setupCtx, logger, opts.BackendOptions)
} else {
uri = toAbsolutePathURI(tb, *targetURLF)
}

if opts.ExtraOptions != nil {
Expand Down
Loading

0 comments on commit 0dfd9c5

Please sign in to comment.