Skip to content

Commit

Permalink
Merge pull request #2089 from ElijahQuinones/addUnitTests
Browse files Browse the repository at this point in the history
add more unit tests to increase test coverage
  • Loading branch information
k8s-ci-robot authored Aug 7, 2024
2 parents 40f2d17 + d683eac commit 18d2c19
Show file tree
Hide file tree
Showing 6 changed files with 367 additions and 14 deletions.
150 changes: 150 additions & 0 deletions pkg/cloud/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,47 @@ func extractVolumeIdentifiers(volumes []types.Volume) (volumeIDs []string, volum
}
return volumeIDs, volumeNames
}
func TestNewCloud(t *testing.T) {

testCases := []struct {
name string
region string
awsSdkDebugLog bool
userAgentExtra string
batchingEnabled bool
}{
{
name: "success: with awsSdkDebugLog, userAgentExtra, and batchingEnabled",
region: "us-east-1",
awsSdkDebugLog: true,
userAgentExtra: "example_user_agent_extra",
batchingEnabled: true,
},
{
name: "success: with only awsSdkDebugLog, and userAgentExtra",
region: "us-east-1",
awsSdkDebugLog: true,
userAgentExtra: "example_user_agent_extra",
},
{
name: "success: with only region",
region: "us-east-1",
},
}
for _, tc := range testCases {
ec2Cloud, err := NewCloud(tc.region, tc.awsSdkDebugLog, tc.userAgentExtra, tc.batchingEnabled)
if err != nil {
t.Fatalf("error %v", err)
}
ec2CloudAscloud := ec2Cloud.(*cloud)
assert.Equal(t, ec2CloudAscloud.region, tc.region)
if tc.batchingEnabled {
assert.NotNil(t, ec2CloudAscloud.bm)
} else {
assert.Nil(t, ec2CloudAscloud.bm)
}
}
}
func TestBatchDescribeVolumes(t *testing.T) {
testCases := []struct {
name string
Expand Down Expand Up @@ -553,6 +593,101 @@ func executeDescribeSnapshotsTest(t *testing.T, c *cloud, snapshotIDs, snapshotN
}
}

func TestCheckDesiredState(t *testing.T) {
testCases := []struct {
name string
volumeId string
desiredSizeGiB int32
options *ModifyDiskOptions
expErr error
}{
{
name: "sucess: normal path",
volumeId: "vol-001",
desiredSizeGiB: 5,
options: &ModifyDiskOptions{
VolumeType: VolumeTypeGP2,
IOPS: 3000,
Throughput: 1000,
},
},
{
name: "failure: volume is still being expanded",
volumeId: "vol-001",
desiredSizeGiB: 500,
options: &ModifyDiskOptions{
VolumeType: VolumeTypeGP2,
IOPS: 3000,
Throughput: 1000,
},
expErr: fmt.Errorf("volume \"vol-001\" is still being expanded to 500 size"),
},
{
name: "failure: volume is still being modified to iops",
volumeId: "vol-001",
desiredSizeGiB: 50,
options: &ModifyDiskOptions{
VolumeType: VolumeTypeGP2,
IOPS: 4000,
Throughput: 1000,
},
expErr: fmt.Errorf("volume \"vol-001\" is still being modified to iops 4000"),
},
{
name: "failure: volume is still being modifed to type",
volumeId: "vol-001",
desiredSizeGiB: 50,
options: &ModifyDiskOptions{
VolumeType: VolumeTypeGP3,
IOPS: 3000,
Throughput: 1000,
},
expErr: fmt.Errorf("volume \"vol-001\" is still being modified to type %q", VolumeTypeGP3),
},
{
name: "failure: volume is still being modified to throughput",
volumeId: "vol-001",
desiredSizeGiB: 5,
options: &ModifyDiskOptions{
VolumeType: VolumeTypeGP2,
IOPS: 3000,
Throughput: 2000,
},
expErr: fmt.Errorf("volume \"vol-001\" is still being modified to throughput 2000"),
},
}
for _, tc := range testCases {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockEC2 := NewMockEC2API(mockCtrl)
c := newCloud(mockEC2)
cloudInstance := c.(*cloud)
mockEC2.EXPECT().DescribeVolumes(gomock.Any(), gomock.Any()).Return(&ec2.DescribeVolumesOutput{
Volumes: []types.Volume{
{
VolumeId: aws.String("vol-001"),
Size: aws.Int32(50),
VolumeType: types.VolumeTypeGp2,
Iops: aws.Int32(3000),
Throughput: aws.Int32(1000),
},
},
}, nil)
_, err := cloudInstance.checkDesiredState(context.Background(), tc.volumeId, tc.desiredSizeGiB, tc.options)
if err != nil {
if tc.expErr == nil {
t.Fatalf("Did not expect to get an error but got %q", err)
} else if tc.expErr.Error() != err.Error() {
t.Fatalf("checkDesiredState() failed: expected error %q, got: %q", tc.expErr, err)
}
} else {
if tc.expErr != nil {
t.Fatalf("checkDesiredState() failed: expected error got nothing")
}
}
}
}

func TestBatchDescribeVolumesModifications(t *testing.T) {
testCases := []struct {
name string
Expand Down Expand Up @@ -1197,6 +1332,21 @@ func TestCreateDisk(t *testing.T) {
},
expErr: fmt.Errorf("CreateDisk: multi-attach is only supported for io2 volumes"),
},
{
name: "failure: invalid VolumeType",
volumeName: "vol-test-name",
diskOptions: &DiskOptions{
CapacityBytes: util.GiBToBytes(1),
Tags: map[string]string{VolumeNameTagKey: "vol-test", AwsEbsDriverTagKey: "true"},
VolumeType: "invalidVolumeType",
},
expDisk: &Disk{
VolumeID: "vol-test",
CapacityGiB: 1,
AvailabilityZone: defaultZone,
},
expErr: fmt.Errorf("invalid AWS VolumeType %q", "invalidVolumeType"),
},
}
for _, tc := range testCases {
tc := tc
Expand Down
93 changes: 92 additions & 1 deletion pkg/driver/controller_modify_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,97 @@ const (
invalidParameter = "invalid_parameter"
)

func TestMergeModifyVolumeRequest(t *testing.T) {
testCases := []struct {
name string
input modifyVolumeRequest
existing modifyVolumeRequest
expectedModifyVolumeRequest modifyVolumeRequest
expectError bool
}{
{
name: "Valid merge of size and iops",
input: modifyVolumeRequest{
newSize: 5,
},
existing: modifyVolumeRequest{
modifyDiskOptions: cloud.ModifyDiskOptions{
IOPS: validIopsInt,
},
},
expectedModifyVolumeRequest: modifyVolumeRequest{
newSize: 5,
modifyDiskOptions: cloud.ModifyDiskOptions{
IOPS: validIopsInt,
},
},
expectError: false,
},
{
name: "Different size requested by a previous request",
input: modifyVolumeRequest{
newSize: 4,
},
existing: modifyVolumeRequest{
newSize: 5,
},
expectedModifyVolumeRequest: modifyVolumeRequest{
newSize: 5,
},
expectError: true,
},
{
name: "Different IOPS requested by previous request",
input: modifyVolumeRequest{
modifyDiskOptions: cloud.ModifyDiskOptions{
IOPS: validIopsInt,
},
},
existing: modifyVolumeRequest{
modifyDiskOptions: cloud.ModifyDiskOptions{
IOPS: validIopsInt - 1,
},
},
expectedModifyVolumeRequest: modifyVolumeRequest{
modifyDiskOptions: cloud.ModifyDiskOptions{
IOPS: validIopsInt - 1,
},
},
expectError: true,
},
{
name: "Different Throughput requested by previous request",
input: modifyVolumeRequest{
modifyDiskOptions: cloud.ModifyDiskOptions{
Throughput: validThroughputInt,
},
},
existing: modifyVolumeRequest{
modifyDiskOptions: cloud.ModifyDiskOptions{
Throughput: validThroughputInt - 1,
},
},
expectedModifyVolumeRequest: modifyVolumeRequest{
modifyDiskOptions: cloud.ModifyDiskOptions{
Throughput: validThroughputInt - 1,
},
},
expectError: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := mergeModifyVolumeRequest(tc.input, tc.existing)
assert.Equal(t, tc.expectedModifyVolumeRequest, result)
if tc.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}

func TestParseModifyVolumeParameters(t *testing.T) {
testCases := []struct {
name string
Expand Down Expand Up @@ -90,7 +181,7 @@ func TestParseModifyVolumeParameters(t *testing.T) {
expectError: true,
},
{
name: "deprecated type",
name: "deprecated type but has validType",
params: map[string]string{
ModificationKeyVolumeType: validType,
DeprecatedModificationKeyVolumeType: "deprecated" + validType,
Expand Down
14 changes: 14 additions & 0 deletions pkg/driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,13 @@ func TestCreateVolumeWithFormattingParameters(t *testing.T) {
},
errExpected: false,
},
{
name: "failure with IOPSPerGBKey",
formattingOptionParameters: map[string]string{
IopsPerGBKey: "wrong_value",
},
errExpected: true,
},
{
name: "failure with block size",
formattingOptionParameters: map[string]string{
Expand Down Expand Up @@ -1789,6 +1796,13 @@ func TestCreateVolumeWithFormattingParameters(t *testing.T) {
},
errExpected: true,
},
{
name: "failure with Block Express on io1 volume",
formattingOptionParameters: map[string]string{
BlockExpressKey: "true",
},
errExpected: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
Loading

0 comments on commit 18d2c19

Please sign in to comment.