mirror of
https://github.com/scroll-tech/scroll.git
synced 2026-01-09 14:08:03 -05:00
feat(tests): update docker app (#402)
Co-authored-by: Péter Garamvölgyi <peter@scroll.io> Co-authored-by: ChuhanJin <60994121+ChuhanJin@users.noreply.github.com> Co-authored-by: vincent <419436363@qq.com> Co-authored-by: colinlyguo <651734127@qq.com> Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
This commit is contained in:
@@ -40,9 +40,9 @@ func setupEnv(t *testing.T) (err error) {
|
||||
|
||||
base.RunImages(t)
|
||||
|
||||
cfg.L2Config.RelayerConfig.SenderConfig.Endpoint = base.L1GethEndpoint()
|
||||
cfg.L1Config.RelayerConfig.SenderConfig.Endpoint = base.L2GethEndpoint()
|
||||
cfg.DBConfig.DSN = base.DBEndpoint()
|
||||
cfg.L2Config.RelayerConfig.SenderConfig.Endpoint = base.L1gethImg.Endpoint()
|
||||
cfg.L1Config.RelayerConfig.SenderConfig.Endpoint = base.L2gethImg.Endpoint()
|
||||
cfg.DBConfig = base.DBConfig
|
||||
|
||||
// Create l2geth client.
|
||||
l2Cli, err = base.L2Client()
|
||||
|
||||
@@ -50,7 +50,7 @@ func setupEnv(t *testing.T) {
|
||||
// Load default private key.
|
||||
privateKeys = []*ecdsa.PrivateKey{priv}
|
||||
|
||||
cfg.L1Config.RelayerConfig.SenderConfig.Endpoint = base.L2GethEndpoint()
|
||||
cfg.L1Config.RelayerConfig.SenderConfig.Endpoint = base.L2gethImg.Endpoint()
|
||||
}
|
||||
|
||||
func TestSender(t *testing.T) {
|
||||
|
||||
@@ -82,15 +82,15 @@ func setupEnv(t *testing.T) {
|
||||
base.RunImages(t)
|
||||
|
||||
// Create l1geth container.
|
||||
cfg.L2Config.RelayerConfig.SenderConfig.Endpoint = base.L1GethEndpoint()
|
||||
cfg.L1Config.Endpoint = base.L1GethEndpoint()
|
||||
cfg.L2Config.RelayerConfig.SenderConfig.Endpoint = base.L1gethImg.Endpoint()
|
||||
cfg.L1Config.Endpoint = base.L1gethImg.Endpoint()
|
||||
|
||||
// Create l2geth container.
|
||||
cfg.L1Config.RelayerConfig.SenderConfig.Endpoint = base.L2GethEndpoint()
|
||||
cfg.L2Config.Endpoint = base.L2GethEndpoint()
|
||||
cfg.L1Config.RelayerConfig.SenderConfig.Endpoint = base.L2gethImg.Endpoint()
|
||||
cfg.L2Config.Endpoint = base.L2gethImg.Endpoint()
|
||||
|
||||
// Create db container.
|
||||
cfg.DBConfig.DSN = base.DBEndpoint()
|
||||
cfg.DBConfig = base.DBConfig
|
||||
|
||||
// Create l1geth and l2geth client.
|
||||
l1Client, err = ethclient.Dial(cfg.L1Config.Endpoint)
|
||||
|
||||
@@ -20,7 +20,7 @@ func testStartWatcher(t *testing.T) {
|
||||
assert.NoError(t, migrate.ResetDB(db.GetDB().DB))
|
||||
defer db.Close()
|
||||
|
||||
client, err := ethclient.Dial(base.L1GethEndpoint())
|
||||
client, err := ethclient.Dial(base.L1gethImg.Endpoint())
|
||||
assert.NoError(t, err)
|
||||
|
||||
l1Cfg := cfg.L1Config
|
||||
|
||||
@@ -35,9 +35,9 @@ func setupEnv(t *testing.T) (err error) {
|
||||
|
||||
base.RunImages(t)
|
||||
|
||||
cfg.L2Config.RelayerConfig.SenderConfig.Endpoint = base.L1GethEndpoint()
|
||||
cfg.L1Config.RelayerConfig.SenderConfig.Endpoint = base.L2GethEndpoint()
|
||||
cfg.DBConfig.DSN = base.DBEndpoint()
|
||||
cfg.L2Config.RelayerConfig.SenderConfig.Endpoint = base.L1gethImg.Endpoint()
|
||||
cfg.L1Config.RelayerConfig.SenderConfig.Endpoint = base.L2gethImg.Endpoint()
|
||||
cfg.DBConfig = base.DBConfig
|
||||
|
||||
// Create l2geth client.
|
||||
l2Cli, err = base.L2Client()
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/big"
|
||||
@@ -11,13 +11,11 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/modern-go/reflect2"
|
||||
"github.com/scroll-tech/go-ethereum/ethclient"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"scroll-tech/database"
|
||||
|
||||
"scroll-tech/common/cmd"
|
||||
"scroll-tech/common/utils"
|
||||
)
|
||||
|
||||
@@ -27,208 +25,172 @@ var (
|
||||
dbStartPort = 30000
|
||||
)
|
||||
|
||||
// AppAPI app interface.
|
||||
type AppAPI interface {
|
||||
WaitResult(t *testing.T, timeout time.Duration, keyword string) bool
|
||||
RunApp(waitResult func() bool)
|
||||
WaitExit()
|
||||
ExpectWithTimeout(t *testing.T, parallel bool, timeout time.Duration, keyword string)
|
||||
}
|
||||
|
||||
// App is collection struct of runtime docker images
|
||||
type App struct {
|
||||
l1gethImg ImgInstance
|
||||
l2gethImg ImgInstance
|
||||
L1gethImg GethImgInstance
|
||||
L2gethImg GethImgInstance
|
||||
DBImg ImgInstance
|
||||
|
||||
dbImg ImgInstance
|
||||
dbConfig *database.DBConfig
|
||||
dbFile string
|
||||
dbClient *sql.DB
|
||||
DBConfig *database.DBConfig
|
||||
DBConfigFile string
|
||||
|
||||
// common time stamp.
|
||||
timestamp int
|
||||
Timestamp int
|
||||
}
|
||||
|
||||
// NewDockerApp returns new instance of dokerApp struct
|
||||
func NewDockerApp() *App {
|
||||
timestamp := time.Now().Nanosecond()
|
||||
return &App{
|
||||
timestamp: timestamp,
|
||||
dbFile: fmt.Sprintf("/tmp/%d_db-config.json", timestamp),
|
||||
app := &App{
|
||||
Timestamp: timestamp,
|
||||
L1gethImg: newTestL1Docker(),
|
||||
L2gethImg: newTestL2Docker(),
|
||||
DBImg: newTestDBDocker("postgres"),
|
||||
DBConfigFile: fmt.Sprintf("/tmp/%d_db-config.json", timestamp),
|
||||
}
|
||||
if err := app.mockDBConfig(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return app
|
||||
}
|
||||
|
||||
// RunImages runs all images togather
|
||||
func (b *App) RunImages(t *testing.T) {
|
||||
b.runDBImage(t)
|
||||
b.runL1Geth(t)
|
||||
b.runL2Geth(t)
|
||||
b.RunDBImage(t)
|
||||
b.RunL1Geth(t)
|
||||
b.RunL2Geth(t)
|
||||
}
|
||||
|
||||
func (b *App) runDBImage(t *testing.T) {
|
||||
if b.dbImg != nil {
|
||||
// RunDBImage starts postgres docker container.
|
||||
func (b *App) RunDBImage(t *testing.T) {
|
||||
if b.DBImg.IsRunning() {
|
||||
return
|
||||
}
|
||||
b.dbImg = newTestDBDocker(t, "postgres")
|
||||
if err := b.mockDBConfig(); err != nil {
|
||||
_ = b.dbImg.Stop()
|
||||
b.dbImg = nil
|
||||
_ = os.Remove(b.dbFile)
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// RunDBApp runs DB app with command
|
||||
func (b *App) RunDBApp(t *testing.T, option, keyword string) {
|
||||
args := []string{option, "--config", b.dbFile}
|
||||
app := cmd.NewCmd("db_cli-test", args...)
|
||||
defer app.WaitExit()
|
||||
|
||||
// Wait expect result.
|
||||
app.ExpectWithTimeout(t, true, time.Second*3, keyword)
|
||||
app.RunApp(nil)
|
||||
assert.NoError(t, b.DBImg.Start())
|
||||
var isRun bool
|
||||
// try 5 times until the db is ready.
|
||||
utils.TryTimes(10, func() bool {
|
||||
db, err := sqlx.Open("postgres", b.DBImg.Endpoint())
|
||||
isRun = err == nil && db != nil && db.Ping() == nil
|
||||
return isRun
|
||||
})
|
||||
assert.Equal(t, true, isRun)
|
||||
}
|
||||
|
||||
// Free clear all running images, double check and recycle docker container.
|
||||
func (b *App) Free() {
|
||||
if b.l1gethImg != nil {
|
||||
_ = b.l1gethImg.Stop()
|
||||
b.l1gethImg = nil
|
||||
if b.L1gethImg.IsRunning() {
|
||||
_ = b.L1gethImg.Stop()
|
||||
}
|
||||
if b.l2gethImg != nil {
|
||||
_ = b.l2gethImg.Stop()
|
||||
b.l2gethImg = nil
|
||||
if b.L2gethImg.IsRunning() {
|
||||
_ = b.L2gethImg.Stop()
|
||||
}
|
||||
if b.dbImg != nil {
|
||||
_ = b.dbImg.Stop()
|
||||
b.dbImg = nil
|
||||
_ = os.Remove(b.dbFile)
|
||||
if b.DBImg.IsRunning() {
|
||||
_ = b.DBImg.Stop()
|
||||
_ = os.Remove(b.DBConfigFile)
|
||||
if !utils.IsNil(b.dbClient) {
|
||||
_ = b.dbClient.Close()
|
||||
b.dbClient = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// L1GethEndpoint returns l1gethimg endpoint
|
||||
func (b *App) L1GethEndpoint() string {
|
||||
if b.l1gethImg != nil {
|
||||
return b.l1gethImg.Endpoint()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// L2GethEndpoint returns l2gethimg endpoint
|
||||
func (b *App) L2GethEndpoint() string {
|
||||
if b.l2gethImg != nil {
|
||||
return b.l2gethImg.Endpoint()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// DBEndpoint returns the endpoint of the dbimg
|
||||
func (b *App) DBEndpoint() string {
|
||||
return b.dbImg.Endpoint()
|
||||
}
|
||||
|
||||
func (b *App) runL1Geth(t *testing.T) {
|
||||
if b.l1gethImg != nil {
|
||||
// RunL1Geth starts l1geth docker container.
|
||||
func (b *App) RunL1Geth(t *testing.T) {
|
||||
if b.L1gethImg.IsRunning() {
|
||||
return
|
||||
}
|
||||
b.l1gethImg = newTestL1Docker(t)
|
||||
assert.NoError(t, b.L1gethImg.Start())
|
||||
}
|
||||
|
||||
// L1Client returns a ethclient by dialing running l1geth
|
||||
func (b *App) L1Client() (*ethclient.Client, error) {
|
||||
if b.l1gethImg == nil || reflect2.IsNil(b.l1gethImg) {
|
||||
if utils.IsNil(b.L1gethImg) {
|
||||
return nil, fmt.Errorf("l1 geth is not running")
|
||||
}
|
||||
client, err := ethclient.Dial(b.l1gethImg.Endpoint())
|
||||
client, err := ethclient.Dial(b.L1gethImg.Endpoint())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (b *App) runL2Geth(t *testing.T) {
|
||||
if b.l2gethImg != nil {
|
||||
// RunL2Geth starts l2geth docker container.
|
||||
func (b *App) RunL2Geth(t *testing.T) {
|
||||
if b.L2gethImg.IsRunning() {
|
||||
return
|
||||
}
|
||||
b.l2gethImg = newTestL2Docker(t)
|
||||
assert.NoError(t, b.L2gethImg.Start())
|
||||
}
|
||||
|
||||
// L2Client returns a ethclient by dialing running l2geth
|
||||
func (b *App) L2Client() (*ethclient.Client, error) {
|
||||
if b.l2gethImg == nil || reflect2.IsNil(b.l2gethImg) {
|
||||
if utils.IsNil(b.L2gethImg) {
|
||||
return nil, fmt.Errorf("l2 geth is not running")
|
||||
}
|
||||
client, err := ethclient.Dial(b.l2gethImg.Endpoint())
|
||||
client, err := ethclient.Dial(b.L2gethImg.Endpoint())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// DBClient create and return *sql.DB instance.
|
||||
func (b *App) DBClient(t *testing.T) *sql.DB {
|
||||
if !utils.IsNil(b.dbClient) {
|
||||
return b.dbClient
|
||||
}
|
||||
var (
|
||||
cfg = b.DBConfig
|
||||
err error
|
||||
)
|
||||
b.dbClient, err = sql.Open(cfg.DriverName, cfg.DSN)
|
||||
assert.NoError(t, err)
|
||||
b.dbClient.SetMaxOpenConns(cfg.MaxOpenNum)
|
||||
b.dbClient.SetMaxIdleConns(cfg.MaxIdleNum)
|
||||
assert.NoError(t, b.dbClient.Ping())
|
||||
return b.dbClient
|
||||
}
|
||||
|
||||
func (b *App) mockDBConfig() error {
|
||||
if b.dbConfig == nil {
|
||||
|
||||
b.dbConfig = &database.DBConfig{
|
||||
DSN: "",
|
||||
DriverName: "postgres",
|
||||
MaxOpenNum: 200,
|
||||
MaxIdleNum: 20,
|
||||
}
|
||||
b.DBConfig = &database.DBConfig{
|
||||
DSN: "",
|
||||
DriverName: "postgres",
|
||||
MaxOpenNum: 200,
|
||||
MaxIdleNum: 20,
|
||||
}
|
||||
|
||||
if b.dbImg != nil {
|
||||
b.dbConfig.DSN = b.dbImg.Endpoint()
|
||||
if b.DBImg != nil {
|
||||
b.DBConfig.DSN = b.DBImg.Endpoint()
|
||||
}
|
||||
data, err := json.Marshal(b.dbConfig)
|
||||
data, err := json.Marshal(b.DBConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(b.dbFile, data, 0644) //nolint:gosec
|
||||
return os.WriteFile(b.DBConfigFile, data, 0644) //nolint:gosec
|
||||
}
|
||||
|
||||
func newTestL1Docker(t *testing.T) ImgInstance {
|
||||
func newTestL1Docker() GethImgInstance {
|
||||
id, _ := rand.Int(rand.Reader, big.NewInt(2000))
|
||||
imgL1geth := NewImgGeth("scroll_l1geth", "", "", 0, l1StartPort+int(id.Int64()))
|
||||
assert.NoError(t, imgL1geth.Start())
|
||||
|
||||
// try 3 times to get chainID until is ok.
|
||||
utils.TryTimes(10, func() bool {
|
||||
client, _ := ethclient.Dial(imgL1geth.Endpoint())
|
||||
if client != nil {
|
||||
if _, err := client.ChainID(context.Background()); err == nil {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
return imgL1geth
|
||||
return NewImgGeth("scroll_l1geth", "", "", 0, l1StartPort+int(id.Int64()))
|
||||
}
|
||||
|
||||
func newTestL2Docker(t *testing.T) ImgInstance {
|
||||
func newTestL2Docker() GethImgInstance {
|
||||
id, _ := rand.Int(rand.Reader, big.NewInt(2000))
|
||||
imgL2geth := NewImgGeth("scroll_l2geth", "", "", 0, l2StartPort+int(id.Int64()))
|
||||
assert.NoError(t, imgL2geth.Start())
|
||||
|
||||
// try 3 times to get chainID until is ok.
|
||||
utils.TryTimes(10, func() bool {
|
||||
client, _ := ethclient.Dial(imgL2geth.Endpoint())
|
||||
if client != nil {
|
||||
if _, err := client.ChainID(context.Background()); err == nil {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
return imgL2geth
|
||||
return NewImgGeth("scroll_l2geth", "", "", 0, l2StartPort+int(id.Int64()))
|
||||
}
|
||||
|
||||
func newTestDBDocker(t *testing.T, driverName string) ImgInstance {
|
||||
func newTestDBDocker(driverName string) ImgInstance {
|
||||
id, _ := rand.Int(rand.Reader, big.NewInt(2000))
|
||||
imgDB := NewImgDB(driverName, "123456", "test_db", dbStartPort+int(id.Int64()))
|
||||
assert.NoError(t, imgDB.Start())
|
||||
|
||||
// try 5 times until the db is ready.
|
||||
utils.TryTimes(10, func() bool {
|
||||
db, _ := sqlx.Open(driverName, imgDB.Endpoint())
|
||||
if db != nil {
|
||||
return db.Ping() == nil
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
return imgDB
|
||||
return NewImgDB(driverName, "123456", "test_db", dbStartPort+int(id.Int64()))
|
||||
}
|
||||
|
||||
@@ -75,12 +75,14 @@ func (i *ImgDB) Stop() error {
|
||||
|
||||
// Endpoint return the dsn.
|
||||
func (i *ImgDB) Endpoint() string {
|
||||
if !i.running {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("postgres://postgres:%s@localhost:%d/%s?sslmode=disable", i.password, i.port, i.dbName)
|
||||
}
|
||||
|
||||
// IsRunning returns docker container's running status.
|
||||
func (i *ImgDB) IsRunning() bool {
|
||||
return i.running
|
||||
}
|
||||
|
||||
func (i *ImgDB) prepare() []string {
|
||||
cmd := []string{"docker", "run", "--rm", "--name", i.name, "-p", fmt.Sprintf("%d:5432", i.port)}
|
||||
envs := []string{
|
||||
|
||||
@@ -3,11 +3,13 @@ package docker
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/scroll-tech/go-ethereum/ethclient"
|
||||
|
||||
"scroll-tech/common/cmd"
|
||||
"scroll-tech/common/utils"
|
||||
@@ -23,13 +25,14 @@ type ImgGeth struct {
|
||||
ipcPath string
|
||||
httpPort int
|
||||
wsPort int
|
||||
chainID *big.Int
|
||||
|
||||
running bool
|
||||
cmd *cmd.Cmd
|
||||
}
|
||||
|
||||
// NewImgGeth return geth img instance.
|
||||
func NewImgGeth(image, volume, ipc string, hPort, wPort int) ImgInstance {
|
||||
func NewImgGeth(image, volume, ipc string, hPort, wPort int) GethImgInstance {
|
||||
img := &ImgGeth{
|
||||
image: image,
|
||||
name: fmt.Sprintf("%s-%d", image, time.Now().Nanosecond()),
|
||||
@@ -53,14 +56,27 @@ func (i *ImgGeth) Start() error {
|
||||
_ = i.Stop()
|
||||
return fmt.Errorf("failed to start image: %s", i.image)
|
||||
}
|
||||
|
||||
// try 10 times to get chainID until is ok.
|
||||
utils.TryTimes(10, func() bool {
|
||||
client, err := ethclient.Dial(i.Endpoint())
|
||||
if err == nil && client != nil {
|
||||
i.chainID, err = client.ChainID(context.Background())
|
||||
return err == nil && i.chainID != nil
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsRunning returns docker container's running status.
|
||||
func (i *ImgGeth) IsRunning() bool {
|
||||
return i.running
|
||||
}
|
||||
|
||||
// Endpoint return the connection endpoint.
|
||||
func (i *ImgGeth) Endpoint() string {
|
||||
if !i.running {
|
||||
return ""
|
||||
}
|
||||
switch true {
|
||||
case i.httpPort != 0:
|
||||
return fmt.Sprintf("http://127.0.0.1:%d", i.httpPort)
|
||||
@@ -71,6 +87,11 @@ func (i *ImgGeth) Endpoint() string {
|
||||
}
|
||||
}
|
||||
|
||||
// ChainID return chainID.
|
||||
func (i *ImgGeth) ChainID() *big.Int {
|
||||
return i.chainID
|
||||
}
|
||||
|
||||
func (i *ImgGeth) isOk() bool {
|
||||
keyword := "WebSocket enabled"
|
||||
okCh := make(chan struct{}, 1)
|
||||
|
||||
@@ -8,8 +8,6 @@ import (
|
||||
_ "github.com/lib/pq" //nolint:golint
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
_ "scroll-tech/database/cmd/app"
|
||||
|
||||
"scroll-tech/common/docker"
|
||||
)
|
||||
|
||||
@@ -25,50 +23,32 @@ func TestMain(m *testing.M) {
|
||||
base.Free()
|
||||
}
|
||||
|
||||
func TestStartProcess(t *testing.T) {
|
||||
base.RunImages(t)
|
||||
func TestDB(t *testing.T) {
|
||||
base.RunDBImage(t)
|
||||
|
||||
// migrate db.
|
||||
base.RunDBApp(t, "reset", "successful to reset")
|
||||
base.RunDBApp(t, "migrate", "current version:")
|
||||
db, err := sqlx.Open("postgres", base.DBImg.Endpoint())
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, db.Ping())
|
||||
}
|
||||
|
||||
func TestDocker(t *testing.T) {
|
||||
base.RunImages(t)
|
||||
t.Parallel()
|
||||
t.Run("testL1Geth", testL1Geth)
|
||||
t.Run("testL2Geth", testL2Geth)
|
||||
t.Run("testDB", testDB)
|
||||
}
|
||||
|
||||
func testL1Geth(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
func TestL1Geth(t *testing.T) {
|
||||
base.RunL1Geth(t)
|
||||
|
||||
client, err := base.L1Client()
|
||||
assert.NoError(t, err)
|
||||
|
||||
chainID, err := client.ChainID(ctx)
|
||||
chainID, err := client.ChainID(context.Background())
|
||||
assert.NoError(t, err)
|
||||
t.Logf("chainId: %s", chainID.String())
|
||||
}
|
||||
|
||||
func testL2Geth(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
func TestL2Geth(t *testing.T) {
|
||||
base.RunL2Geth(t)
|
||||
|
||||
client, err := base.L2Client()
|
||||
assert.NoError(t, err)
|
||||
|
||||
chainID, err := client.ChainID(ctx)
|
||||
chainID, err := client.ChainID(context.Background())
|
||||
assert.NoError(t, err)
|
||||
t.Logf("chainId: %s", chainID.String())
|
||||
}
|
||||
|
||||
func testDB(t *testing.T) {
|
||||
driverName := "postgres"
|
||||
|
||||
db, err := sqlx.Open(driverName, base.DBEndpoint())
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, db.Ping())
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package docker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
@@ -26,6 +27,13 @@ type ImgInstance interface {
|
||||
Start() error
|
||||
Stop() error
|
||||
Endpoint() string
|
||||
IsRunning() bool
|
||||
}
|
||||
|
||||
// GethImgInstance based on ImgInstance and add chainID interface.
|
||||
type GethImgInstance interface {
|
||||
ImgInstance
|
||||
ChainID() *big.Int
|
||||
}
|
||||
|
||||
// GetContainerID returns the ID of Container.
|
||||
|
||||
@@ -3,6 +3,8 @@ package utils
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/modern-go/reflect2"
|
||||
)
|
||||
|
||||
// TryTimes try run several times until the function return true.
|
||||
@@ -42,3 +44,8 @@ func Loop(ctx context.Context, period time.Duration, f func()) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// IsNil Check if the interface is empty.
|
||||
func IsNil(i interface{}) bool {
|
||||
return i == nil || reflect2.IsNil(i)
|
||||
}
|
||||
|
||||
@@ -50,13 +50,14 @@ func randomURL() string {
|
||||
}
|
||||
|
||||
func setEnv(t *testing.T) (err error) {
|
||||
// Start postgres and l2geth docker containers.
|
||||
base.RunDBImage(t)
|
||||
base.RunL2Geth(t)
|
||||
|
||||
// Load config.
|
||||
cfg, err = bridge_config.NewConfig("../bridge/config.json")
|
||||
assert.NoError(t, err)
|
||||
base.RunImages(t)
|
||||
|
||||
// Create db container.
|
||||
cfg.DBConfig.DSN = base.DBEndpoint()
|
||||
cfg.DBConfig = base.DBConfig
|
||||
|
||||
templateBlockTrace, err := os.ReadFile("../common/testdata/blockTrace_02.json")
|
||||
if err != nil {
|
||||
|
||||
@@ -19,13 +19,10 @@ var (
|
||||
|
||||
func initEnv(t *testing.T) error {
|
||||
// Start db container.
|
||||
base.RunImages(t)
|
||||
base.RunDBImage(t)
|
||||
|
||||
// Create db orm handler.
|
||||
factory, err := database.NewOrmFactory(&database.DBConfig{
|
||||
DriverName: "postgres",
|
||||
DSN: base.DBEndpoint(),
|
||||
})
|
||||
factory, err := database.NewOrmFactory(base.DBConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -75,7 +75,6 @@ var (
|
||||
batchData1 *types.BatchData
|
||||
batchData2 *types.BatchData
|
||||
|
||||
dbConfig *database.DBConfig
|
||||
base *docker.App
|
||||
ormBlock orm.BlockTraceOrm
|
||||
ormLayer1 orm.L1MessageOrm
|
||||
@@ -85,13 +84,11 @@ var (
|
||||
)
|
||||
|
||||
func setupEnv(t *testing.T) error {
|
||||
// Init db config and start db container.
|
||||
dbConfig = &database.DBConfig{DriverName: "postgres"}
|
||||
base.RunImages(t)
|
||||
dbConfig.DSN = base.DBEndpoint()
|
||||
// Start postgres docker container.
|
||||
base.RunDBImage(t)
|
||||
|
||||
// Create db handler and reset db.
|
||||
factory, err := database.NewOrmFactory(dbConfig)
|
||||
factory, err := database.NewOrmFactory(base.DBConfig)
|
||||
assert.NoError(t, err)
|
||||
db := factory.GetDB()
|
||||
assert.NoError(t, migrate.ResetDB(db.DB))
|
||||
@@ -176,7 +173,7 @@ func TestOrmFactory(t *testing.T) {
|
||||
|
||||
func testOrmBlockTraces(t *testing.T) {
|
||||
// Create db handler and reset db.
|
||||
factory, err := database.NewOrmFactory(dbConfig)
|
||||
factory, err := database.NewOrmFactory(base.DBConfig)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, migrate.ResetDB(factory.GetDB().DB))
|
||||
|
||||
@@ -216,7 +213,7 @@ func testOrmBlockTraces(t *testing.T) {
|
||||
|
||||
func testOrmL1Message(t *testing.T) {
|
||||
// Create db handler and reset db.
|
||||
factory, err := database.NewOrmFactory(dbConfig)
|
||||
factory, err := database.NewOrmFactory(base.DBConfig)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, migrate.ResetDB(factory.GetDB().DB))
|
||||
|
||||
@@ -250,7 +247,7 @@ func testOrmL1Message(t *testing.T) {
|
||||
|
||||
func testOrmL2Message(t *testing.T) {
|
||||
// Create db handler and reset db.
|
||||
factory, err := database.NewOrmFactory(dbConfig)
|
||||
factory, err := database.NewOrmFactory(base.DBConfig)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, migrate.ResetDB(factory.GetDB().DB))
|
||||
|
||||
@@ -286,7 +283,7 @@ func testOrmL2Message(t *testing.T) {
|
||||
// testOrmBlockBatch test rollup result table functions
|
||||
func testOrmBlockBatch(t *testing.T) {
|
||||
// Create db handler and reset db.
|
||||
factory, err := database.NewOrmFactory(dbConfig)
|
||||
factory, err := database.NewOrmFactory(base.DBConfig)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, migrate.ResetDB(factory.GetDB().DB))
|
||||
|
||||
@@ -371,7 +368,7 @@ func testOrmBlockBatch(t *testing.T) {
|
||||
// testOrmSessionInfo test rollup result table functions
|
||||
func testOrmSessionInfo(t *testing.T) {
|
||||
// Create db handler and reset db.
|
||||
factory, err := database.NewOrmFactory(dbConfig)
|
||||
factory, err := database.NewOrmFactory(base.DBConfig)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, migrate.ResetDB(factory.GetDB().DB))
|
||||
dbTx, err := factory.Beginx()
|
||||
|
||||
@@ -16,7 +16,6 @@ import (
|
||||
"github.com/scroll-tech/go-ethereum/rpc"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"scroll-tech/database"
|
||||
_ "scroll-tech/database/cmd/app"
|
||||
|
||||
_ "scroll-tech/roller/cmd/app"
|
||||
@@ -43,7 +42,6 @@ var (
|
||||
wsPort int64
|
||||
|
||||
bridgeFile string
|
||||
dbFile string
|
||||
coordinatorFile string
|
||||
|
||||
bboltDB string
|
||||
@@ -61,7 +59,6 @@ func setupEnv(t *testing.T) {
|
||||
|
||||
// Load reset and store config into a random file.
|
||||
bridgeFile = mockBridgeConfig(t)
|
||||
dbFile = mockDatabaseConfig(t)
|
||||
coordinatorFile = mockCoordinatorConfig(t)
|
||||
rollerFile = mockRollerConfig(t)
|
||||
}
|
||||
@@ -71,49 +68,40 @@ func free(t *testing.T) {
|
||||
|
||||
// Delete temporary files.
|
||||
assert.NoError(t, os.Remove(bridgeFile))
|
||||
assert.NoError(t, os.Remove(dbFile))
|
||||
assert.NoError(t, os.Remove(coordinatorFile))
|
||||
assert.NoError(t, os.Remove(rollerFile))
|
||||
assert.NoError(t, os.Remove(bboltDB))
|
||||
}
|
||||
|
||||
type appAPI interface {
|
||||
OpenLog(open bool)
|
||||
WaitResult(t *testing.T, timeout time.Duration, keyword string) bool
|
||||
RunApp(waitResult func() bool)
|
||||
WaitExit()
|
||||
ExpectWithTimeout(t *testing.T, parallel bool, timeout time.Duration, keyword string)
|
||||
}
|
||||
|
||||
func runMsgRelayerApp(t *testing.T, args ...string) appAPI {
|
||||
func runMsgRelayerApp(t *testing.T, args ...string) docker.AppAPI {
|
||||
args = append(args, "--log.debug", "--config", bridgeFile)
|
||||
app := cmd.NewCmd("message-relayer-test", args...)
|
||||
app.OpenLog(true)
|
||||
return app
|
||||
}
|
||||
|
||||
func runGasOracleApp(t *testing.T, args ...string) appAPI {
|
||||
func runGasOracleApp(t *testing.T, args ...string) docker.AppAPI {
|
||||
args = append(args, "--log.debug", "--config", bridgeFile)
|
||||
app := cmd.NewCmd("gas-oracle-test", args...)
|
||||
app.OpenLog(true)
|
||||
return app
|
||||
}
|
||||
|
||||
func runRollupRelayerApp(t *testing.T, args ...string) appAPI {
|
||||
func runRollupRelayerApp(t *testing.T, args ...string) docker.AppAPI {
|
||||
args = append(args, "--log.debug", "--config", bridgeFile)
|
||||
app := cmd.NewCmd("rollup-relayer-test", args...)
|
||||
app.OpenLog(true)
|
||||
return app
|
||||
}
|
||||
|
||||
func runEventWatcherApp(t *testing.T, args ...string) appAPI {
|
||||
func runEventWatcherApp(t *testing.T, args ...string) docker.AppAPI {
|
||||
args = append(args, "--log.debug", "--config", bridgeFile)
|
||||
app := cmd.NewCmd("event-watcher-test", args...)
|
||||
app.OpenLog(true)
|
||||
return app
|
||||
}
|
||||
|
||||
func runCoordinatorApp(t *testing.T, args ...string) appAPI {
|
||||
func runCoordinatorApp(t *testing.T, args ...string) docker.AppAPI {
|
||||
args = append(args, "--log.debug", "--config", coordinatorFile, "--ws", "--ws.port", strconv.Itoa(int(wsPort)))
|
||||
// start process
|
||||
app := cmd.NewCmd("coordinator-test", args...)
|
||||
@@ -122,7 +110,7 @@ func runCoordinatorApp(t *testing.T, args ...string) appAPI {
|
||||
}
|
||||
|
||||
func runDBCliApp(t *testing.T, option, keyword string) {
|
||||
args := []string{option, "--config", dbFile}
|
||||
args := []string{option, "--config", base.DBConfigFile}
|
||||
app := cmd.NewCmd("db_cli-test", args...)
|
||||
app.OpenLog(true)
|
||||
defer app.WaitExit()
|
||||
@@ -132,7 +120,7 @@ func runDBCliApp(t *testing.T, option, keyword string) {
|
||||
app.RunApp(nil)
|
||||
}
|
||||
|
||||
func runRollerApp(t *testing.T, args ...string) appAPI {
|
||||
func runRollerApp(t *testing.T, args ...string) docker.AppAPI {
|
||||
args = append(args, "--log.debug", "--config", rollerFile)
|
||||
app := cmd.NewCmd("roller-test", args...)
|
||||
app.OpenLog(true)
|
||||
@@ -160,11 +148,11 @@ func mockBridgeConfig(t *testing.T) string {
|
||||
cfg, err := bridgeConfig.NewConfig("../../bridge/config.json")
|
||||
assert.NoError(t, err)
|
||||
|
||||
cfg.L1Config.Endpoint = base.L1GethEndpoint()
|
||||
cfg.L2Config.RelayerConfig.SenderConfig.Endpoint = base.L1GethEndpoint()
|
||||
cfg.L2Config.Endpoint = base.L2GethEndpoint()
|
||||
cfg.L1Config.RelayerConfig.SenderConfig.Endpoint = base.L2GethEndpoint()
|
||||
cfg.DBConfig.DSN = base.DBEndpoint()
|
||||
cfg.L1Config.Endpoint = base.L1gethImg.Endpoint()
|
||||
cfg.L2Config.RelayerConfig.SenderConfig.Endpoint = base.L1gethImg.Endpoint()
|
||||
cfg.L2Config.Endpoint = base.L2gethImg.Endpoint()
|
||||
cfg.L1Config.RelayerConfig.SenderConfig.Endpoint = base.L2gethImg.Endpoint()
|
||||
cfg.DBConfig = base.DBConfig
|
||||
|
||||
// Store changed bridge config into a temp file.
|
||||
data, err := json.Marshal(cfg)
|
||||
@@ -182,9 +170,9 @@ func mockCoordinatorConfig(t *testing.T) string {
|
||||
|
||||
cfg.RollerManagerConfig.Verifier.MockMode = true
|
||||
|
||||
cfg.DBConfig.DSN = base.DBEndpoint()
|
||||
cfg.DBConfig = base.DBConfig
|
||||
|
||||
cfg.L2Config.Endpoint = base.L2GethEndpoint()
|
||||
cfg.L2Config.Endpoint = base.L2gethImg.Endpoint()
|
||||
|
||||
data, err := json.Marshal(cfg)
|
||||
assert.NoError(t, err)
|
||||
@@ -197,11 +185,7 @@ func mockCoordinatorConfig(t *testing.T) string {
|
||||
}
|
||||
|
||||
func mockDatabaseConfig(t *testing.T) string {
|
||||
cfg, err := database.NewConfig("../../database/config.json")
|
||||
assert.NoError(t, err)
|
||||
cfg.DSN = base.DBEndpoint()
|
||||
|
||||
data, err := json.Marshal(cfg)
|
||||
data, err := json.Marshal(base.DBConfig)
|
||||
assert.NoError(t, err)
|
||||
|
||||
file := fmt.Sprintf("/tmp/%d_db-config.json", timestamp)
|
||||
|
||||
Reference in New Issue
Block a user