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

Move mocks to parent package to avoid import cycle #1078

Merged
merged 2 commits into from
Oct 5, 2021
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
8 changes: 4 additions & 4 deletions hack/update-gomock
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

set -euo pipefail

./bin/mockgen -package mocks -destination=./pkg/cloud/mocks/mock_ec2.go -source pkg/cloud/ec2_interface.go
./bin/mockgen -package mocks -destination=./pkg/driver/mocks/mock_cloud.go -source pkg/cloud/cloud_interface.go
./bin/mockgen -package mocks -destination=./pkg/driver/mocks/mock_metadata.go -source pkg/cloud/metadata_interface.go
./bin/mockgen -package mocks -destination=./pkg/driver/mocks/mock_mount.go -source pkg/driver/mount.go
./bin/mockgen -package cloud -destination=./pkg/cloud/mock_ec2.go -source pkg/cloud/ec2_interface.go
./bin/mockgen -package cloud -destination=./pkg/cloud/mock_cloud.go -source pkg/cloud/cloud_interface.go
./bin/mockgen -package cloud -destination=./pkg/cloud/mock_metadata.go -source pkg/cloud/metadata_interface.go
./bin/mockgen -package driver -destination=./pkg/driver/mock_mount.go -source pkg/driver/mount.go
35 changes: 17 additions & 18 deletions pkg/cloud/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/golang/mock/gomock"
dm "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud/devicemanager"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud/mocks"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/util"
)

Expand Down Expand Up @@ -426,7 +425,7 @@ func TestCreateDisk(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockEC2 := mocks.NewMockEC2(mockCtrl)
mockEC2 := NewMockEC2(mockCtrl)
c := newCloud(mockEC2)

volState := tc.volState
Expand Down Expand Up @@ -528,7 +527,7 @@ func TestDeleteDisk(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockEC2 := mocks.NewMockEC2(mockCtrl)
mockEC2 := NewMockEC2(mockCtrl)
c := newCloud(mockEC2)

ctx := context.Background()
Expand Down Expand Up @@ -582,7 +581,7 @@ func TestAttachDisk(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockEC2 := mocks.NewMockEC2(mockCtrl)
mockEC2 := NewMockEC2(mockCtrl)
c := newCloud(mockEC2)

vol := &ec2.Volume{
Expand Down Expand Up @@ -644,7 +643,7 @@ func TestDetachDisk(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockEC2 := mocks.NewMockEC2(mockCtrl)
mockEC2 := NewMockEC2(mockCtrl)
c := newCloud(mockEC2)

vol := &ec2.Volume{
Expand Down Expand Up @@ -713,7 +712,7 @@ func TestGetDiskByName(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockEC2 := mocks.NewMockEC2(mockCtrl)
mockEC2 := NewMockEC2(mockCtrl)
c := newCloud(mockEC2)

vol := &ec2.Volume{
Expand Down Expand Up @@ -795,7 +794,7 @@ func TestGetDiskByID(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockEC2 := mocks.NewMockEC2(mockCtrl)
mockEC2 := NewMockEC2(mockCtrl)
c := newCloud(mockEC2)

ctx := context.Background()
Expand Down Expand Up @@ -894,7 +893,7 @@ func TestCreateSnapshot(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockEC2 := mocks.NewMockEC2(mockCtrl)
mockEC2 := NewMockEC2(mockCtrl)
c := newCloud(mockEC2)

ec2snapshot := &ec2.Snapshot{
Expand Down Expand Up @@ -953,7 +952,7 @@ func TestDeleteSnapshot(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockEC2 := mocks.NewMockEC2(mockCtrl)
mockEC2 := NewMockEC2(mockCtrl)
c := newCloud(mockEC2)

ctx := context.Background()
Expand Down Expand Up @@ -1084,7 +1083,7 @@ func TestResizeDisk(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockEC2 := mocks.NewMockEC2(mockCtrl)
mockEC2 := NewMockEC2(mockCtrl)
// reduce number of steps to reduce test time
volumeModificationWaitSteps = 3
c := newCloud(mockEC2)
Expand Down Expand Up @@ -1170,7 +1169,7 @@ func TestGetSnapshotByName(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockEC2 := mocks.NewMockEC2(mockCtrl)
mockEC2 := NewMockEC2(mockCtrl)
c := newCloud(mockEC2)

ec2snapshot := &ec2.Snapshot{
Expand Down Expand Up @@ -1226,7 +1225,7 @@ func TestGetSnapshotByID(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockEC2 := mocks.NewMockEC2(mockCtrl)
mockEC2 := NewMockEC2(mockCtrl)
c := newCloud(mockEC2)

ec2snapshot := &ec2.Snapshot{
Expand Down Expand Up @@ -1286,7 +1285,7 @@ func TestListSnapshots(t *testing.T) {

mockCtl := gomock.NewController(t)
defer mockCtl.Finish()
mockEC2 := mocks.NewMockEC2(mockCtl)
mockEC2 := NewMockEC2(mockCtl)
c := newCloud(mockEC2)

ctx := context.Background()
Expand Down Expand Up @@ -1328,7 +1327,7 @@ func TestListSnapshots(t *testing.T) {

mockCtl := gomock.NewController(t)
defer mockCtl.Finish()
mockEC2 := mocks.NewMockEC2(mockCtl)
mockEC2 := NewMockEC2(mockCtl)
c := newCloud(mockEC2)

ctx := context.Background()
Expand Down Expand Up @@ -1375,7 +1374,7 @@ func TestListSnapshots(t *testing.T) {

mockCtl := gomock.NewController(t)
defer mockCtl.Finish()
mockEC2 := mocks.NewMockEC2(mockCtl)
mockEC2 := NewMockEC2(mockCtl)
c := newCloud(mockEC2)

ctx := context.Background()
Expand Down Expand Up @@ -1424,7 +1423,7 @@ func TestListSnapshots(t *testing.T) {
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()
mockEC2 := mocks.NewMockEC2(mockCtl)
mockEC2 := NewMockEC2(mockCtl)
c := newCloud(mockEC2)

ctx := context.Background()
Expand All @@ -1441,7 +1440,7 @@ func TestListSnapshots(t *testing.T) {
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()
mockEC2 := mocks.NewMockEC2(mockCtl)
mockEC2 := NewMockEC2(mockCtl)
c := newCloud(mockEC2)

ctx := context.Background()
Expand Down Expand Up @@ -1562,7 +1561,7 @@ func TestWaitForAttachmentState(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockEC2 := mocks.NewMockEC2(mockCtrl)
mockEC2 := NewMockEC2(mockCtrl)
c := newCloud(mockEC2)

attachedVol := &ec2.Volume{
Expand Down
8 changes: 0 additions & 8 deletions pkg/cloud/ec2_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cloud

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/ec2"
)
Expand All @@ -39,10 +38,3 @@ type EC2 interface {
DescribeVolumesModificationsWithContext(ctx aws.Context, input *ec2.DescribeVolumesModificationsInput, opts ...request.Option) (*ec2.DescribeVolumesModificationsOutput, error)
DescribeAvailabilityZonesWithContext(ctx aws.Context, input *ec2.DescribeAvailabilityZonesInput, opts ...request.Option) (*ec2.DescribeAvailabilityZonesOutput, error)
}

type EC2Metadata interface {
Available() bool
// ec2 instance metadata endpoints: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
GetMetadata(string) (string, error)
GetInstanceIdentityDocument() (ec2metadata.EC2InstanceIdentityDocument, error)
}
12 changes: 11 additions & 1 deletion pkg/cloud/metadata_interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cloud

import "github.com/aws/aws-sdk-go/aws/arn"
import (
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
)

// MetadataService represents AWS metadata service.
type MetadataService interface {
Expand All @@ -10,3 +13,10 @@ type MetadataService interface {
GetAvailabilityZone() string
GetOutpostArn() arn.ARN
}

type EC2Metadata interface {
Available() bool
// ec2 instance metadata endpoints: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
GetMetadata(string) (string, error)
GetInstanceIdentityDocument() (ec2metadata.EC2InstanceIdentityDocument, error)
}
3 changes: 1 addition & 2 deletions pkg/cloud/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/golang/mock/gomock"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud/mocks"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -285,7 +284,7 @@ func TestNewMetadataService(t *testing.T) {
}

mockCtrl := gomock.NewController(t)
mockEC2Metadata := mocks.NewMockEC2Metadata(mockCtrl)
mockEC2Metadata := NewMockEC2Metadata(mockCtrl)

ec2MetadataClient := func() (EC2Metadata, error) { return mockEC2Metadata, nil }
k8sAPIClient := func() (kubernetes.Interface, error) { clientsetInitialized = true; return clientset, nil }
Expand Down
33 changes: 16 additions & 17 deletions pkg/driver/mocks/mock_cloud.go → pkg/cloud/mock_cloud.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading