|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/RedisLabs/rediscloud-go-api/redis" |
| 11 | + "github.com/RedisLabs/terraform-provider-rediscloud/provider/client" |
| 12 | + "github.com/RedisLabs/terraform-provider-rediscloud/provider/utils" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 15 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 16 | +) |
| 17 | + |
| 18 | +// TestAccResourceRedisCloudActiveActiveDatabase_enableDefaultUserDebug is a DEBUG version |
| 19 | +// that reuses an existing subscription to speed up testing during development. |
| 20 | +// |
| 21 | +// SETUP: |
| 22 | +// 1. Set DEBUG_SUBSCRIPTION_ID environment variable to an existing AA subscription ID |
| 23 | +// 2. The subscription must have us-east-1 and us-east-2 regions |
| 24 | +// 3. Run with: DEBUG_SUBSCRIPTION_ID=12345 EXECUTE_TESTS=true make testacc TESTARGS='-run=TestAccResourceRedisCloudActiveActiveDatabase_enableDefaultUserDebug' |
| 25 | +// |
| 26 | +// This test will: |
| 27 | +// - Create a new database in the existing subscription |
| 28 | +// - Update it through 2 test steps (global=true variants) |
| 29 | +// - Delete the database at the end |
| 30 | +func TestAccResourceRedisCloudActiveActiveDatabase_enableDefaultUserDebug(t *testing.T) { |
| 31 | + utils.AccRequiresEnvVar(t, "EXECUTE_TESTS") |
| 32 | + |
| 33 | + subscriptionID := os.Getenv("DEBUG_SUBSCRIPTION_ID") |
| 34 | + if subscriptionID == "" { |
| 35 | + t.Skip("DEBUG_SUBSCRIPTION_ID not set - skipping debug test") |
| 36 | + } |
| 37 | + |
| 38 | + databaseName := acctest.RandomWithPrefix("debug-enable-default-user") |
| 39 | + databasePassword := acctest.RandString(20) |
| 40 | + |
| 41 | + const databaseResourceName = "rediscloud_active_active_subscription_database.example" |
| 42 | + |
| 43 | + resource.ParallelTest(t, resource.TestCase{ |
| 44 | + PreCheck: func() { testAccPreCheck(t); testAccAwsPreExistingCloudAccountPreCheck(t) }, |
| 45 | + ProviderFactories: providerFactories, |
| 46 | + CheckDestroy: testAccCheckActiveActiveDatabaseDestroy, |
| 47 | + Steps: []resource.TestStep{ |
| 48 | + // Step 1: global=true, both regions inherit |
| 49 | + { |
| 50 | + PreConfig: func() { |
| 51 | + t.Logf("DEBUG Step 1: global=true, both inherit (subscription: %s, database: %s)", subscriptionID, databaseName) |
| 52 | + }, |
| 53 | + Config: fmt.Sprintf( |
| 54 | + utils.GetTestConfig(t, "./activeactive/testdata/enable_default_user_debug_step1.tf"), |
| 55 | + subscriptionID, |
| 56 | + databaseName, |
| 57 | + databasePassword, |
| 58 | + ), |
| 59 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 60 | + // Global setting |
| 61 | + resource.TestCheckResourceAttr(databaseResourceName, "global_enable_default_user", "true"), |
| 62 | + resource.TestCheckResourceAttr(databaseResourceName, "subscription_id", subscriptionID), |
| 63 | + |
| 64 | + // Both regions should exist |
| 65 | + resource.TestCheckResourceAttr(databaseResourceName, "override_region.#", "2"), |
| 66 | + |
| 67 | + // Neither region should have enable_default_user in state |
| 68 | + resource.TestCheckNoResourceAttr(databaseResourceName, "override_region.0.enable_default_user"), |
| 69 | + resource.TestCheckNoResourceAttr(databaseResourceName, "override_region.1.enable_default_user"), |
| 70 | + |
| 71 | + // API check |
| 72 | + testCheckEnableDefaultUserInAPIDebug(databaseResourceName, true, map[string]*bool{ |
| 73 | + "us-east-1": nil, |
| 74 | + "us-east-2": nil, |
| 75 | + }), |
| 76 | + ), |
| 77 | + }, |
| 78 | + // Step 2: global=true, us-east-1 explicit false |
| 79 | + { |
| 80 | + PreConfig: func() { |
| 81 | + t.Logf("DEBUG Step 2: global=true, us-east-1 explicit false") |
| 82 | + }, |
| 83 | + Config: fmt.Sprintf( |
| 84 | + utils.GetTestConfig(t, "./activeactive/testdata/enable_default_user_debug_step2.tf"), |
| 85 | + subscriptionID, |
| 86 | + databaseName, |
| 87 | + databasePassword, |
| 88 | + ), |
| 89 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 90 | + // Global setting |
| 91 | + resource.TestCheckResourceAttr(databaseResourceName, "global_enable_default_user", "true"), |
| 92 | + |
| 93 | + // Two regions |
| 94 | + resource.TestCheckResourceAttr(databaseResourceName, "override_region.#", "2"), |
| 95 | + |
| 96 | + // us-east-1 has explicit false |
| 97 | + resource.TestCheckTypeSetElemNestedAttrs(databaseResourceName, "override_region.*", map[string]string{ |
| 98 | + "name": "us-east-1", |
| 99 | + "enable_default_user": "false", |
| 100 | + }), |
| 101 | + |
| 102 | + // API check |
| 103 | + testCheckEnableDefaultUserInAPIDebug(databaseResourceName, true, map[string]*bool{ |
| 104 | + "us-east-1": redis.Bool(false), |
| 105 | + "us-east-2": nil, |
| 106 | + }), |
| 107 | + ), |
| 108 | + }, |
| 109 | + }, |
| 110 | + }) |
| 111 | +} |
| 112 | + |
| 113 | +// testCheckEnableDefaultUserInAPIDebug is identical to the regular version but for the debug test |
| 114 | +func testCheckEnableDefaultUserInAPIDebug(resourceName string, expectedGlobal bool, expectedRegions map[string]*bool) resource.TestCheckFunc { |
| 115 | + return func(s *terraform.State) error { |
| 116 | + rs, ok := s.RootModule().Resources[resourceName] |
| 117 | + if !ok { |
| 118 | + return fmt.Errorf("resource not found: %s", resourceName) |
| 119 | + } |
| 120 | + |
| 121 | + subIdStr := rs.Primary.Attributes["subscription_id"] |
| 122 | + dbIdStr := rs.Primary.Attributes["db_id"] |
| 123 | + |
| 124 | + subId, err := strconv.Atoi(subIdStr) |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("failed to parse subscription_id: %v", err) |
| 127 | + } |
| 128 | + |
| 129 | + dbId, err := strconv.Atoi(dbIdStr) |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("failed to parse db_id: %v", err) |
| 132 | + } |
| 133 | + |
| 134 | + apiClient, err := client.NewClient() |
| 135 | + if err != nil { |
| 136 | + return fmt.Errorf("failed to get API client: %v", err) |
| 137 | + } |
| 138 | + |
| 139 | + ctx := context.Background() |
| 140 | + db, err := apiClient.Client.Database.GetActiveActive(ctx, subId, dbId) |
| 141 | + if err != nil { |
| 142 | + return fmt.Errorf("failed to get database from API: %v", err) |
| 143 | + } |
| 144 | + |
| 145 | + if db.GlobalEnableDefaultUser == nil { |
| 146 | + return fmt.Errorf("API returned nil for GlobalEnableDefaultUser") |
| 147 | + } |
| 148 | + actualGlobal := redis.BoolValue(db.GlobalEnableDefaultUser) |
| 149 | + if actualGlobal != expectedGlobal { |
| 150 | + return fmt.Errorf("API global_enable_default_user: expected %v, got %v", expectedGlobal, actualGlobal) |
| 151 | + } |
| 152 | + |
| 153 | + for _, regionDb := range db.CrdbDatabases { |
| 154 | + regionName := redis.StringValue(regionDb.Region) |
| 155 | + |
| 156 | + if regionDb.Security == nil || regionDb.Security.EnableDefaultUser == nil { |
| 157 | + return fmt.Errorf("API returned nil for region %s EnableDefaultUser", regionName) |
| 158 | + } |
| 159 | + |
| 160 | + actualRegionValue := redis.BoolValue(regionDb.Security.EnableDefaultUser) |
| 161 | + |
| 162 | + expectedValue, hasExplicitOverride := expectedRegions[regionName] |
| 163 | + |
| 164 | + var expectedRegionValue bool |
| 165 | + if hasExplicitOverride && expectedValue != nil { |
| 166 | + expectedRegionValue = *expectedValue |
| 167 | + } else { |
| 168 | + expectedRegionValue = expectedGlobal |
| 169 | + } |
| 170 | + |
| 171 | + if actualRegionValue != expectedRegionValue { |
| 172 | + return fmt.Errorf("API region %s enable_default_user: expected %v, got %v", |
| 173 | + regionName, expectedRegionValue, actualRegionValue) |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return nil |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +// testAccCheckActiveActiveDatabaseDestroy verifies the database was destroyed (subscription remains) |
| 182 | +func testAccCheckActiveActiveDatabaseDestroy(s *terraform.State) error { |
| 183 | + apiClient, err := client.NewClient() |
| 184 | + if err != nil { |
| 185 | + return err |
| 186 | + } |
| 187 | + |
| 188 | + for _, rs := range s.RootModule().Resources { |
| 189 | + if rs.Type != "rediscloud_active_active_subscription_database" { |
| 190 | + continue |
| 191 | + } |
| 192 | + |
| 193 | + subId, err := strconv.Atoi(rs.Primary.Attributes["subscription_id"]) |
| 194 | + if err != nil { |
| 195 | + continue |
| 196 | + } |
| 197 | + |
| 198 | + dbId, err := strconv.Atoi(rs.Primary.Attributes["db_id"]) |
| 199 | + if err != nil { |
| 200 | + continue |
| 201 | + } |
| 202 | + |
| 203 | + ctx := context.Background() |
| 204 | + db, err := apiClient.Client.Database.GetActiveActive(ctx, subId, dbId) |
| 205 | + if err != nil { |
| 206 | + // Database not found is expected |
| 207 | + if _, ok := err.(*redis.NotFound); ok { |
| 208 | + continue |
| 209 | + } |
| 210 | + return err |
| 211 | + } |
| 212 | + |
| 213 | + if db != nil { |
| 214 | + return fmt.Errorf("database %d still exists in subscription %d", dbId, subId) |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + return nil |
| 219 | +} |
0 commit comments