-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathxpenvfuncs_test.go
More file actions
505 lines (462 loc) · 13.1 KB
/
Copy pathxpenvfuncs_test.go
File metadata and controls
505 lines (462 loc) · 13.1 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package xpenvfuncs
import (
"context"
"fmt"
"testing"
"github.com/crossplane-contrib/xp-testing/pkg/vendored"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"sigs.k8s.io/e2e-framework/pkg/env"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/third_party/helm"
)
func TestCompose(t *testing.T) {
incEnvFunc := func(i *int) env.Func {
return func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
*i++
return ctx, nil
}
}
errEnvFunc := func(err error) env.Func {
return func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
return ctx, err
}
}
t.Run(
"nop", func(t *testing.T) {
pctx := context.Background()
ctx, err := Compose()(pctx, nil)
require.NoError(t, err)
require.Equal(t, pctx, ctx)
},
)
t.Run(
"passes ctx and cfg to child envfunc", func(t *testing.T) {
invoked := false
pctx := context.Background()
pcfg := &envconf.Config{}
ctx, err := Compose(
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
invoked = true
require.Equal(t, pctx, ctx)
require.Equal(t, pcfg, cfg)
return ctx, nil
},
)(pctx, pcfg)
require.True(t, invoked)
require.NoError(t, err)
require.Equal(t, pctx, ctx)
},
)
t.Run(
"invokes all configured child funcs", func(t *testing.T) {
invocations := 0
_, err := Compose(
incEnvFunc(&invocations),
incEnvFunc(&invocations),
incEnvFunc(&invocations),
)(context.Background(), &envconf.Config{})
require.NoError(t, err)
require.Equal(t, 3, invocations)
},
)
t.Run(
"stops processing in case of error", func(t *testing.T) {
invocations := 0
_, err := Compose(
incEnvFunc(&invocations),
incEnvFunc(&invocations),
errEnvFunc(fmt.Errorf("stop here")),
incEnvFunc(&invocations),
)(context.Background(), &envconf.Config{})
require.EqualError(t, err, "stop here")
require.Equal(t, 2, invocations)
},
)
}
func TestGetClusterControlPlaneName(t *testing.T) {
require.Equal(t, "my-cluster-control-plane", getClusterControlPlaneName("my-cluster"))
}
func TestRenderTemplate(t *testing.T) {
type args struct {
template string
data interface{}
}
type expects struct {
rendered string
errorMessage string
}
tests := []struct {
description string
args args
expects expects
}{
{
description: "nop",
},
{
description: "simple string without replacements",
args: args{
template: "This is a simple string and nothing should be replaced!",
},
expects: expects{
rendered: "This is a simple string and nothing should be replaced!",
},
},
{
description: "simple string with single replacement",
args: args{
template: "Hello {{.Subject}}!",
data: struct {
Subject string
}{
Subject: "World",
},
},
expects: expects{
rendered: "Hello World!",
},
},
{
description: "multiline string with multiple replacements",
args: args{
template: `apiVersion: pkg.crossplane.io/v1
kind: Provider
metadata:
name: {{.Name}}
spec:
package: {{.Package}}
packagePullPolicy: Never`,
data: struct {
Name string
Package string
}{
Name: "my-provider",
Package: "my-registry.local/path/to/my-provider:1.2.3",
},
},
expects: expects{
rendered: `apiVersion: pkg.crossplane.io/v1
kind: Provider
metadata:
name: my-provider
spec:
package: my-registry.local/path/to/my-provider:1.2.3
packagePullPolicy: Never`,
},
},
{
description: "multiline string with condition is true",
args: args{
template: `apiVersion: pkg.crossplane.io/v1
kind: Provider
metadata:
name: {{.Name}}
spec:
package: {{.Package}}
packagePullPolicy: Never
{{- if .ControllerConfig }}
controllerConfigRef:
name: {{.ControllerConfig}}
{{end}}`,
data: struct {
Name string
Package string
ControllerConfig string
}{
Name: "my-provider",
Package: "my-registry.local/path/to/my-provider:1.2.3",
ControllerConfig: "my-controller-config-ref",
},
},
expects: expects{
rendered: `apiVersion: pkg.crossplane.io/v1
kind: Provider
metadata:
name: my-provider
spec:
package: my-registry.local/path/to/my-provider:1.2.3
packagePullPolicy: Never
controllerConfigRef:
name: my-controller-config-ref
`,
},
},
{
description: "multiline string with condition is false",
args: args{
template: `apiVersion: pkg.crossplane.io/v1
kind: Provider
metadata:
name: {{.Name}}
spec:
package: {{.Package}}
packagePullPolicy: Never
{{- if .ControllerConfig }}
controllerConfigRef:
name: {{.ControllerConfig}}
{{end}}`,
data: struct {
Name string
Package string
ControllerConfig string
}{
Name: "my-provider",
Package: "my-registry.local/path/to/my-provider:1.2.3",
},
},
expects: expects{
rendered: `apiVersion: pkg.crossplane.io/v1
kind: Provider
metadata:
name: my-provider
spec:
package: my-registry.local/path/to/my-provider:1.2.3
packagePullPolicy: Never`,
},
},
{
description: "with runtimeConfigRef",
args: args{
template: `apiVersion: pkg.crossplane.io/v1
kind: Provider
metadata:
name: {{.Name}}
spec:
package: {{.Package}}
packagePullPolicy: Never
{{- if .ControllerConfig }}
runtimeConfigRef:
name: {{.RuntimeConfig}}
{{end}}`,
data: struct {
Name string
Package string
RuntimeConfig string
}{
Name: "my-provider",
Package: "my-registry.local/path/to/my-provider:1.2.3",
RuntimeConfig: "my-runtime-config-ref",
},
},
expects: expects{
rendered: `apiVersion: pkg.crossplane.io/v1
kind: Provider
metadata:
name: my-provider
spec:
package: my-registry.local/path/to/my-provider:1.2.3
packagePullPolicy: Never
runtimeConfigRef:
name: my-runtime-config-ref
`,
},
},
}
for _, test := range tests {
t.Run(
test.description, func(t *testing.T) {
rendered, err := renderTemplate(test.args.template, test.args.data)
if len(test.expects.errorMessage) == 0 {
if err == nil {
require.Equal(t, test.expects.rendered, rendered)
}
} else {
require.Error(t, err)
}
},
)
}
}
var dummyErrFunc = func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
return ctx, errors.New("dummy err")
}
func TestDummyErr(t *testing.T) {
t.Run(
"dummyErrFunc returns error", func(t *testing.T) {
_, err := dummyErrFunc(nil, nil)
require.Error(t, err)
},
)
}
func TestConditional(t *testing.T) {
t.Run(
"executes", func(t *testing.T) {
conditionalFn := Conditional(dummyErrFunc, true)
_, err := conditionalFn(nil, nil)
require.Error(t, err)
},
)
t.Run(
"doesnt execute", func(t *testing.T) {
conditionalFn := Conditional(dummyErrFunc, false)
_, err := conditionalFn(nil, nil)
require.NoError(t, err)
},
)
}
func TestIgnoreErr(t *testing.T) {
t.Run(
"no error thown", func(t *testing.T) {
conditionalFn := IgnoreErr(dummyErrFunc)
_, err := conditionalFn(nil, nil)
require.NoError(t, err)
},
)
}
func TestIgnoreMatchedErr(t *testing.T) {
t.Run(
"no error thown", func(t *testing.T) {
conditionalFn := IgnoreMatchedErr(dummyErrFunc, func(err error) bool {
return false
})
_, err := conditionalFn(nil, nil)
require.NoError(t, err)
},
)
t.Run(
"error thown", func(t *testing.T) {
conditionalFn := IgnoreMatchedErr(dummyErrFunc, func(err error) bool {
return true
})
_, err := conditionalFn(nil, nil)
require.NoError(t, err)
},
)
}
func Test_ValidateTestSetup(t *testing.T) {
tests := []struct {
name string
setup ValidateTestSetupOptions
wantError bool
}{
{
name: "v2.0.0 setup with registry",
setup: ValidateTestSetupOptions{
"v2.0.0", "xpkg.crossplane.io", nil,
},
wantError: true,
},
{
name: ">v2 setup with registry",
setup: ValidateTestSetupOptions{
"v2.1.0", "xpkg.crossplane.io", nil,
},
wantError: true,
},
{
name: "<v2 setup with registry",
setup: ValidateTestSetupOptions{
"v1.20.1", "xpkg.crossplane.io", nil,
},
wantError: false,
},
{
name: "v2 with controllerconfig",
setup: ValidateTestSetupOptions{
"v2.0.0", "", &vendored.ControllerConfig{},
},
wantError: true,
},
{
name: "implicit v2 without controllerconfig",
setup: ValidateTestSetupOptions{},
wantError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
envFunc := ValidateTestSetup(tt.setup)
_, err := envFunc(nil, nil)
if tt.wantError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
// applyHelmOpts is a small helper that materialises a slice of helm.Option into
// a concrete helm.Opts value so the resulting state can be asserted against in
// tests.
func applyHelmOpts(opts []helm.Option) helm.Opts {
var o helm.Opts
for _, op := range opts {
op(&o)
}
return o
}
func TestResolveCrossplaneChartRepoURL(t *testing.T) {
t.Run("empty override returns upstream default", func(t *testing.T) {
require.Equal(t, "https://charts.crossplane.io/stable", resolveCrossplaneChartRepoURL(""))
require.Equal(t, defaultCrossplaneChartRepoURL, resolveCrossplaneChartRepoURL(""))
})
t.Run("non-empty override is honoured", func(t *testing.T) {
require.Equal(t, "https://example.com/charts", resolveCrossplaneChartRepoURL("https://example.com/charts"))
})
}
func TestBuildCrossplaneHelmInstallOpts(t *testing.T) {
const cacheName = "package-cache"
t.Run("default behaviour - empty chart ref uses repo chart reference", func(t *testing.T) {
got := applyHelmOpts(buildCrossplaneHelmInstallOpts("", cacheName, nil))
require.Equal(t, "crossplane", got.Name)
require.Equal(t, "crossplane-system", got.Namespace)
// helm.WithReleaseName sets the ReleaseName field. The helm package's
// getCommand falls back to ReleaseName when Chart is empty, so this is
// what the helm install picks up as the chart reference.
require.Equal(t, helmRepoName+"/crossplane", got.ReleaseName)
require.Empty(t, got.Chart, "Chart must be empty when installing from a repo")
require.Equal(t, "10m", got.Timeout)
require.True(t, got.Wait)
require.Contains(t, got.Args, "--set")
require.Contains(t, got.Args, fmt.Sprintf("packageCache.pvc=%s", cacheName))
})
t.Run("file-path chart ref sets Chart and leaves ReleaseName empty", func(t *testing.T) {
const chartRef = "/tmp/crossplane-1.16.0.tgz"
got := applyHelmOpts(buildCrossplaneHelmInstallOpts(chartRef, cacheName, nil))
require.Equal(t, "crossplane", got.Name)
require.Equal(t, "crossplane-system", got.Namespace)
require.Equal(t, chartRef, got.Chart)
require.Empty(t, got.ReleaseName, "ReleaseName must be empty when installing from a chart ref")
})
t.Run("OCI chart ref sets Chart and leaves ReleaseName empty", func(t *testing.T) {
const chartRef = "oci://xpkg.crossplane.io/crossplane/crossplane"
got := applyHelmOpts(buildCrossplaneHelmInstallOpts(chartRef, cacheName, nil))
require.Equal(t, chartRef, got.Chart, "OCI URLs must be passed through to helm install verbatim")
require.Empty(t, got.ReleaseName, "ReleaseName must be empty for OCI installs")
})
t.Run("caller-supplied opts override defaults and are appended last", func(t *testing.T) {
// Version() is a CrossplaneOpt that pushes onto helm.Opts.Version.
// Registry() appends to helm.Opts.Args.
got := applyHelmOpts(buildCrossplaneHelmInstallOpts("", cacheName, []CrossplaneOpt{
Version("v1.16.0"),
Registry("xpkg.upbound.io"),
}))
require.Equal(t, "v1.16.0", got.Version)
// Registry adds two extra args (--set, args={--registry=...}).
require.Contains(t, got.Args, "args={--registry=xpkg.upbound.io}")
})
t.Run("ChartRef CrossplaneOpt sets the Chart field via helm.WithChart", func(t *testing.T) {
const chartRef = "oci://xpkg.crossplane.io/crossplane/crossplane"
// Pass via CrossplaneOpt rather than the chartRef parameter.
got := applyHelmOpts(buildCrossplaneHelmInstallOpts("", cacheName, []CrossplaneOpt{
ChartRef(chartRef),
}))
require.Equal(t, chartRef, got.Chart)
})
}
func TestInstallCrossplaneEntryPoints(t *testing.T) {
// All three entry points return non-nil env.Funcs without invoking them,
// since invocation needs a real kind cluster + helm binary. Smoke-checking
// that the wiring compiles and is reachable is enough at the unit-test
// layer; the real install path is exercised by downstream consumers.
require.NotNil(t, InstallCrossplane("cluster"))
require.NotNil(t, InstallCrossplaneFromChart("cluster", "/tmp/crossplane.tgz"))
require.NotNil(t, InstallCrossplaneFromChart("cluster", "oci://xpkg.crossplane.io/crossplane/crossplane"))
require.NotNil(t, InstallCrossplaneFromRepo("cluster", "https://example.com/charts"))
// The Version/Registry helpers must continue to work as CrossplaneOpts
// against all three entry points (backwards compatibility).
require.NotNil(t, InstallCrossplane("cluster", Version("v1.16.0"), Registry("xpkg.upbound.io")))
require.NotNil(t, InstallCrossplaneFromChart("cluster", "/tmp/c.tgz", Version("v1.16.0")))
require.NotNil(t, InstallCrossplaneFromRepo("cluster", "https://example.com/charts", Version("v1.16.0")))
}