Skip to content

Commit 505ec47

Browse files
committed
try new logstream lifecycle
1 parent dd986d8 commit 505ec47

23 files changed

+240
-166
lines changed

lib/docker/test_env/besu_eth1.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ func NewBesuEth1(networks []string, chainConfig *config.EthereumChainConfig, opt
3636
l: logging.GetTestLogger(nil),
3737
ethereumVersion: config_types.EthereumVersion_Eth1,
3838
}
39-
g.SetDefaultHooks()
4039
for _, opt := range opts {
4140
opt(&g.EnvComponent)
4241
}
42+
err := g.InitLogConsumerConfig(g.l)
43+
if err != nil {
44+
return nil, err
45+
}
4346

4447
if !g.WasRecreated {
4548
// set the container name again after applying functional options as version might have changed
@@ -108,12 +111,7 @@ func (g *Besu) getEth1ContainerRequest() (*tc.ContainerRequest, error) {
108111
ReadOnly: false,
109112
})
110113
},
111-
LifecycleHooks: []tc.ContainerLifecycleHooks{
112-
{
113-
PostStarts: g.PostStartsHooks,
114-
PostStops: g.PostStopsHooks,
115-
},
116-
},
114+
LogConsumerCfg: g.LogConsumerConfig,
117115
}, nil
118116
}
119117

lib/docker/test_env/besu_eth2.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ func NewBesuEth2(networks []string, chainConfig *config.EthereumChainConfig, gen
4141
l: logging.GetTestLogger(nil),
4242
ethereumVersion: config_types.EthereumVersion_Eth2,
4343
}
44-
g.SetDefaultHooks()
4544
for _, opt := range opts {
4645
opt(&g.EnvComponent)
4746
}
47+
err := g.InitLogConsumerConfig(g.l)
48+
if err != nil {
49+
return nil, err
50+
}
4851

4952
if !g.WasRecreated {
5053
// set the container name again after applying functional options as version might have changed
@@ -153,12 +156,7 @@ func (g *Besu) getEth2ContainerRequest() (*tc.ContainerRequest, error) {
153156
ReadOnly: false,
154157
})
155158
},
156-
LifecycleHooks: []tc.ContainerLifecycleHooks{
157-
{
158-
PostStarts: g.PostStartsHooks,
159-
PostStops: g.PostStopsHooks,
160-
},
161-
},
159+
LogConsumerCfg: g.LogConsumerConfig,
162160
}, nil
163161
}
164162

lib/docker/test_env/env_component.go

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@ const (
1919
)
2020

2121
type EnvComponent struct {
22-
ContainerName string `json:"containerName"`
23-
ContainerImage string `json:"containerImage"`
24-
ContainerVersion string `json:"containerVersion"`
25-
ContainerEnvs map[string]string `json:"containerEnvs"`
26-
WasRecreated bool `json:"wasRecreated"`
27-
Networks []string `json:"networks"`
28-
Container tc.Container `json:"-"`
29-
LogStream *logstream.LogStream `json:"-"`
30-
PostStartsHooks []tc.ContainerHook `json:"-"`
31-
PostStopsHooks []tc.ContainerHook `json:"-"`
32-
PreTerminatesHooks []tc.ContainerHook `json:"-"`
33-
LogLevel string `json:"-"`
34-
StartupTimeout time.Duration `json:"-"`
22+
ContainerName string `json:"containerName"`
23+
ContainerImage string `json:"containerImage"`
24+
ContainerVersion string `json:"containerVersion"`
25+
ContainerEnvs map[string]string `json:"containerEnvs"`
26+
WasRecreated bool `json:"wasRecreated"`
27+
Networks []string `json:"networks"`
28+
Container tc.Container `json:"-"`
29+
LogStream *logstream.LogStream `json:"-"`
30+
PostStartsHooks []tc.ContainerHook `json:"-"`
31+
PostStopsHooks []tc.ContainerHook `json:"-"`
32+
PreTerminatesHooks []tc.ContainerHook `json:"-"`
33+
LogLevel string `json:"-"`
34+
StartupTimeout time.Duration `json:"-"`
35+
LogConsumerConfig *tc.LogConsumerConfig `json:"-"`
3536
}
3637

3738
type EnvComponentOption = func(c *EnvComponent)
@@ -94,6 +95,24 @@ func WithPreTerminatesHooks(hooks ...tc.ContainerHook) EnvComponentOption {
9495
}
9596
}
9697

98+
func (ec *EnvComponent) InitLogConsumerConfig(l zerolog.Logger) error {
99+
if ec.LogStream == nil {
100+
l.Warn().Msg("LogStream is nil, cannot initialize LogConsumerConfig. Check your lifecycle and make sure to set LogStream before calling this function")
101+
return nil
102+
}
103+
104+
logConsumer, err := ec.LogStream.LogConsumerForContainer(ec.ContainerName, "")
105+
if err != nil {
106+
return err
107+
}
108+
109+
ec.LogConsumerConfig = &tc.LogConsumerConfig{Consumers: []tc.LogConsumer{logConsumer}, Opts: []tc.LogProductionOption{tc.WithLogProductionTimeout(ec.LogStream.GetLogProducerTimeout())}}
110+
111+
return nil
112+
}
113+
114+
// SetDefaultHooks sets default hooks that connect the container to the log stream and manage log production cycle
115+
// Deprecated: use InitLogConsumerConfig() to use default log production lifecycle
97116
func (ec *EnvComponent) SetDefaultHooks() {
98117
ec.PostStartsHooks = []tc.ContainerHook{
99118
func(ctx context.Context, c tc.Container) error {
@@ -103,6 +122,7 @@ func (ec *EnvComponent) SetDefaultHooks() {
103122
return nil
104123
},
105124
}
125+
// maybe we could use this step to automatically flush all logs, but we don't know at this point if we should do that (as we don't know if the test has failed)
106126
ec.PostStopsHooks = []tc.ContainerHook{
107127
func(ctx context.Context, c tc.Container) error {
108128
if ec.LogStream != nil {

lib/docker/test_env/erigon_eth1.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ func NewErigonEth1(networks []string, chainConfig *config.EthereumChainConfig, o
3737
l: logging.GetTestLogger(nil),
3838
ethereumVersion: config_types.EthereumVersion_Eth1,
3939
}
40-
g.SetDefaultHooks()
4140
for _, opt := range opts {
4241
opt(&g.EnvComponent)
4342
}
43+
err := g.InitLogConsumerConfig(g.l)
44+
if err != nil {
45+
return nil, err
46+
}
4447

4548
if !g.WasRecreated {
4649
// set the container name again after applying functional options as version might have changed
@@ -147,12 +150,7 @@ func (g *Erigon) getEth1ContainerRequest() (*tc.ContainerRequest, error) {
147150
},
148151
)
149152
},
150-
LifecycleHooks: []tc.ContainerLifecycleHooks{
151-
{
152-
PostStarts: g.PostStartsHooks,
153-
PostStops: g.PostStopsHooks,
154-
},
155-
},
153+
LogConsumerCfg: g.LogConsumerConfig,
156154
}, nil
157155
}
158156

lib/docker/test_env/erigon_eth2.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ func NewErigonEth2(networks []string, chainConfig *config.EthereumChainConfig, g
3838
l: logging.GetTestLogger(nil),
3939
ethereumVersion: config_types.EthereumVersion_Eth2,
4040
}
41-
g.SetDefaultHooks()
4241
for _, opt := range opts {
4342
opt(&g.EnvComponent)
4443
}
44+
err := g.InitLogConsumerConfig(g.l)
45+
if err != nil {
46+
return nil, err
47+
}
4548

4649
if !g.WasRecreated {
4750
// set the container name again after applying functional options as version might have changed
@@ -98,12 +101,7 @@ func (g *Erigon) getEth2ContainerRequest() (*tc.ContainerRequest, error) {
98101
ReadOnly: false,
99102
})
100103
},
101-
LifecycleHooks: []tc.ContainerLifecycleHooks{
102-
{
103-
PostStarts: g.PostStartsHooks,
104-
PostStops: g.PostStopsHooks,
105-
},
106-
},
104+
LogConsumerCfg: g.LogConsumerConfig,
107105
}, nil
108106
}
109107

lib/docker/test_env/eth2_init_helpers.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type AfterGenesisHelper struct {
2929
posContainerSettings
3030
}
3131

32-
func NewInitHelper(chainConfig config.EthereumChainConfig, generatedDataHostDir, generatedDataContainerDir string, opts ...EnvComponentOption) *AfterGenesisHelper {
32+
func NewInitHelper(chainConfig config.EthereumChainConfig, generatedDataHostDir, generatedDataContainerDir string, opts ...EnvComponentOption) (*AfterGenesisHelper, error) {
3333
g := &AfterGenesisHelper{
3434
EnvComponent: EnvComponent{
3535
ContainerName: fmt.Sprintf("%s-%s", "after-genesis-helper", uuid.NewString()[0:8]),
@@ -40,11 +40,15 @@ func NewInitHelper(chainConfig config.EthereumChainConfig, generatedDataHostDir,
4040
l: log.Logger,
4141
addressesToFund: []string{},
4242
}
43-
g.SetDefaultHooks()
4443
for _, opt := range opts {
4544
opt(&g.EnvComponent)
4645
}
47-
return g
46+
err := g.InitLogConsumerConfig(g.l)
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
return g, nil
4852
}
4953

5054
func (g *AfterGenesisHelper) WithTestInstance(t *testing.T) *AfterGenesisHelper {
@@ -115,12 +119,7 @@ func (g *AfterGenesisHelper) getContainerRequest(networks []string) (*tc.Contain
115119
ReadOnly: false,
116120
})
117121
},
118-
LifecycleHooks: []tc.ContainerLifecycleHooks{
119-
{
120-
PostStarts: g.PostStartsHooks,
121-
PostStops: g.PostStopsHooks,
122-
},
123-
},
122+
LogConsumerCfg: g.LogConsumerConfig,
124123
}, nil
125124
}
126125

lib/docker/test_env/ethereum_env.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ func (en *EthereumNetwork) startEth1() (blockchain.EVMNetwork, RpcProvider, erro
584584
var clientErr error
585585
switch *en.ExecutionLayer {
586586
case config_types.ExecutionLayer_Geth:
587-
client = NewGethEth1(dockerNetworks, en.EthereumChainConfig, opts...)
587+
client, clientErr = NewGethEth1(dockerNetworks, en.EthereumChainConfig, opts...)
588588
case config_types.ExecutionLayer_Besu:
589589
client, clientErr = NewBesuEth1(dockerNetworks, en.EthereumChainConfig, opts...)
590590
case config_types.ExecutionLayer_Erigon:
@@ -683,7 +683,14 @@ func (en *EthereumNetwork) generateGenesisAndFoldersIfNeeded(baseEthereumFork et
683683

684684
generatedDataContainerDir = genesis.GetGeneratedDataContainerDir()
685685

686-
initHelper := NewInitHelper(*en.EthereumChainConfig, generatedDataHostDir, generatedDataContainerDir).WithTestInstance(en.t)
686+
var initHelper *AfterGenesisHelper
687+
688+
initHelper, err = NewInitHelper(*en.EthereumChainConfig, generatedDataHostDir, generatedDataContainerDir)
689+
if err != nil {
690+
err = errors.Wrap(err, "failed to create init helper")
691+
return
692+
}
693+
initHelper.WithTestInstance(en.t)
687694
err = initHelper.StartContainer()
688695
if err != nil {
689696
err = errors.Wrap(err, "failed to start init helper")

lib/docker/test_env/genesis_generator.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,15 @@ func NewEthGenesisGenerator(chainConfig config.EthereumChainConfig, generatedDat
6565
generatedDataContainerDir: generatedDataContainerDir,
6666
l: log.Logger,
6767
}
68-
g.SetDefaultHooks()
6968
for _, opt := range opts {
7069
opt(&g.EnvComponent)
7170
}
71+
72+
err := g.InitLogConsumerConfig(g.l)
73+
if err != nil {
74+
return nil, err
75+
}
76+
7277
// if the internal docker repo is set then add it to the version
7378
g.EnvComponent.ContainerImage = mirror.AddMirrorToImageIfSet(g.EnvComponent.ContainerImage)
7479
return g, nil
@@ -186,12 +191,7 @@ func (g *EthGenesisGenerator) getContainerRequest(networks []string) (*tc.Contai
186191
ReadOnly: false,
187192
})
188193
},
189-
LifecycleHooks: []tc.ContainerLifecycleHooks{
190-
{
191-
PostStarts: g.PostStartsHooks,
192-
PostStops: g.PostStopsHooks,
193-
},
194-
},
194+
LogConsumerCfg: g.LogConsumerConfig,
195195
}, nil
196196
}
197197

lib/docker/test_env/geth_eth1.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
// NewGethEth1 starts a new Geth Eth1 node running in Docker
26-
func NewGethEth1(networks []string, chainConfig *config.EthereumChainConfig, opts ...EnvComponentOption) *Geth {
26+
func NewGethEth1(networks []string, chainConfig *config.EthereumChainConfig, opts ...EnvComponentOption) (*Geth, error) {
2727
parts := strings.Split(ethereum.DefaultGethEth1Image, ":")
2828
g := &Geth{
2929
EnvComponent: EnvComponent{
@@ -37,18 +37,22 @@ func NewGethEth1(networks []string, chainConfig *config.EthereumChainConfig, opt
3737
l: logging.GetTestLogger(nil),
3838
ethereumVersion: config_types.EthereumVersion_Eth1,
3939
}
40-
g.SetDefaultHooks()
4140
for _, opt := range opts {
4241
opt(&g.EnvComponent)
4342
}
4443

44+
err := g.InitLogConsumerConfig(g.l)
45+
if err != nil {
46+
return nil, err
47+
}
48+
4549
if !g.WasRecreated {
4650
// set the container name again after applying functional options as version might have changed
4751
g.EnvComponent.ContainerName = fmt.Sprintf("%s-%s-%s", "geth-eth1", strings.Replace(g.ContainerVersion, ".", "_", -1), uuid.NewString()[0:8])
4852
}
4953
// if the internal docker repo is set then add it to the version
5054
g.EnvComponent.ContainerImage = mirror.AddMirrorToImageIfSet(g.EnvComponent.ContainerImage)
51-
return g
55+
return g, nil
5256
}
5357

5458
func (g *Geth) getEth1ContainerRequest() (*tc.ContainerRequest, error) {
@@ -155,11 +159,6 @@ func (g *Geth) getEth1ContainerRequest() (*tc.ContainerRequest, error) {
155159
ReadOnly: false,
156160
})
157161
},
158-
LifecycleHooks: []tc.ContainerLifecycleHooks{
159-
{
160-
PostStarts: g.PostStartsHooks,
161-
PostStops: g.PostStopsHooks,
162-
},
163-
},
162+
LogConsumerCfg: g.LogConsumerConfig,
164163
}, nil
165164
}

lib/docker/test_env/geth_eth2.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ func NewGethEth2(networks []string, chainConfig *config.EthereumChainConfig, gen
3939
l: logging.GetTestLogger(nil),
4040
ethereumVersion: config_types.EthereumVersion_Eth2,
4141
}
42-
g.SetDefaultHooks()
4342
for _, opt := range opts {
4443
opt(&g.EnvComponent)
4544
}
45+
err := g.InitLogConsumerConfig(g.l)
46+
if err != nil {
47+
return nil, err
48+
}
4649

4750
if !g.WasRecreated {
4851
// set the container name again after applying functional options as version might have changed
@@ -98,12 +101,7 @@ func (g *Geth) getEth2ContainerRequest() (*tc.ContainerRequest, error) {
98101
ReadOnly: false,
99102
})
100103
},
101-
LifecycleHooks: []tc.ContainerLifecycleHooks{
102-
{
103-
PostStarts: g.PostStartsHooks,
104-
PostStops: g.PostStopsHooks,
105-
},
106-
},
104+
LogConsumerCfg: g.LogConsumerConfig,
107105
}, nil
108106
}
109107

0 commit comments

Comments
 (0)