-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add apply test with -var usage, simplify exec.Cmd assertions
- Loading branch information
Showing
19 changed files
with
529 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package tfexec | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
const ( | ||
checkpointDisableEnvVar = "CHECKPOINT_DISABLE" | ||
logEnvVar = "TF_LOG" | ||
inputEnvVar = "TF_INPUT" | ||
automationEnvVar = "TF_IN_AUTOMATION" | ||
logPathEnvVar = "TF_LOG_PATH" | ||
reattachEnvVar = "TF_REATTACH_PROVIDERS" | ||
|
||
varEnvVarPrefix = "TF_VAR_" | ||
) | ||
|
||
var prohibitedEnvVars = []string{ | ||
inputEnvVar, | ||
automationEnvVar, | ||
logPathEnvVar, | ||
logEnvVar, | ||
reattachEnvVar, | ||
} | ||
|
||
func envMap(environ []string) map[string]string { | ||
env := map[string]string{} | ||
for _, ev := range environ { | ||
parts := strings.SplitN(ev, "=", 2) | ||
if len(parts) == 0 { | ||
continue | ||
} | ||
k := parts[0] | ||
v := "" | ||
if len(parts) == 2 { | ||
v = parts[1] | ||
} | ||
env[k] = v | ||
} | ||
return env | ||
} | ||
|
||
func envSlice(environ map[string]string) []string { | ||
env := []string{} | ||
for k, v := range environ { | ||
env = append(env, k+"="+v) | ||
} | ||
return env | ||
} | ||
|
||
func (tf *Terraform) buildEnv(mergeEnv map[string]string) []string { | ||
// set Terraform level env, if env is nil, fall back to os.Environ | ||
var env map[string]string | ||
if tf.env == nil { | ||
env = envMap(os.Environ()) | ||
} else { | ||
env = make(map[string]string, len(tf.env)) | ||
for k, v := range tf.env { | ||
env[k] = v | ||
} | ||
} | ||
|
||
// override env with any command specific environment | ||
for k, v := range mergeEnv { | ||
env[k] = v | ||
} | ||
|
||
// always propagate CHECKPOINT_DISABLE env var unless it is | ||
// explicitly overridden with tf.SetEnv or command env | ||
if _, ok := env[checkpointDisableEnvVar]; !ok { | ||
env[checkpointDisableEnvVar] = os.Getenv(checkpointDisableEnvVar) | ||
} | ||
|
||
// always override logging | ||
if tf.logPath == "" { | ||
// so logging can't pollute our stderr output | ||
env[logEnvVar] = "" | ||
env[logPathEnvVar] = "" | ||
} else { | ||
env[logPathEnvVar] = tf.logPath | ||
// Log levels other than TRACE are currently unreliable, the CLI recommends using TRACE only. | ||
env[logEnvVar] = "TRACE" | ||
} | ||
|
||
// constant automation override env vars | ||
env[inputEnvVar] = "0" | ||
env[automationEnvVar] = "1" | ||
|
||
return envSlice(env) | ||
} | ||
|
||
func (tf *Terraform) buildTerraformCmd(ctx context.Context, args ...string) *exec.Cmd { | ||
cmd := exec.CommandContext(ctx, tf.execPath, args...) | ||
cmd.Env = tf.buildEnv(nil) | ||
cmd.Dir = tf.workingDir | ||
|
||
tf.logger.Printf("[INFO] running Terraform command: %s", cmdString(cmd)) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package tfexec | ||
|
||
import ( | ||
"os/exec" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
var defaultEnv = []string{ | ||
"TF_LOG=", | ||
"TF_LOG_PATH=", | ||
"TF_INPUT=0", | ||
"TF_IN_AUTOMATION=1", | ||
"CHECKPOINT_DISABLE=", | ||
} | ||
|
||
func assertCmd(t *testing.T, expectedArgs []string, expectedEnv map[string]string, actual *exec.Cmd) { | ||
t.Helper() | ||
|
||
// check args (skip path) | ||
actualArgs := actual.Args[1:] | ||
|
||
if len(expectedArgs) != len(actualArgs) { | ||
t.Fatalf("args mismatch\n\nexpected:\n%v\n\ngot:\n%v", strings.Join(expectedArgs, " "), strings.Join(actualArgs, " ")) | ||
} | ||
for i := range expectedArgs { | ||
if expectedArgs[i] != actualArgs[i] { | ||
t.Fatalf("args mismatch, expected %q, got %q\n\nfull expected:\n%v\n\nfull actual:\n%v", expectedArgs[i], actualArgs[i], strings.Join(expectedArgs, " "), strings.Join(actualArgs, " ")) | ||
} | ||
} | ||
|
||
// check environment | ||
expectedEnv = envMap(append(defaultEnv, envSlice(expectedEnv)...)) | ||
|
||
// compare against raw slice len incase of duplication or something | ||
if len(expectedEnv) != len(actual.Env) { | ||
t.Fatalf("env mismatch\n\nexpected:\n%v\n\ngot:\n%v", envSlice(expectedEnv), actual.Env) | ||
} | ||
|
||
actualEnv := envMap(actual.Env) | ||
|
||
for k, ev := range expectedEnv { | ||
av, ok := actualEnv[k] | ||
if !ok { | ||
t.Fatalf("env mismatch, missing %q\n\nfull expected:\n%v\n\nfull actual:\n%v", k, envSlice(expectedEnv), envSlice(actualEnv)) | ||
} | ||
if ev != av { | ||
t.Fatalf("env mismatch, expected %q, got %q\n\nfull expected:\n%v\n\nfull actual:\n%v", ev, av, envSlice(expectedEnv), envSlice(actualEnv)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.