/
cmd.go
99 lines (85 loc) · 3.26 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package terraform
import (
"fmt"
"strings"
"testing"
"github.com/gruntwork-io/terratest/modules/collections"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/retry"
"github.com/gruntwork-io/terratest/modules/shell"
)
// GetCommonOptions extracts commons terraform options
func GetCommonOptions(options *Options, args ...string) (*Options, []string) {
if options.NoColor && !collections.ListContains(args, "-no-color") {
args = append(args, "-no-color")
}
// if SshAgent is provided, override the local SSH agent with the socket of our in-process agent
if options.SshAgent != nil {
// Initialize EnvVars, if it hasn't been set yet
if options.EnvVars == nil {
options.EnvVars = map[string]string{}
}
options.EnvVars["SSH_AUTH_SOCK"] = options.SshAgent.SocketFile()
}
return options, args
}
// RunTerraformCommand runs terraform with the given arguments and options and return stdout/stderr.
func RunTerraformCommand(t *testing.T, additionalOptions *Options, args ...string) string {
out, err := RunTerraformCommandE(t, additionalOptions, 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, additionalOptions *Options, additionalArgs ...string) (string, error) {
options, args := GetCommonOptions(additionalOptions, additionalArgs...)
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}
})
}
// GetExitCodeForTerraformCommand runs terraform with the given arguments and options and returns exit code
func GetExitCodeForTerraformCommand(t *testing.T, additionalOptions *Options, args ...string) int {
exitCode, err := GetExitCodeForTerraformCommandE(t, additionalOptions, args...)
if err != nil {
t.Fatal(err)
}
return exitCode
}
// GetExitCodeForTerraformCommandE runs terraform with the given arguments and options and returns exit code
func GetExitCodeForTerraformCommandE(t *testing.T, additionalOptions *Options, additionalArgs ...string) (int, error) {
options, args := GetCommonOptions(additionalOptions, additionalArgs...)
logger.Log(t, "Running terraform %v", args)
cmd := shell.Command{
Command: "terraform",
Args: args,
WorkingDir: options.TerraformDir,
Env: options.EnvVars,
}
_, err := shell.RunCommandAndGetOutputE(t, cmd)
if err == nil {
return DefaultSuccessExitCode, nil
}
exitCode, getExitCodeErr := shell.GetExitCodeForRunCommandError(err)
if getExitCodeErr == nil {
return exitCode, nil
}
return DefaultErrorExitCode, getExitCodeErr
}