2019-11-22 13:24:11 +03:00
|
|
|
package migtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-26 20:08:11 +03:00
|
|
|
"github.com/lightningnetwork/lnd/kvdb"
|
2019-11-22 13:24:11 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// MakeDB creates a new instance of the ChannelDB for testing purposes. A
|
|
|
|
// callback which cleans up the created temporary directories is also returned
|
|
|
|
// and intended to be executed after the test completes.
|
2019-12-13 05:22:19 +03:00
|
|
|
func MakeDB() (kvdb.Backend, func(), error) {
|
2020-02-21 16:13:40 +03:00
|
|
|
// Create temporary database for mission control.
|
|
|
|
file, err := ioutil.TempFile("", "*.db")
|
2019-11-22 13:24:11 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-21 16:13:40 +03:00
|
|
|
dbPath := file.Name()
|
kvdb: add timeout options for bbolt (#4787)
* mod: bump btcwallet version to accept db timeout
* btcwallet: add DBTimeOut in config
* kvdb: add database timeout option for bbolt
This commit adds a DBTimeout option in bbolt config. The relevant
functions walletdb.Open/Create are updated to use this config. In
addition, the bolt compacter also applies the new timeout option.
* channeldb: add DBTimeout in db options
This commit adds the DBTimeout option for channeldb. A new unit
test file is created to test the default options. In addition,
the params used in kvdb.Create inside channeldb_test is updated
with a DefaultDBTimeout value.
* contractcourt+routing: use DBTimeout in kvdb
This commit touches multiple test files in contractcourt and routing.
The call of function kvdb.Create and kvdb.Open are now updated with
the new param DBTimeout, using the default value kvdb.DefaultDBTimeout.
* lncfg: add DBTimeout option in db config
The DBTimeout option is added to db config. A new unit test is
added to check the default DB config is created as expected.
* migration: add DBTimeout param in kvdb.Create/kvdb.Open
* keychain: update tests to use DBTimeout param
* htlcswitch+chainreg: add DBTimeout option
* macaroons: support DBTimeout config in creation
This commit adds the DBTimeout during the creation of macaroons.db.
The usage of kvdb.Create and kvdb.Open in its tests are updated with
a timeout value using kvdb.DefaultDBTimeout.
* walletunlocker: add dbTimeout option in UnlockerService
This commit adds a new param, dbTimeout, during the creation of
UnlockerService. This param is then passed to wallet.NewLoader
inside various service calls, specifying a timeout value to be
used when opening the bbolt. In addition, the macaroonService
is also called with this dbTimeout param.
* watchtower/wtdb: add dbTimeout param during creation
This commit adds the dbTimeout param for the creation of both
watchtower.db and wtclient.db.
* multi: add db timeout param for walletdb.Create
This commit adds the db timeout param for the function call
walletdb.Create. It touches only the test files found in chainntnfs,
lnwallet, and routing.
* lnd: pass DBTimeout config to relevant services
This commit enables lnd to pass the DBTimeout config to the following
services/config/functions,
- chainControlConfig
- walletunlocker
- wallet.NewLoader
- macaroons
- watchtower
In addition, the usage of wallet.Create is updated too.
* sample-config: add dbtimeout option
2020-12-08 02:31:49 +03:00
|
|
|
db, err := kvdb.Open(
|
|
|
|
kvdb.BoltBackendName, dbPath, true, kvdb.DefaultDBTimeout,
|
|
|
|
)
|
2019-11-22 13:24:11 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanUp := func() {
|
2020-02-21 16:13:40 +03:00
|
|
|
db.Close()
|
|
|
|
os.RemoveAll(dbPath)
|
2019-11-22 13:24:11 +03:00
|
|
|
}
|
|
|
|
|
2020-02-21 16:13:40 +03:00
|
|
|
return db, cleanUp, nil
|
2019-11-22 13:24:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyMigration is a helper test function that encapsulates the general steps
|
|
|
|
// which are needed to properly check the result of applying migration function.
|
|
|
|
func ApplyMigration(t *testing.T,
|
2019-12-13 05:22:19 +03:00
|
|
|
beforeMigration, afterMigration, migrationFunc func(tx kvdb.RwTx) error,
|
2019-11-22 13:24:11 +03:00
|
|
|
shouldFail bool) {
|
|
|
|
|
2020-03-19 05:14:35 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2019-11-22 13:24:11 +03:00
|
|
|
cdb, cleanUp, err := MakeDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// beforeMigration usually used for populating the database
|
|
|
|
// with test data.
|
2020-10-26 16:06:32 +03:00
|
|
|
err = kvdb.Update(cdb, beforeMigration, func() {})
|
2019-11-22 13:24:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2020-03-19 05:14:35 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2019-11-22 13:24:11 +03:00
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = newError(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil && shouldFail {
|
|
|
|
t.Fatal("error wasn't received on migration stage")
|
|
|
|
} else if err != nil && !shouldFail {
|
|
|
|
t.Fatalf("error was received on migration stage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// afterMigration usually used for checking the database state and
|
|
|
|
// throwing the error if something went wrong.
|
2020-10-26 16:06:32 +03:00
|
|
|
err = kvdb.Update(cdb, afterMigration, func() {})
|
2019-11-22 13:24:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Apply migration.
|
2020-10-26 16:06:32 +03:00
|
|
|
err = kvdb.Update(cdb, migrationFunc, func() {})
|
2019-11-22 13:24:11 +03:00
|
|
|
if err != nil {
|
2020-05-22 01:35:51 +03:00
|
|
|
t.Logf("migration error: %v", err)
|
2019-11-22 13:24:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newError(e interface{}) error {
|
|
|
|
var err error
|
|
|
|
switch e := e.(type) {
|
|
|
|
case error:
|
|
|
|
err = e
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("%v", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|