Skip to content

Commit bd37c6f

Browse files
authored
Merge pull request #578 from karlkfi/karl-coe-error
chore: Test for more specific errors
2 parents 825458e + 4c53ada commit bd37c6f

File tree

5 files changed

+101
-54
lines changed

5 files changed

+101
-54
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/stretchr/testify v1.7.0
1313
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
1414
k8s.io/api v0.23.2
15+
k8s.io/apiextensions-apiserver v0.23.2
1516
k8s.io/apimachinery v0.23.2
1617
k8s.io/cli-runtime v0.23.2
1718
k8s.io/client-go v0.23.2
@@ -98,7 +99,6 @@ require (
9899
gopkg.in/inf.v0 v0.9.1 // indirect
99100
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
100101
gopkg.in/yaml.v2 v2.4.0 // indirect
101-
k8s.io/apiextensions-apiserver v0.23.0 // indirect
102102
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
103103
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect
104104
sigs.k8s.io/kustomize/api v0.10.1 // indirect

go.sum

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,12 +1155,14 @@ honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
11551155
k8s.io/api v0.23.0/go.mod h1:8wmDdLBHBNxtOIytwLstXt5E9PddnZb0GaMcqsvDBpg=
11561156
k8s.io/api v0.23.2 h1:62cpzreV3dCuj0hqPi8r4dyWh48ogMcyh+ga9jEGij4=
11571157
k8s.io/api v0.23.2/go.mod h1:sYuDb3flCtRPI8ghn6qFrcK5ZBu2mhbElxRE95qpwlI=
1158-
k8s.io/apiextensions-apiserver v0.23.0 h1:uii8BYmHYiT2ZTAJxmvc3X8UhNYMxl2A0z0Xq3Pm+WY=
11591158
k8s.io/apiextensions-apiserver v0.23.0/go.mod h1:xIFAEEDlAZgpVBl/1VSjGDmLoXAWRG40+GsWhKhAxY4=
1159+
k8s.io/apiextensions-apiserver v0.23.2 h1:N6CIVAhmF0ahgFKUMDdV/AUyckhUb4nIyVPohPtdUPk=
1160+
k8s.io/apiextensions-apiserver v0.23.2/go.mod h1:9cs7avT6+GfzbB0pambTvH11wcaR85QQg4ovl9s15UU=
11601161
k8s.io/apimachinery v0.23.0/go.mod h1:fFCTTBKvKcwTPFzjlcxp91uPFZr+JA0FubU4fLzzFYc=
11611162
k8s.io/apimachinery v0.23.2 h1:dBmjCOeYBdg2ibcQxMuUq+OopZ9fjfLIR5taP/XKeTs=
11621163
k8s.io/apimachinery v0.23.2/go.mod h1:zDqeV0AK62LbCI0CI7KbWCAYdLg+E+8UXJ0rIz5gmS8=
11631164
k8s.io/apiserver v0.23.0/go.mod h1:Cec35u/9zAepDPPFyT+UMrgqOCjgJ5qtfVJDxjZYmt4=
1165+
k8s.io/apiserver v0.23.2/go.mod h1:Kdt8gafkPev9Gfh+H6lCPbmRu42f7BfhOfHKKa3dtyU=
11641166
k8s.io/cli-runtime v0.23.2 h1:4zOZX78mFSakwe4gef81XDBu94Yu0th6bfveTOx8ZQk=
11651167
k8s.io/cli-runtime v0.23.2/go.mod h1:Ag70akCDvwux4HxY+nH2J3UqE2e6iwSSdG1HE6p1VTU=
11661168
k8s.io/client-go v0.23.0/go.mod h1:hrDnpnK1mSr65lHHcUuIZIXDgEbzc7/683c6hyG4jTA=
@@ -1192,6 +1194,7 @@ rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8
11921194
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
11931195
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
11941196
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.25/go.mod h1:Mlj9PNLmG9bZ6BHFwFKDo5afkpWyUISkb9Me0GnK66I=
1197+
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.27/go.mod h1:tq2nT0Kx7W+/f2JVE+zxYtUhdjuELJkVpNz+x/QN5R4=
11951198
sigs.k8s.io/controller-runtime v0.11.0 h1:DqO+c8mywcZLFJWILq4iktoECTyn30Bkj0CwgqMpZWQ=
11961199
sigs.k8s.io/controller-runtime v0.11.0/go.mod h1:KKwLiTooNGu+JmLZGn9Sl3Gjmfj66eMbCQznLP5zcqA=
11971200
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 h1:fD1pz4yfdADVNfFmcP2aBEtudwUQ1AlLnRBALr33v3s=

test/e2e/continue_on_error_test.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ package e2e
55

66
import (
77
"context"
8-
"errors"
98

109
. "github.com/onsi/ginkgo"
1110
. "github.com/onsi/gomega"
11+
12+
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
13+
apierrors "k8s.io/apimachinery/pkg/api/errors"
1214
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
15+
"k8s.io/apimachinery/pkg/util/validation/field"
16+
cmdutil "k8s.io/kubectl/pkg/cmd/util"
1317
"sigs.k8s.io/cli-utils/pkg/apply"
1418
applyerror "sigs.k8s.io/cli-utils/pkg/apply/error"
1519
"sigs.k8s.io/cli-utils/pkg/apply/event"
@@ -74,8 +78,31 @@ func continueOnErrorTest(ctx context.Context, c client.Client, invConfig invconf
7478
ApplyEvent: &testutil.ExpApplyEvent{
7579
GroupName: "apply-0",
7680
Identifier: object.UnstructuredToObjMetadata(invalidCrdObj),
77-
Error: testutil.EqualErrorType(
78-
applyerror.NewApplyRunError(errors.New("failed to apply")),
81+
Error: testutil.EqualError(
82+
applyerror.NewApplyRunError(
83+
cmdutil.AddSourceToErr(
84+
"creating",
85+
"unstructured",
86+
apierrors.NewInvalid(
87+
invalidCrdObj.GroupVersionKind().GroupKind(),
88+
invalidCrdObj.GetName(),
89+
field.ErrorList{
90+
&field.Error{
91+
Type: field.ErrorTypeInvalid,
92+
Field: "spec.versions",
93+
BadValue: []apiextensions.CustomResourceDefinitionVersion(nil),
94+
Detail: "must have exactly one version marked as storage version",
95+
},
96+
&field.Error{
97+
Type: field.ErrorTypeInvalid,
98+
Field: "status.storedVersions",
99+
BadValue: []string(nil),
100+
Detail: "must have at least one stored version",
101+
},
102+
},
103+
),
104+
),
105+
),
79106
),
80107
},
81108
},

test/e2e/exit_early_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ func exitEarlyTest(ctx context.Context, c client.Client, invConfig invconfig.Inv
5252
// invalid pod validation error
5353
EventType: event.ErrorType,
5454
ErrorEvent: &testutil.ExpErrorEvent{
55-
Err: testutil.EqualErrorString(validation.NewError(
56-
field.Required(field.NewPath("metadata", "name"), "name is required"),
57-
object.UnstructuredToObjMetadata(invalidPodObj),
58-
).Error()),
55+
Err: testutil.EqualError(
56+
validation.NewError(
57+
field.Required(field.NewPath("metadata", "name"), "name is required"),
58+
object.UnstructuredToObjMetadata(invalidPodObj),
59+
),
60+
),
5961
},
6062
},
6163
}

test/e2e/skip_invalid_test.go

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ func skipInvalidTest(ctx context.Context, c client.Client, invConfig invconfig.I
7070
Identifiers: object.ObjMetadataSet{
7171
object.UnstructuredToObjMetadata(invalidPodObj),
7272
},
73-
Error: testutil.EqualErrorString(validation.NewError(
74-
field.Required(field.NewPath("metadata", "name"), "name is required"),
75-
object.UnstructuredToObjMetadata(invalidPodObj),
76-
).Error()),
73+
Error: testutil.EqualError(
74+
validation.NewError(
75+
field.Required(field.NewPath("metadata", "name"), "name is required"),
76+
object.UnstructuredToObjMetadata(invalidPodObj),
77+
),
78+
),
7779
},
7880
},
7981
{
@@ -83,22 +85,24 @@ func skipInvalidTest(ctx context.Context, c client.Client, invConfig invconfig.I
8385
Identifiers: object.ObjMetadataSet{
8486
object.UnstructuredToObjMetadata(pod3Obj),
8587
},
86-
Error: testutil.EqualErrorString(validation.NewError(
87-
object.InvalidAnnotationError{
88-
Annotation: dependson.Annotation,
89-
Cause: graph.ExternalDependencyError{
90-
Edge: graph.Edge{
91-
From: object.UnstructuredToObjMetadata(pod3Obj),
92-
To: object.ObjMetadata{
93-
GroupKind: schema.GroupKind{Kind: "Pod"},
94-
Name: "pod0",
95-
Namespace: namespaceName,
88+
Error: testutil.EqualError(
89+
validation.NewError(
90+
object.InvalidAnnotationError{
91+
Annotation: dependson.Annotation,
92+
Cause: graph.ExternalDependencyError{
93+
Edge: graph.Edge{
94+
From: object.UnstructuredToObjMetadata(pod3Obj),
95+
To: object.ObjMetadata{
96+
GroupKind: schema.GroupKind{Kind: "Pod"},
97+
Name: "pod0",
98+
Namespace: namespaceName,
99+
},
96100
},
97101
},
98102
},
99-
},
100-
object.UnstructuredToObjMetadata(pod3Obj),
101-
).Error()),
103+
object.UnstructuredToObjMetadata(pod3Obj),
104+
),
105+
),
102106
},
103107
},
104108
{
@@ -108,21 +112,23 @@ func skipInvalidTest(ctx context.Context, c client.Client, invConfig invconfig.I
108112
Identifiers: object.ObjMetadataSet{
109113
object.UnstructuredToObjMetadata(podBObj),
110114
},
111-
Error: testutil.EqualErrorString(validation.NewError(
112-
object.InvalidAnnotationError{
113-
Annotation: mutation.Annotation,
114-
Cause: graph.ExternalDependencyError{
115-
Edge: graph.Edge{
116-
From: object.UnstructuredToObjMetadata(podBObj),
117-
To: object.ObjMetadata{
118-
GroupKind: schema.GroupKind{Kind: "Pod"},
119-
Name: "pod-a",
115+
Error: testutil.EqualError(
116+
validation.NewError(
117+
object.InvalidAnnotationError{
118+
Annotation: mutation.Annotation,
119+
Cause: graph.ExternalDependencyError{
120+
Edge: graph.Edge{
121+
From: object.UnstructuredToObjMetadata(podBObj),
122+
To: object.ObjMetadata{
123+
GroupKind: schema.GroupKind{Kind: "Pod"},
124+
Name: "pod-a",
125+
},
120126
},
121127
},
122128
},
123-
},
124-
object.UnstructuredToObjMetadata(podBObj),
125-
).Error()),
129+
object.UnstructuredToObjMetadata(podBObj),
130+
),
131+
),
126132
},
127133
},
128134
{
@@ -133,22 +139,24 @@ func skipInvalidTest(ctx context.Context, c client.Client, invConfig invconfig.I
133139
object.UnstructuredToObjMetadata(podAObj),
134140
object.UnstructuredToObjMetadata(podBObj),
135141
},
136-
Error: testutil.EqualErrorString(validation.NewError(
137-
graph.CyclicDependencyError{
138-
Edges: []graph.Edge{
139-
{
140-
From: object.UnstructuredToObjMetadata(podAObj),
141-
To: object.UnstructuredToObjMetadata(podBObj),
142-
},
143-
{
144-
From: object.UnstructuredToObjMetadata(podBObj),
145-
To: object.UnstructuredToObjMetadata(podAObj),
142+
Error: testutil.EqualError(
143+
validation.NewError(
144+
graph.CyclicDependencyError{
145+
Edges: []graph.Edge{
146+
{
147+
From: object.UnstructuredToObjMetadata(podAObj),
148+
To: object.UnstructuredToObjMetadata(podBObj),
149+
},
150+
{
151+
From: object.UnstructuredToObjMetadata(podBObj),
152+
To: object.UnstructuredToObjMetadata(podAObj),
153+
},
146154
},
147155
},
148-
},
149-
object.UnstructuredToObjMetadata(podAObj),
150-
object.UnstructuredToObjMetadata(podBObj),
151-
).Error()),
156+
object.UnstructuredToObjMetadata(podAObj),
157+
object.UnstructuredToObjMetadata(podBObj),
158+
),
159+
),
152160
},
153161
},
154162
{
@@ -359,8 +367,15 @@ func skipInvalidTest(ctx context.Context, c client.Client, invConfig invconfig.I
359367
Identifiers: object.ObjMetadataSet{
360368
object.UnstructuredToObjMetadata(deployment1Obj),
361369
},
362-
Error: testutil.EqualErrorType(
363-
validation.NewError(nil), // TODO: be more specific
370+
Error: testutil.EqualError(
371+
validation.NewError(
372+
object.InvalidAnnotationError{
373+
Annotation: dependson.Annotation,
374+
Cause: fmt.Errorf("failed to parse object reference (index: 0): %w",
375+
fmt.Errorf("expected 3 or 5 fields, found 1: %q", "invalid")),
376+
},
377+
object.UnstructuredToObjMetadata(deployment1Obj),
378+
),
364379
),
365380
},
366381
},

0 commit comments

Comments
 (0)