2016-05-29 03:54:26 +00:00
/ *
Copyright 2016 The Kubernetes Authors .
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
package azure
import (
"fmt"
"strings"
"testing"
2017-01-11 14:09:48 +00:00
"k8s.io/apimachinery/pkg/types"
2016-11-18 20:58:42 +00:00
"k8s.io/kubernetes/pkg/api/v1"
serviceapi "k8s.io/kubernetes/pkg/api/v1/service"
2016-05-29 03:54:26 +00:00
"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/Azure/go-autorest/autorest/to"
)
var testClusterName = "testCluster"
// Test additional of a new service/port.
func TestReconcileLoadBalancerAddPort ( t * testing . T ) {
az := getTestCloud ( )
2017-05-08 22:02:41 +00:00
svc := getTestService ( "servicea" , v1 . ProtocolTCP , 80 )
2017-03-16 08:30:40 +00:00
configProperties := getTestPublicFipConfigurationProperties ( )
2016-05-29 03:54:26 +00:00
lb := getTestLoadBalancer ( )
2016-08-24 02:55:48 +00:00
nodes := [ ] * v1 . Node { }
2016-05-29 03:54:26 +00:00
2017-05-08 22:02:41 +00:00
svc . Spec . Ports = append ( svc . Spec . Ports , v1 . ServicePort {
Name : fmt . Sprintf ( "port-udp-%d" , 1234 ) ,
Protocol : v1 . ProtocolUDP ,
Port : 1234 ,
NodePort : getBackendPort ( 1234 ) ,
} )
2017-03-16 08:30:40 +00:00
lb , updated , err := az . reconcileLoadBalancer ( lb , & configProperties , testClusterName , & svc , nodes )
2016-05-29 03:54:26 +00:00
if err != nil {
t . Errorf ( "Unexpected error: %q" , err )
}
if ! updated {
t . Error ( "Expected the loadbalancer to need an update" )
}
// ensure we got a frontend ip configuration
2016-12-07 07:16:53 +00:00
if len ( * lb . FrontendIPConfigurations ) != 1 {
2016-05-29 03:54:26 +00:00
t . Error ( "Expected the loadbalancer to have a frontend ip configuration" )
}
validateLoadBalancer ( t , lb , svc )
}
2016-08-22 05:53:55 +00:00
func TestReconcileLoadBalancerNodeHealth ( t * testing . T ) {
az := getTestCloud ( )
2017-05-08 22:02:41 +00:00
svc := getTestService ( "servicea" , v1 . ProtocolTCP , 80 )
2016-08-22 05:53:55 +00:00
svc . Annotations = map [ string ] string {
serviceapi . BetaAnnotationExternalTraffic : serviceapi . AnnotationValueExternalTrafficLocal ,
serviceapi . BetaAnnotationHealthCheckNodePort : "32456" ,
}
2017-03-16 08:30:40 +00:00
configProperties := getTestPublicFipConfigurationProperties ( )
2016-08-22 05:53:55 +00:00
lb := getTestLoadBalancer ( )
2016-08-24 02:55:48 +00:00
nodes := [ ] * v1 . Node { }
2016-08-22 05:53:55 +00:00
2017-03-16 08:30:40 +00:00
lb , updated , err := az . reconcileLoadBalancer ( lb , & configProperties , testClusterName , & svc , nodes )
2016-08-22 05:53:55 +00:00
if err != nil {
t . Errorf ( "Unexpected error: %q" , err )
}
if ! updated {
t . Error ( "Expected the loadbalancer to need an update" )
}
// ensure we got a frontend ip configuration
2016-12-07 07:16:53 +00:00
if len ( * lb . FrontendIPConfigurations ) != 1 {
2016-08-22 05:53:55 +00:00
t . Error ( "Expected the loadbalancer to have a frontend ip configuration" )
}
validateLoadBalancer ( t , lb , svc )
}
2016-05-29 03:54:26 +00:00
// Test removing all services results in removing the frontend ip configuration
2017-03-22 05:27:33 +00:00
func TestReconcileLoadBalancerRemoveService ( t * testing . T ) {
az := getTestCloud ( )
2017-05-08 22:02:41 +00:00
svc := getTestService ( "servicea" , v1 . ProtocolTCP , 80 , 443 )
2017-03-22 05:27:33 +00:00
lb := getTestLoadBalancer ( )
configProperties := getTestPublicFipConfigurationProperties ( )
nodes := [ ] * v1 . Node { }
lb , updated , err := az . reconcileLoadBalancer ( lb , & configProperties , testClusterName , & svc , nodes )
if err != nil {
t . Errorf ( "Unexpected error: %q" , err )
}
validateLoadBalancer ( t , lb , svc )
lb , updated , err = az . reconcileLoadBalancer ( lb , nil , testClusterName , & svc , nodes )
if err != nil {
t . Errorf ( "Unexpected error: %q" , err )
}
if ! updated {
t . Error ( "Expected the loadbalancer to need an update" )
}
// ensure we abandoned the frontend ip configuration
if len ( * lb . FrontendIPConfigurations ) != 0 {
t . Error ( "Expected the loadbalancer to have no frontend ip configuration" )
}
validateLoadBalancer ( t , lb )
}
// Test removing all service ports results in removing the frontend ip configuration
2016-05-29 03:54:26 +00:00
func TestReconcileLoadBalancerRemoveAllPortsRemovesFrontendConfig ( t * testing . T ) {
az := getTestCloud ( )
2017-05-08 22:02:41 +00:00
svc := getTestService ( "servicea" , v1 . ProtocolTCP , 80 )
2016-05-29 03:54:26 +00:00
lb := getTestLoadBalancer ( )
2017-03-16 08:30:40 +00:00
configProperties := getTestPublicFipConfigurationProperties ( )
2016-08-24 02:55:48 +00:00
nodes := [ ] * v1 . Node { }
2016-05-29 03:54:26 +00:00
2017-03-16 08:30:40 +00:00
lb , updated , err := az . reconcileLoadBalancer ( lb , & configProperties , testClusterName , & svc , nodes )
2016-05-29 03:54:26 +00:00
if err != nil {
t . Errorf ( "Unexpected error: %q" , err )
}
2017-03-22 05:27:33 +00:00
validateLoadBalancer ( t , lb , svc )
2016-05-29 03:54:26 +00:00
2017-05-08 22:02:41 +00:00
svcUpdated := getTestService ( "servicea" , v1 . ProtocolTCP )
2016-08-24 02:55:48 +00:00
lb , updated , err = az . reconcileLoadBalancer ( lb , nil , testClusterName , & svcUpdated , nodes )
2016-05-29 03:54:26 +00:00
if err != nil {
t . Errorf ( "Unexpected error: %q" , err )
}
if ! updated {
t . Error ( "Expected the loadbalancer to need an update" )
}
2016-10-13 21:29:50 +00:00
// ensure we abandoned the frontend ip configuration
2016-12-07 07:16:53 +00:00
if len ( * lb . FrontendIPConfigurations ) != 0 {
2016-05-29 03:54:26 +00:00
t . Error ( "Expected the loadbalancer to have no frontend ip configuration" )
}
validateLoadBalancer ( t , lb , svcUpdated )
}
// Test removal of a port from an existing service.
func TestReconcileLoadBalancerRemovesPort ( t * testing . T ) {
az := getTestCloud ( )
2017-05-08 22:02:41 +00:00
svc := getTestService ( "servicea" , v1 . ProtocolTCP , 80 , 443 )
2017-03-16 08:30:40 +00:00
configProperties := getTestPublicFipConfigurationProperties ( )
2016-08-24 02:55:48 +00:00
nodes := [ ] * v1 . Node { }
2016-05-29 03:54:26 +00:00
existingLoadBalancer := getTestLoadBalancer ( svc )
2017-05-08 22:02:41 +00:00
svcUpdated := getTestService ( "servicea" , v1 . ProtocolTCP , 80 )
2017-03-16 08:30:40 +00:00
updatedLoadBalancer , _ , err := az . reconcileLoadBalancer ( existingLoadBalancer , & configProperties , testClusterName , & svcUpdated , nodes )
2016-05-29 03:54:26 +00:00
if err != nil {
t . Errorf ( "Unexpected error: %q" , err )
}
validateLoadBalancer ( t , updatedLoadBalancer , svcUpdated )
}
// Test reconciliation of multiple services on same port
func TestReconcileLoadBalancerMultipleServices ( t * testing . T ) {
az := getTestCloud ( )
2017-05-08 22:02:41 +00:00
svc1 := getTestService ( "servicea" , v1 . ProtocolTCP , 80 , 443 )
svc2 := getTestService ( "serviceb" , v1 . ProtocolTCP , 80 )
2017-03-16 08:30:40 +00:00
configProperties := getTestPublicFipConfigurationProperties ( )
2016-08-24 02:55:48 +00:00
nodes := [ ] * v1 . Node { }
2016-05-29 03:54:26 +00:00
existingLoadBalancer := getTestLoadBalancer ( )
2017-03-16 08:30:40 +00:00
updatedLoadBalancer , _ , err := az . reconcileLoadBalancer ( existingLoadBalancer , & configProperties , testClusterName , & svc1 , nodes )
2016-05-29 03:54:26 +00:00
if err != nil {
t . Errorf ( "Unexpected error: %q" , err )
}
2017-03-16 08:30:40 +00:00
updatedLoadBalancer , _ , err = az . reconcileLoadBalancer ( updatedLoadBalancer , & configProperties , testClusterName , & svc2 , nodes )
2016-05-29 03:54:26 +00:00
if err != nil {
t . Errorf ( "Unexpected error: %q" , err )
}
validateLoadBalancer ( t , updatedLoadBalancer , svc1 , svc2 )
}
func TestReconcileSecurityGroupNewServiceAddsPort ( t * testing . T ) {
az := getTestCloud ( )
2017-05-08 22:02:41 +00:00
svc1 := getTestService ( "serviceea" , v1 . ProtocolTCP , 80 )
2016-05-29 03:54:26 +00:00
sg := getTestSecurityGroup ( )
2017-03-22 05:27:33 +00:00
sg , _ , err := az . reconcileSecurityGroup ( sg , testClusterName , & svc1 , true )
2016-05-29 03:54:26 +00:00
if err != nil {
t . Errorf ( "Unexpected error: %q" , err )
}
validateSecurityGroup ( t , sg , svc1 )
}
2017-03-22 05:27:33 +00:00
func TestReconcileSecurityGroupNewInternalServiceAddsPort ( t * testing . T ) {
az := getTestCloud ( )
svc1 := getInternalTestService ( "serviceea" , 80 )
sg := getTestSecurityGroup ( )
sg , _ , err := az . reconcileSecurityGroup ( sg , testClusterName , & svc1 , true )
if err != nil {
t . Errorf ( "Unexpected error: %q" , err )
}
validateSecurityGroup ( t , sg , svc1 )
}
func TestReconcileSecurityGroupRemoveService ( t * testing . T ) {
2017-05-08 22:02:41 +00:00
service1 := getTestService ( "servicea" , v1 . ProtocolTCP , 81 )
service2 := getTestService ( "serviceb" , v1 . ProtocolTCP , 82 )
2017-03-22 05:27:33 +00:00
sg := getTestSecurityGroup ( service1 , service2 )
validateSecurityGroup ( t , sg , service1 , service2 )
az := getTestCloud ( )
sg , _ , err := az . reconcileSecurityGroup ( sg , testClusterName , & service1 , false )
if err != nil {
t . Errorf ( "Unexpected error: %q" , err )
}
validateSecurityGroup ( t , sg , service2 )
}
2016-05-29 03:54:26 +00:00
func TestReconcileSecurityGroupRemoveServiceRemovesPort ( t * testing . T ) {
az := getTestCloud ( )
2017-05-08 22:02:41 +00:00
svc := getTestService ( "servicea" , v1 . ProtocolTCP , 80 , 443 )
2016-05-29 03:54:26 +00:00
sg := getTestSecurityGroup ( svc )
2017-05-08 22:02:41 +00:00
svcUpdated := getTestService ( "servicea" , v1 . ProtocolTCP , 80 )
2017-03-22 05:27:33 +00:00
sg , _ , err := az . reconcileSecurityGroup ( sg , testClusterName , & svcUpdated , true )
2016-05-29 03:54:26 +00:00
if err != nil {
t . Errorf ( "Unexpected error: %q" , err )
}
validateSecurityGroup ( t , sg , svcUpdated )
}
2016-11-12 06:55:06 +00:00
func TestReconcileSecurityWithSourceRanges ( t * testing . T ) {
az := getTestCloud ( )
2017-05-08 22:02:41 +00:00
svc := getTestService ( "servicea" , v1 . ProtocolTCP , 80 , 443 )
2016-11-12 06:55:06 +00:00
svc . Spec . LoadBalancerSourceRanges = [ ] string {
2017-05-08 21:49:45 +00:00
"192.168.0.0/24" ,
"10.0.0.0/32" ,
2016-11-12 06:55:06 +00:00
}
sg := getTestSecurityGroup ( svc )
2017-03-22 05:27:33 +00:00
sg , _ , err := az . reconcileSecurityGroup ( sg , testClusterName , & svc , true )
2016-11-12 06:55:06 +00:00
if err != nil {
t . Errorf ( "Unexpected error: %q" , err )
}
validateSecurityGroup ( t , sg , svc )
}
2016-05-29 03:54:26 +00:00
func getTestCloud ( ) * Cloud {
return & Cloud {
Config : Config {
TenantID : "tenant" ,
SubscriptionID : "subscription" ,
ResourceGroup : "rg" ,
Location : "westus" ,
VnetName : "vnet" ,
SubnetName : "subnet" ,
SecurityGroupName : "nsg" ,
RouteTableName : "rt" ,
} ,
}
}
func getBackendPort ( port int32 ) int32 {
return port + 10000
}
2017-03-16 08:30:40 +00:00
func getTestPublicFipConfigurationProperties ( ) network . FrontendIPConfigurationPropertiesFormat {
return network . FrontendIPConfigurationPropertiesFormat {
PublicIPAddress : & network . PublicIPAddress { ID : to . StringPtr ( "/this/is/a/public/ip/address/id" ) } ,
}
2016-05-29 03:54:26 +00:00
}
2017-05-08 22:02:41 +00:00
func getTestService ( identifier string , proto v1 . Protocol , requestedPorts ... int32 ) v1 . Service {
2016-11-18 20:58:42 +00:00
ports := [ ] v1 . ServicePort { }
2016-05-29 03:54:26 +00:00
for _ , port := range requestedPorts {
2016-11-18 20:58:42 +00:00
ports = append ( ports , v1 . ServicePort {
2017-05-08 22:02:41 +00:00
Name : fmt . Sprintf ( "port-tcp-%d" , port ) ,
Protocol : proto ,
2016-05-29 03:54:26 +00:00
Port : port ,
NodePort : getBackendPort ( port ) ,
} )
}
2016-11-18 20:58:42 +00:00
svc := v1 . Service {
Spec : v1 . ServiceSpec {
Type : v1 . ServiceTypeLoadBalancer ,
2016-05-29 03:54:26 +00:00
Ports : ports ,
} ,
}
svc . Name = identifier
svc . Namespace = "default"
svc . UID = types . UID ( identifier )
2017-03-22 05:27:33 +00:00
svc . Annotations = make ( map [ string ] string )
return svc
}
func getInternalTestService ( identifier string , requestedPorts ... int32 ) v1 . Service {
2017-05-08 22:02:41 +00:00
svc := getTestService ( identifier , v1 . ProtocolTCP , requestedPorts ... )
2017-03-22 05:27:33 +00:00
svc . Annotations [ ServiceAnnotationLoadBalancerInternal ] = "true"
2016-05-29 03:54:26 +00:00
return svc
}
2016-11-18 20:58:42 +00:00
func getTestLoadBalancer ( services ... v1 . Service ) network . LoadBalancer {
2016-05-29 03:54:26 +00:00
rules := [ ] network . LoadBalancingRule { }
probes := [ ] network . Probe { }
for _ , service := range services {
for _ , port := range service . Spec . Ports {
2017-05-08 21:49:45 +00:00
ruleName := getLoadBalancerRuleName ( & service , port )
2016-05-29 03:54:26 +00:00
rules = append ( rules , network . LoadBalancingRule {
Name : to . StringPtr ( ruleName ) ,
2016-12-07 07:16:53 +00:00
LoadBalancingRulePropertiesFormat : & network . LoadBalancingRulePropertiesFormat {
2016-05-29 03:54:26 +00:00
FrontendPort : to . Int32Ptr ( port . Port ) ,
2016-08-25 08:43:43 +00:00
BackendPort : to . Int32Ptr ( port . Port ) ,
2016-05-29 03:54:26 +00:00
} ,
} )
probes = append ( probes , network . Probe {
Name : to . StringPtr ( ruleName ) ,
2016-12-07 07:16:53 +00:00
ProbePropertiesFormat : & network . ProbePropertiesFormat {
2016-05-29 03:54:26 +00:00
Port : to . Int32Ptr ( port . NodePort ) ,
} ,
} )
}
}
lb := network . LoadBalancer {
2016-12-07 07:16:53 +00:00
LoadBalancerPropertiesFormat : & network . LoadBalancerPropertiesFormat {
2016-05-29 03:54:26 +00:00
LoadBalancingRules : & rules ,
Probes : & probes ,
} ,
}
return lb
}
2016-11-18 20:58:42 +00:00
func getServiceSourceRanges ( service * v1 . Service ) [ ] string {
2016-11-12 06:55:06 +00:00
if len ( service . Spec . LoadBalancerSourceRanges ) == 0 {
2017-03-22 05:27:33 +00:00
if ! requiresInternalLoadBalancer ( service ) {
return [ ] string { "Internet" }
}
2016-11-12 06:55:06 +00:00
}
2017-03-22 05:27:33 +00:00
2016-11-12 06:55:06 +00:00
return service . Spec . LoadBalancerSourceRanges
}
2016-11-18 20:58:42 +00:00
func getTestSecurityGroup ( services ... v1 . Service ) network . SecurityGroup {
2016-05-29 03:54:26 +00:00
rules := [ ] network . SecurityRule { }
for _ , service := range services {
for _ , port := range service . Spec . Ports {
2016-11-12 06:55:06 +00:00
sources := getServiceSourceRanges ( & service )
for _ , src := range sources {
2017-05-08 21:49:45 +00:00
ruleName := getSecurityRuleName ( & service , port , src )
2016-11-12 06:55:06 +00:00
rules = append ( rules , network . SecurityRule {
Name : to . StringPtr ( ruleName ) ,
2016-12-07 07:16:53 +00:00
SecurityRulePropertiesFormat : & network . SecurityRulePropertiesFormat {
2016-11-12 06:55:06 +00:00
SourceAddressPrefix : to . StringPtr ( src ) ,
DestinationPortRange : to . StringPtr ( fmt . Sprintf ( "%d" , port . Port ) ) ,
} ,
} )
}
2016-05-29 03:54:26 +00:00
}
}
sg := network . SecurityGroup {
2016-12-07 07:16:53 +00:00
SecurityGroupPropertiesFormat : & network . SecurityGroupPropertiesFormat {
2016-05-29 03:54:26 +00:00
SecurityRules : & rules ,
} ,
}
return sg
}
2016-11-18 20:58:42 +00:00
func validateLoadBalancer ( t * testing . T , loadBalancer network . LoadBalancer , services ... v1 . Service ) {
2016-05-29 03:54:26 +00:00
expectedRuleCount := 0
2017-03-22 05:27:33 +00:00
expectedFrontendIPCount := 0
2017-05-08 22:02:41 +00:00
expectedProbeCount := 0
2016-05-29 03:54:26 +00:00
for _ , svc := range services {
2017-03-22 05:27:33 +00:00
if len ( svc . Spec . Ports ) > 0 {
expectedFrontendIPCount ++
}
2016-05-29 03:54:26 +00:00
for _ , wantedRule := range svc . Spec . Ports {
expectedRuleCount ++
2017-05-08 21:49:45 +00:00
wantedRuleName := getLoadBalancerRuleName ( & svc , wantedRule )
2016-05-29 03:54:26 +00:00
foundRule := false
2016-12-07 07:16:53 +00:00
for _ , actualRule := range * loadBalancer . LoadBalancingRules {
2016-05-29 03:54:26 +00:00
if strings . EqualFold ( * actualRule . Name , wantedRuleName ) &&
2016-12-07 07:16:53 +00:00
* actualRule . FrontendPort == wantedRule . Port &&
* actualRule . BackendPort == wantedRule . Port {
2016-05-29 03:54:26 +00:00
foundRule = true
break
}
}
if ! foundRule {
2016-08-25 08:43:43 +00:00
t . Errorf ( "Expected load balancer rule but didn't find it: %q" , wantedRuleName )
2016-05-29 03:54:26 +00:00
}
2017-05-08 22:02:41 +00:00
// if UDP rule, there is no probe
if wantedRule . Protocol == v1 . ProtocolUDP {
continue
}
expectedProbeCount ++
2016-05-29 03:54:26 +00:00
foundProbe := false
2016-08-22 05:53:55 +00:00
if serviceapi . NeedsHealthCheck ( & svc ) {
path , port := serviceapi . GetServiceHealthCheckPathPort ( & svc )
2016-12-07 07:16:53 +00:00
for _ , actualProbe := range * loadBalancer . Probes {
2016-08-22 05:53:55 +00:00
if strings . EqualFold ( * actualProbe . Name , wantedRuleName ) &&
2016-12-07 07:16:53 +00:00
* actualProbe . Port == port &&
* actualProbe . RequestPath == path &&
actualProbe . Protocol == network . ProbeProtocolHTTP {
2016-08-22 05:53:55 +00:00
foundProbe = true
break
}
}
} else {
2016-12-07 07:16:53 +00:00
for _ , actualProbe := range * loadBalancer . Probes {
2016-08-22 05:53:55 +00:00
if strings . EqualFold ( * actualProbe . Name , wantedRuleName ) &&
2016-12-07 07:16:53 +00:00
* actualProbe . Port == wantedRule . NodePort {
2016-08-22 05:53:55 +00:00
foundProbe = true
break
}
2016-05-29 03:54:26 +00:00
}
}
if ! foundProbe {
2016-12-07 07:16:53 +00:00
for _ , actualProbe := range * loadBalancer . Probes {
t . Logf ( "Probe: %s %d" , * actualProbe . Name , * actualProbe . Port )
2016-08-22 05:53:55 +00:00
}
2016-08-25 08:43:43 +00:00
t . Errorf ( "Expected loadbalancer probe but didn't find it: %q" , wantedRuleName )
2016-05-29 03:54:26 +00:00
}
}
}
2017-03-22 05:27:33 +00:00
frontendIPCount := len ( * loadBalancer . FrontendIPConfigurations )
if frontendIPCount != expectedFrontendIPCount {
t . Errorf ( "Expected the loadbalancer to have %d frontend IPs. Found %d.\n%v" , expectedFrontendIPCount , frontendIPCount , loadBalancer . FrontendIPConfigurations )
}
2016-12-07 07:16:53 +00:00
lenRules := len ( * loadBalancer . LoadBalancingRules )
2016-05-29 03:54:26 +00:00
if lenRules != expectedRuleCount {
2016-12-07 07:16:53 +00:00
t . Errorf ( "Expected the loadbalancer to have %d rules. Found %d.\n%v" , expectedRuleCount , lenRules , loadBalancer . LoadBalancingRules )
2016-05-29 03:54:26 +00:00
}
2017-05-08 22:02:41 +00:00
2016-12-07 07:16:53 +00:00
lenProbes := len ( * loadBalancer . Probes )
2017-05-08 22:02:41 +00:00
if lenProbes != expectedProbeCount {
2016-05-29 03:54:26 +00:00
t . Errorf ( "Expected the loadbalancer to have %d probes. Found %d." , expectedRuleCount , lenProbes )
}
}
2016-11-18 20:58:42 +00:00
func validateSecurityGroup ( t * testing . T , securityGroup network . SecurityGroup , services ... v1 . Service ) {
2016-05-29 03:54:26 +00:00
expectedRuleCount := 0
for _ , svc := range services {
for _ , wantedRule := range svc . Spec . Ports {
2016-11-12 06:55:06 +00:00
sources := getServiceSourceRanges ( & svc )
for _ , source := range sources {
2017-05-08 21:49:45 +00:00
wantedRuleName := getSecurityRuleName ( & svc , wantedRule , source )
2016-11-12 06:55:06 +00:00
expectedRuleCount ++
foundRule := false
2016-12-07 07:16:53 +00:00
for _ , actualRule := range * securityGroup . SecurityRules {
2016-11-12 06:55:06 +00:00
if strings . EqualFold ( * actualRule . Name , wantedRuleName ) &&
2016-12-07 07:16:53 +00:00
* actualRule . SourceAddressPrefix == source &&
* actualRule . DestinationPortRange == fmt . Sprintf ( "%d" , wantedRule . Port ) {
2016-11-12 06:55:06 +00:00
foundRule = true
break
}
}
if ! foundRule {
t . Errorf ( "Expected security group rule but didn't find it: %q" , wantedRuleName )
2016-05-29 03:54:26 +00:00
}
}
}
}
2016-12-07 07:16:53 +00:00
lenRules := len ( * securityGroup . SecurityRules )
2016-05-29 03:54:26 +00:00
if lenRules != expectedRuleCount {
2016-11-12 06:55:06 +00:00
t . Errorf ( "Expected the loadbalancer to have %d rules. Found %d.\n" , expectedRuleCount , lenRules )
2016-05-29 03:54:26 +00:00
}
}
func TestSecurityRulePriorityPicksNextAvailablePriority ( t * testing . T ) {
rules := [ ] network . SecurityRule { }
var expectedPriority int32 = loadBalancerMinimumPriority + 50
var i int32
for i = loadBalancerMinimumPriority ; i < expectedPriority ; i ++ {
rules = append ( rules , network . SecurityRule {
2016-12-07 07:16:53 +00:00
SecurityRulePropertiesFormat : & network . SecurityRulePropertiesFormat {
2016-05-29 03:54:26 +00:00
Priority : to . Int32Ptr ( i ) ,
} ,
} )
}
priority , err := getNextAvailablePriority ( rules )
if err != nil {
t . Errorf ( "Unexpectected error: %q" , err )
}
if priority != expectedPriority {
t . Errorf ( "Expected priority %d. Got priority %d." , expectedPriority , priority )
}
}
func TestSecurityRulePriorityFailsIfExhausted ( t * testing . T ) {
rules := [ ] network . SecurityRule { }
var i int32
for i = loadBalancerMinimumPriority ; i < loadBalancerMaximumPriority ; i ++ {
rules = append ( rules , network . SecurityRule {
2016-12-07 07:16:53 +00:00
SecurityRulePropertiesFormat : & network . SecurityRulePropertiesFormat {
2016-05-29 03:54:26 +00:00
Priority : to . Int32Ptr ( i ) ,
} ,
} )
}
_ , err := getNextAvailablePriority ( rules )
if err == nil {
t . Error ( "Expectected an error. There are no priority levels left." )
}
}
func TestProtocolTranslationTCP ( t * testing . T ) {
2016-11-18 20:58:42 +00:00
proto := v1 . ProtocolTCP
2016-05-29 03:54:26 +00:00
transportProto , securityGroupProto , probeProto , err := getProtocolsFromKubernetesProtocol ( proto )
if err != nil {
t . Error ( err )
}
2017-05-08 21:49:45 +00:00
if * transportProto != network . TransportProtocolTCP {
2016-05-29 03:54:26 +00:00
t . Errorf ( "Expected TCP LoadBalancer Rule Protocol. Got %v" , transportProto )
}
2017-05-08 21:49:45 +00:00
if * securityGroupProto != network . TCP {
2016-05-29 03:54:26 +00:00
t . Errorf ( "Expected TCP SecurityGroup Protocol. Got %v" , transportProto )
}
2017-05-08 21:49:45 +00:00
if * probeProto != network . ProbeProtocolTCP {
2016-05-29 03:54:26 +00:00
t . Errorf ( "Expected TCP LoadBalancer Probe Protocol. Got %v" , transportProto )
}
}
func TestProtocolTranslationUDP ( t * testing . T ) {
2016-11-18 20:58:42 +00:00
proto := v1 . ProtocolUDP
2017-05-08 21:49:45 +00:00
transportProto , securityGroupProto , probeProto , _ := getProtocolsFromKubernetesProtocol ( proto )
if * transportProto != network . TransportProtocolUDP {
t . Errorf ( "Expected UDP LoadBalancer Rule Protocol. Got %v" , transportProto )
}
if * securityGroupProto != network . UDP {
t . Errorf ( "Expected UDP SecurityGroup Protocol. Got %v" , transportProto )
}
if probeProto != nil {
t . Errorf ( "Expected UDP LoadBalancer Probe Protocol. Got %v" , transportProto )
2016-05-29 03:54:26 +00:00
}
}
// Test Configuration deserialization (json)
func TestNewCloudFromJSON ( t * testing . T ) {
config := ` {
"tenantId" : "--tenant-id--" ,
"subscriptionId" : "--subscription-id--" ,
"aadClientId" : "--aad-client-id--" ,
"aadClientSecret" : "--aad-client-secret--" ,
"resourceGroup" : "--resource-group--" ,
"location" : "--location--" ,
"subnetName" : "--subnet-name--" ,
"securityGroupName" : "--security-group-name--" ,
"vnetName" : "--vnet-name--" ,
2016-10-07 06:59:14 +00:00
"routeTableName" : "--route-table-name--" ,
"primaryAvailabilitySetName" : "--primary-availability-set-name--"
2016-05-29 03:54:26 +00:00
} `
validateConfig ( t , config )
}
// Test Configuration deserialization (yaml)
func TestNewCloudFromYAML ( t * testing . T ) {
config := `
tenantId : -- tenant - id --
subscriptionId : -- subscription - id --
aadClientId : -- aad - client - id --
aadClientSecret : -- aad - client - secret --
resourceGroup : -- resource - group --
location : -- location --
subnetName : -- subnet - name --
securityGroupName : -- security - group - name --
vnetName : -- vnet - name --
routeTableName : -- route - table - name --
2016-10-07 06:59:14 +00:00
primaryAvailabilitySetName : -- primary - availability - set - name --
2016-05-29 03:54:26 +00:00
`
validateConfig ( t , config )
}
func validateConfig ( t * testing . T , config string ) {
configReader := strings . NewReader ( config )
cloud , err := NewCloud ( configReader )
if err != nil {
t . Error ( err )
}
azureCloud , ok := cloud . ( * Cloud )
if ! ok {
t . Error ( "NewCloud returned incorrect type" )
}
if azureCloud . TenantID != "--tenant-id--" {
t . Errorf ( "got incorrect value for TenantID" )
}
if azureCloud . SubscriptionID != "--subscription-id--" {
t . Errorf ( "got incorrect value for SubscriptionID" )
}
if azureCloud . AADClientID != "--aad-client-id--" {
t . Errorf ( "got incorrect value for AADClientID" )
}
if azureCloud . AADClientSecret != "--aad-client-secret--" {
t . Errorf ( "got incorrect value for AADClientSecret" )
}
if azureCloud . ResourceGroup != "--resource-group--" {
t . Errorf ( "got incorrect value for ResourceGroup" )
}
if azureCloud . Location != "--location--" {
t . Errorf ( "got incorrect value for Location" )
}
if azureCloud . SubnetName != "--subnet-name--" {
t . Errorf ( "got incorrect value for SubnetName" )
}
if azureCloud . SecurityGroupName != "--security-group-name--" {
t . Errorf ( "got incorrect value for SecurityGroupName" )
}
if azureCloud . VnetName != "--vnet-name--" {
t . Errorf ( "got incorrect value for VnetName" )
}
if azureCloud . RouteTableName != "--route-table-name--" {
t . Errorf ( "got incorrect value for RouteTableName" )
}
2016-10-07 06:59:14 +00:00
if azureCloud . PrimaryAvailabilitySetName != "--primary-availability-set-name--" {
t . Errorf ( "got incorrect value for PrimaryAvailabilitySetName" )
}
2016-05-29 03:54:26 +00:00
}
func TestDecodeInstanceInfo ( t * testing . T ) {
response := ` { "ID":"_azdev","UD":"0","FD":"99"} `
faultDomain , err := readFaultDomain ( strings . NewReader ( response ) )
if err != nil {
t . Error ( "Unexpected error in ReadFaultDomain" )
}
if faultDomain == nil {
t . Error ( "Fault domain was unexpectedly nil" )
}
if * faultDomain != "99" {
t . Error ( "got incorrect fault domain" )
}
}