Skip to content

Commit

Permalink
test: add account creation leak test
Browse files Browse the repository at this point in the history
fix

fix
  • Loading branch information
andyzhangx committed Apr 16, 2022
1 parent aba979c commit a2bf948
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions test/e2e/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ var _ = ginkgo.AfterSuite(func() {
}
execTestCmd([]testCmd{installDriver, uninstallDriver})

checkAccountCreationLeak()

err := credentials.DeleteAzureCredentialFile()
gomega.Expect(err).NotTo(gomega.HaveOccurred())
})
Expand Down Expand Up @@ -200,6 +202,20 @@ func execTestCmd(cmds []testCmd) {
}
}

func checkAccountCreationLeak() {
creds, err := credentials.CreateAzureCredentialFile()
gomega.Expect(err).NotTo(gomega.HaveOccurred())
azureClient, err := azure.GetClient(creds.Cloud, creds.SubscriptionID, creds.AADClientID, creds.TenantID, creds.AADClientSecret)
gomega.Expect(err).NotTo(gomega.HaveOccurred())

accountNum, err := azureClient.GetAccountNumByResourceGroup(context.TODO(), creds.ResourceGroup)
framework.ExpectNoError(err, fmt.Sprintf("failed to GetAccountNumByResourceGroup(%s): %v", creds.ResourceGroup, err))
ginkgo.By(fmt.Sprintf("GetAccountNumByResourceGroup(%s) returns %d accounts", creds.ResourceGroup, accountNum))

accountLimitInTest := 10
framework.ExpectEqual(accountNum >= accountLimitInTest, false, fmt.Sprintf("current account num %d should not exceed %d", accountNum, accountLimitInTest))
}

// handleFlags sets up all flags and parses the command line.
func handleFlags() {
config.CopyFlags(config.Flags, flag.CommandLine)
Expand Down
12 changes: 12 additions & 0 deletions test/utils/azure/azure_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-02-01/storage"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure"
Expand All @@ -33,6 +34,7 @@ type Client struct {
environment azure.Environment
subscriptionID string
groupsClient resources.GroupsClient
accountsClient storage.AccountsClient
}

func GetClient(cloud, subscriptionID, clientID, tenantID, clientSecret string) (*Client, error) {
Expand Down Expand Up @@ -100,6 +102,14 @@ func (az *Client) DeleteResourceGroup(ctx context.Context, groupName string) err
return nil
}

func (az *Client) GetAccountNumByResourceGroup(ctx context.Context, groupName string) (count int, err error) {
result, err := az.accountsClient.ListByResourceGroup(ctx, groupName)
if err != nil {
return -1, err
}
return len(result.Values()), nil
}

func getOAuthConfig(env azure.Environment, subscriptionID, tenantID string) (*adal.OAuthConfig, error) {
oauthConfig, err := adal.NewOAuthConfig(env.ActiveDirectoryEndpoint, tenantID)
if err != nil {
Expand All @@ -114,10 +124,12 @@ func getClient(env azure.Environment, subscriptionID, tenantID string, armSpt *a
environment: env,
subscriptionID: subscriptionID,
groupsClient: resources.NewGroupsClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
accountsClient: storage.NewAccountsClient(subscriptionID),
}

authorizer := autorest.NewBearerAuthorizer(armSpt)
c.groupsClient.Authorizer = authorizer
c.accountsClient.Authorizer = authorizer

return c
}
Expand Down

0 comments on commit a2bf948

Please sign in to comment.