Skip to content

Commit

Permalink
Clean up Cloud API keys from tests (#651)
Browse files Browse the repository at this point in the history
If a test fails, they aren't always deleted and they tend to add up
This will clean them up before each test run. That way, we'll have at most one of each prefix
  • Loading branch information
julienduchesne authored Sep 15, 2022
1 parent ca46f0d commit 16ee575
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
24 changes: 23 additions & 1 deletion grafana/resource_cloud_api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package grafana
import (
"fmt"
"os"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand All @@ -14,6 +15,9 @@ func TestAccCloudApiKey_Basic(t *testing.T) {
t.Parallel()
CheckCloudAPITestsEnabled(t)

prefix := "testcloudkey-"
testAccDeleteExistingCloudAPIKeys(t, prefix)

var tests = []struct {
role string
}{
Expand All @@ -26,7 +30,7 @@ func TestAccCloudApiKey_Basic(t *testing.T) {

for _, tt := range tests {
t.Run(tt.role, func(t *testing.T) {
resourceName := "zzztest-" + acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
resourceName := prefix + acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

resource.Test(t, resource.TestCase{
ProviderFactories: testAccProviderFactories,
Expand Down Expand Up @@ -104,6 +108,24 @@ func testAccCheckCloudAPIKeyDestroy(s *terraform.State) error {
return nil
}

func testAccDeleteExistingCloudAPIKeys(t *testing.T, prefix string) {
org := os.Getenv("GRAFANA_CLOUD_ORG")
client := testAccProvider.Meta().(*client).gcloudapi
resp, err := client.ListCloudAPIKeys(org)
if err != nil {
t.Error(err)
}

for _, key := range resp.Items {
if strings.HasPrefix(key.Name, prefix) {
err := client.DeleteCloudAPIKey(org, key.Name)
if err != nil {
t.Error(err)
}
}
}
}

func testAccCloudAPIKeyConfig(resourceName, role string) string {
// GRAFANA_CLOUD_ORG is required from the `CheckCloudAPITestsEnabled` function
return fmt.Sprintf(`
Expand Down
9 changes: 5 additions & 4 deletions grafana/resource_synthetic_monitoring_installation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ func TestAccSyntheticMonitoringInstallation(t *testing.T) {

var stack gapi.Stack
stackPrefix := "tfsminstalltest"
testAccDeleteExistingStacks(t, stackPrefix)
stackSlug := GetRandomStackName(stackPrefix)
apiKeyName := "zzztest-" + acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

apiKeyPrefix := "testsminstall-"
testAccDeleteExistingCloudAPIKeys(t, apiKeyPrefix)
apiKeyName := apiKeyPrefix + acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccDeleteExistingStacks(t, stackPrefix)
},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccStackCheckDestroy(&stack),
Steps: []resource.TestStep{
Expand Down

0 comments on commit 16ee575

Please sign in to comment.