2019-11-22 13:24:11 +03:00
|
|
|
package migtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2019-12-13 05:22:19 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb/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()
|
2019-12-13 05:22:19 +03:00
|
|
|
db, err := kvdb.Open(kvdb.BoltBackendName, dbPath, true)
|
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) {
|
|
|
|
|
|
|
|
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() {
|
|
|
|
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
|
|
|
|
}
|