Skip to content

Commit

Permalink
modify testcontainer (scroll-tech#1302)
Browse files Browse the repository at this point in the history
Co-authored-by: georgehao <[email protected]>
  • Loading branch information
AlexStocks and georgehao authored Apr 30, 2024
1 parent a3635db commit 46b1ff3
Show file tree
Hide file tree
Showing 19 changed files with 121 additions and 177 deletions.
127 changes: 0 additions & 127 deletions common/docker-compose/l1/docker-compose.go

This file was deleted.

1 change: 0 additions & 1 deletion common/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.21
require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/bits-and-blooms/bitset v1.12.0
github.com/cloudflare/cfssl v0.0.0-20180223231731-4e2dcbde5004
github.com/docker/docker v25.0.3+incompatible
github.com/gin-contrib/pprof v1.4.0
github.com/gin-gonic/gin v1.9.1
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ services:
mkdir -p /data/execution &&
cp -a /execution/* /data/execution/"
volumes:
- ${HOST_PATH:-../../..}/common/docker-compose/l1/consensus:/consensus
- ${HOST_PATH:-../../..}/common/docker-compose/l1/execution:/execution
- ../../common/testcontainers/consensus:/consensus
- ../../common/testcontainers/execution:/execution
- data:/data

# Creates a genesis state for the beacon chain using a YAML configuration file and
Expand Down Expand Up @@ -96,7 +96,7 @@ services:
- --nodiscover
- --syncmode=full
ports:
- ${GETH_HTTP_PORT:-8545}:8545
- 8545
depends_on:
geth-genesis:
condition: service_completed_successfully
Expand Down
File renamed without changes.
82 changes: 82 additions & 0 deletions common/testcontainers/testcontainers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"time"

"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/compose"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/testcontainers/testcontainers-go/wait"
"gorm.io/gorm"
Expand All @@ -20,6 +23,7 @@ type TestcontainerApps struct {
postgresContainer *postgres.PostgresContainer
l1GethContainer *testcontainers.DockerContainer
l2GethContainer *testcontainers.DockerContainer
poSL1Container compose.ComposeStack

// common time stamp in nanoseconds.
Timestamp int
Expand Down Expand Up @@ -106,6 +110,55 @@ func (t *TestcontainerApps) StartL2GethContainer() error {
return nil
}

// StartPoSL1Container starts the PoS L1 container by running the associated Docker Compose configuration
func (t *TestcontainerApps) StartPoSL1Container() error {
var (
err error
rootDir string
dockerComposeFile string
)

if rootDir, err = findProjectRootDir(); err != nil {
return fmt.Errorf("failed to find project root directory: %v", err)
}

dockerComposeFile = filepath.Join(rootDir, "common", "testcontainers", "docker-compose.yml")

if t.poSL1Container, err = compose.NewDockerCompose([]string{dockerComposeFile}...); err != nil {
return err
}
err = t.poSL1Container.WaitForService("geth", wait.NewHTTPStrategy("/").
WithPort("8545/tcp").
WithStartupTimeout(15*time.Second)).
Up(context.Background())
if err != nil {
t.poSL1Container = nil
return fmt.Errorf("failed to start PoS L1 container: %w", err)
}
return nil
}

// GetPoSL1EndPoint returns the endpoint of the running PoS L1 endpoint
func (t *TestcontainerApps) GetPoSL1EndPoint() (string, error) {
if t.poSL1Container == nil {
return "", fmt.Errorf("PoS L1 container is not running")
}
contrainer, err := t.poSL1Container.ServiceContainer(context.Background(), "geth")
if err != nil {
panic(err)
}
return contrainer.PortEndpoint(context.Background(), "8545/tcp", "http")
}

// GetPoSL1Client returns a ethclient by dialing running PoS L1 client
func (t *TestcontainerApps) GetPoSL1Client() (*ethclient.Client, error) {
endpoint, err := t.GetPoSL1EndPoint()
if err != nil {
return nil, err
}
return ethclient.Dial(endpoint)
}

// GetDBEndPoint returns the endpoint of the running postgres container
func (t *TestcontainerApps) GetDBEndPoint() (string, error) {
if t.postgresContainer == nil || !t.postgresContainer.IsRunning() {
Expand Down Expand Up @@ -197,4 +250,33 @@ func (t *TestcontainerApps) Free() {
log.Printf("failed to stop scroll_l2geth container: %s", err)
}
}
if t.poSL1Container != nil {
if err := t.poSL1Container.Down(context.Background(), compose.RemoveOrphans(true), compose.RemoveVolumes(true), compose.RemoveImagesLocal); err != nil {
log.Printf("failed to stop PoS L1 container: %s", err)
} else {
t.poSL1Container = nil
}
}
}

// findProjectRootDir find project root directory
func findProjectRootDir() (string, error) {
currentDir, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("failed to get working directory: %w", err)
}

for {
_, err := os.Stat(filepath.Join(currentDir, "go.work"))
if err == nil {
return currentDir, nil
}

parentDir := filepath.Dir(currentDir)
if parentDir == currentDir {
return "", fmt.Errorf("go.work file not found in any parent directory")
}

currentDir = parentDir
}
}
15 changes: 14 additions & 1 deletion common/testcontainers/testcontainers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ func TestNewTestcontainerApps(t *testing.T) {
ethclient *ethclient.Client
)

// test start testcontainers
testApps := NewTestcontainerApps()

// test start testcontainers
assert.NoError(t, testApps.StartPostgresContainer())
endpoint, err = testApps.GetDBEndPoint()
assert.NoError(t, err)
Expand All @@ -43,6 +44,14 @@ func TestNewTestcontainerApps(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, ethclient)

assert.NoError(t, testApps.StartPoSL1Container())
endpoint, err = testApps.GetPoSL1EndPoint()
assert.NoError(t, err)
assert.NotEmpty(t, endpoint)
ethclient, err = testApps.GetPoSL1Client()
assert.NoError(t, err)
assert.NotNil(t, ethclient)

// test free testcontainers
testApps.Free()
endpoint, err = testApps.GetDBEndPoint()
Expand All @@ -56,4 +65,8 @@ func TestNewTestcontainerApps(t *testing.T) {
endpoint, err = testApps.GetL2GethEndPoint()
assert.EqualError(t, err, "l2 geth is not running")
assert.Empty(t, endpoint)

endpoint, err = testApps.GetPoSL1EndPoint()
assert.EqualError(t, err, "PoS L1 container is not running")
assert.Empty(t, endpoint)
}
3 changes: 1 addition & 2 deletions coordinator/test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ import (
"github.com/stretchr/testify/assert"
"gorm.io/gorm"

"scroll-tech/database/migrate"

"scroll-tech/common/testcontainers"
"scroll-tech/common/types"
"scroll-tech/common/types/encoding"
"scroll-tech/common/types/message"
"scroll-tech/common/version"
"scroll-tech/database/migrate"

"scroll-tech/coordinator/internal/config"
"scroll-tech/coordinator/internal/controller/api"
Expand Down
4 changes: 2 additions & 2 deletions prover/cmd/app/mock_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (

"github.com/scroll-tech/go-ethereum/rpc"

"scroll-tech/prover/config"

"scroll-tech/common/cmd"
"scroll-tech/common/testcontainers"
"scroll-tech/common/types/message"
"scroll-tech/common/utils"

"scroll-tech/prover/config"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions rollup/cmd/mock_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"testing"
"time"

"scroll-tech/rollup/internal/config"

"scroll-tech/common/cmd"
"scroll-tech/common/testcontainers"
"scroll-tech/common/utils"

"scroll-tech/rollup/internal/config"
)

// MockApp mockApp-test client manager.
Expand Down
16 changes: 4 additions & 12 deletions rollup/internal/controller/relayer/relayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/stretchr/testify/assert"

"scroll-tech/common/database"
dockercompose "scroll-tech/common/docker-compose/l1"
"scroll-tech/common/testcontainers"
"scroll-tech/common/types/encoding"
"scroll-tech/common/types/encoding/codecv0"
Expand All @@ -26,9 +25,7 @@ var (
// config
cfg *config.Config

testApps *testcontainers.TestcontainerApps
posL1TestEnv *dockercompose.PoSL1TestEnv

testApps *testcontainers.TestcontainerApps
// l2geth client
l2Cli *ethclient.Client

Expand All @@ -53,15 +50,13 @@ func setupEnv(t *testing.T) {
cfg, err = config.NewConfig("../../../conf/config.json")
assert.NoError(t, err)

posL1TestEnv, err = dockercompose.NewPoSL1TestEnv()
assert.NoError(t, err, "failed to create PoS L1 test environment")
assert.NoError(t, posL1TestEnv.Start(), "failed to start PoS L1 test environment")

testApps = testcontainers.NewTestcontainerApps()
assert.NoError(t, testApps.StartPostgresContainer())
assert.NoError(t, testApps.StartL2GethContainer())
assert.NoError(t, testApps.StartPoSL1Container())

cfg.L2Config.RelayerConfig.SenderConfig.Endpoint = posL1TestEnv.Endpoint()
cfg.L2Config.RelayerConfig.SenderConfig.Endpoint, err = testApps.GetPoSL1EndPoint()
assert.NoError(t, err)
cfg.L1Config.RelayerConfig.SenderConfig.Endpoint, err = testApps.GetL2GethEndPoint()
assert.NoError(t, err)

Expand Down Expand Up @@ -110,9 +105,6 @@ func TestMain(m *testing.M) {
if testApps != nil {
testApps.Free()
}
if posL1TestEnv != nil {
posL1TestEnv.Stop()
}
}()
m.Run()
}
Expand Down
Loading

0 comments on commit 46b1ff3

Please sign in to comment.