Add timeout to integration test for execCommand to fail faster

This commit is contained in:
Kristoffer Dalby 2021-10-31 09:58:01 +00:00
parent 264e5964f6
commit ca15a53fad
No known key found for this signature in database
GPG Key ID: 09F62DC067465735

View File

@ -39,7 +39,7 @@ var (
headscale dockertest.Resource headscale dockertest.Resource
) )
var tailscaleVersions = []string{"1.14.3", "1.12.3"} var tailscaleVersions = []string{"1.16.2", "1.14.3", "1.12.3"}
type TestNamespace struct { type TestNamespace struct {
count int count int
@ -99,6 +99,19 @@ func executeCommand(resource *dockertest.Resource, cmd []string, env []string) (
var stdout bytes.Buffer var stdout bytes.Buffer
var stderr bytes.Buffer var stderr bytes.Buffer
// TODO(kradalby): Make configurable
timeout := 10 * time.Second
type result struct {
exitCode int
err error
}
resultChan := make(chan result, 1)
// Run your long running function in it's own goroutine and pass back it's
// response into our channel.
go func() {
exitCode, err := resource.Exec( exitCode, err := resource.Exec(
cmd, cmd,
dockertest.ExecOptions{ dockertest.ExecOptions{
@ -107,11 +120,17 @@ func executeCommand(resource *dockertest.Resource, cmd []string, env []string) (
StdErr: &stderr, StdErr: &stderr,
}, },
) )
if err != nil { resultChan <- result{exitCode, err}
return "", err }()
// Listen on our channel AND a timeout channel - which ever happens first.
select {
case res := <-resultChan:
if res.err != nil {
return "", res.err
} }
if exitCode != 0 { if res.exitCode != 0 {
fmt.Println("Command: ", cmd) fmt.Println("Command: ", cmd)
fmt.Println("stdout: ", stdout.String()) fmt.Println("stdout: ", stdout.String())
fmt.Println("stderr: ", stderr.String()) fmt.Println("stderr: ", stderr.String())
@ -119,6 +138,9 @@ func executeCommand(resource *dockertest.Resource, cmd []string, env []string) (
} }
return stdout.String(), nil return stdout.String(), nil
case <-time.After(timeout):
return "", fmt.Errorf("command timed out after %s", timeout)
}
} }
func saveLog(resource *dockertest.Resource, basePath string) error { func saveLog(resource *dockertest.Resource, basePath string) error {