From dbc971ba57f563f3d0ca105fee588744ede988a6 Mon Sep 17 00:00:00 2001 From: Bartek Tofel Date: Wed, 15 May 2024 18:19:58 +0200 Subject: [PATCH] fix clean up order (#13205) --- integration-tests/docker/test_env/test_env.go | 1 + .../docker/test_env/test_env_builder.go | 72 +++++++++---------- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/integration-tests/docker/test_env/test_env.go b/integration-tests/docker/test_env/test_env.go index 7cb618b8dae..f874b223e31 100644 --- a/integration-tests/docker/test_env/test_env.go +++ b/integration-tests/docker/test_env/test_env.go @@ -274,6 +274,7 @@ func (te *CLClusterTestEnv) handleNodeCoverageReports(testName string) error { if showHTMLCoverageReport || isCI { // Stop all nodes in the chainlink cluster. // This is needed to get go coverage profile from the node containers https://go.dev/doc/build-cover#FAQ + // TODO: fix this as it results in: ERR LOG AFTER TEST ENDED ... INF 🐳 Stopping container err := te.ClCluster.Stop() if err != nil { return err diff --git a/integration-tests/docker/test_env/test_env_builder.go b/integration-tests/docker/test_env/test_env_builder.go index 0089ce0778a..707f7d9222b 100644 --- a/integration-tests/docker/test_env/test_env_builder.go +++ b/integration-tests/docker/test_env/test_env_builder.go @@ -23,7 +23,6 @@ import ( "github.com/smartcontractkit/chainlink-testing-framework/networks" "github.com/smartcontractkit/chainlink-testing-framework/testreporters" "github.com/smartcontractkit/chainlink-testing-framework/utils/osutil" - evmcfg "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/toml" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" @@ -283,42 +282,8 @@ func (b *CLTestEnvBuilder) Build() (*CLClusterTestEnv, error) { if err != nil { return nil, err } - } - - if b.hasKillgrave { - if b.te.DockerNetwork == nil { - return nil, fmt.Errorf("test environment builder failed: %w", fmt.Errorf("cannot start mock adapter without a network")) - } - - b.te.MockAdapter = test_env.NewKillgrave([]string{b.te.DockerNetwork.Name}, "", test_env.WithLogStream(b.te.LogStream)) - - err = b.te.StartMockAdapter() - if err != nil { - return nil, err - } - } - - if b.t != nil { - b.te.WithTestInstance(b.t) - } - - switch b.cleanUpType { - case CleanUpTypeStandard: - b.t.Cleanup(func() { - // Cleanup test environment - if err := b.te.Cleanup(CleanupOpts{TestName: b.t.Name()}); err != nil { - b.l.Error().Err(err).Msg("Error cleaning up test environment") - } - }) - case CleanUpTypeCustom: - b.t.Cleanup(b.cleanUpCustomFn) - case CleanUpTypeNone: - b.l.Warn().Msg("test environment won't be cleaned up") - case "": - return b.te, fmt.Errorf("test environment builder failed: %w", fmt.Errorf("explicit cleanup type must be set when building test environment")) - } - if b.te.LogStream != nil { + // this clean up has to be added as the FIRST one, because cleanup functions are executed in reverse order (LIFO) if b.t != nil { b.t.Cleanup(func() { b.l.Info().Msg("Shutting down LogStream") @@ -379,7 +344,42 @@ func (b *CLTestEnvBuilder) Build() (*CLClusterTestEnv, error) { } }) } + } + + if b.hasKillgrave { + if b.te.DockerNetwork == nil { + return nil, fmt.Errorf("test environment builder failed: %w", fmt.Errorf("cannot start mock adapter without a network")) + } + + b.te.MockAdapter = test_env.NewKillgrave([]string{b.te.DockerNetwork.Name}, "", test_env.WithLogStream(b.te.LogStream)) + + err = b.te.StartMockAdapter() + if err != nil { + return nil, err + } + } + + if b.t != nil { + b.te.WithTestInstance(b.t) + } + switch b.cleanUpType { + case CleanUpTypeStandard: + b.t.Cleanup(func() { + // Cleanup test environment + if err := b.te.Cleanup(CleanupOpts{TestName: b.t.Name()}); err != nil { + b.l.Error().Err(err).Msg("Error cleaning up test environment") + } + }) + case CleanUpTypeCustom: + b.t.Cleanup(b.cleanUpCustomFn) + case CleanUpTypeNone: + b.l.Warn().Msg("test environment won't be cleaned up") + case "": + return b.te, fmt.Errorf("test environment builder failed: %w", fmt.Errorf("explicit cleanup type must be set when building test environment")) + } + + if b.te.LogStream != nil { // this is not the cleanest way to do this, but when we originally build ethereum networks, we don't have the logstream reference // so we need to rebuild them here and pass logstream to them for i := range b.privateEthereumNetworks {