Skip to content

Commit

Permalink
add proto test to confirm it is correct
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Nichols <n3wscott@chainguard.dev>
  • Loading branch information
n3wscott committed Jul 19, 2022
1 parent ae4b7be commit d7b5740
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
4 changes: 4 additions & 0 deletions binding/format/protobuf/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.1.0 // indirect
go.uber.org/zap v1.10.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)

Expand Down
7 changes: 7 additions & 0 deletions binding/format/protobuf/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand All @@ -20,18 +21,24 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLD
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac h1:7zkz7BUtwNFFqcowJ+RIgu2MaV/MapERkDIy+mwPyjs=
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
117 changes: 117 additions & 0 deletions binding/format/protobuf/v2/protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ package format_test

import (
"net/url"
"reflect"
"testing"
stdtime "time"

"github.com/stretchr/testify/require"

cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/cloudevents/sdk-go/v2/types"

Expand Down Expand Up @@ -91,3 +93,118 @@ func TestProtobufFormatWithProtobufCodec(t *testing.T) {
require.NoError(e2.DataAs(payload2))
require.True(payload2.GetCeBoolean())
}

func TestFromProto(t *testing.T) {
tests := []struct {
name string
proto *pb.CloudEvent
want *event.Event
wantErr bool
}{{
name: "happy binary json",
proto: &pb.CloudEvent{
SpecVersion: "1.0",
Id: "abc-123",
Source: "/source",
Type: "some.type",
Attributes: map[string]*pb.CloudEventAttributeValue{
"datacontenttype": {Attr: &pb.CloudEventAttributeValue_CeString{CeString: "application/json"}},
"extra1": {Attr: &pb.CloudEventAttributeValue_CeString{CeString: "extra1 value"}},
"extra2": {Attr: &pb.CloudEventAttributeValue_CeInteger{CeInteger: 2}},
"extra3": {Attr: &pb.CloudEventAttributeValue_CeBoolean{CeBoolean: true}},
"extra4": {Attr: &pb.CloudEventAttributeValue_CeUri{CeUri: "https://example.com"}},
},
Data: &pb.CloudEvent_BinaryData{
BinaryData: []byte(`{"unit":"test"}`),
},
},
want: func() *event.Event {
out := event.New(cloudevents.VersionV1)
out.SetID("abc-123")
out.SetSource("/source")
out.SetType("some.type")
_ = out.SetData("application/json", map[string]interface{}{"unit": "test"})
out.SetExtension("extra1", "extra1 value")
out.SetExtension("extra2", 2)
out.SetExtension("extra3", true)
out.SetExtension("extra4", url.URL{Scheme: "https", Host: "example.com"})
return &out
}(),
wantErr: false,
}, {
name: "happy text",
proto: &pb.CloudEvent{
SpecVersion: "1.0",
Id: "abc-123",
Source: "/source",
Type: "some.type",
Attributes: map[string]*pb.CloudEventAttributeValue{
"datacontenttype": {Attr: &pb.CloudEventAttributeValue_CeString{CeString: "text/plain"}},
"extra1": {Attr: &pb.CloudEventAttributeValue_CeString{CeString: "extra1 value"}},
"extra2": {Attr: &pb.CloudEventAttributeValue_CeInteger{CeInteger: 2}},
"extra3": {Attr: &pb.CloudEventAttributeValue_CeBoolean{CeBoolean: true}},
"extra4": {Attr: &pb.CloudEventAttributeValue_CeUri{CeUri: "https://example.com"}},
},
Data: &pb.CloudEvent_TextData{
TextData: `this is some text with a "quote"`,
},
},
want: func() *event.Event {
out := event.New(cloudevents.VersionV1)
out.SetID("abc-123")
out.SetSource("/source")
out.SetType("some.type")
_ = out.SetData("text/plain", `this is some text with a "quote"`)
out.SetExtension("extra1", "extra1 value")
out.SetExtension("extra2", 2)
out.SetExtension("extra3", true)
out.SetExtension("extra4", url.URL{Scheme: "https", Host: "example.com"})
return &out
}(),
wantErr: false,
}, {
name: "happy json as text",
proto: &pb.CloudEvent{
SpecVersion: "1.0",
Id: "abc-123",
Source: "/source",
Type: "some.type",
Attributes: map[string]*pb.CloudEventAttributeValue{
"datacontenttype": {Attr: &pb.CloudEventAttributeValue_CeString{CeString: "application/json"}},
"extra1": {Attr: &pb.CloudEventAttributeValue_CeString{CeString: "extra1 value"}},
"extra2": {Attr: &pb.CloudEventAttributeValue_CeInteger{CeInteger: 2}},
"extra3": {Attr: &pb.CloudEventAttributeValue_CeBoolean{CeBoolean: true}},
"extra4": {Attr: &pb.CloudEventAttributeValue_CeUri{CeUri: "https://example.com"}},
},
Data: &pb.CloudEvent_TextData{
TextData: `{"unit":"test"}`,
},
},
want: func() *event.Event {
out := event.New(cloudevents.VersionV1)
out.SetID("abc-123")
out.SetSource("/source")
out.SetType("some.type")
_ = out.SetData("application/json", `{"unit":"test"}`)
out.SetExtension("extra1", "extra1 value")
out.SetExtension("extra2", 2)
out.SetExtension("extra3", true)
out.SetExtension("extra4", url.URL{Scheme: "https", Host: "example.com"})
return &out
}(),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := format.FromProto(tt.proto)
if (err != nil) != tt.wantErr {
t.Errorf("FromProto() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FromProto() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit d7b5740

Please sign in to comment.