Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve eventing v1beta1 conversion tests #3395

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was getting a segfault here, discovered that with a new test case

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why that ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDE was complaining unused var and I listen to my IDE

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)
}
}