Skip to content

Commit b34da1e

Browse files
Fix TestAccGrafanaServiceAccountFromCloud (#1231)
This is a weird one. This test was failing but returning: `a stack with the name 'tfserviceaccounttestqnkmr08pvm' already exists` See https://github.com/grafana/terraform-provider-grafana/actions/runs/7211112859/job/19645774937 The reason for that error was that the stack name in the test was too long (over 29 chars) and the API returns a 409 Conflict in that case (weird). I changed the stack name + I fixed the error management and now the test passes
1 parent 7cd039c commit b34da1e

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

internal/resources/cloud/resource_cloud_stack.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,14 @@ func CreateStack(ctx context.Context, d *schema.ResourceData, meta interface{})
241241
err := retry.RetryContext(ctx, 1*time.Minute, func() *retry.RetryError {
242242
stackID, err := client.NewStack(stack)
243243
switch {
244-
case err != nil && strings.Contains(err.Error(), "409"):
245-
// If the API returns a 409, it means that the stack already exists
244+
case err != nil && strings.Contains(strings.ToLower(err.Error()), "conflict"):
245+
// If the API returns a conflict error, it means that the stack already exists
246246
// It may also mean that the stack was recently deleted and is still in the process of being deleted
247247
// In that case, we want to retry
248248
time.Sleep(10 * time.Second) // Do not retry too fast, default is 500ms
249-
return retry.RetryableError(fmt.Errorf("a stack with the name '%s' already exists", stack.Name))
249+
return retry.RetryableError(fmt.Errorf("a stack with the name '%s' already exists: %w", stack.Name, err))
250250
case err != nil:
251-
// If we had an error that isn't a 409 (already exists), try to read the stack
251+
// If we had an error that isn't a a conflict error (already exists), try to read the stack
252252
// Sometimes, the stack is created but the API returns an error (e.g. 504)
253253
readStack, readErr := client.StackBySlug(stack.Slug)
254254
if readErr == nil {

internal/resources/cloud/resource_cloud_stack_service_account_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cloud_test
33
import (
44
"errors"
55
"fmt"
6+
"strings"
67
"testing"
78
"time"
89

@@ -17,7 +18,7 @@ func TestAccGrafanaServiceAccountFromCloud(t *testing.T) {
1718
testutils.CheckCloudAPITestsEnabled(t)
1819

1920
var stack gapi.Stack
20-
prefix := "tfserviceaccounttest"
21+
prefix := "tfsatest"
2122
slug := GetRandomStackName(prefix)
2223

2324
resource.ParallelTest(t, resource.TestCase{
@@ -83,17 +84,19 @@ func testAccGrafanaServiceAccountCheckDestroyCloud(s *terraform.State) error {
8384
}
8485

8586
for _, sa := range response {
87+
if strings.HasPrefix(sa.Name, "test-service-account-") {
88+
continue // this is a service account created by this test
89+
}
90+
8691
tokens, err := c.GetServiceAccountTokens(sa.ID)
8792
if err != nil {
8893
return err
8994
}
9095
if len(tokens) > 0 {
9196
return fmt.Errorf("found unexpected service account tokens for service account %s: %v", sa.Name, tokens)
9297
}
93-
}
9498

95-
if len(response) > 0 {
96-
return fmt.Errorf("found unexpected service accounts: %v", response)
99+
return fmt.Errorf("found unexpected service account: %v", sa)
97100
}
98101

99102
return nil

0 commit comments

Comments
 (0)