Skip to content

Commit

Permalink
Merge pull request #3307 from nilo19/cleanup/enrich-cloudprovider-test
Browse files Browse the repository at this point in the history
Azure: Enrich unit tests for azure_cloud_provider
  • Loading branch information
k8s-ci-robot authored Jul 9, 2020
2 parents ad95c9d + 4decfcd commit c452eee
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions cluster-autoscaler/cloudprovider/azure/azure_cloud_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ package azure

import (
"fmt"
"io/ioutil"
"os"
"testing"

apiv1 "k8s.io/api/core/v1"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
"k8s.io/autoscaler/cluster-autoscaler/config"
azclients "k8s.io/legacy-cloud-providers/azure/clients"
"k8s.io/legacy-cloud-providers/azure/clients/vmssclient/mockvmssclient"
"k8s.io/legacy-cloud-providers/azure/clients/vmssvmclient/mockvmssvmclient"

Expand Down Expand Up @@ -153,3 +157,115 @@ func TestNodeGroupForNodeWithNoProviderId(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, group, nil)
}

func TestBuildAzure(t *testing.T) {
expectedConfig := &Config{
Cloud: "AzurePublicCloud",
Location: "southeastasia",
TenantID: "tenantId",
SubscriptionID: "subId",
ResourceGroup: "rg",
VMType: "vmss",
AADClientID: "clientId",
AADClientSecret: "clientsecret",
MaxDeploymentsCount: 10,
CloudProviderRateLimitConfig: CloudProviderRateLimitConfig{
RateLimitConfig: azclients.RateLimitConfig{
CloudProviderRateLimit: false,
CloudProviderRateLimitBucket: 5,
CloudProviderRateLimitBucketWrite: 5,
CloudProviderRateLimitQPS: 1,
CloudProviderRateLimitQPSWrite: 1,
},
InterfaceRateLimit: &azclients.RateLimitConfig{
CloudProviderRateLimit: false,
CloudProviderRateLimitBucket: 5,
CloudProviderRateLimitBucketWrite: 5,
CloudProviderRateLimitQPS: 1,
CloudProviderRateLimitQPSWrite: 1,
},
VirtualMachineRateLimit: &azclients.RateLimitConfig{
CloudProviderRateLimit: false,
CloudProviderRateLimitBucket: 5,
CloudProviderRateLimitBucketWrite: 5,
CloudProviderRateLimitQPS: 1,
CloudProviderRateLimitQPSWrite: 1,
},
StorageAccountRateLimit: &azclients.RateLimitConfig{
CloudProviderRateLimit: false,
CloudProviderRateLimitBucket: 5,
CloudProviderRateLimitBucketWrite: 5,
CloudProviderRateLimitQPS: 1,
CloudProviderRateLimitQPSWrite: 1,
},
DiskRateLimit: &azclients.RateLimitConfig{
CloudProviderRateLimit: false,
CloudProviderRateLimitBucket: 5,
CloudProviderRateLimitBucketWrite: 5,
CloudProviderRateLimitQPS: 1,
CloudProviderRateLimitQPSWrite: 1,
},
VirtualMachineScaleSetRateLimit: &azclients.RateLimitConfig{
CloudProviderRateLimit: false,
CloudProviderRateLimitBucket: 5,
CloudProviderRateLimitBucketWrite: 5,
CloudProviderRateLimitQPS: 1,
CloudProviderRateLimitQPSWrite: 1,
},
},
}

cloudConfig := "cfg.json"
azureCfg := `{
"cloud": "AzurePublicCloud",
"tenantId": "tenantId",
"subscriptionId": "subId",
"aadClientId": "clientId",
"aadClientSecret": "clientsecret",
"resourceGroup": "rg",
"location": "southeastasia"
}`
err := ioutil.WriteFile(cloudConfig, []byte(azureCfg), 0644)
assert.Nil(t, err)
defer os.Remove(cloudConfig)

os.Setenv("ARM_CLOUD", "AzurePublicCloud")
os.Setenv("LOCATION", "southeastasia")
os.Setenv("ARM_RESOURCE_GROUP", "rg")
os.Setenv("ARM_SUBSCRIPTION_ID", "subId")
os.Setenv("ARM_TENANT_ID", "tenantId")
os.Setenv("ARM_CLIENT_ID", "clientId")
os.Setenv("ARM_CLIENT_SECRET", "clientsecret")

resourceLimiter := &cloudprovider.ResourceLimiter{}
discoveryOptions := cloudprovider.NodeGroupDiscoveryOptions{}

testCases := []struct {
name string
cloudConfig string
}{
{
name: "BuildAzure should create Azure Manager using cloud-config file",
cloudConfig: cloudConfig,
},
{
name: "BuildAzure should create Azure Manager using environment variable",
},
}

for _, test := range testCases {
opts := config.AutoscalingOptions{
CloudConfig: test.cloudConfig,
}
cloudProvider := BuildAzure(opts, discoveryOptions, resourceLimiter)
assert.Equal(t, expectedConfig, cloudProvider.(*AzureCloudProvider).azureManager.config, test.name)
}

os.Unsetenv("ARM_CLOUD")
os.Unsetenv("LOCATION")
os.Unsetenv("ARM_RESOURCE_GROUP")
os.Unsetenv("ARM_SUBSCRIPTION_ID")
os.Unsetenv("ARM_TENANT_ID")
os.Unsetenv("ARM_CLIENT_ID")
os.Unsetenv("ARM_CLIENT_SECRET")
}

0 comments on commit c452eee

Please sign in to comment.