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

Refactor controller unit test with proper mock #269

Merged
merged 9 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from 7 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: 2 additions & 0 deletions hack/update-gomock
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ IMPORT_PATH=github.com/kubernetes-sigs/aws-ebs-csi-driver

mockgen -package=mocks -destination=./pkg/cloud/mocks/mock_ec2.go ${IMPORT_PATH}/pkg/cloud EC2
mockgen -package=mocks -destination=./pkg/cloud/mocks/mock_ec2metadata.go ${IMPORT_PATH}/pkg/cloud EC2Metadata
mockgen -package=mocks -destination=./pkg/driver/mocks/mock_cloud.go ${IMPORT_PATH}/pkg/cloud Cloud
mockgen -package=mocks -destination=./pkg/driver/mocks/mock_metadata_service.go ${IMPORT_PATH}/pkg/cloud MetadataService

8 changes: 4 additions & 4 deletions pkg/cloud/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,10 @@ func TestGetSnapshotByName(t *testing.T) {

func newCloud(mockEC2 EC2) Cloud {
return &cloud{
metadata: &metadata{
instanceID: "test-instance",
region: "test-region",
availabilityZone: defaultZone,
metadata: &Metadata{
InstanceID: "test-instance",
Region: "test-region",
AvailabilityZone: defaultZone,
},
dm: dm.NewDeviceManager(),
ec2: mockEC2,
Expand Down
30 changes: 15 additions & 15 deletions pkg/cloud/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,27 @@ type MetadataService interface {
GetAvailabilityZone() string
}

type metadata struct {
instanceID string
region string
availabilityZone string
type Metadata struct {
InstanceID string
Region string
AvailabilityZone string
}

var _ MetadataService = &metadata{}
var _ MetadataService = &Metadata{}

// GetInstanceID returns the instance identification.
func (m *metadata) GetInstanceID() string {
return m.instanceID
func (m *Metadata) GetInstanceID() string {
return m.InstanceID
}

// GetRegion returns the region which the instance is in.
func (m *metadata) GetRegion() string {
return m.region
func (m *Metadata) GetRegion() string {
return m.Region
}

// GetAvailabilityZone returns the Availability Zone which the instance is in.
func (m *metadata) GetAvailabilityZone() string {
return m.availabilityZone
func (m *Metadata) GetAvailabilityZone() string {
return m.AvailabilityZone
}

// NewMetadataService returns a new MetadataServiceImplementation.
Expand All @@ -80,9 +80,9 @@ func NewMetadataService(svc EC2Metadata) (MetadataService, error) {
return nil, fmt.Errorf("could not get valid EC2 availavility zone")
}

return &metadata{
instanceID: doc.InstanceID,
region: doc.Region,
availabilityZone: doc.AvailabilityZone,
return &Metadata{
InstanceID: doc.InstanceID,
Region: doc.Region,
AvailabilityZone: doc.AvailabilityZone,
}, nil
}
210 changes: 125 additions & 85 deletions pkg/cloud/mocks/mock_ec2.go

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

Loading