Skip to content

Commit 5d88e9e

Browse files
author
k-yomo
committed
Add prefix for resource names not to delete unexpected resources with teardown command
1 parent 9734145 commit 5d88e9e

11 files changed

+29
-18
lines changed

internal/algoliautil/testutil.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package algoliautil
2+
3+
const TestIndexNamePrefix = "tf_provider_test"

internal/provider/data_source_index_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestAccDataSourceIndex(t *testing.T) {
11-
indexName := randStringStartWithAlpha(100)
11+
indexName := randResourceID(100)
1212
dataSourceName := fmt.Sprintf("data.algolia_index.%s", indexName)
1313

1414
resource.ParallelTest(t, resource.TestCase{

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package provider
22

33
import (
44
"context"
5+
56
"github.com/algolia/algoliasearch-client-go/v3/algolia/region"
67
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
78
"github.com/algolia/algoliasearch-client-go/v3/algolia/suggestions"

internal/provider/resource_api_key_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func TestAccResourceAPIKey(t *testing.T) {
15-
name := randStringStartWithAlpha(100)
15+
name := randResourceID(100)
1616
resourceName := fmt.Sprintf("algolia_api_key.%s", name)
1717

1818
resource.ParallelTest(t, resource.TestCase{

internal/provider/resource_index_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// TODO: Cover all fields
1313
func TestAccResourceIndex(t *testing.T) {
14-
indexName := randStringStartWithAlpha(100)
14+
indexName := randResourceID(100)
1515
resourceName := fmt.Sprintf("algolia_index.%s", indexName)
1616

1717
resource.ParallelTest(t, resource.TestCase{
@@ -90,7 +90,7 @@ func TestAccResourceIndexWithReplica(t *testing.T) {
9090
// TODO: Remove t.Skip() once the issue is resolved.
9191
t.Skip()
9292

93-
primaryIndexName := randStringStartWithAlpha(80)
93+
primaryIndexName := randResourceID(80)
9494
replicaIndexName := fmt.Sprintf("%s_replica", primaryIndexName)
9595
primaryIndexResourceName := fmt.Sprintf("algolia_index.%s", primaryIndexName)
9696
replicaIndexResourceName := fmt.Sprintf("algolia_index.%s", replicaIndexName)

internal/provider/resource_query_suggestions_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
)
1313

1414
func TestAccResourceQuerySuggestions(t *testing.T) {
15-
indexName := randStringStartWithAlpha(100)
16-
sourceIndexName := randStringStartWithAlpha(100)
15+
indexName := randResourceID(100)
16+
sourceIndexName := randResourceID(100)
1717
resourceName := fmt.Sprintf("algolia_query_suggestions.%s", indexName)
1818

1919
resource.ParallelTest(t, resource.TestCase{

internal/provider/resource_rule_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
)
1212

1313
func TestAccResourceRule(t *testing.T) {
14-
indexName := randStringStartWithAlpha(100)
15-
objectID := randStringStartWithAlpha(64)
14+
indexName := randResourceID(100)
15+
objectID := randResourceID(64)
1616
resourceName := fmt.Sprintf("algolia_rule.%s", objectID)
1717

1818
resource.ParallelTest(t, resource.TestCase{

internal/provider/resource_synonyms_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestAccResourceSynonyms(t *testing.T) {
13-
indexName := randStringStartWithAlpha(100)
13+
indexName := randResourceID(100)
1414
resourceName := fmt.Sprintf("algolia_synonyms.%s", indexName)
1515

1616
resource.ParallelTest(t, resource.TestCase{

internal/provider/resource_virtual_index_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestAccResourceVirtualIndex(t *testing.T) {
1313
// TODO: Remove t.Skip() once the issue is resolved.
1414
t.Skip()
1515

16-
indexName := randStringStartWithAlpha(80)
16+
indexName := randResourceID(80)
1717
virtualIndexName := fmt.Sprintf("%s_virtual", indexName)
1818
indexResourceName := fmt.Sprintf("algolia_index.%s", indexName)
1919
virtualIndexResourceName := fmt.Sprintf("algolia_virtual_index.%s", virtualIndexName)

internal/provider/testutils.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-provider-algolia/internal/algoliautil"
89
"github.com/rs/xid"
910
)
1011

@@ -17,14 +18,15 @@ func testCheckResourceListAttr(name, key string, values []string) resource.TestC
1718
return resource.ComposeTestCheckFunc(testCheckFuncs...)
1819
}
1920

20-
// The first character must be alphabet for algolia resources
21-
func randStringStartWithAlpha(length int) string {
22-
const uuidLen = 21 // firstChar + xid(20 chars)
23-
uuid := acctest.RandStringFromCharSet(1, acctest.CharSetAlpha) + xid.New().String()
21+
// randResourceID generates unique id string
22+
// id length must be longer than (prefix + uuid length)
23+
func randResourceID(length int) string {
24+
// The first character must be alphabet for algolia resources
25+
uuid := algoliautil.TestIndexNamePrefix + xid.New().String()
2426

25-
if length < uuidLen {
26-
return uuid[:length]
27+
if length < len(uuid) {
28+
panic(fmt.Sprintf("length must be longer than %d", len(uuid)))
2729
}
2830

29-
return uuid + acctest.RandStringFromCharSet(length-uuidLen, acctest.CharSetAlphaNum)
31+
return uuid + acctest.RandStringFromCharSet(length-len(uuid), acctest.CharSetAlphaNum)
3032
}

0 commit comments

Comments
 (0)