Skip to content

Commit

Permalink
Merge pull request #269 from zacharya/master
Browse files Browse the repository at this point in the history
Refactor controller unit test with proper mock
  • Loading branch information
k8s-ci-robot authored Apr 8, 2019
2 parents 4896db5 + ba5c1c7 commit c67ab06
Show file tree
Hide file tree
Showing 14 changed files with 1,783 additions and 700 deletions.
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

0 comments on commit c67ab06

Please sign in to comment.