Skip to content

Commit

Permalink
Improve eventing v1beta1 conversion tests (knative#3395)
Browse files Browse the repository at this point in the history
  • Loading branch information
aliok authored and lberk committed Jun 26, 2020
1 parent 99435cb commit 00f51ca
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/eventing/v1/broker_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"testing"
)

func TestBrokerConversionBadType(t *testing.T) {
func TestBrokerConversionHighestVersion(t *testing.T) {
good, bad := &Broker{}, &Broker{}

if err := good.ConvertTo(context.Background(), bad); err == nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/eventing/v1/trigger_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"testing"
)

func TestTriggerConversionBadType(t *testing.T) {
func TestTriggerConversionHighestVersion(t *testing.T) {
good, bad := &Trigger{}, &Trigger{}

if err := good.ConvertTo(context.Background(), bad); err == nil {
Expand Down
12 changes: 8 additions & 4 deletions pkg/apis/eventing/v1beta1/broker_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ func (source *Broker) ConvertTo(ctx context.Context, to apis.Convertible) error
switch sink := to.(type) {
case *v1.Broker:
sink.Spec.Config = source.Spec.Config
if err := source.Spec.Delivery.ConvertTo(ctx, sink.Spec.Delivery); err != nil {
return err
if source.Spec.Delivery != nil {
if err := source.Spec.Delivery.ConvertTo(ctx, sink.Spec.Delivery); err != nil {
return err
}
}
sink.Status.Status = source.Status.Status
sink.Status.Address = source.Status.Address
Expand All @@ -45,8 +47,10 @@ func (sink *Broker) ConvertFrom(ctx context.Context, from apis.Convertible) erro
switch source := from.(type) {
case *v1.Broker:
sink.Spec.Config = source.Spec.Config
if err := source.Spec.Delivery.ConvertFrom(ctx, sink.Spec.Delivery); err != nil {
return err
if source.Spec.Delivery != nil {
if err := source.Spec.Delivery.ConvertFrom(ctx, sink.Spec.Delivery); err != nil {
return err
}
}
sink.Status.Status = source.Status.Status
sink.Status.Address = source.Status.Address
Expand Down
26 changes: 26 additions & 0 deletions pkg/apis/eventing/v1beta1/broker_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,22 @@ package v1beta1
import (
"context"
"testing"

v1 "knative.dev/eventing/pkg/apis/eventing/v1"
)

func TestBrokerConversion(t *testing.T) {
objv1betav1, objv1 := &Broker{}, &v1.Broker{}

if err := objv1betav1.ConvertTo(context.Background(), objv1); err != nil {
t.Errorf("ConvertTo() = %#v, unexpected error", objv1betav1)
}

if err := objv1betav1.ConvertFrom(context.Background(), objv1); err != nil {
t.Errorf("ConvertFrom() = %#v, unexpected error", objv1)
}
}

func TestBrokerConversionBadType(t *testing.T) {
good, bad := &Broker{}, &Trigger{}

Expand All @@ -32,3 +46,15 @@ func TestBrokerConversionBadType(t *testing.T) {
t.Errorf("ConvertFrom() = %#v, wanted error", good)
}
}

func TestBrokerConversionBadVersion(t *testing.T) {
good, bad := &Broker{}, &Broker{}

if err := good.ConvertTo(context.Background(), bad); err == nil {
t.Errorf("ConvertTo() = %#v, wanted error", bad)
}

if err := good.ConvertFrom(context.Background(), bad); err == nil {
t.Errorf("ConvertFrom() = %#v, wanted error", good)
}
}
2 changes: 1 addition & 1 deletion pkg/apis/eventing/v1beta1/eventtype_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"testing"
)

func TestEventTypeConversionBadType(t *testing.T) {
func TestEventTypeConversionHighestVersion(t *testing.T) {
good, bad := &EventType{}, &EventType{}

if err := good.ConvertTo(context.Background(), bad); err == nil {
Expand Down
16 changes: 8 additions & 8 deletions pkg/apis/eventing/v1beta1/trigger_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ import (
)

// ConvertTo implements apis.Convertible
func (source *Trigger) ConvertTo(ctx context.Context, to apis.Convertible) error {
func (source *Trigger) ConvertTo(_ context.Context, to apis.Convertible) error {
switch sink := to.(type) {
case *v1.Trigger:
sink.Spec.Broker = source.Spec.Broker
sink.Spec.Subscriber = source.Spec.Subscriber
if source.Spec.Filter != nil {
sink.Spec.Filter = &v1.TriggerFilter{}
}
for k, v := range source.Spec.Filter.Attributes {
sink.Spec.Filter.Attributes[k] = v
for k, v := range source.Spec.Filter.Attributes {
sink.Spec.Filter.Attributes[k] = v
}
}
sink.Status.Status = source.Status.Status
sink.Status.SubscriberURI = source.Status.SubscriberURI
Expand All @@ -45,16 +45,16 @@ func (source *Trigger) ConvertTo(ctx context.Context, to apis.Convertible) error
}

// ConvertFrom implements apis.Convertible
func (sink *Trigger) ConvertFrom(ctx context.Context, from apis.Convertible) error {
func (sink *Trigger) ConvertFrom(_ context.Context, from apis.Convertible) error {
switch source := from.(type) {
case *v1.Trigger:
sink.Spec.Broker = source.Spec.Broker
sink.Spec.Subscriber = source.Spec.Subscriber
if source.Spec.Filter != nil {
sink.Spec.Filter = &TriggerFilter{}
}
for k, v := range source.Spec.Filter.Attributes {
sink.Spec.Filter.Attributes[k] = v
for k, v := range source.Spec.Filter.Attributes {
sink.Spec.Filter.Attributes[k] = v
}
}
sink.Status.Status = source.Status.Status
sink.Status.SubscriberURI = source.Status.SubscriberURI
Expand Down
26 changes: 26 additions & 0 deletions pkg/apis/eventing/v1beta1/trigger_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,22 @@ package v1beta1
import (
"context"
"testing"

v1 "knative.dev/eventing/pkg/apis/eventing/v1"
)

func TestTriggerConversion(t *testing.T) {
objv1betav1, objv1 := &Trigger{}, &v1.Trigger{}

if err := objv1betav1.ConvertTo(context.Background(), objv1); err != nil {
t.Errorf("ConvertTo() = %#v, unexpected error", objv1betav1)
}

if err := objv1betav1.ConvertFrom(context.Background(), objv1); err != nil {
t.Errorf("ConvertFrom() = %#v, unexpected error", objv1)
}
}

func TestTriggerConversionBadType(t *testing.T) {
good, bad := &Trigger{}, &Broker{}

Expand All @@ -32,3 +46,15 @@ func TestTriggerConversionBadType(t *testing.T) {
t.Errorf("ConvertFrom() = %#v, wanted error", good)
}
}

func TestTriggerConversionBadVersion(t *testing.T) {
good, bad := &Trigger{}, &Trigger{}

if err := good.ConvertTo(context.Background(), bad); err == nil {
t.Errorf("ConvertTo() = %#v, wanted error", bad)
}

if err := good.ConvertFrom(context.Background(), bad); err == nil {
t.Errorf("ConvertFrom() = %#v, wanted error", good)
}
}

0 comments on commit 00f51ca

Please sign in to comment.