@@ -28,6 +28,7 @@ import (
28
28
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
29
29
"github.com/google/go-cmp/cmp"
30
30
"github.com/googleapis/gax-go/v2/apierror"
31
+ computev1 "google.golang.org/api/compute/v1"
31
32
"google.golang.org/api/googleapi"
32
33
"google.golang.org/grpc/codes"
33
34
"google.golang.org/grpc/status"
@@ -36,6 +37,7 @@ import (
36
37
const (
37
38
volIDZoneFmt = "projects/%s/zones/%s/disks/%s"
38
39
volIDRegionFmt = "projects/%s/regions/%s/disks/%s"
40
+ testDiskName = "test-disk"
39
41
)
40
42
41
43
func TestBytesToGbRoundDown (t * testing.T ) {
@@ -1954,3 +1956,196 @@ func TestNewCombinedError(t *testing.T) {
1954
1956
})
1955
1957
}
1956
1958
}
1959
+ func TestIsUpdateIopsThroughputValuesAllowed (t * testing.T ) {
1960
+ testcases := []struct {
1961
+ name string
1962
+ diskType string
1963
+ expectResult bool
1964
+ }{
1965
+ {
1966
+ name : "Hyperdisk returns true" ,
1967
+ diskType : "hyperdisk-balanced" ,
1968
+ expectResult : true ,
1969
+ },
1970
+ {
1971
+ name : "PD disk returns true" ,
1972
+ diskType : "pd-ssd" ,
1973
+ expectResult : false ,
1974
+ },
1975
+ {
1976
+ name : "Unknown disk type" ,
1977
+ diskType : "not-a-disk-type-we-know" ,
1978
+ expectResult : false ,
1979
+ },
1980
+ }
1981
+ for _ , tc := range testcases {
1982
+ t .Run (tc .name , func (t * testing.T ) {
1983
+ disk := & computev1.Disk {
1984
+ Name : "test-disk" ,
1985
+ Type : tc .diskType ,
1986
+ }
1987
+ gotResult := IsUpdateIopsThroughputValuesAllowed (disk )
1988
+ if gotResult != tc .expectResult {
1989
+ t .Errorf ("IsUpdateIopsThroughputValuesAllowed: got %v, want %v" , gotResult , tc .expectResult )
1990
+ }
1991
+ })
1992
+ }
1993
+ }
1994
+
1995
+ func TestGetMinIopsThroughput (t * testing.T ) {
1996
+ testcases := []struct {
1997
+ name string
1998
+ existingDisk * computev1.Disk
1999
+ reqGb int64
2000
+ expectResult bool
2001
+ expectMinIops int64
2002
+ expectMinThroughput int64
2003
+ }{
2004
+ {
2005
+ name : "Hyperdisk Balanced 4 GiB to 5GiB" ,
2006
+ existingDisk : & computev1.Disk {
2007
+ Name : testDiskName ,
2008
+ Type : "hyperdisk-balanced" ,
2009
+ ProvisionedIops : 2000 ,
2010
+ ProvisionedThroughput : 140 ,
2011
+ SizeGb : 4 ,
2012
+ },
2013
+ reqGb : 5 ,
2014
+ expectResult : true ,
2015
+ expectMinIops : 2500 ,
2016
+ expectMinThroughput : 0 , // 0 indicates no change to throughput
2017
+ },
2018
+ {
2019
+ name : "Hyperdisk Balanced 5 GiB to 6GiB" ,
2020
+ existingDisk : & computev1.Disk {
2021
+ Name : testDiskName ,
2022
+ Type : "hyperdisk-balanced" ,
2023
+ ProvisionedIops : 2500 ,
2024
+ ProvisionedThroughput : 145 ,
2025
+ SizeGb : 5 ,
2026
+ },
2027
+ reqGb : 6 ,
2028
+ expectResult : true ,
2029
+ expectMinIops : 3000 ,
2030
+ expectMinThroughput : 0 , // 0 indicates no change to throughput
2031
+ },
2032
+ {
2033
+ name : "Hyperdisk Balanced 6 GiB to 10GiB - no adjustment" ,
2034
+ existingDisk : & computev1.Disk {
2035
+ Name : testDiskName ,
2036
+ Type : "hyperdisk-balanced" ,
2037
+ ProvisionedIops : 3000 ,
2038
+ ProvisionedThroughput : 145 ,
2039
+ SizeGb : 6 ,
2040
+ },
2041
+ reqGb : 10 ,
2042
+ expectResult : false ,
2043
+ expectMinIops : 0 , // 0 indicates no change to iops
2044
+ expectMinThroughput : 0 , // 0 indicates no change to throughput
2045
+ },
2046
+ {
2047
+ name : "Hyperdisk Extreme with min IOPS value as 2 will adjust IOPs" ,
2048
+ existingDisk : & computev1.Disk {
2049
+ Name : testDiskName ,
2050
+ Type : "hyperdisk-extreme" ,
2051
+ ProvisionedIops : 128 ,
2052
+ SizeGb : 64 ,
2053
+ },
2054
+ reqGb : 65 ,
2055
+ expectResult : true ,
2056
+ expectMinIops : 130 ,
2057
+ expectMinThroughput : 0 , // 0 indicates no change to throughput
2058
+ },
2059
+ {
2060
+ name : "Hyperdisk Extreme 64GiB to 70 GiB - no adjustment" ,
2061
+ existingDisk : & computev1.Disk {
2062
+ Name : testDiskName ,
2063
+ Type : "hyperdisk-extreme" ,
2064
+ ProvisionedIops : 3000 ,
2065
+ SizeGb : 64 ,
2066
+ },
2067
+ reqGb : 70 ,
2068
+ expectResult : false ,
2069
+ expectMinIops : 0 , // 0 indicates no change to iops
2070
+ expectMinThroughput : 0 , // 0 indicates no change to throughput
2071
+ },
2072
+ {
2073
+ name : "Hyperdisk ML with min throughput per GB will adjust throughput" ,
2074
+ existingDisk : & computev1.Disk {
2075
+ Name : testDiskName ,
2076
+ Type : "hyperdisk-ml" ,
2077
+ ProvisionedThroughput : 400 ,
2078
+ SizeGb : 3334 ,
2079
+ },
2080
+ reqGb : 3400 ,
2081
+ expectResult : true ,
2082
+ expectMinThroughput : 408 ,
2083
+ },
2084
+ {
2085
+ name : "Hyperdisk ML 64GiB to 100 GiB - no adjustment" ,
2086
+ existingDisk : & computev1.Disk {
2087
+ Name : testDiskName ,
2088
+ Type : "hyperdisk-ml" ,
2089
+ ProvisionedThroughput : 6400 ,
2090
+ SizeGb : 64 ,
2091
+ },
2092
+ reqGb : 100 ,
2093
+ expectResult : false ,
2094
+ expectMinIops : 0 , // 0 indicates no change to iops
2095
+ expectMinThroughput : 0 , // 0 indicates no change to throughput
2096
+ },
2097
+ {
2098
+ name : "Hyperdisk throughput with min throughput per GB will adjust throughput" ,
2099
+ existingDisk : & computev1.Disk {
2100
+ Name : testDiskName ,
2101
+ Type : "hyperdisk-throughput" ,
2102
+ ProvisionedThroughput : 20 ,
2103
+ SizeGb : 2048 ,
2104
+ },
2105
+ reqGb : 3072 ,
2106
+ expectResult : true ,
2107
+ expectMinIops : 0 ,
2108
+ expectMinThroughput : 30 ,
2109
+ },
2110
+ {
2111
+ name : "Hyperdisk throughput 2TiB to 4TiB - no adjustment" ,
2112
+ existingDisk : & computev1.Disk {
2113
+ Name : testDiskName ,
2114
+ Type : "hyperdisk-throughput" ,
2115
+ ProvisionedThroughput : 567 ,
2116
+ SizeGb : 2048 ,
2117
+ },
2118
+ reqGb : 4096 ,
2119
+ expectResult : false ,
2120
+ expectMinIops : 0 , // 0 indicates no change to iops
2121
+ expectMinThroughput : 0 , // 0 indicates no change to throughput
2122
+ },
2123
+ {
2124
+ name : "Unknown disk type, no need to update" ,
2125
+ existingDisk : & computev1.Disk {
2126
+ Name : testDiskName ,
2127
+ Type : "unknown-type" ,
2128
+ },
2129
+ reqGb : 5 ,
2130
+ expectResult : false ,
2131
+ expectMinIops : 0 ,
2132
+ expectMinThroughput : 0 , // 0 indicates no change to throughput
2133
+ },
2134
+ }
2135
+ for _ , tc := range testcases {
2136
+ t .Run (tc .name , func (t * testing.T ) {
2137
+ gotNeeded , gotMinIops , gotMinThroughput := GetMinIopsThroughput (tc .existingDisk , tc .reqGb )
2138
+ if gotNeeded != tc .expectResult {
2139
+ t .Errorf ("GetMinIopsThroughput: got %v, want %v" , gotNeeded , tc .expectResult )
2140
+ }
2141
+
2142
+ if gotMinIops != tc .expectMinIops {
2143
+ t .Errorf ("GetMinIopsThroughput Iops: got %v, want %v" , gotMinIops , tc .expectMinIops )
2144
+ }
2145
+
2146
+ if gotMinThroughput != tc .expectMinThroughput {
2147
+ t .Errorf ("GetMinIopsThroughput Throughput: got %v, want %v" , gotMinThroughput , tc .expectMinThroughput )
2148
+ }
2149
+ })
2150
+ }
2151
+ }
0 commit comments