From 16ee5757e30fac90a5e3b708a191da73731af86a Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Thu, 15 Sep 2022 07:19:50 -0400 Subject: [PATCH] Clean up Cloud API keys from tests (#651) 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 --- grafana/resource_cloud_api_key_test.go | 24 ++++++++++++++++++- ..._synthetic_monitoring_installation_test.go | 9 +++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/grafana/resource_cloud_api_key_test.go b/grafana/resource_cloud_api_key_test.go index 9d2ffcff2..77b2c5856 100644 --- a/grafana/resource_cloud_api_key_test.go +++ b/grafana/resource_cloud_api_key_test.go @@ -3,6 +3,7 @@ package grafana import ( "fmt" "os" + "strings" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -14,6 +15,9 @@ func TestAccCloudApiKey_Basic(t *testing.T) { t.Parallel() CheckCloudAPITestsEnabled(t) + prefix := "testcloudkey-" + testAccDeleteExistingCloudAPIKeys(t, prefix) + var tests = []struct { role string }{ @@ -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, @@ -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(` diff --git a/grafana/resource_synthetic_monitoring_installation_test.go b/grafana/resource_synthetic_monitoring_installation_test.go index 330ab64f2..ce41e9b56 100644 --- a/grafana/resource_synthetic_monitoring_installation_test.go +++ b/grafana/resource_synthetic_monitoring_installation_test.go @@ -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{