Skip to content

Commit

Permalink
test: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
comtalyst committed Aug 3, 2024
1 parent 408cdca commit ecd0123
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 18 deletions.
15 changes: 12 additions & 3 deletions cluster-autoscaler/cloudprovider/azure/azure_agent_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,12 @@ func TestDeleteOutdatedDeployments(t *testing.T) {

err := testAS.deleteOutdatedDeployments()
assert.Equal(t, test.expectedErr, err, test.desc)
existedDeployments, err2 := testAS.manager.azClient.deploymentClient.List(context.Background(), "")
existedDeployments, _ := testAS.manager.azClient.deploymentClient.List(context.Background(), "")
existedDeploymentsNames := make(map[string]bool)
for _, deployment := range existedDeployments {
existedDeploymentsNames[*deployment.Name] = true
}
assert.Equal(t, test.expectedDeploymentsNames, existedDeploymentsNames, test.desc)
assert.NoError(t, err2.Error())
}
}

Expand Down Expand Up @@ -209,6 +208,9 @@ func TestGetVMIndexes(t *testing.T) {
as.manager.config.VMType = providerazureconsts.VMTypeStandard
ac, err := newAzureCache(as.manager.azClient, refreshInterval, *as.manager.config)
assert.NoError(t, err)
as.manager.azureCache = ac

sortedIndexes, indexToVM, err := as.GetVMIndexes()
assert.NoError(t, err)
assert.Equal(t, 2, len(sortedIndexes))
assert.Equal(t, 2, len(indexToVM))
Expand Down Expand Up @@ -250,8 +252,12 @@ func TestGetCurSize(t *testing.T) {

as.lastRefresh = time.Now()
curSize, err := as.getCurSize()
assert.NoError(t, err)
assert.Equal(t, int64(1), curSize)

as.lastRefresh = time.Now().Add(-1 * 3 * time.Minute)
curSize, err = as.getCurSize()
assert.NoError(t, err)
assert.Equal(t, int64(2), curSize)
}

Expand Down Expand Up @@ -298,13 +304,16 @@ func TestAgentPoolIncreaseSize(t *testing.T) {
assert.NoError(t, err)
err = as.IncreaseSize(4)
expectedErr = fmt.Errorf("size increase too large - desired:6 max:5")
assert.Error(t, expectedErr, err)

err = as.IncreaseSize(2)
assert.NoError(t, err)
}

func TestDecreaseTargetSize(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

as := newTestAgentPool(newTestAzureManager(t), "as")
as.curSize = 3
mockVMClient := mockvmclient.NewMockInterface(ctrl)
as.manager.azClient.virtualMachinesClient = mockVMClient
Expand Down
41 changes: 30 additions & 11 deletions cluster-autoscaler/cloudprovider/azure/azure_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,28 @@ import (
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/stretchr/testify/assert"
azclient "sigs.k8s.io/cloud-provider-azure/pkg/azclient"
providerazure "sigs.k8s.io/cloud-provider-azure/pkg/provider"
providerazureconfig "sigs.k8s.io/cloud-provider-azure/pkg/provider/config"
)

func TestGetServicePrincipalTokenFromCertificate(t *testing.T) {
config := &Config{
TenantID: "TenantID",
AADClientID: "AADClientID",
AADClientCertPath: "./testdata/test.pfx",
AADClientCertPassword: "id",
Config: providerazure.Config{
AzureAuthConfig: providerazureconfig.AzureAuthConfig{
ARMClientConfig: azclient.ARMClientConfig{
TenantID: "TenantID",
},
AzureAuthConfig: azclient.AzureAuthConfig{
AADClientID: "AADClientID",
AADClientCertPath: "./testdata/test.pfx",
AADClientCertPassword: "id",
},
},
},
}
env := &azure.PublicCloud
token, err := newServicePrincipalTokenFromCredentials(config, env)
token, err := providerazureconfig.GetServicePrincipalToken(&config.AzureAuthConfig, env, "")
assert.NoError(t, err)

oauthConfig, err := adal.NewOAuthConfig(env.ActiveDirectoryEndpoint, config.TenantID)
Expand All @@ -45,17 +56,25 @@ func TestGetServicePrincipalTokenFromCertificate(t *testing.T) {
spt, err := adal.NewServicePrincipalTokenFromCertificate(
*oauthConfig, config.AADClientID, certificate, privateKey, env.ServiceManagementEndpoint)
assert.NoError(t, err)
assert.Equal(t, token, spt)
assert.Equal(t, token.Token(), spt.Token())
}

func TestGetServicePrincipalTokenFromCertificateWithoutPassword(t *testing.T) {
config := &Config{
TenantID: "TenantID",
AADClientID: "AADClientID",
AADClientCertPath: "./testdata/testnopassword.pfx",
Config: providerazure.Config{
AzureAuthConfig: providerazureconfig.AzureAuthConfig{
ARMClientConfig: azclient.ARMClientConfig{
TenantID: "TenantID",
},
AzureAuthConfig: azclient.AzureAuthConfig{
AADClientID: "AADClientID",
AADClientCertPath: "./testdata/testnopassword.pfx",
},
},
},
}
env := &azure.PublicCloud
token, err := newServicePrincipalTokenFromCredentials(config, env)
token, err := providerazureconfig.GetServicePrincipalToken(&config.AzureAuthConfig, env, "")
assert.NoError(t, err)

oauthConfig, err := adal.NewOAuthConfig(env.ActiveDirectoryEndpoint, config.TenantID)
Expand All @@ -67,5 +86,5 @@ func TestGetServicePrincipalTokenFromCertificateWithoutPassword(t *testing.T) {
spt, err := adal.NewServicePrincipalTokenFromCertificate(
*oauthConfig, config.AADClientID, certificate, privateKey, env.ServiceManagementEndpoint)
assert.NoError(t, err)
assert.Equal(t, token, spt)
assert.Equal(t, token.Token(), spt.Token())
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func newTestAzureManager(t *testing.T) *AzureManager {
MaxDeploymentsCount: 2,
Deployment: "deployment",
EnableForceDelete: false,
Location: "eastus",
},
azClient: &azClient{
virtualMachineScaleSetsClient: mockVMSSClient,
Expand Down
3 changes: 3 additions & 0 deletions cluster-autoscaler/cloudprovider/azure/azure_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ func BuildAzureConfig(configReader io.Reader) (*Config, error) {
if _, err = assignIntFromEnvIfExists(&cfg.VmssVmsCacheJitter, "AZURE_VMSS_VMS_CACHE_JITTER"); err != nil {
return nil, err
}
if _, err = assignIntFromEnvIfExists(&cfg.GetVmssSizeRefreshPeriod, "AZURE_GET_VMSS_SIZE_REFRESH_PERIOD"); err != nil {
return nil, err
}
if _, err = assignInt64FromEnvIfExists(&cfg.MaxDeploymentsCount, "AZURE_MAX_DEPLOYMENT_COUNT"); err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ func TestCreateAzureManagerWithNilConfig(t *testing.T) {
t.Run("invalid int for AZURE_GET_VMSS_SIZE_REFRESH_PERIOD", func(t *testing.T) {
t.Setenv("AZURE_GET_VMSS_SIZE_REFRESH_PERIOD", "invalidint")
manager, err := createAzureManagerInternal(nil, cloudprovider.NodeGroupDiscoveryOptions{}, mockAzClient)
expectedErr := fmt.Errorf("failed to parse AZURE_GET_VMSS_SIZE_REFRESH_PERIOD \"invalidint\": strconv.Atoi: parsing \"invalidint\": invalid syntax")
expectedErr := fmt.Errorf("failed to parse AZURE_GET_VMSS_SIZE_REFRESH_PERIOD \"invalidint\": strconv.ParseInt: parsing \"invalidint\": invalid syntax")
assert.Nil(t, manager)
assert.Equal(t, expectedErr, err, "Return err does not match, expected: %v, actual: %v", expectedErr, err)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ func (scaleSet *ScaleSet) updateInstanceCache() error {
}

if orchestrationMode == compute.Flexible {
if scaleSet.manager.config.EnableVmssFlex {
if scaleSet.manager.config.EnableVmssFlexNodes {
return scaleSet.buildScaleSetCacheForFlex()
}
return fmt.Errorf("vmss - %q with Flexible orchestration detected but 'enableVmssFlex' feature flag is turned off", scaleSet.Name)
return fmt.Errorf("vmss - %q with Flexible orchestration detected but 'enableVmssFlexNodes' feature flag is turned off", scaleSet.Name)
} else if orchestrationMode == compute.Uniform {
return scaleSet.buildScaleSetCacheForUniform()
}
Expand Down

0 comments on commit ecd0123

Please sign in to comment.