forked from hashicorp/terraform-exec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_test.go
98 lines (84 loc) · 2.81 KB
/
cmd_test.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
package tfexec
import (
"fmt"
"os/exec"
"strings"
"testing"
"github.com/hashicorp/terraform-exec/internal/version"
)
func TestMergeUserAgent(t *testing.T) {
for i, c := range []struct {
expected string
uas []string
}{
{"foo/1 bar/2", []string{"foo/1", "bar/2"}},
{"foo/1 bar/2", []string{"foo/1 bar/2"}},
{"foo/1 bar/2", []string{"", "foo/1", "bar/2"}},
{"foo/1 bar/2", []string{"", "foo/1 bar/2"}},
{"foo/1 bar/2", []string{" ", "foo/1 bar/2"}},
{"foo/1 bar/2", []string{"foo/1", "", "bar/2"}},
{"foo/1 bar/2", []string{"foo/1", " ", "bar/2"}},
// comments
{"foo/1 (bar/1 bar/2 bar/3) bar/2", []string{"foo/1 (bar/1 bar/2 bar/3)", "bar/2"}},
} {
t.Run(fmt.Sprintf("%d %s", i, c.expected), func(t *testing.T) {
actual := mergeUserAgent(c.uas...)
if c.expected != actual {
t.Fatalf("expected %q, got %q", c.expected, actual)
}
})
}
}
func defaultEnv() []string {
return []string{
"CHECKPOINT_DISABLE=",
"TF_APPEND_USER_AGENT=HashiCorp-terraform-exec/" + version.ModuleVersion(),
"TF_IN_AUTOMATION=1",
"TF_LOG_PATH=",
"TF_LOG=",
"TF_WORKSPACE=",
}
}
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)...))
actualEnv := envMap(actual.Env)
if len(actualEnv) != len(actual.Env) {
t.Fatalf("duplication in actual env, unable to assert: %v", actual.Env)
}
// ignore tempdir related env vars from comparison
for _, k := range []string{"TMPDIR", "TMP", "TEMP", "USERPROFILE"} {
if _, ok := expectedEnv[k]; ok {
t.Logf("ignoring env var %q", k)
delete(expectedEnv, k)
}
if _, ok := actualEnv[k]; ok {
t.Logf("ignoring env var %q", k)
delete(actualEnv, k)
}
}
// compare against raw slice len incase of duplication or something
if len(expectedEnv) != len(actualEnv) {
t.Fatalf("env mismatch\n\nexpected:\n%v\n\ngot:\n%v", envSlice(expectedEnv), 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))
}
}
}