Skip to content

Commit

Permalink
Move interfaces to _interface.go files and generate mocks from them
Browse files Browse the repository at this point in the history
  • Loading branch information
wongma7 committed Dec 3, 2021
1 parent 78b89db commit 6e2684e
Show file tree
Hide file tree
Showing 18 changed files with 400 additions and 337 deletions.
9 changes: 4 additions & 5 deletions hack/update-gomock
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.


set -euo pipefail

IMPORT_PATH=github.com/kubernetes-sigs/aws-efs-csi-driver
mockgen -package=mocks -destination=./pkg/driver/mocks/mock_mount.go ${IMPORT_PATH}/pkg/driver Mounter
mockgen -package=mocks -destination=./pkg/cloud/mocks/mock_ec2metadata.go ${IMPORT_PATH}/pkg/cloud EC2Metadata
mockgen -package=mocks -destination=./pkg/cloud/mocks/mock_taskmetadata.go ${IMPORT_PATH}/pkg/cloud TaskMetadataService
mockgen -package=cloud -destination=./pkg/cloud/mock_efs.go -source pkg/cloud/efs_interface.go
mockgen -package=cloud -destination=./pkg/cloud/mock_cloud.go -source pkg/cloud/cloud_interface.go
mockgen -package=cloud -destination=./pkg/cloud/mock_metadata.go -source pkg/cloud/metadata_interface.go
mockgen -package=driver -destination=./pkg/driver/mock_mounter.go -source pkg/driver/mounter.go
24 changes: 3 additions & 21 deletions pkg/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import (
"context"
"errors"
"fmt"
"math/rand"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/efs"
"k8s.io/klog"
"math/rand"
"time"
)

const (
Expand Down Expand Up @@ -74,24 +74,6 @@ type MountTarget struct {
IPAddress string
}

// Efs abstracts efs client(https://docs.aws.amazon.com/sdk-for-go/api/service/efs/)
type Efs interface {
CreateAccessPointWithContext(aws.Context, *efs.CreateAccessPointInput, ...request.Option) (*efs.CreateAccessPointOutput, error)
DeleteAccessPointWithContext(aws.Context, *efs.DeleteAccessPointInput, ...request.Option) (*efs.DeleteAccessPointOutput, error)
DescribeAccessPointsWithContext(aws.Context, *efs.DescribeAccessPointsInput, ...request.Option) (*efs.DescribeAccessPointsOutput, error)
DescribeFileSystemsWithContext(aws.Context, *efs.DescribeFileSystemsInput, ...request.Option) (*efs.DescribeFileSystemsOutput, error)
DescribeMountTargetsWithContext(aws.Context, *efs.DescribeMountTargetsInput, ...request.Option) (*efs.DescribeMountTargetsOutput, error)
}

type Cloud interface {
GetMetadata() MetadataService
CreateAccessPoint(ctx context.Context, volumeName string, accessPointOpts *AccessPointOptions) (accessPoint *AccessPoint, err error)
DeleteAccessPoint(ctx context.Context, accessPointId string) (err error)
DescribeAccessPoint(ctx context.Context, accessPointId string) (accessPoint *AccessPoint, err error)
DescribeFileSystem(ctx context.Context, fileSystemId string) (fs *FileSystem, err error)
DescribeMountTargets(ctx context.Context, fileSystemId, az string) (fs *MountTarget, err error)
}

type cloud struct {
metadata MetadataService
efs Efs
Expand Down
12 changes: 12 additions & 0 deletions pkg/cloud/cloud_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cloud

import "context"

type Cloud interface {
GetMetadata() MetadataService
CreateAccessPoint(ctx context.Context, volumeName string, accessPointOpts *AccessPointOptions) (accessPoint *AccessPoint, err error)
DeleteAccessPoint(ctx context.Context, accessPointId string) (err error)
DescribeAccessPoint(ctx context.Context, accessPointId string) (accessPoint *AccessPoint, err error)
DescribeFileSystem(ctx context.Context, fileSystemId string) (fs *FileSystem, err error)
DescribeMountTargets(ctx context.Context, fileSystemId, az string) (fs *MountTarget, err error)
}
41 changes: 20 additions & 21 deletions pkg/cloud/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/efs"
"github.com/golang/mock/gomock"
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/cloud/mocks"
)

type errtyp struct {
Expand All @@ -36,7 +35,7 @@ func TestCreateAccessPoint(t *testing.T) {
name: "Success",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockCtl)
mockEfs := NewMockEfs(mockCtl)
c := &cloud{
efs: mockEfs,
}
Expand Down Expand Up @@ -98,7 +97,7 @@ func TestCreateAccessPoint(t *testing.T) {
name: "Fail",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockCtl)
mockEfs := NewMockEfs(mockCtl)
c := &cloud{efs: mockEfs}

req := &AccessPointOptions{
Expand All @@ -122,7 +121,7 @@ func TestCreateAccessPoint(t *testing.T) {
name: "Fail: Access Denied",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockCtl)
mockEfs := NewMockEfs(mockCtl)
c := &cloud{efs: mockEfs}

req := &AccessPointOptions{
Expand Down Expand Up @@ -164,7 +163,7 @@ func TestDeleteAccessPoint(t *testing.T) {
name: "Success",
testFunc: func(t *testing.T) {
mockctl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}

output := &efs.DeleteAccessPointOutput{}
Expand All @@ -182,7 +181,7 @@ func TestDeleteAccessPoint(t *testing.T) {
name: "Fail: Access Point Not Found",
testFunc: func(t *testing.T) {
mockctl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}

ctx := context.Background()
Expand All @@ -201,7 +200,7 @@ func TestDeleteAccessPoint(t *testing.T) {
name: "Fail: Access Denied",
testFunc: func(t *testing.T) {
mockctl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}

ctx := context.Background()
Expand All @@ -220,7 +219,7 @@ func TestDeleteAccessPoint(t *testing.T) {
name: "Fail: Other",
testFunc: func(t *testing.T) {
mockctl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}

ctx := context.Background()
Expand Down Expand Up @@ -256,7 +255,7 @@ func TestDescribeAccessPoint(t *testing.T) {
name: "Success",
testFunc: func(t *testing.T) {
mockctl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}

output := &efs.DescribeAccessPointsOutput{
Expand Down Expand Up @@ -308,7 +307,7 @@ func TestDescribeAccessPoint(t *testing.T) {
name: "Fail: DescribeAccessPoint result has 0 access points",
testFunc: func(t *testing.T) {
mockctl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}

output := &efs.DescribeAccessPointsOutput{
Expand All @@ -328,7 +327,7 @@ func TestDescribeAccessPoint(t *testing.T) {
name: "Fail: DescribeAccessPoint result has more than 1 access points",
testFunc: func(t *testing.T) {
mockctl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}

output := &efs.DescribeAccessPointsOutput{
Expand Down Expand Up @@ -387,7 +386,7 @@ func TestDescribeAccessPoint(t *testing.T) {
name: "Fail: Access Point Not Found",
testFunc: func(t *testing.T) {
mockctl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}

ctx := context.Background()
Expand All @@ -406,7 +405,7 @@ func TestDescribeAccessPoint(t *testing.T) {
name: "Fail: Access Denied",
testFunc: func(t *testing.T) {
mockctl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}

ctx := context.Background()
Expand All @@ -425,7 +424,7 @@ func TestDescribeAccessPoint(t *testing.T) {
name: "Fail: Other",
testFunc: func(t *testing.T) {
mockctl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}

ctx := context.Background()
Expand Down Expand Up @@ -455,7 +454,7 @@ func TestDescribeFileSystem(t *testing.T) {
name: "Success",
testFunc: func(t *testing.T) {
mockctl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}

output := &efs.DescribeFileSystemsOutput{
Expand Down Expand Up @@ -491,7 +490,7 @@ func TestDescribeFileSystem(t *testing.T) {
name: "Fail: DescribeFileSystems result has 0 file systems",
testFunc: func(t *testing.T) {
mockctl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}

output := &efs.DescribeFileSystemsOutput{
Expand All @@ -511,7 +510,7 @@ func TestDescribeFileSystem(t *testing.T) {
name: "Fail: DescribeFileSystem result has more than 1 file-system",
testFunc: func(t *testing.T) {
mockctl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}

output := &efs.DescribeFileSystemsOutput{
Expand Down Expand Up @@ -546,7 +545,7 @@ func TestDescribeFileSystem(t *testing.T) {
name: "Fail: File System Not Found",
testFunc: func(t *testing.T) {
mockctl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}

ctx := context.Background()
Expand All @@ -565,7 +564,7 @@ func TestDescribeFileSystem(t *testing.T) {
name: "Fail: Access Denied",
testFunc: func(t *testing.T) {
mockctl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}

ctx := context.Background()
Expand All @@ -584,7 +583,7 @@ func TestDescribeFileSystem(t *testing.T) {
name: "Fail: Other",
testFunc: func(t *testing.T) {
mockctl := gomock.NewController(t)
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}

ctx := context.Background()
Expand Down Expand Up @@ -706,7 +705,7 @@ func TestDescribeMountTargets(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
mockctl := gomock.NewController(t)
defer mockctl.Finish()
mockEfs := mocks.NewMockEfs(mockctl)
mockEfs := NewMockEfs(mockctl)
c := &cloud{efs: mockEfs}
ctx := context.Background()

Expand Down
16 changes: 16 additions & 0 deletions pkg/cloud/efs_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cloud

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/efs"
)

// Efs abstracts efs client(https://docs.aws.amazon.com/sdk-for-go/api/service/efs/)
type Efs interface {
CreateAccessPointWithContext(aws.Context, *efs.CreateAccessPointInput, ...request.Option) (*efs.CreateAccessPointOutput, error)
DeleteAccessPointWithContext(aws.Context, *efs.DeleteAccessPointInput, ...request.Option) (*efs.DeleteAccessPointOutput, error)
DescribeAccessPointsWithContext(aws.Context, *efs.DescribeAccessPointsInput, ...request.Option) (*efs.DescribeAccessPointsOutput, error)
DescribeFileSystemsWithContext(aws.Context, *efs.DescribeFileSystemsInput, ...request.Option) (*efs.DescribeFileSystemsOutput, error)
DescribeMountTargetsWithContext(aws.Context, *efs.DescribeMountTargetsInput, ...request.Option) (*efs.DescribeMountTargetsOutput, error)
}
15 changes: 2 additions & 13 deletions pkg/cloud/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,12 @@ package cloud

import (
"fmt"
"os"

"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"os"
)

type EC2Metadata interface {
Available() bool
GetInstanceIdentityDocument() (ec2metadata.EC2InstanceIdentityDocument, error)
}

// MetadataService represents AWS metadata service.
type MetadataService interface {
GetInstanceID() string
GetRegion() string
GetAvailabilityZone() string
}

type metadata struct {
instanceID string
region string
Expand Down
7 changes: 2 additions & 5 deletions pkg/cloud/task_metadata.go → pkg/cloud/metadata_ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,18 @@ package cloud
import (
"encoding/json"
"fmt"
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/util"
"net/http"
"os"
"strings"
"time"

"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/util"
)

const (
taskMetadataV4EnvName = "ECS_CONTAINER_METADATA_URI_V4"
)

type TaskMetadataService interface {
GetTMDSV4Response() ([]byte, error)
}

type taskMetadata struct {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package cloud
import (
"encoding/json"
"fmt"
"github.com/golang/mock/gomock"
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/cloud/mocks"
"os"
"testing"

"github.com/golang/mock/gomock"
)

var (
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestGetTaskMetadataService(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockTaskMetadata := mocks.NewMockTaskMetadataService(mockCtrl)
mockTaskMetadata := NewMockTaskMetadataService(mockCtrl)
jsonData, _ := json.Marshal(tc.returnResponse)
mockTaskMetadata.EXPECT().GetTMDSV4Response().Return(jsonData, tc.err)

Expand Down
19 changes: 19 additions & 0 deletions pkg/cloud/metadata_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cloud

import "github.com/aws/aws-sdk-go/aws/ec2metadata"

// MetadataService represents AWS metadata service.
type MetadataService interface {
GetInstanceID() string
GetRegion() string
GetAvailabilityZone() string
}

type EC2Metadata interface {
Available() bool
GetInstanceIdentityDocument() (ec2metadata.EC2InstanceIdentityDocument, error)
}

type TaskMetadataService interface {
GetTMDSV4Response() ([]byte, error)
}
3 changes: 1 addition & 2 deletions pkg/cloud/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/golang/mock/gomock"
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/cloud/mocks"
)

var (
Expand Down Expand Up @@ -107,7 +106,7 @@ func TestGetEC2MetadataService(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockEC2Metadata := mocks.NewMockEC2Metadata(mockCtrl)
mockEC2Metadata := NewMockEC2Metadata(mockCtrl)

mockEC2Metadata.EXPECT().Available().Return(tc.isAvailable)
if tc.isAvailable {
Expand Down
Loading

0 comments on commit 6e2684e

Please sign in to comment.