Skip to content

Commit

Permalink
Update coverage action config
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Frittoli <[email protected]>
  • Loading branch information
afrittoli committed Jul 16, 2024
1 parent 0dc9b1d commit 10aa909
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 8 deletions.
1 change: 0 additions & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,3 @@ jobs:
uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0
with:
token: ${{ secrets.CODECOV_TOKEN }}
exclude: docs/**
2 changes: 2 additions & 0 deletions codecov.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignore:
- docs/examples/**
8 changes: 4 additions & 4 deletions pkg/api/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ func Validate(event CDEventReader) error {
if err := json.Unmarshal([]byte(jsonString), &v); err != nil {
return fmt.Errorf("cannot unmarshal event json: %v", err)
}
// Validate the "jsonschema" tags
err = sch.Validate(v)
// Validate the "validate" tags
err = validate.Struct(event)
if err != nil {
return err
}
// Validate the "validate" tags
err = validate.Struct(event)
// Validate the "jsonschema" tags
err = sch.Validate(v)
if err != nil {
return err
}
Expand Down
90 changes: 87 additions & 3 deletions pkg/api/bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"
"testing"

"github.com/cdevents/sdk-go/pkg/api"
Expand All @@ -43,6 +44,7 @@ var (
testSubjectId = "mySubject123"
testValue = "testValue"
testArtifactId = "pkg:oci/myapp@sha256%3A0b31b1c02ff458ad9b7b81cbdf8f028bd54699fa151f221d1e8de6817db93427"
testInvalidArtifactId = "not-in-purl-format"
testDataJson = testData{TestValues: []map[string]string{{"k1": "v1"}, {"k2": "v2"}}}
testDataJsonUnmarshalled = map[string]any{
"testValues": []any{map[string]any{"k1": string("v1")}, map[string]any{"k2": string("v2")}},
Expand All @@ -61,14 +63,39 @@ var (
"additionalProperties": true,
"type": "object"
}`
testCustomSchemaJson = fmt.Sprintf(testCustomSchemaJsonTemplate, testSchemaUri)
testCustomSchemas = map[string][]byte{
testSchemaUri: []byte(testCustomSchemaJson),
testCustomSchemaJson = fmt.Sprintf(testCustomSchemaJsonTemplate, testSchemaUri)
testSchemaUriStricter = "https://myorg.com/schema/stricter"
testCustomSchemaJsonStricterTemplate = `{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "%s",
"additionalProperties": true,
"type": "object",
"properties": {
"customData": {
"type": "object",
"additionalProperties": false,
"properties": {
"important": {
"type": "string"
}
},
"required": [
"important"
]
}
}
}`
testCustomSchemaJsonStricterJson = fmt.Sprintf(testCustomSchemaJsonStricterTemplate, testSchemaUriStricter)
testCustomSchemas = map[string][]byte{
testSchemaUri: []byte(testCustomSchemaJson),
testSchemaUriStricter: []byte(testCustomSchemaJsonStricterJson),
}

eventJsonCustomData *testapi.FooSubjectBarPredicateEvent
eventNonJsonCustomData *testapi.FooSubjectBarPredicateEvent
eventJsonCustomDataUnmarshalled *testapi.FooSubjectBarPredicateEvent
eventJsonCustomDataCustomSchema *testapi.FooSubjectBarPredicateEvent
eventInvalidArtifactIdFormat *testapi.FooSubjectBarPredicateEvent

eventJsonCustomDataFile = "json_custom_data"
eventImplicitJsonCustomDataFile = "implicit_json_custom_data"
Expand All @@ -80,6 +107,21 @@ var (
Type: api.CDEventType{
Subject: "invalid",
Predicate: "invalid",
Version: "#not@semver", // Invalid version format
},
Version: "9.9.9",
},
api.ContextLinks{},
api.ContextCustom{},
},
}

eventUnknownType = &testapi.FooSubjectBarPredicateEvent{
Context: api.ContextV04{
api.Context{
Type: api.CDEventType{
Subject: "invalid", // Unknown subject
Predicate: "invalid", // Unknown predicate
Version: "1.2.3",
},
Version: "9.9.9",
Expand Down Expand Up @@ -134,6 +176,19 @@ func init() {
elr, elp, ele,
}

setContext(eventInvalidType, testSubjectId)
setContextV04(eventInvalidType, true, true)
eventInvalidType.SetSubjectArtifactId(testArtifactId)

setContext(eventUnknownType, testSubjectId)
setContextV04(eventUnknownType, true, true)
eventUnknownType.SetSubjectArtifactId(testArtifactId)

eventInvalidArtifactIdFormat, _ = testapi.NewFooSubjectBarPredicateEvent()
setContext(eventInvalidArtifactIdFormat, testSubjectId)
setContextV04(eventInvalidArtifactIdFormat, true, true)
eventInvalidArtifactIdFormat.SetSubjectArtifactId(testInvalidArtifactId)

eventJsonCustomData, _ = testapi.NewFooSubjectBarPredicateEvent()
setContext(eventJsonCustomData, testSubjectId)
setContextV04(eventJsonCustomData, true, true)
Expand Down Expand Up @@ -164,6 +219,17 @@ func init() {
err = eventNonJsonCustomData.SetCustomData("application/xml", testDataXml)
panicOnError(err)

eventJsonCustomDataCustomSchema, _ = testapi.NewFooSubjectBarPredicateEvent()
setContext(eventJsonCustomDataCustomSchema, testSubjectId)
setContextV04(eventJsonCustomDataCustomSchema, true, true)
eventJsonCustomDataCustomSchema.SetSchemaUri(testSchemaUriStricter)
eventJsonCustomDataCustomSchema.SetSubjectReferenceField(&api.Reference{Id: testChangeId})
eventJsonCustomDataCustomSchema.SetSubjectPlainField(testValue)
eventJsonCustomDataCustomSchema.SetSubjectArtifactId(testArtifactId)
eventJsonCustomDataCustomSchema.SetSubjectObjectField(&testapi.FooSubjectBarPredicateSubjectContentObjectField{Required: testChangeId, Optional: testSource})
err = eventJsonCustomDataCustomSchema.SetCustomData("application/json", testDataJson)
panicOnError(err)

for id, jsonBytes := range testCustomSchemas {
err = api.LoadJsonSchema(id, jsonBytes)
panicOnError(err)
Expand Down Expand Up @@ -247,19 +313,37 @@ func TestAsCloudEventInvalid(t *testing.T) {
tests := []struct {
name string
event api.CDEventReader
error string
}{{
name: "nil event",
event: nil,
error: "nil CDEvent cannot be rendered as CloudEvent",
}, {
name: "event with invalid type",
event: eventInvalidType,
error: "cannot validate CDEvent Key: 'FooSubjectBarPredicateEventV2_2_3.Context.Context.Type.",
}, {
name: "event with unknown type",
event: eventUnknownType,
error: "cannot validate CDEvent jsonschema validation failed with 'https://cdevents.dev/99.1.0/schema/foosubject-barpredicate-event#'",
}, {
name: "event with invalid artifact id format",
event: eventInvalidArtifactIdFormat,
error: "cannot validate CDEvent Key: 'FooSubjectBarPredicateEventV2_2_3.Subject.Content.ArtifactId'",
}, {
name: "does not match the custom schema",
event: eventJsonCustomDataCustomSchema,
error: "cannot validate CDEvent jsonschema validation failed with 'https://myorg.com/schema/stricter#",
}}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
_, err := api.AsCloudEvent(tc.event)
if err == nil {
t.Fatalf("expected it to fail, but it didn't")
}
if !strings.HasPrefix(err.Error(), tc.error) {
t.Errorf("error %s does not start with the expected prefix %s", err.Error(), tc.error)
}
})
}
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/api/v04/conformance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -613,13 +614,31 @@ func TestExamples(t *testing.T) {
if d := cmp.Diff(consumed.GetLinks(), produced.GetLinks()); d != "" {
t.Errorf("args: diff(-want,+got):\n%s", d)
}
// Coverage for GetCustomSchema
consumedSchema, err := consumed.GetCustomSchema()
if err != nil {
t.Errorf("failed to obtain the consumed event custom schema: %v", err)
}
if d := cmp.Diff(consumedSchema.ID, producedSchema.ID); d != "" {
t.Errorf("args: diff(-want,+got):\n%s", d)
}
// Check the case of no custom schema
produced.SetSchemaUri("")
producedSchema, err = produced.GetCustomSchema()
if producedSchema != nil || err != nil {
t.Errorf("expected nil schema and error when schema is not set, got schema %v, error %v", producedSchema, err)
}
// Check the case of custom schema missing from the DB
notFoundSchema := "https://this.is.not.found/in/the/db"
produced.SetSchemaUri(notFoundSchema)
producedSchema, err = produced.GetCustomSchema()
if err == nil {
t.Errorf("expected an error when schema is not found, got schema %v, error %v", producedSchema, err)
}
expectedError := fmt.Sprintf("schema with id %s could not be found", notFoundSchema)
if !strings.HasPrefix(err.Error(), expectedError) {
t.Errorf("error %s does not start with the expected prefix %s", err.Error(), expectedError)
}
})
}
}

0 comments on commit 10aa909

Please sign in to comment.