This repository has been archived by the owner on Aug 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathintegration_test.go
79 lines (69 loc) · 1.71 KB
/
integration_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
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
"reflect"
"sort"
"strings"
"testing"
)
var binPath string
func TestMain(m *testing.M) {
tmpFile, err := ioutil.TempFile("", "secretly")
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmpFile.Name())
binPath = tmpFile.Name()
if err := exec.Command("go", "build", "-o", binPath).Run(); err != nil {
log.Fatal(err)
}
ex := m.Run()
os.Remove(binPath) // defer doesn't run before os exit
os.Exit(ex)
}
func Test_cliEnv(t *testing.T) {
tests := []struct {
name string
environ []string
wantEnv []string
wantErr bool
}{
{"totally empty", []string{}, []string{}, false},
{"passes through", []string{"FOO_BAR=BAZ"}, []string{"FOO_BAR=BAZ"}, false},
{"AWS error", []string{"SECRETLY_NAMESPACE=BAZ"}, nil, true},
{"AWS error multiple namespaces", []string{"SECRETLY_NAMESPACE=BAZ,BAR"}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// we're going to override the PATH var, so look it up
envPath, err := exec.LookPath("env")
if err != nil {
t.Fatal(err)
}
cmd := exec.Command(binPath, envPath)
cmd.Env = tt.environ
out, err := cmd.Output()
if err != nil {
if !tt.wantErr {
t.Fatal(err)
}
// errored as expected
return
}
// always allocate array b/c reflect.DeepEqual treats empty and nil slices differently
outputEnv := make([]string, 0)
for _, line := range strings.Split(string(out), "\n") {
if strings.Trim(line, " ") != "" {
outputEnv = append(outputEnv, strings.Trim(line, " "))
}
}
sort.Strings(outputEnv)
if !reflect.DeepEqual(outputEnv, tt.wantEnv) {
t.Errorf("cli = got %v, want %v", outputEnv, tt.wantEnv)
}
})
}
}