github.com/argoproj/argo-cd/v3@v3.2.1/util/exec/exec_norace_test.go (about)

     1  //go:build !race
     2  
     3  package exec
     4  
     5  import (
     6  	"testing"
     7  	"time"
     8  
     9  	log "github.com/sirupsen/logrus"
    10  	"github.com/sirupsen/logrus/hooks/test"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  // FIXME: there's a race in RunCommand that causes the test to fail when -race is enabled. The race is in the timeout
    16  // handling, which shares bytes buffers between the exec goroutine and the timeout handler code.
    17  func TestRunCommandTimeout(t *testing.T) {
    18  	hook := test.NewGlobal()
    19  	log.SetLevel(log.DebugLevel)
    20  	defer log.SetLevel(log.InfoLevel)
    21  
    22  	output, err := RunCommand("sh", CmdOpts{Timeout: 500 * time.Millisecond}, "-c", "echo hello world && echo my-error >&2 && sleep 2")
    23  	assert.Equal(t, "hello world", output)
    24  	require.EqualError(t, err, "`sh -c echo hello world && echo my-error >&2 && sleep 2` failed timeout after 500ms")
    25  
    26  	assert.Len(t, hook.Entries, 3)
    27  
    28  	entry := hook.Entries[0]
    29  	assert.Equal(t, log.InfoLevel, entry.Level)
    30  	assert.Equal(t, "sh -c echo hello world && echo my-error >&2 && sleep 2", entry.Message)
    31  	assert.Contains(t, entry.Data, "dir")
    32  	assert.Contains(t, entry.Data, "execID")
    33  
    34  	entry = hook.Entries[1]
    35  	assert.Equal(t, log.DebugLevel, entry.Level)
    36  	assert.Equal(t, "hello world\n", entry.Message)
    37  	assert.Contains(t, entry.Data, "duration")
    38  	assert.Contains(t, entry.Data, "execID")
    39  
    40  	entry = hook.Entries[2]
    41  	assert.Equal(t, log.ErrorLevel, entry.Level)
    42  	assert.Equal(t, "`sh -c echo hello world && echo my-error >&2 && sleep 2` failed timeout after 500ms", entry.Message)
    43  	assert.Contains(t, entry.Data, "execID")
    44  }