|
| 1 | +--- |
| 2 | +name: tf-acceptance-test-gen |
| 3 | +description: Generate acceptance tests for a named Terraform resource or data source in the Couchbase Capella provider. |
| 4 | +--- |
| 5 | + |
| 6 | +# Terraform Acceptance Test Generator |
| 7 | + |
| 8 | +Acceptance tests live in `acceptance_tests/` and are named `<feature>_acceptance_test.go`. |
| 9 | + |
| 10 | +## Test structure |
| 11 | + |
| 12 | +Every resource test follows this pattern: |
| 13 | + |
| 14 | +```go |
| 15 | +func TestAcc<Feature>Resource(t *testing.T) { |
| 16 | + resourceName := randomStringWithPrefix("tf_acc_<feature>_") |
| 17 | + resourceReference := "couchbase-capella_<type_name>." + resourceName |
| 18 | + |
| 19 | + resource.ParallelTest(t, resource.TestCase{ |
| 20 | + ProtoV6ProviderFactories: globalProtoV6ProviderFactory, |
| 21 | + Steps: []resource.TestStep{ |
| 22 | + // Create and Read |
| 23 | + { |
| 24 | + Config: testAcc<Feature>ResourceConfig(resourceName, <values...>), |
| 25 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 26 | + testAccExists<Feature>Resource(t, resourceReference), |
| 27 | + resource.TestCheckResourceAttr(resourceReference, "organization_id", globalOrgId), |
| 28 | + resource.TestCheckResourceAttr(resourceReference, "<required_field>", "<expected_value>"), |
| 29 | + resource.TestCheckResourceAttrSet(resourceReference, "id"), |
| 30 | + resource.TestCheckResourceAttrSet(resourceReference, "<computed_field>"), |
| 31 | + ), |
| 32 | + }, |
| 33 | + // ImportState |
| 34 | + { |
| 35 | + ResourceName: resourceReference, |
| 36 | + ImportStateIdFunc: generate<Feature>ImportIdForResource(resourceReference), |
| 37 | + ImportState: true, |
| 38 | + }, |
| 39 | + // Update (omit if resource has no updatable fields) |
| 40 | + { |
| 41 | + Config: testAcc<Feature>ResourceConfig(resourceName, <updated_values...>), |
| 42 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 43 | + resource.TestCheckResourceAttr(resourceReference, "<updated_field>", "<new_value>"), |
| 44 | + ), |
| 45 | + }, |
| 46 | + }, |
| 47 | + // Delete testing automatically occurs in TestCase |
| 48 | + }) |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +- Always use `resource.ParallelTest()`. |
| 53 | +- Always include an ImportState step. |
| 54 | +- Use `TestCheckResourceAttr` for fields with known values; `TestCheckResourceAttrSet` for computed fields. |
| 55 | +- Never hardcode IDs — use `globalOrgId`, `globalProjectId`, `globalClusterId`, `globalBucketId`, `globalAppServiceId`. |
| 56 | + |
| 57 | +## Writing meaningful assertions |
| 58 | + |
| 59 | +The goal of each assertion is to guard a specific behaviour, not to confirm the test ran. Apply these rules: |
| 60 | + |
| 61 | +**Assert values, not just presence.** Every field you set in the config must be asserted with `TestCheckResourceAttr` using the exact value you set. `TestCheckResourceAttrSet` only proves a field is non-empty — it does not prove the provider stored the right value. Reserve `TestCheckResourceAttrSet` for fields whose values are genuinely unknowable at test time (server-generated IDs, timestamps, status fields set by the API). |
| 62 | + |
| 63 | +**After an update, assert the full state.** The update step must check every field — not just the one that changed. This catches regressions where updating field A silently resets field B to its default. |
| 64 | + |
| 65 | +**Error tests must be specific.** The `ExpectError` regex must match a substring of the real error message for that specific invalid input, not a generic pattern like `"error"` or `"invalid"`. Read the resource's `Create` implementation and `internal/errors/` to find the actual message. A test that accepts any error is not guarding the right behaviour. |
| 66 | + |
| 67 | +**Optional fields need their own test.** If a resource has optional fields that change API behaviour (not just metadata), write a dedicated test that sets those fields and asserts the resulting state. Don't rely on the happy-path test covering them incidentally. |
| 68 | + |
| 69 | +**Default values must be verified.** If a field has a default, write a test that omits it and asserts the default value appears in state. This confirms the provider applies and reads back the default correctly. |
| 70 | + |
| 71 | +## Config builder |
| 72 | + |
| 73 | +```go |
| 74 | +func testAcc<Feature>ResourceConfig(resourceName string, field1 <type>) string { |
| 75 | + return fmt.Sprintf(` |
| 76 | + %[1]s |
| 77 | +
|
| 78 | + resource "couchbase-capella_<type_name>" "%[2]s" { |
| 79 | + organization_id = "%[3]s" |
| 80 | + project_id = "%[4]s" |
| 81 | + cluster_id = "%[5]s" |
| 82 | + <field> = <format_verb> |
| 83 | + } |
| 84 | + `, globalProviderBlock, resourceName, globalOrgId, globalProjectId, globalClusterId, field1) |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +Always start with `%[1]s` for `globalProviderBlock`. Number format verbs sequentially. |
| 89 | + |
| 90 | +## Import ID function |
| 91 | + |
| 92 | +Read the `ImportState()` function in `internal/resources/<feature>.go` to find the exact composite key format, then implement: |
| 93 | + |
| 94 | +```go |
| 95 | +func generate<Feature>ImportIdForResource(resourceReference string) resource.ImportStateIdFunc { |
| 96 | + return func(state *terraform.State) (string, error) { |
| 97 | + var rawState map[string]string |
| 98 | + for _, m := range state.Modules { |
| 99 | + if len(m.Resources) > 0 { |
| 100 | + if v, ok := m.Resources[resourceReference]; ok { |
| 101 | + rawState = v.Primary.Attributes |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + return fmt.Sprintf( |
| 106 | + "id=%s,cluster_id=%s,project_id=%s,organization_id=%s", |
| 107 | + rawState["id"], rawState["cluster_id"], rawState["project_id"], rawState["organization_id"], |
| 108 | + ), nil |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +## Error-case test |
| 114 | + |
| 115 | +```go |
| 116 | +func TestAcc<Feature>ResourceInvalid<Field>(t *testing.T) { |
| 117 | + resourceName := randomStringWithPrefix("tf_acc_<feature>_") |
| 118 | + resource.ParallelTest(t, resource.TestCase{ |
| 119 | + ProtoV6ProviderFactories: globalProtoV6ProviderFactory, |
| 120 | + Steps: []resource.TestStep{ |
| 121 | + { |
| 122 | + Config: testAcc<Feature>ResourceConfig(resourceName, <invalid_value>), |
| 123 | + ExpectError: regexp.MustCompile("<substring of expected error>"), |
| 124 | + }, |
| 125 | + }, |
| 126 | + }) |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +## Data source tests |
| 131 | + |
| 132 | +- Test function names for datasources should use the pattern `TestAccDatasource<Feature>`. |
| 133 | + For example: `func TestAccDatasourceCluster(t *testing.T)` |
| 134 | +- Omit the ImportState and Update steps. |
| 135 | + |
| 136 | +## Verifying and running the tests |
| 137 | + |
| 138 | +At session start the environment check reports which variables are set. If all three required variables (`TF_VAR_host`, `TF_VAR_auth_token`, `TF_VAR_organization_id`) are present, run only the tests for the resource you just wrote: |
| 139 | + |
| 140 | +```bash |
| 141 | +TF_ACC=1 go test -timeout=60m -v ./acceptance_tests/ -run TestAcc<Feature> |
| 142 | +``` |
| 143 | + |
| 144 | +When credentials are not available the tests cannot be executed. The only verification possible is that the file compiles: |
| 145 | + |
| 146 | +```bash |
| 147 | +go test -c ./acceptance_tests |
| 148 | +``` |
| 149 | + |
| 150 | +If compile errors are found fix and recompile to verify no errors. |
| 151 | + |
| 152 | +## Reference examples |
| 153 | + |
| 154 | +| Scenario | File | |
| 155 | +|---|---| |
| 156 | +| Simple resource with update | `acceptance_tests/snapshot_backup_acceptance_test.go` | |
| 157 | +| Resource with many optional fields | `acceptance_tests/apikey_acceptance_test.go` | |
| 158 | +| Complex nested resource | `acceptance_tests/cluster_acceptance_test.go` | |
0 commit comments