Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting validation error from apply server dry run output #333

Merged
merged 2 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion controllers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ func parseApplyError(in []byte) string {
if line != "" &&
!strings.HasSuffix(line, "created") &&
!strings.HasSuffix(line, "created (dry run)") &&
!strings.HasSuffix(line, "created (server dry run)") &&
!strings.HasSuffix(line, "configured") &&
!strings.HasSuffix(line, "configured (dry run)") &&
!strings.HasSuffix(line, "configured (server dry run)") &&
!strings.HasSuffix(line, "unchanged") &&
!strings.HasSuffix(line, "unchanged (dry run)") {
!strings.HasSuffix(line, "unchanged (dry run)") &&
!strings.HasSuffix(line, "unchanged (server dry run)") {
errors += line + "\n"
}
}
Expand Down
58 changes: 41 additions & 17 deletions controllers/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,52 @@ import (
"testing"
)

func Test_parseApplyError(t *testing.T) {
filtered := parseApplyError([]byte(`
func TestParseApplyError(t *testing.T) {
tests := []struct {
name string
in []byte
filtered string
}{
{
"apply",
[]byte(`
gitrepository.source.toolkit.fluxcd.io/flux-workspaces unchanged
ingressroute.traefik.containo.us/flux-receiver configured
service/notification-controller created
The Service "webhook-receiver" is invalid: spec.clusterIP: Invalid value: "10.200.133.61": field is immutable`))
filtered = strings.TrimSpace(filtered)
numLines := len(strings.Split(filtered, "\n"))
if numLines != 1 {
t.Errorf("Should filter out all but one line from the error output, but got %d lines", numLines)
}
}

func Test_parseApplyError_dryRun(t *testing.T) {
filtered := parseApplyError([]byte(`
The Service "webhook-receiver" is invalid: spec.clusterIP: Invalid value: "10.200.133.61": field is immutable
`),
`The Service "webhook-receiver" is invalid: spec.clusterIP: Invalid value: "10.200.133.61": field is immutable`,
},
{
"client dry-run",
[]byte(`
gitrepository.source.toolkit.fluxcd.io/flux-workspaces unchanged (dry run)
ingressroute.traefik.containo.us/flux-receiver configured (dry run)
service/notification-controller created (dry run)
error: error validating data: unknown field "ima ge" in io.k8s.api.core.v1.Container`))
filtered = strings.TrimSpace(filtered)
numLines := len(strings.Split(filtered, "\n"))
if numLines != 1 {
t.Errorf("Should filter out all but one line from the error output, but got %d lines", numLines)
error: error validating data: unknown field "ima ge" in io.k8s.api.core.v1.Container
`),
`error: error validating data: unknown field "ima ge" in io.k8s.api.core.v1.Container`,
},
{
"server dry-run",
[]byte(`
gitrepository.source.toolkit.fluxcd.io/flux-workspaces unchanged (server dry run)
ingressroute.traefik.containo.us/flux-receiver configured (server dry run)
service/notification-controller created (server dry run)
error: error validating data: unknown field "ima ge" in io.k8s.api.core.v1.Container
`),
`error: error validating data: unknown field "ima ge" in io.k8s.api.core.v1.Container`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filtered := parseApplyError(tt.in)
filtered = strings.TrimSpace(filtered)

if tt.filtered != filtered {
t.Errorf("expected %q, but actual %q", tt.filtered, filtered)
}
})
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also check for a (partial) string match here to confirm the right string is returned.

Copy link
Contributor Author

@superbrothers superbrothers Apr 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated. e91129c

}