-
Notifications
You must be signed in to change notification settings - Fork 280
/
main_test.go
112 lines (96 loc) · 2.36 KB
/
main_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"fmt"
"os"
"reflect"
"testing"
"github.com/stretchr/testify/require"
"github.com/databus23/helm-diff/v3/cmd"
)
func TestMain(m *testing.M) {
if os.Getenv(env) == envValue {
os.Exit(runFakeHelm())
}
os.Exit(m.Run())
}
func TestHelmDiff(t *testing.T) {
os.Setenv(env, envValue)
defer os.Unsetenv(env)
helmBin, helmBinSet := os.LookupEnv("HELM_BIN")
os.Setenv("HELM_BIN", os.Args[0])
defer func() {
if helmBinSet {
os.Setenv("HELM_BIN", helmBin)
} else {
os.Unsetenv("HELM_BIN")
}
}()
os.Args = []string{"helm-diff", "upgrade", "-f", "test/testdata/test-values.yaml", "test-release", "test/testdata/test-chart"}
require.NoError(t, cmd.New().Execute())
}
const (
env = "BECOME_FAKE_HELM"
envValue = "1"
)
type fakeHelmSubcmd struct {
cmd []string
args []string
stdout string
stderr string
exitCode int
}
var helmSubcmdStubs = []fakeHelmSubcmd{
{
cmd: []string{"version"},
stdout: `version.BuildInfo{Version:"v3.1.0-rc.1", GitCommit:"12345", GitTreeState:"clean", GoVersion:"go1.20.12"}`,
},
{
cmd: []string{"get", "manifest"},
args: []string{"test-release"},
stdout: `---
# Source: test-chart/templates/cm.yaml
`,
},
{
cmd: []string{"template"},
args: []string{"test-release", "test/testdata/test-chart", "--values", "test/testdata/test-values.yaml", "--validate", "--is-upgrade"},
},
{
cmd: []string{"get", "hooks"},
args: []string{"test-release"},
},
}
func runFakeHelm() int {
var stub *fakeHelmSubcmd
if len(os.Args) < 2 {
_, _ = fmt.Fprintln(os.Stderr, "fake helm does not support invocations without subcommands")
return 1
}
cmdAndArgs := os.Args[1:]
for i := range helmSubcmdStubs {
s := helmSubcmdStubs[i]
if reflect.DeepEqual(s.cmd, cmdAndArgs[:len(s.cmd)]) {
stub = &s
break
}
}
if stub == nil {
_, _ = fmt.Fprintf(os.Stderr, "no stub for %s\n", cmdAndArgs)
return 1
}
want := stub.args
if want == nil {
want = []string{}
}
got := cmdAndArgs[len(stub.cmd):]
if !reflect.DeepEqual(want, got) {
_, _ = fmt.Fprintf(os.Stderr, "want: %v\n", want)
_, _ = fmt.Fprintf(os.Stderr, "got : %v\n", got)
_, _ = fmt.Fprintf(os.Stderr, "args : %v\n", os.Args)
_, _ = fmt.Fprintf(os.Stderr, "env : %v\n", os.Environ())
return 1
}
_, _ = fmt.Fprintf(os.Stdout, "%s", stub.stdout)
_, _ = fmt.Fprintf(os.Stderr, "%s", stub.stderr)
return stub.exitCode
}