Repository navigation
Expand file tree
/
Copy pathutil_test.go
More file actions
278 lines (253 loc) · 7.32 KB
/
Copy pathutil_test.go
File metadata and controls
278 lines (253 loc) · 7.32 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package chartify
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
)
func TestHasChartLock(t *testing.T) {
tests := []struct {
name string
lockName string // empty means no lock file is created
want bool
}{
{name: "no lock file", lockName: "", want: false},
{name: "Chart.lock present", lockName: "Chart.lock", want: true},
{name: "requirements.lock present", lockName: "requirements.lock", want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir()
if tt.lockName != "" {
path := filepath.Join(dir, tt.lockName)
if err := os.WriteFile(path, []byte("dependencies: []\n"), 0644); err != nil {
t.Fatalf("writing fixture: %v", err)
}
}
if got := hasChartLock(dir); got != tt.want {
t.Errorf("hasChartLock() = %v, want %v", got, tt.want)
}
})
}
}
func TestIsLockOutOfSyncErr(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{name: "out of sync message", err: fmt.Errorf("the lock file (Chart.lock) is out of sync with the dependencies listed in Chart.yaml"), want: true},
{name: "lock file is out of date", err: fmt.Errorf("lock file is out of date"), want: true},
{name: "network error", err: fmt.Errorf("network timeout fetching dependency"), want: false},
{name: "auth error", err: fmt.Errorf("401 unauthorized"), want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isLockOutOfSyncErr(tt.err); got != tt.want {
t.Errorf("isLockOutOfSyncErr() = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateFlagChain(t *testing.T) {
testcases := []struct {
flag string
values []string
expect string
}{
{
flag: "foo",
values: []string{"1"},
expect: " --foo 1",
},
{
flag: "foo",
values: []string{"1", "2"},
expect: " --foo 1 --foo 2",
},
{
flag: "f",
values: []string{"a"},
expect: " -f a",
},
{
flag: "f",
values: []string{"a", "b"},
expect: " -f a -f b",
},
}
for i, tc := range testcases {
actual := createFlagChain(tc.flag, tc.values)
if diff := cmp.Diff(tc.expect, actual); diff != "" {
t.Errorf("case %d:\n%s", i, diff)
}
}
}
// TestFindSemVerInfo tests the FindSemVerInfo function.
func TestFindSemVerInfo(t *testing.T) {
tests := []struct {
name string
version string
want string
wantErr bool
}{
{
name: "valid semver for v4",
version: "{Version:kustomize/v4.5.7 GitCommit:56d82a8378dfc8dc3b3b1085e5a6e67b82966bd7 BuildDate:2022-08-02T16:35:54Z GoOs:darwin GoArch:arm64}",
want: "v4.5.7",
wantErr: false,
},
{
name: "valid semver for v3",
version: "{Version:kustomize/v3.10.0 GitCommit:602ad8aa98e2e17f6c9119e027a09757e63c8bec BuildDate:2021-02-10T00:00:50Z GoOs:linux GoArch:amd64}",
want: "v3.10.0",
wantErr: false,
},
{
name: "invalid semver",
version: "version",
want: "",
wantErr: true,
},
{
name: "valid semver for v5",
version: "v5.0.0",
want: "v5.0.0",
wantErr: false,
},
{
name: "semver with extra info",
version: "v1.2.3-alpha+001",
want: "v1.2.3-alpha+001",
wantErr: false,
},
{
name: "semver not start with v",
version: "1.2.3",
want: "v1.2.3",
wantErr: false,
},
{
name: "helm version info with WARNING message",
version: "WARNING: both 'platformCommand' and 'command' are set in \"<HOMEDIR>/snap/code/194/.local/share/helm/plugins/helm-secrets/plugin.yaml\" (this will become an error in a future Helm version)\nv4.0.0+g99cd196",
want: "v4.0.0+g99cd196",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := FindSemVerInfo(tt.version)
if (err != nil) != tt.wantErr {
t.Errorf("FindSemVerInfo() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("FindSemVerInfo() = %v, want %v", got, tt.want)
}
})
}
}
func TestKustomizeBin(t *testing.T) {
t.Run("KustomizeBinary option is set", func(t *testing.T) {
r := New(KustomizeBin("/custom/kustomize"))
got := r.kustomizeBin()
want := "/custom/kustomize"
if got != want {
t.Errorf("kustomizeBin() = %v, want %v", got, want)
}
})
t.Run("KUSTOMIZE_BIN environment variable", func(t *testing.T) {
origVal, hadVal := os.LookupEnv("KUSTOMIZE_BIN")
defer func() {
if hadVal {
os.Setenv("KUSTOMIZE_BIN", origVal)
} else {
os.Unsetenv("KUSTOMIZE_BIN")
}
}()
os.Setenv("KUSTOMIZE_BIN", "/custom/kustomize")
r := New()
got := r.kustomizeBin()
want := "/custom/kustomize"
if got != want {
t.Errorf("kustomizeBin() = %v, want %v", got, want)
}
})
t.Run("fallback to kubectl kustomize when kustomize not found", func(t *testing.T) {
tmpDir := t.TempDir()
binDir := filepath.Join(tmpDir, "bin")
require.NoError(t, os.MkdirAll(binDir, 0755))
kubectlPath := filepath.Join(binDir, "kubectl")
kubectlContent := []byte("#!/bin/sh\necho 'kubectl version'\n")
require.NoError(t, os.WriteFile(kubectlPath, kubectlContent, 0755))
origPath := os.Getenv("PATH")
defer os.Setenv("PATH", origPath)
os.Setenv("PATH", binDir)
// Ensure KUSTOMIZE_BIN does not override PATH-based lookup.
origKustomizeBin, hadKustomizeBin := os.LookupEnv("KUSTOMIZE_BIN")
defer func() {
if hadKustomizeBin {
os.Setenv("KUSTOMIZE_BIN", origKustomizeBin)
}
}()
os.Unsetenv("KUSTOMIZE_BIN")
r := New()
got := r.kustomizeBin()
want := "kubectl kustomize"
if got != want {
t.Errorf("kustomizeBin() = %v, want %v", got, want)
}
})
t.Run("use kustomize when both kustomize and kubectl exist in PATH", func(t *testing.T) {
tmpDir := t.TempDir()
binDir := filepath.Join(tmpDir, "bin")
require.NoError(t, os.MkdirAll(binDir, 0755))
kustomizePath := filepath.Join(binDir, "kustomize")
kustomizeContent := []byte("#!/bin/sh\necho 'kustomize version'\n")
require.NoError(t, os.WriteFile(kustomizePath, kustomizeContent, 0755))
kubectlPath := filepath.Join(binDir, "kubectl")
kubectlContent := []byte("#!/bin/sh\necho 'kubectl version'\n")
require.NoError(t, os.WriteFile(kubectlPath, kubectlContent, 0755))
origPath := os.Getenv("PATH")
defer os.Setenv("PATH", origPath)
os.Setenv("PATH", binDir)
// Ensure KUSTOMIZE_BIN does not override PATH-based lookup.
origKustomizeBin, hadKustomizeBin := os.LookupEnv("KUSTOMIZE_BIN")
defer func() {
if hadKustomizeBin {
os.Setenv("KUSTOMIZE_BIN", origKustomizeBin)
}
}()
os.Unsetenv("KUSTOMIZE_BIN")
r := New()
got := r.kustomizeBin()
want := "kustomize"
if got != want {
t.Errorf("kustomizeBin() = %v, want %v", got, want)
}
})
t.Run("return kustomize as fallback when neither kustomize nor kubectl exist", func(t *testing.T) {
tmpDir := t.TempDir()
binDir := filepath.Join(tmpDir, "bin")
require.NoError(t, os.MkdirAll(binDir, 0755))
origPath := os.Getenv("PATH")
defer os.Setenv("PATH", origPath)
os.Setenv("PATH", binDir)
// Ensure KUSTOMIZE_BIN does not override PATH-based lookup.
origKustomizeBin, hadKustomizeBin := os.LookupEnv("KUSTOMIZE_BIN")
defer func() {
if hadKustomizeBin {
os.Setenv("KUSTOMIZE_BIN", origKustomizeBin)
}
}()
os.Unsetenv("KUSTOMIZE_BIN")
r := New()
got := r.kustomizeBin()
want := "kustomize"
if got != want {
t.Errorf("kustomizeBin() = %v, want %v", got, want)
}
})
}