Skip to content

Commit

Permalink
Add unit tests for invalid batch describe requests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSirenko committed Apr 2, 2024
1 parent c3a8d30 commit 9f64d48
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
13 changes: 8 additions & 5 deletions pkg/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ var (

// ErrInvalidArgument is returned if parameters were rejected by cloud provider
ErrInvalidArgument = errors.New("invalid argument")

// ErrInvalidRequest is returned if parameters were rejected by driver
ErrInvalidRequest = errors.New("invalid request")
)

// Set during build time via -ldflags
Expand Down Expand Up @@ -432,7 +435,7 @@ func (c *cloud) batchDescribeVolumes(request *ec2.DescribeVolumesInput) (*types.
task = request.Filters[0].Values[0]

default:
return nil, fmt.Errorf("batchDescribeVolumes: invalid request, request: %v", request)
return nil, fmt.Errorf("%w: batchDescribeVolumes: request: %v", ErrInvalidRequest, request)
}

ch := make(chan batcher.BatchResult[*types.Volume])
Expand Down Expand Up @@ -675,7 +678,7 @@ func (c *cloud) batchDescribeVolumesModifications(request *ec2.DescribeVolumesMo
if len(request.VolumeIds) == 1 && request.VolumeIds[0] != "" {
task = request.VolumeIds[0]
} else {
return nil, fmt.Errorf("batchDescribeVolumesModifications: invalid request, request: %v", request)
return nil, fmt.Errorf("%w: batchDescribeVolumesModifications: invalid request, request: %v", ErrInvalidRequest, request)
}

ch := make(chan batcher.BatchResult[*types.VolumeModification])
Expand All @@ -691,7 +694,7 @@ func (c *cloud) batchDescribeVolumesModifications(request *ec2.DescribeVolumesMo
return r.Result, nil
}

// ResizeOrModifyDisk resizes an EBS volume in GiB increments, rouding up to the next possible allocatable unit, and/or modifies an EBS
// ResizeOrModifyDisk resizes an EBS volume in GiB increments, rounding up to the next possible allocatable unit, and/or modifies an EBS
// volume with the parameters in ModifyDiskOptions.
// The resizing operation is performed only when newSizeBytes != 0.
// It returns the volume size after this call or an error if the size couldn't be determined or the volume couldn't be modified.
Expand Down Expand Up @@ -795,7 +798,7 @@ func (c *cloud) batchDescribeInstances(request *ec2.DescribeInstancesInput) (*ty
if len(request.InstanceIds) == 1 && request.InstanceIds[0] != "" {
task = request.InstanceIds[0]
} else {
return nil, fmt.Errorf("batchDescribeInstances: invalid request, request: %v", request)
return nil, fmt.Errorf("%w: batchDescribeInstances: request: %v", ErrInvalidRequest, request)
}

ch := make(chan batcher.BatchResult[*types.Instance])
Expand Down Expand Up @@ -1161,7 +1164,7 @@ func (c *cloud) batchDescribeSnapshots(request *ec2.DescribeSnapshotsInput) (*ty
task = request.Filters[0].Values[0]

default:
return nil, fmt.Errorf("batchDescribeSnapshots: invalid request, request: %v", request)
return nil, fmt.Errorf("%w: batchDescribeSnapshots: request: %v", ErrInvalidRequest, request)
}

ch := make(chan batcher.BatchResult[*types.Snapshot])
Expand Down
35 changes: 33 additions & 2 deletions pkg/cloud/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestBatchDescribeVolumes(t *testing.T) {
expErr: fmt.Errorf("volume not found"),
},
{
name: "TestBatchDescribeVolumes: invalid tag",
name: "fail: invalid tag",
volumes: []types.Volume{
{
Tags: []types.Tag{
Expand All @@ -158,12 +158,19 @@ func TestBatchDescribeVolumes(t *testing.T) {
},
},
mockFunc: func(mockEC2 *MockEC2API, expErr error, volumes []types.Volume) {

volumeOutput := &ec2.DescribeVolumesOutput{Volumes: volumes}
mockEC2.EXPECT().DescribeVolumes(gomock.Any(), gomock.Any()).Return(volumeOutput, expErr).Times(0)
},
expErr: fmt.Errorf("invalid tag"),
},
{
name: "fail: invalid request",
volumes: []types.Volume{{VolumeId: aws.String("")}},
mockFunc: func(mockEC2 *MockEC2API, expErr error, volumes []types.Volume) {
mockEC2.EXPECT().DescribeVolumes(gomock.Any(), gomock.Any()).Return(nil, nil).Times(0)
},
expErr: ErrInvalidRequest,
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -273,6 +280,14 @@ func TestBatchDescribeInstances(t *testing.T) {
},
expErr: fmt.Errorf("generic EC2 API error"),
},
{
name: "fail: invalid request",
instanceIds: []string{""},
mockFunc: func(mockEC2 *MockEC2API, expErr error, reservations []types.Reservation) {
mockEC2.EXPECT().DescribeInstances(gomock.Any(), gomock.Any()).Return(nil, nil).Times(0)
},
expErr: ErrInvalidRequest,
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -440,6 +455,14 @@ func TestBatchDescribeSnapshots(t *testing.T) {
},
expErr: ErrNotFound,
},
{
name: "fail: invalid request",
snapshots: []types.Snapshot{{SnapshotId: aws.String("")}},
mockFunc: func(mockEC2 *MockEC2API, expErr error, snapshots []types.Snapshot) {
mockEC2.EXPECT().DescribeSnapshots(gomock.Any(), gomock.Any()).Return(nil, nil).Times(0)
},
expErr: ErrInvalidRequest,
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -551,6 +574,14 @@ func TestBatchDescribeVolumesModifications(t *testing.T) {
},
expErr: fmt.Errorf("generic EC2 API error"),
},
{
name: "fail: invalid request",
volumeIds: []string{""},
mockFunc: func(mockEC2 *MockEC2API, expErr error, volumeModifications []types.VolumeModification) {
mockEC2.EXPECT().DescribeVolumesModifications(gomock.Any(), gomock.Any()).Return(nil, expErr).Times(0)
},
expErr: ErrInvalidRequest,
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 9f64d48

Please sign in to comment.