@@ -25,6 +25,7 @@ import (
25
25
"testing"
26
26
27
27
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
28
+ "github.com/google/go-cmp/cmp"
28
29
"google.golang.org/api/googleapi"
29
30
"google.golang.org/grpc/codes"
30
31
"google.golang.org/grpc/status"
@@ -1101,3 +1102,214 @@ func TestIsValidDiskEncryptionKmsKey(t *testing.T) {
1101
1102
}
1102
1103
}
1103
1104
}
1105
+
1106
+ func TestFieldsFromResourceName (t * testing.T ) {
1107
+ testcases := []struct {
1108
+ name string
1109
+ resourceName string
1110
+ expectedProject string
1111
+ expectedZone string
1112
+ expectedName string
1113
+ expectedErr bool
1114
+ }{
1115
+ {
1116
+ name : "StoragePool_WithValidResourceName_ReturnsFields" ,
1117
+ resourceName : "projects/my-project/zones/us-central1-a/storagePools/storagePool-1" ,
1118
+ expectedProject : "my-project" ,
1119
+ expectedZone : "us-central1-a" ,
1120
+ expectedName : "storagePool-1" ,
1121
+ },
1122
+ {
1123
+ name : "StoragePool_WithFullResourceURL_ReturnsError" ,
1124
+ resourceName : "https://www.googleapis.com/compute/v1/projects/project/zones/zone/storagePools/storagePool" ,
1125
+ expectedErr : true ,
1126
+ },
1127
+ {
1128
+ name : "StoragePool_WithMissingProject_ReturnsError" ,
1129
+ resourceName : "zones/us-central1-a/storagePools/storagePool-1" ,
1130
+ expectedErr : true ,
1131
+ },
1132
+ {
1133
+ name : "StoragePool_WithMissingZone_ReturnsError" ,
1134
+ resourceName : "projects/my-project/storagePools/storagePool-1" ,
1135
+ expectedErr : true ,
1136
+ },
1137
+ {
1138
+ name : "StoragePool_WithMissingStoragePoolName_ReturnsError" ,
1139
+ resourceName : "projects/my-project/zones/us-central1-a/storagePool-1" ,
1140
+ expectedErr : true ,
1141
+ },
1142
+ }
1143
+ for _ , tc := range testcases {
1144
+ t .Run (tc .name , func (t * testing.T ) {
1145
+ project , zone , name , err := fieldsFromStoragePoolResourceName (tc .resourceName )
1146
+ input := fmt .Sprintf ("fieldsFromStoragePoolResourceName(%q)" , tc .resourceName )
1147
+ gotErr := err != nil
1148
+ if gotErr != tc .expectedErr {
1149
+ t .Errorf ("%s error presence = %v, expected error presence = %v" , input , gotErr , tc .expectedErr )
1150
+ }
1151
+ if project != tc .expectedProject || zone != tc .expectedZone || name != tc .expectedName {
1152
+ t .Errorf ("%s returned {project: %q, zone: %q, name: %q}, expected {project: %q, zone: %q, name: %q}" , input , project , zone , name , tc .expectedProject , tc .expectedZone , tc .expectedName )
1153
+ }
1154
+ })
1155
+ }
1156
+ }
1157
+
1158
+ func TestZones (t * testing.T ) {
1159
+ testcases := []struct {
1160
+ name string
1161
+ storagePools []StoragePool
1162
+ expectedZones []string
1163
+ expectedErr bool
1164
+ }{
1165
+ {
1166
+ name : "StoragePools_WithValidResourceNames_ReturnsZones" ,
1167
+ storagePools : []StoragePool {
1168
+ {
1169
+ Project : "my-project" ,
1170
+ Zone : "us-central1-a" ,
1171
+ Name : "storagePool-1" ,
1172
+ ResourceName : "projects/my-project/zones/us-central1-a/storagePools/storagePool-1" ,
1173
+ },
1174
+ {
1175
+ Project : "my-project" ,
1176
+ Zone : "us-central1-b" ,
1177
+ Name : "storagePool-2" ,
1178
+ ResourceName : "projects/my-project/zones/us-central1-b/storagePools/storagePool-2" ,
1179
+ },
1180
+ },
1181
+ expectedZones : []string {"us-central1-a" , "us-central1-b" },
1182
+ },
1183
+ {
1184
+ name : "StoragePools_WithDuplicateZone_ReturnsError" ,
1185
+ storagePools : []StoragePool {
1186
+ {
1187
+ Project : "my-project" ,
1188
+ Zone : "us-central1-a" ,
1189
+ Name : "storagePool-1" ,
1190
+ ResourceName : "projects/my-project/zones/us-central1-a/storagePools/storagePool-1" ,
1191
+ },
1192
+ {
1193
+ Project : "my-project" ,
1194
+ Zone : "us-central1-a" ,
1195
+ Name : "storagePool-2" ,
1196
+ ResourceName : "projects/my-project/zones/us-central1-a/storagePools/storagePool-2" ,
1197
+ },
1198
+ },
1199
+ expectedErr : true ,
1200
+ },
1201
+ }
1202
+ for _ , tc := range testcases {
1203
+ t .Run (tc .name , func (t * testing.T ) {
1204
+ zones , err := StoragePoolZones (tc .storagePools )
1205
+ input := fmt .Sprintf ("StoragePoolZones(%q)" , tc .storagePools )
1206
+ gotErr := err != nil
1207
+ if gotErr != tc .expectedErr {
1208
+ t .Errorf ("%s error presence = %v, expected error presence = %v" , input , gotErr , tc .expectedErr )
1209
+ }
1210
+ if diff := cmp .Diff (tc .expectedZones , zones ); diff != "" {
1211
+ t .Errorf ("%s: -want err, +got err\n %s" , input , diff )
1212
+ }
1213
+ })
1214
+ }
1215
+ }
1216
+
1217
+ func TestStoragePoolInZone (t * testing.T ) {
1218
+ testcases := []struct {
1219
+ name string
1220
+ storagePools []StoragePool
1221
+ zone string
1222
+ expectedStoragePool * StoragePool
1223
+ expectedErr bool
1224
+ }{
1225
+ {
1226
+ name : "ValidStoragePools_ReturnsStoragePoolInZone" ,
1227
+ storagePools : []StoragePool {
1228
+ {
1229
+ Project : "my-project" ,
1230
+ Zone : "us-central1-a" ,
1231
+ Name : "storagePool-1" ,
1232
+ ResourceName : "projects/my-project/zones/us-central1-a/storagePools/storagePool-1" ,
1233
+ },
1234
+ {
1235
+ Project : "my-project" ,
1236
+ Zone : "us-central1-b" ,
1237
+ Name : "storagePool-2" ,
1238
+ ResourceName : "projects/my-project/zones/us-central1-b/storagePools/storagePool-2" ,
1239
+ },
1240
+ },
1241
+ zone : "us-central1-a" ,
1242
+ expectedStoragePool : & StoragePool {
1243
+ Project : "my-project" ,
1244
+ Zone : "us-central1-a" ,
1245
+ Name : "storagePool-1" ,
1246
+ ResourceName : "projects/my-project/zones/us-central1-a/storagePools/storagePool-1" ,
1247
+ },
1248
+ },
1249
+ {
1250
+ name : "StoragePoolNotInZone_ReturnsNil" ,
1251
+ storagePools : []StoragePool {
1252
+ {
1253
+ Project : "my-project" ,
1254
+ Zone : "us-central1-a" ,
1255
+ Name : "storagePool-1" ,
1256
+ ResourceName : "projects/my-project/zones/us-central1-a/storagePools/storagePool-1" ,
1257
+ },
1258
+ },
1259
+ zone : "us-central1-b" ,
1260
+ expectedStoragePool : nil ,
1261
+ },
1262
+ }
1263
+ for _ , tc := range testcases {
1264
+ t .Run (tc .name , func (t * testing.T ) {
1265
+ sp := StoragePoolInZone (tc .storagePools , tc .zone )
1266
+ input := fmt .Sprintf ("StoragePoolInZone(%q)" , tc .storagePools )
1267
+ if diff := cmp .Diff (tc .expectedStoragePool , sp ); diff != "" {
1268
+ t .Errorf ("%s: -want, +got \n %s" , input , diff )
1269
+ }
1270
+ })
1271
+ }
1272
+ }
1273
+
1274
+ func TestUnorderedSlicesEqual (t * testing.T ) {
1275
+ testcases := []struct {
1276
+ name string
1277
+ slice1 []string
1278
+ slice2 []string
1279
+ expectedSlicesEqual bool
1280
+ }{
1281
+ {
1282
+ name : "OrderedSlicesEqual_ReturnsTrue" ,
1283
+ slice1 : []string {"us-central1-a" , "us-central1-b" },
1284
+ slice2 : []string {"us-central1-a" , "us-central1-b" },
1285
+ expectedSlicesEqual : true ,
1286
+ },
1287
+ {
1288
+ name : "UnorderedSlicesEqual_ReturnsTrue" ,
1289
+ slice1 : []string {"us-central1-a" , "us-central1-b" },
1290
+ slice2 : []string {"us-central1-b" , "us-central1-a" },
1291
+ expectedSlicesEqual : true ,
1292
+ },
1293
+ {
1294
+ name : "SlicesNotEqualSameLength_ReturnsFalse" ,
1295
+ slice1 : []string {"us-central1-a" , "us-central1-b" },
1296
+ slice2 : []string {"us-central1-a" , "us-central1-a" },
1297
+ expectedSlicesEqual : false ,
1298
+ },
1299
+ {
1300
+ name : "SlicesNotEqualDifferentLength_ReturnsFalse" ,
1301
+ slice1 : []string {"us-central1-a" },
1302
+ slice2 : []string {},
1303
+ expectedSlicesEqual : false ,
1304
+ },
1305
+ }
1306
+ for _ , tc := range testcases {
1307
+ t .Run (tc .name , func (t * testing.T ) {
1308
+ slicesEqual := UnorderedSlicesEqual (tc .slice1 , tc .slice2 )
1309
+ input := fmt .Sprintf ("UnorderedSlicesEqual(%v, %v)" , tc .slice1 , tc .slice2 )
1310
+ if diff := cmp .Diff (tc .expectedSlicesEqual , slicesEqual ); diff != "" {
1311
+ t .Errorf ("%s: -want, +got \n %s" , input , diff )
1312
+ }
1313
+ })
1314
+ }
1315
+ }
0 commit comments