/
cmd.go
47 lines (40 loc) · 1.43 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package terraform
import (
"fmt"
"strings"
"testing"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/retry"
"github.com/gruntwork-io/terratest/modules/shell"
)
// RunTerraformCommand runs terraform with the given arguments and options and return stdout/stderr.
func RunTerraformCommand(t *testing.T, options *Options, args ...string) string {
out, err := RunTerraformCommandE(t, options, args...)
if err != nil {
t.Fatal(err)
}
return out
}
// RunTerraformCommandE runs terraform with the given arguments and options and return stdout/stderr.
func RunTerraformCommandE(t *testing.T, options *Options, args ...string) (string, error) {
description := fmt.Sprintf("Running terraform %v", args)
return retry.DoWithRetryE(t, description, options.MaxRetries, options.TimeBetweenRetries, func() (string, error) {
cmd := shell.Command{
Command: "terraform",
Args: args,
WorkingDir: options.TerraformDir,
Env: options.EnvVars,
}
out, err := shell.RunCommandAndGetOutputE(t, cmd)
if err == nil {
return out, nil
}
for errorText, errorMessage := range options.RetryableTerraformErrors {
if strings.Contains(out, errorText) {
logger.Logf(t, "terraform failed with the error '%s' but this error was expected and warrants a retry. Further details: %s\n", errorText, errorMessage)
return out, err
}
}
return out, retry.FatalError{Underlying: err}
})
}