Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
Zlaticanin committed Nov 3, 2023
1 parent 50ba261 commit 6ff9c4b
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import (
// }
// ]
// EOF

type VaultCredResponse struct {
Data struct {
ClientId string `json:"client_id"`
Expand All @@ -75,7 +74,7 @@ func TestSPCredentials(t *testing.T) {

// use dynamic credentials from Vault instead of hardcoding them.
// Use a regular HTTP client to make the request
req, err := http.NewRequest(http.MethodGet, "http://localhost:8200/v1/local-azure/creds/test-role", nil)
req, err := http.NewRequest(http.MethodGet, "http://localhost:8200/v1/azure/creds/test-role", nil)
assert.NoError(t, err)
req.Header.Add("X-Vault-Token", "root")

Expand All @@ -89,12 +88,11 @@ func TestSPCredentials(t *testing.T) {

clientID := response.Data.ClientId
clientSecret := response.Data.ClientSecret
fmt.Println("client_id: ", clientID)
fmt.Println("clientSecret: ", clientSecret)

// Introduce a delay between generating creds and using them
// time.Sleep(5 * time.Second)

newUUID := uuid.New().String()
var successes uint64
var wg sync.WaitGroup
creds, err := azidentity.NewClientSecretCredential(
Expand All @@ -109,7 +107,7 @@ func TestSPCredentials(t *testing.T) {
for i := 0; i < n; i++ {
go func() {
defer wg.Done()
count, err := helper(ctx, resourceGroupClient)
count, err := createResourceGroup(ctx, resourceGroupClient, newUUID)
if err == nil {
atomic.AddUint64(&successes, 1)
_ = count
Expand All @@ -127,24 +125,24 @@ func TestSPCredentials(t *testing.T) {
fmt.Println("Num Auth failures", authFailures)
assert.EqualValues(t, n, successes, successes)

err = cleanup(ctx, resourceGroupClient)
err = cleanupResourceGroups(ctx, resourceGroupClient, newUUID)
assert.NoError(t, err)
}

func helper(ctx context.Context, rgClient *armresources.ResourceGroupsClient) (armresources.ResourceGroupsClientCreateOrUpdateResponse, error) {
func createResourceGroup(ctx context.Context, rgClient *armresources.ResourceGroupsClient, newUUID string) (armresources.ResourceGroupsClientCreateOrUpdateResponse, error) {
resp, err := rgClient.CreateOrUpdate(ctx, fmt.Sprintf("%v-%v", "vault-test", uuid.New().String()), armresources.ResourceGroup{
Location: to.StringPtr("West US"),
Tags: map[string]*string{"created_by": to.StringPtr("vault-test-{UUID}")},
Tags: map[string]*string{"created_by": to.StringPtr(fmt.Sprintf("vault-test-%s", newUUID))},
}, nil)
if err != nil {
return armresources.ResourceGroupsClientCreateOrUpdateResponse{}, err
}
return resp, nil
}

func cleanup(ctx context.Context, rgClient *armresources.ResourceGroupsClient) error {
func cleanupResourceGroups(ctx context.Context, rgClient *armresources.ResourceGroupsClient, newUUID string) error {
pager := rgClient.NewListPager(&armresources.ResourceGroupsClientListOptions{
Filter: to.StringPtr("tagName eq 'created_by' and tagValue eq 'vault-test-{UUID}'"),
Filter: to.StringPtr(fmt.Sprintf("tagName eq 'created_by' and tagValue eq 'vault-test-%s'", newUUID)),
Top: nil,
})
var counter uint64
Expand Down

0 comments on commit 6ff9c4b

Please sign in to comment.