@@ -4,19 +4,36 @@ import (
44 "context"
55 "flag"
66 "fmt"
7+ "regexp"
8+ "strconv"
9+ "testing"
10+
711 "github.com/RedisLabs/rediscloud-go-api/redis"
8- client2 "github.com/RedisLabs/terraform-provider-rediscloud/provider/client"
12+ "github.com/RedisLabs/terraform-provider-rediscloud/provider/client"
913 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1014 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1115 "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
12- "regexp"
13- "strconv"
14- "testing"
1516)
1617
1718var essentialsMarketplaceFlag = flag .Bool ("essentialsMarketplace" , false ,
1819 "Add this flag '-essentialsMarketplace' to run tests for marketplace associated accounts" )
1920
21+ // testAccPreCheckEssentialsSubscription checks if an essentials subscription already exists
22+ // and fails fast to avoid the test failing after provisioning attempts
23+ func testAccPreCheckEssentialsSubscription (t * testing.T ) {
24+ testAccPreCheck (t )
25+
26+ apiClient := testProvider .Meta ().(* client.ApiClient )
27+ subs , err := apiClient .Client .FixedSubscriptions .List (context .TODO ())
28+ if err != nil {
29+ t .Fatalf ("Failed to list essentials subscriptions: %v" , err )
30+ }
31+
32+ if len (subs ) > 0 {
33+ t .Skipf ("Essentials subscription already exists (ID: %d). Redis Cloud allows only 1 essentials subscription per account. Please delete the existing subscription before running this test." , redis .IntValue (subs [0 ].ID ))
34+ }
35+ }
36+
2037func TestAccResourceRedisCloudEssentialsSubscription_Free_CRUDI (t * testing.T ) {
2138
2239 testAccRequiresEnvVar (t , "EXECUTE_TESTS" )
@@ -28,7 +45,7 @@ func TestAccResourceRedisCloudEssentialsSubscription_Free_CRUDI(t *testing.T) {
2845 const datasourceName = "data.rediscloud_essentials_subscription.example"
2946
3047 resource .Test (t , resource.TestCase {
31- PreCheck : func () { testAccPreCheck (t ) },
48+ PreCheck : func () { testAccPreCheckEssentialsSubscription (t ) },
3249 ProviderFactories : providerFactories ,
3350 CheckDestroy : testAccCheckEssentialsSubscriptionDestroy ,
3451 Steps : []resource.TestStep {
@@ -93,7 +110,7 @@ func TestAccResourceRedisCloudEssentialsSubscription_Paid_CreditCard_CRUDI(t *te
93110 const datasourceName = "data.rediscloud_essentials_subscription.example"
94111
95112 resource .Test (t , resource.TestCase {
96- PreCheck : func () { testAccPreCheck (t ) },
113+ PreCheck : func () { testAccPreCheckEssentialsSubscription (t ) },
97114 ProviderFactories : providerFactories ,
98115 CheckDestroy : testAccCheckEssentialsSubscriptionDestroy ,
99116 Steps : []resource.TestStep {
@@ -159,7 +176,7 @@ func TestAccResourceRedisCloudEssentialsSubscription_Paid_NoPaymentType_CRUDI(t
159176 const datasourceName = "data.rediscloud_essentials_subscription.example"
160177
161178 resource .Test (t , resource.TestCase {
162- PreCheck : func () { testAccPreCheck (t ) },
179+ PreCheck : func () { testAccPreCheckEssentialsSubscription (t ) },
163180 ProviderFactories : providerFactories ,
164181 CheckDestroy : testAccCheckEssentialsSubscriptionDestroy ,
165182 Steps : []resource.TestStep {
@@ -230,7 +247,7 @@ func TestAccResourceRedisCloudEssentialsSubscription_Paid_Marketplace_CRUDI(t *t
230247 const datasourceName = "data.rediscloud_essentials_subscription.example"
231248
232249 resource .Test (t , resource.TestCase {
233- PreCheck : func () { testAccPreCheck (t ) },
250+ PreCheck : func () { testAccPreCheckEssentialsSubscription (t ) },
234251 ProviderFactories : providerFactories ,
235252 CheckDestroy : testAccCheckEssentialsSubscriptionDestroy ,
236253 Steps : []resource.TestStep {
@@ -414,7 +431,7 @@ data "rediscloud_essentials_subscription" "example" {
414431`
415432
416433func testAccCheckEssentialsSubscriptionDestroy (s * terraform.State ) error {
417- client := testProvider .Meta ().(* client2 .ApiClient )
434+ apiClient := testProvider .Meta ().(* client .ApiClient )
418435
419436 for _ , r := range s .RootModule ().Resources {
420437 if r .Type != "rediscloud_essentials_subscription" {
@@ -426,7 +443,7 @@ func testAccCheckEssentialsSubscriptionDestroy(s *terraform.State) error {
426443 return err
427444 }
428445
429- subs , err := client .Client .FixedSubscriptions .List (context .TODO ())
446+ subs , err := apiClient .Client .FixedSubscriptions .List (context .TODO ())
430447 if err != nil {
431448 return err
432449 }
0 commit comments