Skip to content

Commit

Permalink
Test cleanup - should not include functional changes (#421)
Browse files Browse the repository at this point in the history
* Updating unit tests to use t.Run, standardize imports and mock naming
* removed additional references to 'mesh'
  • Loading branch information
erikfuller authored Oct 4, 2023
1 parent 3e156e8 commit 60f3b3a
Show file tree
Hide file tree
Showing 32 changed files with 3,561 additions and 3,531 deletions.
205 changes: 105 additions & 100 deletions controllers/eventhandlers/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,35 @@ package eventhandlers
import (
"context"
"errors"
"fmt"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
gateway_api_v1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gateway_api "sigs.k8s.io/gateway-api/apis/v1beta1"
gwv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

mock_client "github.com/aws/aws-application-networking-k8s/mocks/controller-runtime/client"
"github.com/aws/aws-application-networking-k8s/pkg/apis/applicationnetworking/v1alpha1"
anv1alpha1 "github.com/aws/aws-application-networking-k8s/pkg/apis/applicationnetworking/v1alpha1"
"github.com/aws/aws-application-networking-k8s/pkg/model/core"
"github.com/aws/aws-application-networking-k8s/pkg/utils/gwlog"
)

func createHTTPRoute(name, namespace string, backendRef gateway_api.BackendObjectReference) gateway_api.HTTPRoute {
return gateway_api.HTTPRoute{
func createHTTPRoute(name, namespace string, backendRef gwv1beta1.BackendObjectReference) gwv1beta1.HTTPRoute {
return gwv1beta1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: gateway_api.HTTPRouteSpec{
Rules: []gateway_api.HTTPRouteRule{
Spec: gwv1beta1.HTTPRouteSpec{
Rules: []gwv1beta1.HTTPRouteRule{
{
BackendRefs: []gateway_api.HTTPBackendRef{
BackendRefs: []gwv1beta1.HTTPBackendRef{
{
BackendRef: gateway_api.BackendRef{
BackendRef: gwv1beta1.BackendRef{
BackendObjectReference: backendRef,
},
},
Expand All @@ -45,47 +46,47 @@ func TestServiceToRoutes(t *testing.T) {
c := gomock.NewController(t)
defer c.Finish()

routes := []gateway_api.HTTPRoute{
createHTTPRoute("invalid-kind", "ns1", gateway_api.BackendObjectReference{
Kind: (*gateway_api.Kind)(pointer.String("NotService")),
routes := []gwv1beta1.HTTPRoute{
createHTTPRoute("invalid-kind", "ns1", gwv1beta1.BackendObjectReference{
Kind: (*gwv1beta1.Kind)(pointer.String("NotService")),
Name: "test-service",
}),
createHTTPRoute("invalid-nil-kind", "ns1", gateway_api.BackendObjectReference{
createHTTPRoute("invalid-nil-kind", "ns1", gwv1beta1.BackendObjectReference{
Kind: nil,
Namespace: nil,
Name: "test-service",
}),
createHTTPRoute("invalid-nil-group", "ns1", gateway_api.BackendObjectReference{
Kind: (*gateway_api.Kind)(pointer.String("Service")),
createHTTPRoute("invalid-nil-group", "ns1", gwv1beta1.BackendObjectReference{
Kind: (*gwv1beta1.Kind)(pointer.String("Service")),
Namespace: nil,
Name: "test-service",
}),
createHTTPRoute("invalid-group", "ns1", gateway_api.BackendObjectReference{
Group: (*gateway_api.Group)(pointer.String("not-core")),
Kind: (*gateway_api.Kind)(pointer.String("Service")),
createHTTPRoute("invalid-group", "ns1", gwv1beta1.BackendObjectReference{
Group: (*gwv1beta1.Group)(pointer.String("not-core")),
Kind: (*gwv1beta1.Kind)(pointer.String("Service")),
Namespace: nil,
Name: "test-service",
}),
createHTTPRoute("valid-inferred-namespace", "ns1", gateway_api.BackendObjectReference{
Group: (*gateway_api.Group)(pointer.String("")),
Kind: (*gateway_api.Kind)(pointer.String("Service")),
createHTTPRoute("valid-inferred-namespace", "ns1", gwv1beta1.BackendObjectReference{
Group: (*gwv1beta1.Group)(pointer.String("")),
Kind: (*gwv1beta1.Kind)(pointer.String("Service")),
Namespace: nil,
Name: "test-service",
}),
createHTTPRoute("valid-explicit-namespace", "ns1", gateway_api.BackendObjectReference{
Group: (*gateway_api.Group)(pointer.String("")),
Kind: (*gateway_api.Kind)(pointer.String("Service")),
Namespace: (*gateway_api.Namespace)(pointer.String("ns1")),
createHTTPRoute("valid-explicit-namespace", "ns1", gwv1beta1.BackendObjectReference{
Group: (*gwv1beta1.Group)(pointer.String("")),
Kind: (*gwv1beta1.Kind)(pointer.String("Service")),
Namespace: (*gwv1beta1.Namespace)(pointer.String("ns1")),
Name: "test-service",
}),
createHTTPRoute("invalid-different-namespace", "ns1", gateway_api.BackendObjectReference{
Kind: (*gateway_api.Kind)(pointer.String("Service")),
Namespace: (*gateway_api.Namespace)(pointer.String("ns2")),
createHTTPRoute("invalid-different-namespace", "ns1", gwv1beta1.BackendObjectReference{
Kind: (*gwv1beta1.Kind)(pointer.String("Service")),
Namespace: (*gwv1beta1.Namespace)(pointer.String("ns2")),
Name: "test-service",
}),
createHTTPRoute("invalid-different-name", "ns1", gateway_api.BackendObjectReference{
Kind: (*gateway_api.Kind)(pointer.String("Service")),
Namespace: (*gateway_api.Namespace)(pointer.String("ns1")),
createHTTPRoute("invalid-different-name", "ns1", gwv1beta1.BackendObjectReference{
Kind: (*gwv1beta1.Kind)(pointer.String("Service")),
Namespace: (*gwv1beta1.Namespace)(pointer.String("ns1")),
Name: "not-test-service",
}),
}
Expand All @@ -96,7 +97,7 @@ func TestServiceToRoutes(t *testing.T) {

mockClient := mock_client.NewMockClient(c)
mockClient.EXPECT().List(gomock.Any(), gomock.Any()).DoAndReturn(
func(ctx context.Context, routeList *gateway_api.HTTPRouteList, _ ...interface{}) error {
func(ctx context.Context, routeList *gwv1beta1.HTTPRouteList, _ ...interface{}) error {
for _, route := range routes {
routeList.Items = append(routeList.Items, route)
}
Expand Down Expand Up @@ -127,34 +128,34 @@ func TestTargetGroupPolicyToService(t *testing.T) {

testCases := []struct {
namespace string
targetKind gateway_api.Kind
targetNamespace *gateway_api.Namespace
targetKind gwv1beta1.Kind
targetNamespace *gwv1beta1.Namespace
serviceFound bool
success bool
}{
{
namespace: ns1,
targetKind: "Service",
targetNamespace: (*gateway_api.Namespace)(&ns2),
targetNamespace: (*gwv1beta1.Namespace)(&ns2),
success: false,
},
{
namespace: ns1,
targetKind: "NotService",
targetNamespace: (*gateway_api.Namespace)(&ns1),
targetNamespace: (*gwv1beta1.Namespace)(&ns1),
success: false,
},
{
namespace: ns1,
targetKind: "Service",
targetNamespace: (*gateway_api.Namespace)(&ns1),
targetNamespace: (*gwv1beta1.Namespace)(&ns1),
serviceFound: false,
success: false,
},
{
namespace: ns1,
targetKind: "Service",
targetNamespace: (*gateway_api.Namespace)(&ns1),
targetNamespace: (*gwv1beta1.Namespace)(&ns1),
serviceFound: true,
success: true,
},
Expand All @@ -167,33 +168,35 @@ func TestTargetGroupPolicyToService(t *testing.T) {
},
}

for _, tt := range testCases {
mockClient := mock_client.NewMockClient(c)
mapper := &resourceMapper{log: gwlog.FallbackLogger, client: mockClient}
if tt.serviceFound {
mockClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
} else {
mockClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("fail")).AnyTimes()
}
svc := mapper.TargetGroupPolicyToService(context.Background(), &v1alpha1.TargetGroupPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "test-policy",
Namespace: tt.namespace,
},
Spec: v1alpha1.TargetGroupPolicySpec{
TargetRef: &gateway_api_v1alpha2.PolicyTargetReference{
Group: "",
Kind: tt.targetKind,
Name: "test-service",
Namespace: tt.targetNamespace,
for i, tt := range testCases {
t.Run(fmt.Sprintf("TGPolicyToService_%d", i), func(t *testing.T) {
mockClient := mock_client.NewMockClient(c)
mapper := &resourceMapper{log: gwlog.FallbackLogger, client: mockClient}
if tt.serviceFound {
mockClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
} else {
mockClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("fail")).AnyTimes()
}
svc := mapper.TargetGroupPolicyToService(context.Background(), &anv1alpha1.TargetGroupPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "test-policy",
Namespace: tt.namespace,
},
},
Spec: anv1alpha1.TargetGroupPolicySpec{
TargetRef: &gwv1alpha2.PolicyTargetReference{
Group: "",
Kind: tt.targetKind,
Name: "test-service",
Namespace: tt.targetNamespace,
},
},
})
if tt.success {
assert.NotNil(t, svc)
} else {
assert.Nil(t, svc)
}
})
if tt.success {
assert.NotNil(t, svc)
} else {
assert.Nil(t, svc)
}
}
}

Expand All @@ -207,45 +210,45 @@ func TestVpcAssociationPolicyToGateway(t *testing.T) {
testCases := []struct {
testCaseName string
namespace string
targetKind gateway_api.Kind
targetNamespace *gateway_api.Namespace
targetKind gwv1beta1.Kind
targetNamespace *gwv1beta1.Namespace
gatewayFound bool
expectSuccess bool
}{
{
testCaseName: "namespace not match",
namespace: ns1,
targetKind: "Gateway",
targetNamespace: (*gateway_api.Namespace)(&ns2),
targetNamespace: (*gwv1beta1.Namespace)(&ns2),
expectSuccess: false,
},
{
testCaseName: "targetKind not match scenario 1",
namespace: ns1,
targetKind: "NotGateway",
targetNamespace: (*gateway_api.Namespace)(&ns1),
targetNamespace: (*gwv1beta1.Namespace)(&ns1),
expectSuccess: false,
},
{
testCaseName: "targetKind not match scenario 2",
namespace: ns1,
targetKind: "Service",
targetNamespace: (*gateway_api.Namespace)(&ns1),
targetNamespace: (*gwv1beta1.Namespace)(&ns1),
expectSuccess: false,
},
{
testCaseName: "gateway not found",
namespace: ns1,
targetKind: "Gateway",
targetNamespace: (*gateway_api.Namespace)(&ns1),
targetNamespace: (*gwv1beta1.Namespace)(&ns1),
gatewayFound: false,
expectSuccess: false,
},
{
testCaseName: "gateway found, targetRef namespace match",
namespace: ns1,
targetKind: "Gateway",
targetNamespace: (*gateway_api.Namespace)(&ns1),
targetNamespace: (*gwv1beta1.Namespace)(&ns1),
gatewayFound: true,
expectSuccess: true,
},
Expand All @@ -260,39 +263,41 @@ func TestVpcAssociationPolicyToGateway(t *testing.T) {
}

for _, tt := range testCases {
mockClient := mock_client.NewMockClient(c)
mapper := &resourceMapper{log: gwlog.FallbackLogger, client: mockClient}
if tt.gatewayFound {
mockClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
} else {
mockClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("fail")).AnyTimes()
}
var targetRefGroupName string
if tt.targetKind == "Gateway" {
targetRefGroupName = gateway_api.GroupName
} else if tt.targetKind == "Service" {
targetRefGroupName = corev1.GroupName
}
t.Run(tt.testCaseName, func(t *testing.T) {
mockClient := mock_client.NewMockClient(c)
mapper := &resourceMapper{log: gwlog.FallbackLogger, client: mockClient}
if tt.gatewayFound {
mockClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
} else {
mockClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("fail")).AnyTimes()
}
var targetRefGroupName string
if tt.targetKind == "Gateway" {
targetRefGroupName = gwv1beta1.GroupName
} else if tt.targetKind == "Service" {
targetRefGroupName = corev1.GroupName
}

gw := mapper.VpcAssociationPolicyToGateway(context.Background(), &v1alpha1.VpcAssociationPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "test--vpc-association-policy",
Namespace: tt.namespace,
},
gw := mapper.VpcAssociationPolicyToGateway(context.Background(), &anv1alpha1.VpcAssociationPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "test--vpc-association-policy",
Namespace: tt.namespace,
},

Spec: v1alpha1.VpcAssociationPolicySpec{
TargetRef: &gateway_api_v1alpha2.PolicyTargetReference{
Group: gateway_api.Group(targetRefGroupName),
Kind: tt.targetKind,
Name: "test-gw",
Namespace: tt.targetNamespace,
Spec: anv1alpha1.VpcAssociationPolicySpec{
TargetRef: &gwv1alpha2.PolicyTargetReference{
Group: gwv1beta1.Group(targetRefGroupName),
Kind: tt.targetKind,
Name: "test-gw",
Namespace: tt.targetNamespace,
},
},
},
})
if tt.expectSuccess {
assert.NotNil(t, gw)
} else {
assert.Nil(t, gw)
}
})
if tt.expectSuccess {
assert.NotNil(t, gw)
} else {
assert.Nil(t, gw)
}
}
}
Loading

0 comments on commit 60f3b3a

Please sign in to comment.