-
Notifications
You must be signed in to change notification settings - Fork 110
/
formatted_error_test.go
102 lines (80 loc) · 3.27 KB
/
formatted_error_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
// Copyright 2024 The Carvel Authors.
// SPDX-License-Identifier: Apache-2.0
package e2e
import (
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestFormattedError(t *testing.T) {
env := BuildEnv(t)
logger := Logger{}
kapp := Kapp{t, env.Namespace, env.KappBinaryPath, logger}
yaml1 := `
---
apiVersion: batch/v1
kind: Job
metadata:
name: successful-job
namespace: default
spec:
selector:
matchLabels:
blah: balh
template:
metadata:
name: successful-job
labels:
foo: foo
spec:
containers:
- name: successful-job
image: busybox
command: ["/bin/sh", "-c", "sleep 11"]
restartPolicy: Never
`
name := "test-formatted-error"
cleanUp := func() {
kapp.RunWithOpts([]string{"delete", "-a", name}, RunOpts{AllowError: true})
}
cleanUp()
defer cleanUp()
logger.Section("deploy with errors", func() {
expectedErr := strings.TrimSpace(`
kapp: Error: create job/successful-job (batch/v1) namespace: default:
Creating resource job/successful-job (batch/v1) namespace: default:
API server says:
Job.batch "successful-job" is invalid:
- spec.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string{"blah":"balh", -replaced-, -replaced-}, MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: 'selector' not auto-generated
- spec.template.metadata.labels: Invalid value: map[string]string{-replaced-, "foo":"foo", "job-name":"successful-job", -replaced-, -replaced-}: 'selector' does not match template 'labels'
(reason: Invalid)
`)
minorVersion, err := getServerMinorVersion()
require.NoErrorf(t, err, "Error getting k8s server minor version")
// batch.kubernetes.io/controller-uid and batch.kubernetes.io/job-name annotaions are added
if minorVersion >= 27 {
expectedErr = strings.TrimSpace(`
kapp: Error: create job/successful-job (batch/v1) namespace: default:
Creating resource job/successful-job (batch/v1) namespace: default:
API server says:
Job.batch "successful-job" is invalid:
- spec.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string{-replaced-, "blah":"balh", -replaced-}, MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: 'selector' not auto-generated
- spec.template.metadata.labels: Invalid value: map[string]string{-replaced-, -replaced-, -replaced-, "foo":"foo", "job-name":"successful-job", -replaced-, -replaced-}: 'selector' does not match template 'labels'
(reason: Invalid)
`)
}
_, err = kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", name},
RunOpts{StdinReader: strings.NewReader(yaml1), AllowError: true})
out := strings.ReplaceAll(err.Error(), "`", "'")
replaceAnns := regexp.MustCompile(`"kapp\.k14s\.io\/(app|association)":"[^"]+"`)
out = replaceAnns.ReplaceAllString(out, "-replaced-")
replaceUIDs := regexp.MustCompile(`"controller-uid":"[^"]+"`)
out = replaceUIDs.ReplaceAllString(out, "-replaced-")
replaceUIDs = regexp.MustCompile(`"batch\.kubernetes\.io\/controller-uid":"[^"]+"`)
out = replaceUIDs.ReplaceAllString(out, "-replaced-")
replaceUIDs = regexp.MustCompile(`"batch\.kubernetes\.io\/job-name":"[^"]+"`)
out = replaceUIDs.ReplaceAllString(out, "-replaced-")
require.Containsf(t, out, expectedErr, "Expected to see expected err in output, but did not")
})
}