Skip to content

Commit

Permalink
test: replace l1GethContainer with poSL1Container (#1312)
Browse files Browse the repository at this point in the history
Co-authored-by: TKTech660 <[email protected]>
  • Loading branch information
luky116 and TKTech660 authored Apr 30, 2024
1 parent 0723b46 commit d1bec53
Show file tree
Hide file tree
Showing 8 changed files with 6 additions and 78 deletions.
60 changes: 1 addition & 59 deletions common/testcontainers/testcontainers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
// TestcontainerApps testcontainers struct
type TestcontainerApps struct {
postgresContainer *postgres.PostgresContainer
l1GethContainer *testcontainers.DockerContainer
l2GethContainer *testcontainers.DockerContainer
poSL1Container compose.ComposeStack

Expand Down Expand Up @@ -57,33 +56,6 @@ func (t *TestcontainerApps) StartPostgresContainer() error {
return nil
}

// StartL1GethContainer starts a L1Geth container
func (t *TestcontainerApps) StartL1GethContainer() error {
if t.l1GethContainer != nil && t.l1GethContainer.IsRunning() {
return nil
}
req := testcontainers.ContainerRequest{
Image: "scroll_l1geth",
ExposedPorts: []string{"8546/tcp", "8545/tcp"},
WaitingFor: wait.ForAll(
wait.ForListeningPort("8546").WithStartupTimeout(100*time.Second),
wait.ForListeningPort("8545").WithStartupTimeout(100*time.Second),
),
Cmd: []string{"--log.debug", "ANY"},
}
genericContainerReq := testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
}
container, err := testcontainers.GenericContainer(context.Background(), genericContainerReq)
if err != nil {
log.Printf("failed to start scroll_l1geth container: %s", err)
return err
}
t.l1GethContainer, _ = container.(*testcontainers.DockerContainer)
return nil
}

// StartL2GethContainer starts a L2Geth container
func (t *TestcontainerApps) StartL2GethContainer() error {
if t.l2GethContainer != nil && t.l2GethContainer.IsRunning() {
Expand Down Expand Up @@ -145,7 +117,7 @@ func (t *TestcontainerApps) GetPoSL1EndPoint() (string, error) {
}
contrainer, err := t.poSL1Container.ServiceContainer(context.Background(), "geth")
if err != nil {
panic(err)
return "", err
}
return contrainer.PortEndpoint(context.Background(), "8545/tcp", "http")
}
Expand All @@ -167,18 +139,6 @@ func (t *TestcontainerApps) GetDBEndPoint() (string, error) {
return t.postgresContainer.ConnectionString(context.Background(), "sslmode=disable")
}

// GetL1GethEndPoint returns the endpoint of the running L1Geth container
func (t *TestcontainerApps) GetL1GethEndPoint() (string, error) {
if t.l1GethContainer == nil || !t.l1GethContainer.IsRunning() {
return "", fmt.Errorf("l1 geth is not running")
}
endpoint, err := t.l1GethContainer.PortEndpoint(context.Background(), "8546/tcp", "ws")
if err != nil {
return "", err
}
return endpoint, nil
}

// GetL2GethEndPoint returns the endpoint of the running L2Geth container
func (t *TestcontainerApps) GetL2GethEndPoint() (string, error) {
if t.l2GethContainer == nil || !t.l2GethContainer.IsRunning() {
Expand Down Expand Up @@ -206,19 +166,6 @@ func (t *TestcontainerApps) GetGormDBClient() (*gorm.DB, error) {
return database.InitDB(dbCfg)
}

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

// GetL2GethClient returns a ethclient by dialing running L2Geth
func (t *TestcontainerApps) GetL2GethClient() (*ethclient.Client, error) {
endpoint, err := t.GetL2GethEndPoint()
Expand All @@ -240,11 +187,6 @@ func (t *TestcontainerApps) Free() {
log.Printf("failed to stop postgres container: %s", err)
}
}
if t.l1GethContainer != nil && t.l1GethContainer.IsRunning() {
if err := t.l1GethContainer.Terminate(ctx); err != nil {
log.Printf("failed to stop scroll_l1geth container: %s", err)
}
}
if t.l2GethContainer != nil && t.l2GethContainer.IsRunning() {
if err := t.l2GethContainer.Terminate(ctx); err != nil {
log.Printf("failed to stop scroll_l2geth container: %s", err)
Expand Down
12 changes: 0 additions & 12 deletions common/testcontainers/testcontainers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ func TestNewTestcontainerApps(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, gormDBclient)

assert.NoError(t, testApps.StartL1GethContainer())
endpoint, err = testApps.GetL1GethEndPoint()
assert.NoError(t, err)
assert.NotEmpty(t, endpoint)
ethclient, err = testApps.GetL1GethClient()
assert.NoError(t, err)
assert.NotNil(t, ethclient)

assert.NoError(t, testApps.StartL2GethContainer())
endpoint, err = testApps.GetL2GethEndPoint()
assert.NoError(t, err)
Expand All @@ -58,10 +50,6 @@ func TestNewTestcontainerApps(t *testing.T) {
assert.EqualError(t, err, "postgres is not running")
assert.Empty(t, endpoint)

endpoint, err = testApps.GetL1GethEndPoint()
assert.EqualError(t, err, "l1 geth is not running")
assert.Empty(t, endpoint)

endpoint, err = testApps.GetL2GethEndPoint()
assert.EqualError(t, err, "l2 geth is not running")
assert.Empty(t, endpoint)
Expand Down
2 changes: 1 addition & 1 deletion rollup/cmd/mock_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (b *MockApp) MockConfig(store bool) error {
return err
}

l1GethEndpoint, err := b.testApps.GetL1GethEndPoint()
l1GethEndpoint, err := b.testApps.GetPoSL1EndPoint()
if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion rollup/internal/controller/sender/sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func setupEnv(t *testing.T) {

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

Expand Down
2 changes: 1 addition & 1 deletion rollup/internal/controller/watcher/l1_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

func setupL1Watcher(t *testing.T) (*L1WatcherClient, *gorm.DB) {
db := setupDB(t)
client, err := testApps.GetL1GethClient()
client, err := testApps.GetPoSL1Client()
assert.NoError(t, err)
l1Cfg := cfg.L1Config
watcher := NewL1WatcherClient(context.Background(), client, l1Cfg.StartHeight, l1Cfg.Confirmations, l1Cfg.L1MessageQueueAddress, l1Cfg.RelayerConfig.RollupContractAddress, db, nil)
Expand Down
4 changes: 2 additions & 2 deletions rollup/internal/controller/watcher/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ func setupEnv(t *testing.T) (err error) {

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

cfg.L2Config.RelayerConfig.SenderConfig.Endpoint, err = testApps.GetL1GethEndPoint()
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
1 change: 0 additions & 1 deletion rollup/tests/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func setupEnv(t *testing.T) {

testApps = tc.NewTestcontainerApps()
assert.NoError(t, testApps.StartPostgresContainer())
assert.NoError(t, testApps.StartL1GethContainer())
assert.NoError(t, testApps.StartL2GethContainer())
assert.NoError(t, testApps.StartPoSL1Container())
rollupApp = bcmd.NewRollupApp(testApps, "../conf/config.json")
Expand Down
2 changes: 1 addition & 1 deletion tests/integration-test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestMain(m *testing.M) {
func setupEnv(t *testing.T) {
testApps = testcontainers.NewTestcontainerApps()
assert.NoError(t, testApps.StartPostgresContainer())
assert.NoError(t, testApps.StartL1GethContainer())
assert.NoError(t, testApps.StartPoSL1Container())
assert.NoError(t, testApps.StartL2GethContainer())
rollupApp = bcmd.NewRollupApp(testApps, "../../rollup/conf/config.json")
}
Expand Down

0 comments on commit d1bec53

Please sign in to comment.