Skip to content

Commit c6a2c4f

Browse files
committed
fix: add unit tests for truncate long subnet name on lb ip configuration
1 parent 1a55d0f commit c6a2c4f

File tree

1 file changed

+74
-5
lines changed

1 file changed

+74
-5
lines changed

staging/src/k8s.io/legacy-cloud-providers/azure/azure_standard_test.go

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,7 @@ func TestGetLoadBalancingRuleName(t *testing.T) {
261261

262262
svc := &v1.Service{
263263
ObjectMeta: meta.ObjectMeta{
264-
Annotations: map[string]string{
265-
ServiceAnnotationLoadBalancerInternalSubnet: "subnet",
266-
ServiceAnnotationLoadBalancerInternal: "true",
267-
},
264+
Annotations: map[string]string{ },
268265
UID: "257b9655-5137-4ad2-b091-ef3f07043ad3",
269266
},
270267
}
@@ -334,7 +331,79 @@ func TestGetLoadBalancingRuleName(t *testing.T) {
334331
svc.Annotations[ServiceAnnotationLoadBalancerInternalSubnet] = c.subnetName
335332
svc.Annotations[ServiceAnnotationLoadBalancerInternal] = strconv.FormatBool(c.isInternal)
336333

337-
loadbalancerName := az.getLoadBalancerRuleName(svc, c.protocol, c.port)
334+
loadbalancerRuleName := az.getLoadBalancerRuleName(svc, c.protocol, c.port)
335+
assert.Equal(t, c.expected, loadbalancerRuleName, c.description)
336+
}
337+
}
338+
339+
func TestgetFrontendIPConfigName(t *testing.T) {
340+
az := getTestCloud()
341+
az.PrimaryAvailabilitySetName = "primary"
342+
343+
svc := &v1.Service{
344+
ObjectMeta: meta.ObjectMeta{
345+
Annotations: map[string]string{
346+
ServiceAnnotationLoadBalancerInternalSubnet: "subnet",
347+
ServiceAnnotationLoadBalancerInternal: "true",
348+
},
349+
UID: "257b9655-5137-4ad2-b091-ef3f07043ad3",
350+
},
351+
}
352+
353+
cases := []struct {
354+
description string
355+
subnetName string
356+
isInternal bool
357+
useStandardLB bool
358+
expected string
359+
}{
360+
{
361+
description: "internal lb should have subnet name on the frontend ip configuration name",
362+
subnetName: "shortsubnet",
363+
isInternal: true,
364+
useStandardLB: true,
365+
expected: "a257b965551374ad2b091ef3f07043ad-shortsubnet",
366+
},
367+
{
368+
description: "internal standard lb should have subnet name on the frontend ip configuration name but truncated to 80 charactors",
369+
subnetName: "averylonnnngggnnnnnnnnnnnnnnnnnnnnnngggggggggggggggggggggggggggggggggggggsubet",
370+
isInternal: true,
371+
useStandardLB: true,
372+
expected: "a257b965551374ad2b091ef3f07043ad-averylonnnngggnnnnnnnnnnnnnnnnnnnnnngg",
373+
},
374+
{
375+
description: "internal basic lb should have subnet name on the frontend ip configuration name but truncated to 80 charactors",
376+
subnetName: "averylonnnngggnnnnnnnnnnnnnnnnnnnnnngggggggggggggggggggggggggggggggggggggsubet",
377+
isInternal: true,
378+
useStandardLB: false,
379+
expected: "a257b965551374ad2b091ef3f07043ad-averylonnnngggnnnnnnnnnnnnnnnnnnnnnngg",
380+
},
381+
{
382+
description: "external standard lb should not have subnet name on the frontend ip configuration name",
383+
subnetName: "shortsubnet",
384+
isInternal: false,
385+
useStandardLB: true,
386+
expected: "a257b965551374ad2b091ef3f07043ad",
387+
},
388+
{
389+
description: "external basic lb should not have subnet name on the frontend ip configuration name",
390+
subnetName: "shortsubnet",
391+
isInternal: false,
392+
useStandardLB: false,
393+
expected: "a257b965551374ad2b091ef3f07043ad",
394+
},
395+
}
396+
397+
for _, c := range cases {
398+
if c.useStandardLB {
399+
az.Config.LoadBalancerSku = loadBalancerSkuStandard
400+
} else {
401+
az.Config.LoadBalancerSku = loadBalancerSkuBasic
402+
}
403+
svc.Annotations[ServiceAnnotationLoadBalancerInternalSubnet] = c.subnetName
404+
svc.Annotations[ServiceAnnotationLoadBalancerInternal] = strconv.FormatBool(c.isInternal)
405+
406+
loadbalancerName := az.getFrontendIPConfigName(svc)
338407
assert.Equal(t, c.expected, loadbalancerName, c.description)
339408
}
340409
}

0 commit comments

Comments
 (0)