Skip to content

Commit 10aa909

Browse files
committed
Update coverage action config
Signed-off-by: Andrea Frittoli <[email protected]>
1 parent 0dc9b1d commit 10aa909

File tree

5 files changed

+112
-8
lines changed

5 files changed

+112
-8
lines changed

.github/workflows/coverage.yml

-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,3 @@ jobs:
3939
uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0
4040
with:
4141
token: ${{ secrets.CODECOV_TOKEN }}
42-
exclude: docs/**

codecov.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ignore:
2+
- docs/examples/**

pkg/api/bindings.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ func Validate(event CDEventReader) error {
154154
if err := json.Unmarshal([]byte(jsonString), &v); err != nil {
155155
return fmt.Errorf("cannot unmarshal event json: %v", err)
156156
}
157-
// Validate the "jsonschema" tags
158-
err = sch.Validate(v)
157+
// Validate the "validate" tags
158+
err = validate.Struct(event)
159159
if err != nil {
160160
return err
161161
}
162-
// Validate the "validate" tags
163-
err = validate.Struct(event)
162+
// Validate the "jsonschema" tags
163+
err = sch.Validate(v)
164164
if err != nil {
165165
return err
166166
}

pkg/api/bindings_test.go

+87-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"encoding/json"
2323
"fmt"
2424
"os"
25+
"strings"
2526
"testing"
2627

2728
"github.com/cdevents/sdk-go/pkg/api"
@@ -43,6 +44,7 @@ var (
4344
testSubjectId = "mySubject123"
4445
testValue = "testValue"
4546
testArtifactId = "pkg:oci/myapp@sha256%3A0b31b1c02ff458ad9b7b81cbdf8f028bd54699fa151f221d1e8de6817db93427"
47+
testInvalidArtifactId = "not-in-purl-format"
4648
testDataJson = testData{TestValues: []map[string]string{{"k1": "v1"}, {"k2": "v2"}}}
4749
testDataJsonUnmarshalled = map[string]any{
4850
"testValues": []any{map[string]any{"k1": string("v1")}, map[string]any{"k2": string("v2")}},
@@ -61,14 +63,39 @@ var (
6163
"additionalProperties": true,
6264
"type": "object"
6365
}`
64-
testCustomSchemaJson = fmt.Sprintf(testCustomSchemaJsonTemplate, testSchemaUri)
65-
testCustomSchemas = map[string][]byte{
66-
testSchemaUri: []byte(testCustomSchemaJson),
66+
testCustomSchemaJson = fmt.Sprintf(testCustomSchemaJsonTemplate, testSchemaUri)
67+
testSchemaUriStricter = "https://myorg.com/schema/stricter"
68+
testCustomSchemaJsonStricterTemplate = `{
69+
"$schema": "https://json-schema.org/draft/2020-12/schema",
70+
"$id": "%s",
71+
"additionalProperties": true,
72+
"type": "object",
73+
"properties": {
74+
"customData": {
75+
"type": "object",
76+
"additionalProperties": false,
77+
"properties": {
78+
"important": {
79+
"type": "string"
80+
}
81+
},
82+
"required": [
83+
"important"
84+
]
85+
}
86+
}
87+
}`
88+
testCustomSchemaJsonStricterJson = fmt.Sprintf(testCustomSchemaJsonStricterTemplate, testSchemaUriStricter)
89+
testCustomSchemas = map[string][]byte{
90+
testSchemaUri: []byte(testCustomSchemaJson),
91+
testSchemaUriStricter: []byte(testCustomSchemaJsonStricterJson),
6792
}
6893

6994
eventJsonCustomData *testapi.FooSubjectBarPredicateEvent
7095
eventNonJsonCustomData *testapi.FooSubjectBarPredicateEvent
7196
eventJsonCustomDataUnmarshalled *testapi.FooSubjectBarPredicateEvent
97+
eventJsonCustomDataCustomSchema *testapi.FooSubjectBarPredicateEvent
98+
eventInvalidArtifactIdFormat *testapi.FooSubjectBarPredicateEvent
7299

73100
eventJsonCustomDataFile = "json_custom_data"
74101
eventImplicitJsonCustomDataFile = "implicit_json_custom_data"
@@ -80,6 +107,21 @@ var (
80107
Type: api.CDEventType{
81108
Subject: "invalid",
82109
Predicate: "invalid",
110+
Version: "#not@semver", // Invalid version format
111+
},
112+
Version: "9.9.9",
113+
},
114+
api.ContextLinks{},
115+
api.ContextCustom{},
116+
},
117+
}
118+
119+
eventUnknownType = &testapi.FooSubjectBarPredicateEvent{
120+
Context: api.ContextV04{
121+
api.Context{
122+
Type: api.CDEventType{
123+
Subject: "invalid", // Unknown subject
124+
Predicate: "invalid", // Unknown predicate
83125
Version: "1.2.3",
84126
},
85127
Version: "9.9.9",
@@ -134,6 +176,19 @@ func init() {
134176
elr, elp, ele,
135177
}
136178

179+
setContext(eventInvalidType, testSubjectId)
180+
setContextV04(eventInvalidType, true, true)
181+
eventInvalidType.SetSubjectArtifactId(testArtifactId)
182+
183+
setContext(eventUnknownType, testSubjectId)
184+
setContextV04(eventUnknownType, true, true)
185+
eventUnknownType.SetSubjectArtifactId(testArtifactId)
186+
187+
eventInvalidArtifactIdFormat, _ = testapi.NewFooSubjectBarPredicateEvent()
188+
setContext(eventInvalidArtifactIdFormat, testSubjectId)
189+
setContextV04(eventInvalidArtifactIdFormat, true, true)
190+
eventInvalidArtifactIdFormat.SetSubjectArtifactId(testInvalidArtifactId)
191+
137192
eventJsonCustomData, _ = testapi.NewFooSubjectBarPredicateEvent()
138193
setContext(eventJsonCustomData, testSubjectId)
139194
setContextV04(eventJsonCustomData, true, true)
@@ -164,6 +219,17 @@ func init() {
164219
err = eventNonJsonCustomData.SetCustomData("application/xml", testDataXml)
165220
panicOnError(err)
166221

222+
eventJsonCustomDataCustomSchema, _ = testapi.NewFooSubjectBarPredicateEvent()
223+
setContext(eventJsonCustomDataCustomSchema, testSubjectId)
224+
setContextV04(eventJsonCustomDataCustomSchema, true, true)
225+
eventJsonCustomDataCustomSchema.SetSchemaUri(testSchemaUriStricter)
226+
eventJsonCustomDataCustomSchema.SetSubjectReferenceField(&api.Reference{Id: testChangeId})
227+
eventJsonCustomDataCustomSchema.SetSubjectPlainField(testValue)
228+
eventJsonCustomDataCustomSchema.SetSubjectArtifactId(testArtifactId)
229+
eventJsonCustomDataCustomSchema.SetSubjectObjectField(&testapi.FooSubjectBarPredicateSubjectContentObjectField{Required: testChangeId, Optional: testSource})
230+
err = eventJsonCustomDataCustomSchema.SetCustomData("application/json", testDataJson)
231+
panicOnError(err)
232+
167233
for id, jsonBytes := range testCustomSchemas {
168234
err = api.LoadJsonSchema(id, jsonBytes)
169235
panicOnError(err)
@@ -247,19 +313,37 @@ func TestAsCloudEventInvalid(t *testing.T) {
247313
tests := []struct {
248314
name string
249315
event api.CDEventReader
316+
error string
250317
}{{
251318
name: "nil event",
252319
event: nil,
320+
error: "nil CDEvent cannot be rendered as CloudEvent",
253321
}, {
254322
name: "event with invalid type",
255323
event: eventInvalidType,
324+
error: "cannot validate CDEvent Key: 'FooSubjectBarPredicateEventV2_2_3.Context.Context.Type.",
325+
}, {
326+
name: "event with unknown type",
327+
event: eventUnknownType,
328+
error: "cannot validate CDEvent jsonschema validation failed with 'https://cdevents.dev/99.1.0/schema/foosubject-barpredicate-event#'",
329+
}, {
330+
name: "event with invalid artifact id format",
331+
event: eventInvalidArtifactIdFormat,
332+
error: "cannot validate CDEvent Key: 'FooSubjectBarPredicateEventV2_2_3.Subject.Content.ArtifactId'",
333+
}, {
334+
name: "does not match the custom schema",
335+
event: eventJsonCustomDataCustomSchema,
336+
error: "cannot validate CDEvent jsonschema validation failed with 'https://myorg.com/schema/stricter#",
256337
}}
257338
for _, tc := range tests {
258339
t.Run(tc.name, func(t *testing.T) {
259340
_, err := api.AsCloudEvent(tc.event)
260341
if err == nil {
261342
t.Fatalf("expected it to fail, but it didn't")
262343
}
344+
if !strings.HasPrefix(err.Error(), tc.error) {
345+
t.Errorf("error %s does not start with the expected prefix %s", err.Error(), tc.error)
346+
}
263347
})
264348
}
265349
}

pkg/api/v04/conformance_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"os"
2525
"path/filepath"
26+
"strings"
2627
"testing"
2728
"time"
2829

@@ -613,13 +614,31 @@ func TestExamples(t *testing.T) {
613614
if d := cmp.Diff(consumed.GetLinks(), produced.GetLinks()); d != "" {
614615
t.Errorf("args: diff(-want,+got):\n%s", d)
615616
}
617+
// Coverage for GetCustomSchema
616618
consumedSchema, err := consumed.GetCustomSchema()
617619
if err != nil {
618620
t.Errorf("failed to obtain the consumed event custom schema: %v", err)
619621
}
620622
if d := cmp.Diff(consumedSchema.ID, producedSchema.ID); d != "" {
621623
t.Errorf("args: diff(-want,+got):\n%s", d)
622624
}
625+
// Check the case of no custom schema
626+
produced.SetSchemaUri("")
627+
producedSchema, err = produced.GetCustomSchema()
628+
if producedSchema != nil || err != nil {
629+
t.Errorf("expected nil schema and error when schema is not set, got schema %v, error %v", producedSchema, err)
630+
}
631+
// Check the case of custom schema missing from the DB
632+
notFoundSchema := "https://this.is.not.found/in/the/db"
633+
produced.SetSchemaUri(notFoundSchema)
634+
producedSchema, err = produced.GetCustomSchema()
635+
if err == nil {
636+
t.Errorf("expected an error when schema is not found, got schema %v, error %v", producedSchema, err)
637+
}
638+
expectedError := fmt.Sprintf("schema with id %s could not be found", notFoundSchema)
639+
if !strings.HasPrefix(err.Error(), expectedError) {
640+
t.Errorf("error %s does not start with the expected prefix %s", err.Error(), expectedError)
641+
}
623642
})
624643
}
625644
}

0 commit comments

Comments
 (0)