-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow easier testing of resource's disappearance #73
Comments
Additional context by @radeksimko (from duplicate issue hashicorp/terraform-plugin-sdk#9):
|
Just to provide a current status update from the AWS Provider on this one, we implemented a helper function awhile back to at least make this generic: func testAccCheckResourceDisappears(provider *schema.Provider, resource *schema.Resource, resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
resourceState, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("resource not found: %s", resourceName)
}
if resourceState.Primary.ID == "" {
return fmt.Errorf("resource ID missing: %s", resourceName)
}
return resource.Delete(resource.Data(resourceState.Primary), provider.Meta())
}
} This allows us to implement tests like the following: func TestAccAWSAppsyncGraphqlApi_disappears(t *testing.T) {
var api1 appsync.GraphqlApi
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_appsync_graphql_api.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSAppSync(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsAppsyncGraphqlApiDestroy,
Steps: []resource.TestStep{
{
Config: testAccAppsyncGraphqlApiConfig_AuthenticationType(rName, "API_KEY"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsAppsyncGraphqlApiExists(resourceName, &api1),
testAccCheckResourceDisappears(testAccProvider, resourceAwsAppsyncGraphqlApi(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
} Not the most ideal, but it saves us a lot of extra work for new resources. |
Since its being discussed over in hashicorp/terraform-plugin-framework#617 -- To throw a proposal out there involving no coupling to a specific SDK, one could imagine having a test step which handles the equivalent of calling resource.TestCase{
// ... other fields
Steps: []resource.TestStep{
// Create our resource and state for disappearance testing.
{
Config: `resource "examplecloud_thing" "test" {}`,
},
// Design sketch -- the example Targets and PreservePriorState fields do not exist today.
// Targets would allow a complex configuration to test only the desired resource.
// PreservePriorState would keep the existing Terraform state from the prior step.
{
Config: `resource "examplecloud_thing" "test" {}`,
Destroy: true,
Targets: []string{"examplecloud_thing.test"},
PreservePriorState: true,
},
// Verify refresh behavior for now-missing resource.
{
Config: `resource "examplecloud_thing" "test" {}`,
PlanOnly: true, // or RefreshState: true
},
},
} |
With the introduction of the terraform-plugin-testing module (migration guide), we are transferring feature requests relating to the |
Problem statement
It is a convention for Terraform to not error out when a resource was deleted out of bound (e.g. by hand or another tooling outside of Terraform) and instead wipe it out of the state and automatically plan its recreation.
For this to work though each provider has to implement such behaviour correctly for each resource and this is not trivial nor easy to implement today.
The common pattern to achieve this is:
Read
which callsd.SetId("")
(i.e. remove resource from state) if resource is not found via API (e.g. when API returns 404 error) and issueWARN
log message in such case. This could/should be a user-visible warning, but that requires Allow providers to return post-apply & post-configure warnings terraform-plugin-sdk#230Delete
explicitly, to simulate the failure mode and hasExpectNonEmptyPlan: true
. Any acceptance tests generally expects all operations to not error.Example
https://github.com/terraform-providers/terraform-provider-aws/blob/master/aws/resource_aws_cloudwatch_log_stream.go#L73-L77
https://github.com/terraform-providers/terraform-provider-aws/blob/f39af7fff691603f5dc5b553c618d1dbdb6ea0df/aws/resource_aws_cloudwatch_log_stream_test.go#L33-L52
Proposal
Exact implementation is TBD, but it would be great to come up with something as simple as a flag for
resource.TestStep
which automatically tests these scenarios, so that more provider developers get into the habit of testing this more regularly and we really make this a widely accepted convention in providers.The text was updated successfully, but these errors were encountered: