Merge pull request #54260 from itowlson/azure-lb-session-affinity

Automatic merge from submit-queue (batch tested with PRs 55217, 54260). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Unit tests for Azure service session affinity

**What this PR does / why we need it**: We added session affinity support in the Azure load balancer in commit 8b50b83067. This PR adds unit tests for this behaviour.

**Which issue this PR fixes**: None

**Special notes for your reviewer**: None

**Release note**:

```release-note
NONE
```
This commit is contained in:
Kubernetes Submit Queue 2017-11-20 05:41:25 -08:00 committed by GitHub
commit 142579c16d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -293,6 +293,89 @@ func TestReconcileLoadBalancerMultipleServices(t *testing.T) {
validateLoadBalancer(t, updatedLoadBalancer, svc1, svc2)
}
func findLBRuleForPort(lbRules []network.LoadBalancingRule, port int32) (network.LoadBalancingRule, error) {
for _, lbRule := range lbRules {
if *lbRule.FrontendPort == port {
return lbRule, nil
}
}
return network.LoadBalancingRule{}, fmt.Errorf("Expected LB rule with port %d but none found", port)
}
func TestServiceDefaultsToNoSessionPersistence(t *testing.T) {
az := getTestCloud()
svc := getTestService("service-sa-omitted", v1.ProtocolTCP, 7170)
configProperties := getTestPublicFipConfigurationProperties()
lb := getTestLoadBalancer()
nodes := []*v1.Node{}
lb, _, err := az.reconcileLoadBalancer(lb, &configProperties, testClusterName, &svc, nodes)
if err != nil {
t.Errorf("Unexpected error reconciling svc1: %q", err)
}
validateLoadBalancer(t, lb, svc)
lbRule, err := findLBRuleForPort(*lb.LoadBalancingRules, 7170)
if err != nil {
t.Error(err)
}
if lbRule.LoadDistribution != network.Default {
t.Errorf("Expected LB rule to have default load distribution but was %s", lbRule.LoadDistribution)
}
}
func TestServiceRespectsNoSessionAffinity(t *testing.T) {
az := getTestCloud()
svc := getTestService("service-sa-none", v1.ProtocolTCP, 7170)
svc.Spec.SessionAffinity = v1.ServiceAffinityNone
configProperties := getTestPublicFipConfigurationProperties()
lb := getTestLoadBalancer()
nodes := []*v1.Node{}
lb, _, err := az.reconcileLoadBalancer(lb, &configProperties, testClusterName, &svc, nodes)
if err != nil {
t.Errorf("Unexpected error reconciling svc1: %q", err)
}
validateLoadBalancer(t, lb, svc)
lbRule, err := findLBRuleForPort(*lb.LoadBalancingRules, 7170)
if err != nil {
t.Error(err)
}
if lbRule.LoadDistribution != network.Default {
t.Errorf("Expected LB rule to have default load distribution but was %s", lbRule.LoadDistribution)
}
}
func TestServiceRespectsClientIPSessionAffinity(t *testing.T) {
az := getTestCloud()
svc := getTestService("service-sa-clientip", v1.ProtocolTCP, 7170)
svc.Spec.SessionAffinity = v1.ServiceAffinityClientIP
configProperties := getTestPublicFipConfigurationProperties()
lb := getTestLoadBalancer()
nodes := []*v1.Node{}
lb, _, err := az.reconcileLoadBalancer(lb, &configProperties, testClusterName, &svc, nodes)
if err != nil {
t.Errorf("Unexpected error reconciling svc1: %q", err)
}
validateLoadBalancer(t, lb, svc)
lbRule, err := findLBRuleForPort(*lb.LoadBalancingRules, 7170)
if err != nil {
t.Error(err)
}
if lbRule.LoadDistribution != network.SourceIP {
t.Errorf("Expected LB rule to have SourceIP load distribution but was %s", lbRule.LoadDistribution)
}
}
func TestReconcileSecurityGroupNewServiceAddsPort(t *testing.T) {
az := getTestCloud()
svc1 := getTestService("serviceea", v1.ProtocolTCP, 80)