Skip to content

Commit

Permalink
feat: print stack traces after 2 minutes if the test is not finished
Browse files Browse the repository at this point in the history
  • Loading branch information
pgimalac committed Nov 28, 2023
1 parent 8bfeaac commit 88f89e6
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion comp/core/secrets/secretsimpl/fetch_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import (
"os"
"os/exec"
"runtime"
"runtime/pprof"
"testing"
"time"

"github.com/DataDog/datadog-agent/comp/core/secrets"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/DataDog/datadog-agent/comp/core/secrets"
)

var (
Expand Down Expand Up @@ -81,6 +84,32 @@ func TestLimitBuffer(t *testing.T) {
}

func TestExecCommandError(t *testing.T) {
// TODO: remove once the issue is resolved
//
// This test has an issue on Windows where it can block entirely and timeout after 4 minutes
// (while the go test timeout is 3 minutes), and in that case it doesn't print stack traces.
//
// As a workaround, we explicitly write routine stack traces in an artifact if the test
// is not finished after 2 minutes.
var done chan struct{}
if _, ok := os.LookupEnv("CI_PIPELINE_ID"); ok && runtime.GOOS == "windows" {
go func() {
select {
case <-done:
case <-time.After(2 * time.Minute):
// files junit-*.tgz are automatically considered as artifacts
file, err := os.OpenFile(`C:\mnt\junit-TestExecCommandError.tgz`, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "could not write stack traces: %v", err)
return
}
defer file.Close()
fmt.Fprintf(file, "test will timeout, printing goroutine stack traces:\n")
pprof.Lookup("goroutine").WriteTo(file, 2)
}
}()
}

inputPayload := "{\"version\": \"" + secrets.PayloadVersion + "\" , \"secrets\": [\"sec1\", \"sec2\"]}"

t.Run("Empty secretBackendCommand", func(t *testing.T) {
Expand Down Expand Up @@ -147,6 +176,8 @@ func TestExecCommandError(t *testing.T) {
require.NotNil(t, err)
assert.Equal(t, "error while running './test/response_too_long/response_too_long"+binExtension+"': command output was too long: exceeded 20 bytes", err.Error())
})

done <- struct{}{}
}

func TestFetchSecretExeceError(t *testing.T) {
Expand Down

0 comments on commit 88f89e6

Please sign in to comment.