Skip to content

Commit ddd2278

Browse files
committed
Confirm the right string is returned
Signed-off-by: Kazuki Suda <[email protected]>
1 parent 178ae70 commit ddd2278

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

controllers/utils_test.go

+38-27
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,52 @@ import (
55
"testing"
66
)
77

8-
func Test_parseApplyError(t *testing.T) {
9-
filtered := parseApplyError([]byte(`
8+
func TestParseApplyError(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
in []byte
12+
filtered string
13+
}{
14+
{
15+
"apply",
16+
[]byte(`
1017
gitrepository.source.toolkit.fluxcd.io/flux-workspaces unchanged
1118
ingressroute.traefik.containo.us/flux-receiver configured
1219
service/notification-controller created
13-
The Service "webhook-receiver" is invalid: spec.clusterIP: Invalid value: "10.200.133.61": field is immutable`))
14-
filtered = strings.TrimSpace(filtered)
15-
numLines := len(strings.Split(filtered, "\n"))
16-
if numLines != 1 {
17-
t.Errorf("Should filter out all but one line from the error output, but got %d lines", numLines)
18-
}
19-
}
20-
21-
func Test_parseApplyError_dryRun(t *testing.T) {
22-
filtered := parseApplyError([]byte(`
20+
The Service "webhook-receiver" is invalid: spec.clusterIP: Invalid value: "10.200.133.61": field is immutable
21+
`),
22+
`The Service "webhook-receiver" is invalid: spec.clusterIP: Invalid value: "10.200.133.61": field is immutable`,
23+
},
24+
{
25+
"client dry-run",
26+
[]byte(`
2327
gitrepository.source.toolkit.fluxcd.io/flux-workspaces unchanged (dry run)
2428
ingressroute.traefik.containo.us/flux-receiver configured (dry run)
2529
service/notification-controller created (dry run)
26-
error: error validating data: unknown field "ima ge" in io.k8s.api.core.v1.Container`))
27-
filtered = strings.TrimSpace(filtered)
28-
numLines := len(strings.Split(filtered, "\n"))
29-
if numLines != 1 {
30-
t.Errorf("Should filter out all but one line from the error output, but got %d lines", numLines)
31-
}
32-
}
33-
34-
func Test_parseApplyError_serverDryRun(t *testing.T) {
35-
filtered := parseApplyError([]byte(`
30+
error: error validating data: unknown field "ima ge" in io.k8s.api.core.v1.Container
31+
`),
32+
`error: error validating data: unknown field "ima ge" in io.k8s.api.core.v1.Container`,
33+
},
34+
{
35+
"server dry-run",
36+
[]byte(`
3637
gitrepository.source.toolkit.fluxcd.io/flux-workspaces unchanged (server dry run)
3738
ingressroute.traefik.containo.us/flux-receiver configured (server dry run)
3839
service/notification-controller created (server dry run)
39-
error: error validating data: unknown field "ima ge" in io.k8s.api.core.v1.Container`))
40-
filtered = strings.TrimSpace(filtered)
41-
numLines := len(strings.Split(filtered, "\n"))
42-
if numLines != 1 {
43-
t.Errorf("Should filter out all but one line from the error output, but got %d lines", numLines)
40+
error: error validating data: unknown field "ima ge" in io.k8s.api.core.v1.Container
41+
`),
42+
`error: error validating data: unknown field "ima ge" in io.k8s.api.core.v1.Container`,
43+
},
44+
}
45+
46+
for _, tt := range tests {
47+
t.Run(tt.name, func(t *testing.T) {
48+
filtered := parseApplyError(tt.in)
49+
filtered = strings.TrimSpace(filtered)
50+
51+
if tt.filtered != filtered {
52+
t.Errorf("expected %q, but actual %q", tt.filtered, filtered)
53+
}
54+
})
4455
}
4556
}

0 commit comments

Comments
 (0)