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

Unhelpful/unclear error message when required provider configurations are missing #34594

Closed
stusklinar opened this issue Jan 31, 2024 · 5 comments · Fixed by #34595
Closed

Unhelpful/unclear error message when required provider configurations are missing #34594

stusklinar opened this issue Jan 31, 2024 · 5 comments · Fixed by #34595
Labels
bug confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code

Comments

@stusklinar
Copy link

Terraform Version

1.7.1

Terraform Configuration Files

main.tf


terraform {
  required_version = "<= 1.7.1"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">= 3.24.0, < 4.0.0"
      configuration_aliases = [
        azurerm.azsubscription
      ]
    }
  }

variable "name" {
  description = "Name of the Resource Group"
  type        = string
}

variable "location" {
  description = "Location of the Resource Group"
  type        = string
  default     = "UK South"
}
resource "azurerm_resource_group" "this" {
  name     = var.name
  location = var.location
}

test.tftest.hcl


provider "azurerm" {
  features {}
}

run "validate_functions" {

  command = apply

    providers = {
    azurerm.azsubscription = azurerm
  }

  variables {
    name     = "rg-terraform-testing"
    location = "uksouth"
  }

  assert {
    condition     = azurerm_resource_group.this.name == var.name
    error_message = "function name did not match expected"
  }

  assert {
    condition     =  azurerm_resource_group.this.location == var.location
    error_message = "location did not match uksouth"
  }

}

Debug Output

TF Trace Log Output

Expected Behavior

Tests should pass

Actual Behavior

tests/module.tftest.hcl... in progress
  run "setup_tests"... pass
  run "validate_functions"... fail
╷
│ Error: Insufficient features blocks
│ 
│   on <empty> line 0:
│   (source code not available)
│ 
│ At least 1 "features" blocks are required.
╵
tests/module.tftest.hcl... tearing down
tests/module.tftest.hcl... fail

Failure! 1 passed, 1 failed.

Steps to Reproduce

terraform init
terraform test

Additional Context

This is a largely reduced version of our problem - but illustrates the issue much the same.

We have modules that accept different providers for different subscriptions/accounts - in this simple mock-up we get the same error, of injecting a provider expects features.

References

@stusklinar stusklinar added bug new new issue not yet triaged labels Jan 31, 2024
@liamcervante
Copy link
Member

Hi @stusklinar, thanks for filing this!

The problem is that you are only providing a definition for the azurerm.azsubscription provider within the test and not the non-aliased azurerm provider which also requires a dedicated provider block with the features block set.

I think you can get your test to work if you expand the providers block within the run "validate_functions" like this:

providers = {
  azurerm = azurerm
  azurerm.azsubscription = azurerm
}

This provides a concrete provider definition for all the required providers in your configuration.

I do think that the error message you are receiving is super unhelpful, I'll expand on that in a follow up comment to keep things separate.

Hopefully this unblocks you!

@liamcervante
Copy link
Member

On the error message, this isn't actually something that is being reported by the testing framework itself. If I add the provider block directly to your configuration and execute terraform plan I receive the same error:

terraform {
  required_version = "<= 1.7.1"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">= 3.24.0, < 4.0.0"
      configuration_aliases = [
        azurerm.azsubscription
      ]
    }
  }
}

provider "azurerm" {
    alias = "azsubscription"

    features {}
}

variable "name" {
  description = "Name of the Resource Group"
  type        = string
}

variable "location" {
  description = "Location of the Resource Group"
  type        = string
  default     = "UK South"
}
resource "azurerm_resource_group" "this" {
  name     = var.name
  location = var.location
}
~/terraform/34594 > terraform plan 
var.name
  Name of the Resource Group

  Enter a value: thing


Planning failed. Terraform encountered an error while generating this plan.

╷
│ Error: Insufficient features blocks
│ 
│   on <empty> line 0:
│   (source code not available)
│ 
│ At least 1 "features" blocks are required.
╵

The (source code not available) message is because the provider block is not present in the config, so Terraform is creating a virtual configuration block which is missing the required block, and then reports the error without any source code available.

I'm going to change the title of this issue, and we can use it to track improving the error message for this case so that in future it will state the real problem, which is that the provider block must be explicitly provided in the configuration if we have required attributes or blocks.

Thanks!

@liamcervante liamcervante changed the title Terraform Test does not work with injected providers that have required configuration blocks (e.g. azurerm) Unhelpful/unclear error message when required provider configurations are missing Jan 31, 2024
@liamcervante liamcervante added confirmed a Terraform Core team member has reproduced this issue and removed new new issue not yet triaged labels Jan 31, 2024
@liamcervante
Copy link
Member

We do actually add some additional context for missing provider config if the provider validation call itself fails. This happens here.

We don't any extra context if the schema validation itself fails (where the features block is marked as required) here.

I think we can just take the additional context we add in the first case, and also add it in the second case. Then we'd get an error that looks like this:

╷
│ Error: Invalid provider configuration
│ 
│ Provider "registry.terraform.io/hashicorp/azurerm" requires explicit configuration. Add a provider block to the root module and configure the
│ provider's required arguments as described in the provider documentation.
│ 
╵

Which provides a much better hint about the root cause of the problem, compared to the existing cryptic errors that are returned currently.

@liamcervante liamcervante added the explained a Terraform Core team member has described the root cause of this issue in code label Jan 31, 2024
@stusklinar
Copy link
Author

This is great - thanks for this - consider me unblocked

Copy link
Contributor

github-actions bot commented Mar 2, 2024

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code
Projects
None yet
2 participants