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

mock_provider does not work with providers that have required configuration blocks (e.g. azurerm) #34489

Closed
arylatt opened this issue Jan 5, 2024 · 7 comments
Labels
bug confirmed a Terraform Core team member has reproduced this issue v1.7 Issues (primarily bugs) reported against v1.7 releases

Comments

@arylatt
Copy link

arylatt commented Jan 5, 2024

Terraform Version

Terraform v1.7.0-rc1
on windows_amd64
+ provider registry.terraform.io/hashicorp/azurerm v3.86.0

Terraform Configuration Files

main.tf:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "3.86.0"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "test" {
  name     = "test-rg"
  location = "eastus2"
}

main.tftest.hcl:

mock_provider "azurerm" {}

run "main" {
  assert {
    condition     = (azurerm_resource_group.test.name == "test-rg")
    error_message = "name wrong"
  }
}

Debug Output

https://gist.github.com/arylatt/796ee2f5a8576cba2195985c4e7acd33.

Expected Behavior

Mock provider should initialize and tests should run.

Actual Behavior

Since the azurerm provider requires a features block to be defined, the provider initialization fails. There is no way to provide configuration to the mock provider to avoid this error.

Steps to Reproduce

  1. terraform init
  2. terraform test

Additional Context

https://github.com/hashicorp/terraform/blob/v1.7.0-rc1/internal/configs/mock_provider.go#L35-L36 suggests that there is an expectation that mock providers won't need config. Not sure if this assumption should be changed (since there are providers that require config) or if I should raise this against terraform-providers/terraform-provider-azurerm?

References

No response

@arylatt arylatt added bug new new issue not yet triaged labels Jan 5, 2024
@liamcervante liamcervante added confirmed a Terraform Core team member has reproduced this issue v1.7 Issues (primarily bugs) reported against v1.7 releases and removed new new issue not yet triaged labels Jan 5, 2024
@liamcervante
Copy link
Member

liamcervante commented Jan 5, 2024

Hi @arylatt, thanks for filing this! This is a problem with the mocking framework so you've filed it in the right place - it shouldn't be validating the configuration against the providers schema. We also caught this internally yesterday, and it should have been fixed in #34481.

That fix will be released in the next release candidate - hopefully next week, but maybe the one after.

Thanks so much for trying out the new features, and please keep filing any other issues you find or any potential feature requests.

@arylatt
Copy link
Author

arylatt commented Jan 5, 2024

@liamcervante awesome, yes building from the v1.7 branch and running my tests against that version of Terraform it works. Thanks!

@arnaudlh
Copy link

It seems we are having the same issue with azuread provider:
image

@liamcervante
Copy link
Member

Hi @arnaudlh, what version of Terraform are you using for this? Or can you share your config? I'm unable to reproduce this as the following is working for me on Terraform v1.7.0.

# main.tf

provider "azuread" {}

resource "azuread_group" "example" {
  display_name     = "what"
  security_enabled = true
}
# main.tftest.hcl

mock_provider "azuread" {}

run "test" {
  assert {
    condition     = azuread_group.example.display_name == "what"
    error_message = "wrong display name"
  }
}
~/terraform/34489 > terraform test
main.tftest.hcl... in progress
  run "test"... pass
main.tftest.hcl... tearing down
main.tftest.hcl... pass

Success! 1 passed, 0 failed.
~/terraform/34489 > terraform version
Terraform v1.7.0
on darwin_arm64
+ provider registry.terraform.io/hashicorp/azuread v2.47.0

@stusklinar
Copy link

A note on recreation, please use the azurerm provider to test, not azuread.

This is still an issue on 1.7.1

@liamcervante
Copy link
Member

Hi @stusklinar, are you able to provide some reproduction config for your error? I am able to get this working on Terraform v1.7.1 with both azurerm and azuread.

# main.tf 

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "main" {
  name     = "example"
  location = "West Europe"
}
# main.tftest.hcl

mock_provider "azurerm" {}

run "test" {
  assert {
    condition     = azurerm_resource_group.main.name == "example"
    error_message = "wrong name"
  }
}
~/terraform/34489/component > terraform test
main.tftest.hcl... in progress
  run "test"... pass
main.tftest.hcl... tearing down
main.tftest.hcl... pass

Success! 1 passed, 0 failed.
~/terraform/34489/component > terraform version
Terraform v1.7.1
on darwin_arm64
+ provider registry.terraform.io/hashicorp/azurerm v3.89.0
~/terraform/34489/component > 

Thanks!

Copy link
Contributor

github-actions bot commented Mar 1, 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 1, 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 v1.7 Issues (primarily bugs) reported against v1.7 releases
Projects
None yet
Development

No branches or pull requests

4 participants