Skip to content

Commit

Permalink
Add unit tests for VPCEndpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Darryl Sw <[email protected]>
  • Loading branch information
darryl-sw committed Sep 17, 2021
1 parent 1192d35 commit 095919a
Show file tree
Hide file tree
Showing 3 changed files with 628 additions and 22 deletions.
45 changes: 45 additions & 0 deletions pkg/clients/ec2/fake/vpcendpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package fake

import (
"context"

"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)

// MockVPCEndpointClient for testing
type MockVPCEndpointClient struct {
ec2iface.EC2API

MockCreateVpcEndpointWithContext func(context.Context, *ec2.CreateVpcEndpointInput, ...request.Option) (*ec2.CreateVpcEndpointOutput, error)
MockDeleteVpcEndpoints func(*ec2.DeleteVpcEndpointsInput) (*ec2.DeleteVpcEndpointsOutput, error)
MockModifyVpcEndpointWithContext func(context.Context, *ec2.ModifyVpcEndpointInput, ...request.Option) (*ec2.ModifyVpcEndpointOutput, error)
MockDescribeVpcEndpoints func(*ec2.DescribeVpcEndpointsInput) (*ec2.DescribeVpcEndpointsOutput, error)
MockDescribeVpcEndpointsWithContext func(context.Context, *ec2.DescribeVpcEndpointsInput, ...request.Option) (*ec2.DescribeVpcEndpointsOutput, error)
}

// CreateVpcEndpointWithContext mocks CreateVpcEndpointWithContext
func (m *MockVPCEndpointClient) CreateVpcEndpointWithContext(ctx context.Context, input *ec2.CreateVpcEndpointInput, req ...request.Option) (*ec2.CreateVpcEndpointOutput, error) {
return m.MockCreateVpcEndpointWithContext(ctx, input)
}

// DeleteVpcEndpoints mocks DeleteVpcEndpoints
func (m *MockVPCEndpointClient) DeleteVpcEndpoints(input *ec2.DeleteVpcEndpointsInput) (*ec2.DeleteVpcEndpointsOutput, error) {
return m.MockDeleteVpcEndpoints(input)
}

// ModifyVpcEndpointWithContext mocks ModifyVpcEndpointWithContext
func (m *MockVPCEndpointClient) ModifyVpcEndpointWithContext(ctx context.Context, input *ec2.ModifyVpcEndpointInput, req ...request.Option) (*ec2.ModifyVpcEndpointOutput, error) {
return m.MockModifyVpcEndpointWithContext(ctx, input)
}

// DescribeVpcEndpoints mocks DescribeVpcEndpoints
func (m *MockVPCEndpointClient) DescribeVpcEndpoints(input *ec2.DescribeVpcEndpointsInput) (*ec2.DescribeVpcEndpointsOutput, error) {
return m.MockDescribeVpcEndpoints(input)
}

// DescribeVpcEndpointsWithContext mocks DescribeVpcEndpointsWithContext
func (m *MockVPCEndpointClient) DescribeVpcEndpointsWithContext(ctx context.Context, input *ec2.DescribeVpcEndpointsInput, req ...request.Option) (*ec2.DescribeVpcEndpointsOutput, error) {
return m.MockDescribeVpcEndpointsWithContext(ctx, input)
}
36 changes: 14 additions & 22 deletions pkg/controller/ec2/vpcendpoint/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,8 @@ import (
// SetupVPCEndpoint adds a controller that reconciles VPCEndpoint.
func SetupVPCEndpoint(mgr ctrl.Manager, l logging.Logger, rl workqueue.RateLimiter, poll time.Duration) error {
name := managed.ControllerName(svcapitypes.VPCEndpointGroupKind)
opts := []option{
func(e *external) {
c := &custom{client: e.client, kube: e.kube}
e.delete = c.delete
e.preCreate = preCreate
e.postCreate = postCreate
e.postObserve = postObserve
e.isUpToDate = isUpToDate
e.preUpdate = c.preUpdate
e.postUpdate = postUpdate
e.filterList = filterList
},
}
opts := []option{setupExternal}

return ctrl.NewControllerManagedBy(mgr).
Named(name).
WithOptions(controller.Options{
Expand All @@ -61,6 +50,18 @@ func SetupVPCEndpoint(mgr ctrl.Manager, l logging.Logger, rl workqueue.RateLimit
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name)))))
}

func setupExternal(e *external) {
c := &custom{client: e.client, kube: e.kube}
e.delete = c.delete
e.preCreate = preCreate
e.postCreate = postCreate
e.postObserve = postObserve
e.isUpToDate = isUpToDate
e.preUpdate = c.preUpdate
e.postUpdate = postUpdate
e.filterList = filterList
}

type custom struct {
kube client.Client
client svcsdkapi.EC2API
Expand Down Expand Up @@ -297,11 +298,6 @@ func generateVPCEndpointSDK(vpcEndpoint *ec2.VpcEndpoint) *svcapitypes.VPCEndpoi

return vpcEndpointSDK
}
<<<<<<< HEAD
<<<<<<< HEAD
=======
=======
>>>>>>> 6e1bfde0 (Reduce cyclomatic complexity for preUpdate)

/*
formatModifyVpcEndpointInput takes in a ModifyVpcEndpointInput, and sets
Expand Down Expand Up @@ -358,7 +354,6 @@ compare:

return result
}
<<<<<<< HEAD

/*
listCompareStringPtrIsSame takes in 2 list of string pointers,
Expand Down Expand Up @@ -387,6 +382,3 @@ compare:

return true
}
>>>>>>> 58d44389 (Reduce cyclomatic complexity for isUpToDate)
=======
>>>>>>> 6e1bfde0 (Reduce cyclomatic complexity for preUpdate)
Loading

0 comments on commit 095919a

Please sign in to comment.