Repository: kubernetes-sigs/gcp-compute-persistent-disk-csi-driver
Pull request #1131 (2023-02-14 00:44:12)

Add more unit tests for hyperdisk feature

**What type of PR is this?**

/kind cleanup

**What this PR does / why we need it**:
Add more tests to clarify use case, clean up test format.

**Which issue(s) this PR fixes**:
Fixes #

**Special notes for your reviewer**:

**Does this PR introduce a user-facing change?**:
```release-note
NONE
```


k8s-ci-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubernetes-sigs/gcp-compute-persistent-disk-csi-driver/pull/1131#" title="Author self-approved">sunnylovestiramisu</a>*
**Once this PR has been reviewed and has the lgtm label**, please assign saikat-royc for approval. For more information see [the Kubernetes Code Review Process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process).

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Fgcp-compute-persistent-disk-csi-driver).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/kubernetes-sigs/gcp-compute-persistent-disk-csi-driver/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["saikat-royc"]} -->
k8s-ci-robot: @sunnylovestiramisu: The following test **failed**, say `/retest` to rerun all failed tests or `/retest-required` to rerun all mandatory failed tests:

Test name | Commit | Details | Required | Rerun command
--- | --- | --- | --- | ---
pull-gcp-compute-persistent-disk-csi-driver-unit | 7717caa4316ec644098fccef2d083bee500f4bcd | [link](https://prow.k8s.io/view/gs/kubernetes-jenkins/pr-logs/pull/sigs.k8s.io_gcp-compute-persistent-disk-csi-driver/1131/pull-gcp-compute-persistent-disk-csi-driver-unit/1625294810153750528/) | true | `/test pull-gcp-compute-persistent-disk-csi-driver-unit`

[Full PR test history](https://prow.k8s.io/pr-history?org=kubernetes-sigs&repo=gcp-compute-persistent-disk-csi-driver&pr=1131). [Your PR dashboard](https://prow.k8s.io/pr?query=is%3Apr%20state%3Aopen%20author%3Asunnylovestiramisu). Please help us cut down on flakes by [linking to](https://git.k8s.io/community/contributors/devel/sig-testing/flaky-tests.md#filing-issues-for-flaky-tests) an [open issue](https://github.com/kubernetes-sigs/gcp-compute-persistent-disk-csi-driver/issues?q=is:issue+is:open) when you hit one in your PR.

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
sunnylovestiramisu: /retest
k8s-ci-robot: @sunnylovestiramisu: The following test **failed**, say `/retest` to rerun all failed tests or `/retest-required` to rerun all mandatory failed tests:

Test name | Commit | Details | Required | Rerun command
--- | --- | --- | --- | ---
pull-gcp-compute-persistent-disk-csi-driver-unit | 2d251eaaba4ca16d49b8e796b7bd0672d84c0952 | [link](https://prow.k8s.io/view/gs/kubernetes-jenkins/pr-logs/pull/sigs.k8s.io_gcp-compute-persistent-disk-csi-driver/1131/pull-gcp-compute-persistent-disk-csi-driver-unit/1631442903563767808/) | true | `/test pull-gcp-compute-persistent-disk-csi-driver-unit`

[Full PR test history](https://prow.k8s.io/pr-history?org=kubernetes-sigs&repo=gcp-compute-persistent-disk-csi-driver&pr=1131). [Your PR dashboard](https://prow.k8s.io/pr?query=is%3Apr%20state%3Aopen%20author%3Asunnylovestiramisu). Please help us cut down on flakes by [linking to](https://git.k8s.io/community/contributors/devel/sig-testing/flaky-tests.md#filing-issues-for-flaky-tests) an [open issue](https://github.com/kubernetes-sigs/gcp-compute-persistent-disk-csi-driver/issues?q=is:issue+is:open) when you hit one in your PR.

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
sunnylovestiramisu: /retest
leiyiz: also consider change the name from `ConvertMiBStringToInt64(str string)` to `ConvertMiStringToInt64(str string)`?
sunnylovestiramisu: > also consider change the name from `ConvertMiBStringToInt64(str string)` to `ConvertMiStringToInt64(str string)`?

That is a good suggestion, will change to that now

Patch:
diff --git a/pkg/common/parameters.go b/pkg/common/parameters.go
index 4137fe3d..411b28ab 100644
--- a/pkg/common/parameters.go
+++ b/pkg/common/parameters.go
@@ -158,7 +158,7 @@ func ExtractAndDefaultParameters(parameters map[string]string, driverName string
 			}
 			p.ProvisionedIOPSOnCreate = paramProvisionedIOPSOnCreate
 		case ParameterKeyProvisionedThroughputOnCreate:
-			paramProvisionedThroughputOnCreate, err := ConvertMiBStringToInt64(v)
+			paramProvisionedThroughputOnCreate, err := ConvertMiStringToInt64(v)
 			if err != nil {
 				return p, fmt.Errorf("parameters contain invalid provisionedThroughputOnCreate parameter: %w", err)
 			}
diff --git a/pkg/common/utils.go b/pkg/common/utils.go
index 86cc3f9d..5e0486ae 100644
--- a/pkg/common/utils.go
+++ b/pkg/common/utils.go
@@ -260,8 +260,8 @@ func ConvertStringToInt64(str string) (int64, error) {
 	return volumehelpers.RoundUpToB(quantity)
 }
 
-// ConvertMiBStringToInt64 converts a GiB string to int64
-func ConvertMiBStringToInt64(str string) (int64, error) {
+// ConvertMiStringToInt64 converts a GiB string to int64
+func ConvertMiStringToInt64(str string) (int64, error) {
 	quantity, err := resource.ParseQuantity(str)
 	if err != nil {
 		return -1, err


Test patch:
diff --git a/pkg/common/utils_test.go b/pkg/common/utils_test.go
index b08aee54..b6b264eb 100644
--- a/pkg/common/utils_test.go
+++ b/pkg/common/utils_test.go
@@ -612,13 +612,13 @@ func TestConvertStringToInt64(t *testing.T) {
 		{
 			desc:        "invalid empty string",
 			inputStr:    "",
-			expInt64:    10000,
+			expInt64:    0,
 			expectError: true,
 		},
 		{
 			desc:        "invalid string",
 			inputStr:    "ew%65",
-			expInt64:    10000,
+			expInt64:    0,
 			expectError: true,
 		},
 		{
@@ -630,7 +630,7 @@ func TestConvertStringToInt64(t *testing.T) {
 		{
 			desc:        "invalid GB string",
 			inputStr:    "10GB",
-			expInt64:    10000,
+			expInt64:    0,
 			expectError: true,
 		},
 		{
@@ -698,7 +698,7 @@ func TestConvertStringToInt64(t *testing.T) {
 	}
 }
 
-func TestConvertMiBStringToInt64(t *testing.T) {
+func TestConvertMiStringToInt64(t *testing.T) {
 	tests := []struct {
 		desc        string
 		inputStr    string
@@ -706,75 +706,87 @@ func TestConvertMiBStringToInt64(t *testing.T) {
 		expectError bool
 	}{
 		{
-			"valid number string",
-			"10000",
-			1,
-			false,
+			desc:        "valid number string",
+			inputStr:    "10000",
+			expInt64:    1,
+			expectError: false,
 		},
 		{
-			"round Ki to MiB",
-			"1000Ki",
-			1,
-			false,
+			desc:        "round Ki to MiB",
+			inputStr:    "1000Ki",
+			expInt64:    1,
+			expectError: false,
 		},
 		{
-			"round k to MiB",
-			"1000k",
-			1,
-			false,
+			desc:        "round k to MiB",
+			inputStr:    "1000k",
+			expInt64:    1,
+			expectError: false,
 		},
 		{
-			"round Mi to MiB",
-			"1000Mi",
-			1000,
-			false,
+			desc:        "round Mi to MiB",
+			inputStr:    "1000Mi",
+			expInt64:    1000,
+			expectError: false,
 		},
 		{
-			"round M to MiB",
-			"1000M",
-			954,
-			false,
+			desc:        "round M to MiB",
+			inputStr:    "1000M",
+			expInt64:    954,
+			expectError: false,
 		},
 		{
-			"round G to MiB",
-			"1000G",
-			953675,
-			false,
+			desc:        "round G to MiB",
+			inputStr:    "1000G",
+			expInt64:    953675,
+			expectError: false,
 		},
 		{
-			"round Gi to MiB",
-			"10000Gi",
-			10240000,
-			false,
+			desc:        "round Gi to MiB",
+			inputStr:    "10000Gi",
+			expInt64:    10240000,
+			expectError: false,
 		},
 		{
-			"round decimal to MiB",
-			"1.2Gi",
-			1229,
-			false,
+			desc:        "round decimal to MiB",
+			inputStr:    "1.2Gi",
+			expInt64:    1229,
+			expectError: false,
 		},
 		{
-			"round big value to MiB",
-			"8191Pi",
-			8795019280384,
-			false,
+			desc:        "round big value to MiB",
+			inputStr:    "8191Pi",
+			expInt64:    8795019280384,
+			expectError: false,
 		},
 		{
-			"invalid empty string",
-			"",
-			10000,
-			true,
+			desc:        "invalid empty string",
+			inputStr:    "",
+			expInt64:    0,
+			expectError: true,
 		},
 		{
-			"invalid string",
-			"ew%65",
-			10000,
-			true,
+			desc:        "invalid KiB string",
+			inputStr:    "10KiB",
+			expInt64:    10000,
+			expectError: true,
+		},
+		{
+			desc:        "invalid GB string",
+			inputStr:    "10GB",
+			expInt64:    0,
+			expectError: true,
+		},
+		{
+			desc:        "invalid string",
+			inputStr:    "ew%65",
+			expInt64:    0,
+			expectError: true,
 		},
 	}
 	for _, tc := range tests {
 		t.Run(tc.desc, func(t *testing.T) {
-			actualInt64, err := ConvertMiBStringToInt64(tc.inputStr)
+			actualInt64, err := ConvertMiStringToInt64(tc.inputStr)
 			if err != nil && !tc.expectError {
 				t.Errorf("Got error %v converting string to int64 %s; expect no error", err, tc.inputStr)
 			}