Skip to content
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

Terraform test execution doesn't handle prevent_destroy=true #34960

Open
daveS2 opened this issue Apr 8, 2024 · 3 comments
Open

Terraform test execution doesn't handle prevent_destroy=true #34960

daveS2 opened this issue Apr 8, 2024 · 3 comments
Labels
enhancement new new issue not yet triaged terraform test

Comments

@daveS2
Copy link

daveS2 commented Apr 8, 2024

Terraform Version

Terraform v1.7.5
on darwin_amd64
+ provider registry.terraform.io/hashicorp/google v5.23.0

Terraform Configuration Files

Terraform conifg:

terraform {
  required_version = "1.7.5"
}

resource "google_kms_crypto_key" "key" {
  name            = "resource-name"
  key_ring        = "keyring"
  rotation_period = "10000000s"

  lifecycle {
    prevent_destroy = true
  }
}

tftest.hcl file:

mock_provider "google" {

}


run "test"{
    assert {
    condition     = google_kms_crypto_key.key.name == "resource-name"
    error_message = "Name is not correct"
  }
}

Debug Output

https://gist.github.com/daveS2/fd251cc2cb61af3c8df0f3966aadd253

Expected Behavior

The prevent destroy should be handled by either expect failures:

run "test"{

    assert {
    condition     = google_kms_crypto_key.key.name == "resource-name"
    error_message = "Name is not correct"
  }
  expect_failures = [
    google_kms_crypto_key.key
  ]
}

Which doesn't work or it should overridable:

mock_provider "google" {
 override_resource{
    target = google_kms_crypto_key.key
    values = {
        lifecycle = {prevent_destroy=false}
    }
 }
}

Actual Behavior

The tests always fail

Steps to Reproduce

  1. terraform init
  2. terraform test

Additional Context

For resources where we want destroy protection, terraform mocks always attempt to destroy but we cannot capture the error thrown or override the value

References

No response

@daveS2 daveS2 added bug new new issue not yet triaged labels Apr 8, 2024
@liamcervante
Copy link
Member

Hi @daveS2, thanks for filing this.

The override_resource block will only provide values for computed attributes. It won't override attributes that are supplied from the config. The aim of the mock provider is to replace the behaviour of the provider, in this case providing values for computed attributes that would normally be provided by the underlying cloud provider. Unfortunately, you cannot change the values within the configuration using the mocking framework.

Potentially, you could introduce a variable into your module that defaults to true and you can override it within your tests?

# main.tf

variable "prevent_destroy" {
  type = boolean
  default = true
}

resource "google_kms_crypto_key" "key" {
  name            = "resource-name"
  key_ring        = "keyring"
  rotation_period = "10000000s"

  lifecycle {
    prevent_destroy = var.prevent_destroy
  }
}
# main.tftest.hcl

run "test" {
  variables = {
    prevent_destroy = false
  }

  assert {
    // ...
  }
}

I appreciate changing the configuration to support the tests isn't always the best solution, but it might work for you here?

@liamcervante liamcervante added the waiting-response An issue/pull request is waiting for a response from the community label Apr 9, 2024
@daveS2
Copy link
Author

daveS2 commented Apr 9, 2024

Thank you for your response, this would work, but I am not sure you can pass a variable into a lifecycle block?
If I change my terraform code:

resource "google_kms_crypto_key" "key" {
  name            = "resource-name"
  key_ring        = "keyring"
  rotation_period = "10000000s"

  lifecycle {
    prevent_destroy = var.prevent_destroy
  }
}

variable "prevent_destroy" {
  default = true
}

And then run terraform test, I get the following errors:

│ Error: Variables not allowed
│ 
│   on demo.tf line 11, in resource "google_kms_crypto_key" "key":11:     prevent_destroy = var.prevent_destroy
│ 
│ Variables may not be used here.
╵
╷
│ Error: Unsuitable value type
│ 
│   on demo.tf line 11, in resource "google_kms_crypto_key" "key":11:     prevent_destroy = var.prevent_destroy
│ 
│ Unsuitable value: value must be known

@liamcervante
Copy link
Member

Apologies, I didn't know that you couldn't use variables for this. Unfortunately, I can't think of a way to handle this within the current capabilities of the testing framework.

I'll retag this as an enhancement request. I'm not sure the best way to handle this currently. Potentially, we could make the testing framework ignore the prevent_destroy attribute within the lifecycle block.

Sorry for the missing functionality!

@liamcervante liamcervante added enhancement terraform test and removed waiting-response An issue/pull request is waiting for a response from the community bug labels Apr 10, 2024
@liamcervante liamcervante changed the title Terraform test mocks don't handle prevent_destroy=true and override appears to not work Terraform test execution doesn't handle prevent_destroy=true Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement new new issue not yet triaged terraform test
Projects
None yet
Development

No branches or pull requests

2 participants