|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "regexp" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 11 | +) |
| 12 | + |
| 13 | +// Generates the base Terraform config for a Pro Subscription with QPF |
| 14 | +func formatSubscriptionConfig(name, cloudAccountName, qpf, extraConfig string) string { |
| 15 | + return fmt.Sprintf(` |
| 16 | +data "rediscloud_payment_method" "card" { |
| 17 | + card_type = "Visa" |
| 18 | +} |
| 19 | +
|
| 20 | +data "rediscloud_cloud_account" "account" { |
| 21 | + exclude_internal_account = true |
| 22 | + provider_type = "AWS" |
| 23 | + name = "%s" |
| 24 | +} |
| 25 | +
|
| 26 | +resource "rediscloud_subscription" "example" { |
| 27 | + name = "%s" |
| 28 | + payment_method_id = data.rediscloud_payment_method.card.id |
| 29 | + memory_storage = "ram" |
| 30 | +
|
| 31 | + allowlist { |
| 32 | + cidrs = ["192.168.0.0/16"] |
| 33 | + security_group_ids = [] |
| 34 | + } |
| 35 | +
|
| 36 | + cloud_provider { |
| 37 | + provider = data.rediscloud_cloud_account.account.provider_type |
| 38 | + cloud_account_id = data.rediscloud_cloud_account.account.id |
| 39 | + region { |
| 40 | + region = "eu-west-1" |
| 41 | + networking_deployment_cidr = "10.0.0.0/24" |
| 42 | + preferred_availability_zones = ["eu-west-1a"] |
| 43 | + } |
| 44 | + } |
| 45 | +
|
| 46 | + creation_plan { |
| 47 | + dataset_size_in_gb = 1 |
| 48 | + throughput_measurement_by = "operations-per-second" |
| 49 | + throughput_measurement_value = 1000 |
| 50 | + quantity = 1 |
| 51 | + replication = false |
| 52 | + support_oss_cluster_api = false |
| 53 | + query_performance_factor = "%s" |
| 54 | + |
| 55 | + %s |
| 56 | + } |
| 57 | +}`, cloudAccountName, name, qpf, extraConfig) |
| 58 | +} |
| 59 | + |
| 60 | +// Generic test helper for error cases |
| 61 | +func testSubErrorCase(t *testing.T, config string, expectedError *regexp.Regexp) { |
| 62 | + resource.ParallelTest(t, resource.TestCase{ |
| 63 | + PreCheck: func() { testAccPreCheck(t); testAccAwsPreExistingCloudAccountPreCheck(t) }, |
| 64 | + ProviderFactories: providerFactories, |
| 65 | + CheckDestroy: testAccCheckProSubscriptionDestroy, |
| 66 | + Steps: []resource.TestStep{ |
| 67 | + { |
| 68 | + Config: config, |
| 69 | + ExpectError: expectedError, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +func TestAccResourceRedisCloudProSubscription_qpf(t *testing.T) { |
| 76 | + name := acctest.RandomWithPrefix(testResourcePrefix) |
| 77 | + testCloudAccountName := os.Getenv("AWS_TEST_CLOUD_ACCOUNT_NAME") |
| 78 | + const resourceName = "rediscloud_subscription.example" |
| 79 | + |
| 80 | + resource.ParallelTest(t, resource.TestCase{ |
| 81 | + PreCheck: func() { testAccPreCheck(t); testAccAwsPreExistingCloudAccountPreCheck(t) }, |
| 82 | + ProviderFactories: providerFactories, |
| 83 | + CheckDestroy: testAccCheckProSubscriptionDestroy, |
| 84 | + Steps: []resource.TestStep{ |
| 85 | + { |
| 86 | + Config: formatSubscriptionConfig(name, testCloudAccountName, "2x", `modules = ["RediSearch"]`), |
| 87 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 88 | + resource.TestCheckResourceAttr(resourceName, "name", name), |
| 89 | + resource.TestCheckResourceAttr(resourceName, "payment_method", "credit-card"), |
| 90 | + resource.TestCheckResourceAttr(resourceName, "cloud_provider.0.provider", "AWS"), |
| 91 | + resource.TestCheckResourceAttr(resourceName, "cloud_provider.0.region.0.preferred_availability_zones.#", "1"), |
| 92 | + resource.TestCheckResourceAttrSet(resourceName, "cloud_provider.0.region.0.networks.0.networking_subnet_id"), |
| 93 | + resource.TestCheckResourceAttr(resourceName, "creation_plan.#", "1"), |
| 94 | + resource.TestCheckResourceAttr(resourceName, "creation_plan.0.average_item_size_in_bytes", "0"), |
| 95 | + resource.TestCheckResourceAttr(resourceName, "creation_plan.0.dataset_size_in_gb", "1"), |
| 96 | + resource.TestCheckResourceAttr(resourceName, "creation_plan.0.query_performance_factor", "2x"), |
| 97 | + |
| 98 | + resource.TestCheckResourceAttr(resourceName, "creation_plan.0.modules.#", "1"), |
| 99 | + resource.TestCheckResourceAttr(resourceName, "creation_plan.0.modules.0", "RediSearch"), |
| 100 | + resource.TestCheckResourceAttr(resourceName, "creation_plan.0.quantity", "1"), |
| 101 | + resource.TestCheckResourceAttr(resourceName, "creation_plan.0.replication", "false"), |
| 102 | + resource.TestCheckResourceAttr(resourceName, "creation_plan.0.support_oss_cluster_api", "false"), |
| 103 | + resource.TestCheckResourceAttr(resourceName, "creation_plan.0.throughput_measurement_by", "operations-per-second"), |
| 104 | + resource.TestCheckResourceAttr(resourceName, "creation_plan.0.throughput_measurement_value", "1000"), |
| 105 | + ), |
| 106 | + }, |
| 107 | + }, |
| 108 | + }) |
| 109 | +} |
| 110 | + |
| 111 | +func TestAccResourceRedisCloudProSubscription_missingModule(t *testing.T) { |
| 112 | + name := acctest.RandomWithPrefix(testResourcePrefix) |
| 113 | + password := acctest.RandString(20) |
| 114 | + testCloudAccountName := os.Getenv("AWS_TEST_CLOUD_ACCOUNT_NAME") |
| 115 | + |
| 116 | + config := formatDatabaseConfig(name, testCloudAccountName, password, "4x", "") |
| 117 | + |
| 118 | + testSubErrorCase(t, config, regexp.MustCompile("query_performance_factor\" requires the \"modules\" key to be explicitly defined in HCL")) |
| 119 | +} |
| 120 | + |
| 121 | +func TestAccResourceRedisCloudProSubscription_missingRediSearchModule(t *testing.T) { |
| 122 | + name := acctest.RandomWithPrefix(testResourcePrefix) |
| 123 | + password := acctest.RandString(20) |
| 124 | + testCloudAccountName := os.Getenv("AWS_TEST_CLOUD_ACCOUNT_NAME") |
| 125 | + |
| 126 | + config := formatDatabaseConfig(name, testCloudAccountName, password, "4x", `modules = [{ name = "RediBloom" }]`) |
| 127 | + |
| 128 | + testSubErrorCase(t, config, regexp.MustCompile("query_performance_factor\" requires the \"modules\" list to contain \"RediSearch")) |
| 129 | +} |
0 commit comments