hashicorp / hashicorp/terraform-plugin-sdk

testing_new_config.go It is possible for a testStep to be both empty and non-empty plan post apply

Open
#1,383 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Go
Stars
485
Forks
244
Avg merge
19h 57m
Merged PRs (30d)
4

Description

### SDK version
Latest

I'm implementing a switchover, where a primary becomes the replica, and the replica becomes the primary in one apply. This causes some issues in testing. After this operation is applied, a refresh or plan is needed to verify that plan is not empty. Currently, acceptance tests say that after applying this config, the plan is both empty and non-empty.

Here's a summary of https://github.com/hashicorp/terraform-plugin-sdk/blob/main/helper/resource/testing_new_config.go
Line 70: Apply config
Line 117: WorkingDir.CreatePlan()

Line 133:
```go
if !planIsEmpty(plan) && !step.ExpectNonEmptyPlan {
check if plan was not empty
}
```

At this time, plan is "not empty", we just need a refresh and it will be an empty plan.
...
line 149: we do a refresh. Now, the plan is empty. (we should've done this before we do the plan check in line 133)
line 161: we do another plan after the refresh.

Now, here is why we see "schrodinger's plan"; plan is "both empty and non-empty"
line 188:
```go

else if step.ExpectNonEmptyPlan && planIsEmpty(plan) {
check and expect that plan is empty
}
```

The plan was non-empty before refresh in line 149, and is now empty.

### Terraform Configuration Files
Initially:
```hcl
resource "google_sql_database_instance" "original-primary" {
name = "%s"
region = "us-east7"
database_version = "SQLSERVER_2019_ENTERPRISE"
deletion_protection = false

root_password = "sqlserver1"
settings {
tier = "db-perf-optimized-N-2"
edition = "ENTERPRISE_PLUS"
}
}

resource "google_sql_database_instance" "original-replica" {
name = "%s"
region = "us-west2"
database_version = "SQLSERVER_2019_ENTERPRISE"
master_instance_name = google_sql_database_instance.original-primary.name
deletion_protection = false
root_password = "sqlserver1"
replica_configuration {
cascadable_replica = true
}

settings {
tier = "db-perf-optimized-N-2"
edition = "ENTERPRISE_PLUS"
}
}

`, primaryName, replicaName)
...
```
In the next config step, we apply this config:
```hcl
resource "google_sql_database_instance" "original-primary" {
name = "%s"
region = "us-east7"
database_version = "SQLSERVER_2019_ENTERPRISE"
deletion_protection = false
root_password = "sqlserver1"
instance_type = "READ_REPLICA_INSTANCE"
master_instance_name = "%s"
replica_configuration {
cascadable_replica = true
}
replica_names = []
settings {
tier = "db-perf-optimized-N-2"
edition = "ENTERPRISE_PLUS"
}
}

resource "google_sql_database_instance" "original-replica" {
name = "%s"
region = "us-west2"
database_version = "SQLSERVER_2019_ENTERPRISE"
deletion_protection = false
root_password = "sqlserver1"
instance_type = "CLOUD_SQL_INSTANCE"
replica_names = [google_sql_database_instance.original-primary.name]
settings {
tier = "db-perf-optimized-N-2"
edition = "ENTERPRISE_PLUS"
}
}
`, primaryName, replicaName, replicaName)
```

After applying this config step, a refresh is needed before we do checks.
I have:
{
Config: googleSqlDatabaseInstance_switchover(primaryName, replicaName),
ExpectNonEmptyPlan: true, // I commend and uncomment, and the error says that this step is both empty and non-empty.
},

### Expected Behavior

When I commend and uncomment ExpectNonEmptyPlan: true, and the error should say that the step is either empty and non-empty, not BOTH.

### Actual Behavior
The error says that the config test step results in an empty, and also non-empty plan.
### Steps to Reproduce
Run this test with my pull request (switchover implementation)
https://github.com/GoogleCloudPlatform/magic-modules/pull/11850
### References
https://github.com/GoogleCloudPlatform/magic-modules/pull/11850

Contributor guide

Open the contributing guide

Research direction

Start in helper/resource/testing_new_config.go, especially the apply, plan, refresh, and ExpectNonEmptyPlan checks around lines 70, 117, 133, 149, 161, and 188. Reproduce the behavior with the switchover test from the referenced Magic Modules pull request. Done means a test step is evaluated consistently rather than reported as both empty and non-empty after refresh.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, terraform
Domain
devtools, testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.