Skip to content

Commit

Permalink
Better Log Collection (#10824)
Browse files Browse the repository at this point in the history
  • Loading branch information
kalverra authored Sep 28, 2023
1 parent bfdd7d1 commit de737a5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 42 deletions.
80 changes: 39 additions & 41 deletions integration-tests/docker/test_env/test_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,45 @@ func (te *CLClusterTestEnv) Cleanup(t *testing.T) error {
return errors.New("chainlink nodes are nil, unable to return funds from chainlink nodes")
}

// TODO: This is an imperfect and temporary solution, see TT-590 for a more sustainable solution
// Collect logs if the test fails, or if we just want them
if t.Failed() || os.Getenv("TEST_LOG_COLLECT") == "true" {
folder := fmt.Sprintf("./logs/%s-%s", t.Name(), time.Now().Format("2006-01-02T15-04-05"))
if err := os.MkdirAll(folder, os.ModePerm); err != nil {
return err
}

te.l.Info().Msg("Collecting test logs")
eg := &errgroup.Group{}
for _, n := range te.CLNodes {
node := n
eg.Go(func() error {
logFileName := filepath.Join(folder, fmt.Sprintf("node-%s.log", node.ContainerName))
logFile, err := os.OpenFile(logFileName, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer logFile.Close()
logReader, err := node.Container.Logs(context.Background())
if err != nil {
return err
}
_, err = io.Copy(logFile, logReader)
if err != nil {
return err
}
te.l.Info().Str("Node", node.ContainerName).Str("File", logFileName).Msg("Wrote Logs")
return nil
})
}

if err := eg.Wait(); err != nil {
return err
}

te.l.Info().Str("Logs Location", folder).Msg("Wrote test logs")
}

// Check if we need to return funds
if te.EVMClient.NetworkSimulated() {
te.l.Info().Str("Network Name", te.EVMClient.GetNetworkName()).
Expand Down Expand Up @@ -277,46 +316,5 @@ func (te *CLClusterTestEnv) Cleanup(t *testing.T) error {
}
}

// TODO: This is an imperfect and temporary solution, see TT-590 for a more sustainable solution
// Collect logs if the test failed
if !t.Failed() {
return nil
}

folder := fmt.Sprintf("./logs/%s-%s", t.Name(), time.Now().Format("2006-01-02T15-04-05"))
if err := os.MkdirAll(folder, os.ModePerm); err != nil {
return err
}

te.l.Warn().Msg("Test failed, collecting logs")
eg := &errgroup.Group{}
for _, n := range te.CLNodes {
node := n
eg.Go(func() error {
logFileName := filepath.Join(folder, fmt.Sprintf("node-%s.log", node.ContainerName))
logFile, err := os.OpenFile(logFileName, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer logFile.Close()
logReader, err := node.Container.Logs(context.Background())
if err != nil {
return err
}
_, err = io.Copy(logFile, logReader)
if err != nil {
return err
}
te.l.Info().Str("Node", node.ContainerName).Str("File", logFileName).Msg("Wrote Logs")
return nil
})
}

if err := eg.Wait(); err != nil {
return err
}

te.l.Info().Str("Logs Location", folder).Msg("Wrote Logs for Failed Test")

return nil
}
2 changes: 1 addition & 1 deletion integration-tests/example.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# `source ./integration-tests/.env`

########## General Test Settings ##########
export KEEP_ENVIRONMENTS="Never" # Always | OnFail | Never
export CHAINLINK_IMAGE="public.ecr.aws/chainlink/chainlink" # Image repo to pull the Chainlink image from
export CHAINLINK_VERSION="2.4.0" # Version of the Chainlink image to pull
export CHAINLINK_ENV_USER="Satoshi-Nakamoto" # Name of the person running the tests (change to your own)
export TEST_LOG_LEVEL="info" # info | debug | trace
export TEST_LOG_COLLECT="false" # true | false, whether to collect the test logs after the test is complete, this will always be done if the test fails, regardless of this setting

########## Soak/Chaos/Load Test Specific Settings ##########
# Remote Runner
Expand Down

0 comments on commit de737a5

Please sign in to comment.