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

kvdb: use Docker based postgres fixture for unit tests #8555

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions kvdb/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"

_ "github.com/btcsuite/btcwallet/walletdb/bdb" // Import to register backend.
"github.com/stretchr/testify/require"
)

const (
Expand Down Expand Up @@ -244,7 +246,7 @@ func updateLastCompactionDate(dbFile string) error {
// on the TestBackend constant which is conditionally compiled with build tag.
// The passed path is used to hold all db files, while the name is only used
// for bbolt.
func GetTestBackend(path, name string) (Backend, func(), error) {
func GetTestBackend(t testing.TB, path, name string) (Backend, func()) {
empty := func() {}

// Note that for tests, we expect only one db backend build flag
Expand All @@ -255,39 +257,34 @@ func GetTestBackend(path, name string) (Backend, func(), error) {
key := filepath.Join(path, name)
keyHash := sha256.Sum256([]byte(key))

f, err := NewPostgresFixture("test_" + hex.EncodeToString(
f := NewPostgresFixture(t, "test_"+hex.EncodeToString(
keyHash[:]),
)
if err != nil {
return nil, func() {}, err
}

return f.DB(), func() {
_ = f.DB().Close()
}, nil
}

case EtcdBackend:
etcdConfig, cancel, err := StartEtcdTestBackend(path, 0, 0, "")
if err != nil {
return nil, empty, err
}
require.NoError(t, err, "unable to start etcd test backend")
backend, err := Open(
EtcdBackendName, context.TODO(), etcdConfig,
)
return backend, cancel, err

return backend, cancel

case SqliteBackend:
dbPath := filepath.Join(path, name)
keyHash := sha256.Sum256([]byte(dbPath))
sqliteDb, err := StartSqliteTestBackend(
path, name, "test_"+hex.EncodeToString(keyHash[:]),
)
if err != nil {
return nil, empty, err
}
require.NoError(t, err, "unable to start sqlite test backend")

return sqliteDb, func() {
_ = sqliteDb.Close()
}, nil
}

default:
db, err := GetBoltBackend(&BoltBackendConfig{
Expand All @@ -296,11 +293,10 @@ func GetTestBackend(path, name string) (Backend, func(), error) {
NoFreelistSync: true,
DBTimeout: DefaultDBTimeout,
})
if err != nil {
return nil, nil, err
}
return db, empty, nil
require.NoError(t, err, "unable to start bbolt test backend")
return db, empty
}

return nil, nil, fmt.Errorf("unknown backend")
require.Fail(t, "unknown backend")
return nil, func() {}
}
6 changes: 4 additions & 2 deletions kvdb/backend_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kvdb

import (
"fmt"
"testing"
"time"
)

Expand Down Expand Up @@ -43,6 +44,7 @@ func GetBoltBackend(cfg *BoltBackendConfig) (Backend, error) {
return nil, fmt.Errorf("bolt backend not supported in WebAssembly")
}

func GetTestBackend(path, name string) (Backend, func(), error) {
return nil, nil, fmt.Errorf("bolt backend not supported in WebAssembly")
func GetTestBackend(t testing.TB, path, name string) (Backend, func()) {
t.Fatalf("bolt backend not supported in WebAssembly")
return nil, nil
}
24 changes: 21 additions & 3 deletions kvdb/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ require (
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
github.com/btcsuite/btcwallet/walletdb v1.3.6-0.20210803004036-eebed51155ec
github.com/davecgh/go-spew v1.1.1
github.com/fergusstrange/embedded-postgres v1.25.0
github.com/google/btree v1.0.1
github.com/jackc/pgconn v1.14.0
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa
github.com/jackc/pgx/v4 v4.18.1
github.com/lib/pq v1.10.4
github.com/lightningnetwork/lnd/healthcheck v1.0.0
github.com/ory/dockertest/v3 v3.10.0
github.com/stretchr/testify v1.8.2
go.etcd.io/bbolt v1.3.7
go.etcd.io/etcd/api/v3 v3.5.7
Expand All @@ -21,20 +22,30 @@ require (
)

require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/continuity v0.3.0 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/docker/cli v20.10.17+incompatible // indirect
github.com/docker/docker v20.10.7+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
Expand All @@ -45,13 +56,18 @@ require (
github.com/json-iterator/go v1.1.11 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/lib/pq v1.10.4 // indirect
github.com/lightningnetwork/lnd/ticker v1.0.0 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-sqlite3 v1.14.16 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.1.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
Expand All @@ -63,7 +79,9 @@ require (
github.com/soheilhy/cmux v0.1.5 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
go.etcd.io/etcd/client/v2 v2.305.7 // indirect
go.etcd.io/etcd/pkg/v3 v3.5.7 // indirect
Expand Down
Loading
Loading