How to reference a setup run block while using a non-hashicorp provider?

Hello,
I’m trying to write a Terraform test (using the terraform test command) with the non-hashicorp Proxmox provider, but pulling the secrets from HashiCorp Vault. According to the documentation, this is achieved by adding a run block with a setup module
tests/test.tftest.hcl

provider "vault" {
    address = "<my_address>"
}

provider "proxmox" {
  pm_api_url  = "<pm_api_url>"
  pm_user     = run.setup.proxmox_user["username"]
  pm_password = run.setup.proxmox_user["password"]
}

run "setup" {
    module {
        source = "./tests/setup"
    }
}

run "verify_linux_instance" {
  command = plan
}

tests/setup/main.tf

data "vault_kv_secret_v2" "proxmox_user" {
    ....
}
output "proxmox_user" {
    value = data.vault_kv_secret_v2.proxmox_user.data
    sensitive = true
}

I have the required_providers set up in the root module:
providers.tf

terraform {
  required_providers {
    proxmox = {
      source  = "telmate/proxmox"
      version = "3.0.1-rc1"
    }
  }
}

When I do this, I am getting an error that I hadn’t seen before adding the setup run block:

│ Error: Provider type mismatch
│ 
│   on tests/test.tftest.hcl line 20:
│   20: provider "proxmox" {
│ 
│ The provider "proxmox" in tests/test.tftest.hcl represents provider
│ "registry.terraform.io/hashicorp/proxmox", but "proxmox" in the root module
│ represents "registry.terraform.io/telmate/proxmox".
│ 
│ This means the provider definition for "proxmox" within tests/test.tftest.hcl, or
│ other provider definitions with the same name, have been referenced by multiple run
│ blocks and assigned to different provider types.

As I write this, I am now able to work around this by adding an alias to my provider

provider "proxmox" {
  alias  = "primary"
  ...
}

and adding a reference to the provider alias

run "verify_linux_instance" {
  providers = {
    proxmox = proxmox.primary
  }
  command = plan
}

this seems to remove the provider from the scope of the setup block, and therefore said provider is not affected by said block, but it appears that this setup block affects the provider sources even when said providers aren’t used (can’t be used even, as they depend upon this) in the setup block.

(Terraform v1.7.5 on MacOS)

Thanks!

Hi @shawnsmith88,

If I’m understanding what you’ve described correctly, I think what’s missing in your configuration is that the module in ./tests/setup must also declare that it depends on the telmate/proxmox provider.

Each module has its own separate namespace of local provider names like “proxmox”, and so each module must include a required_providers block to declare which fully-qualified provider address (like telmate/proxmox) each of the local names refers to.

Hi @apparentlymart,
thanks for taking the time to look at this!
Your reply got me thinking, and I found what my actual issue was - I actually had not run a terraform init command after creating the ./tests/setup module, which actually printed this error first, before a bunch of messages about the provider:

│ Error: Module not installed
│ 
│   on tests/test.hcl line 23, in run "setup":
│   23:     module {
│ 
│ This module is not yet installed. Run "terraform init" to install all modules
│ required by this configuration.

So the fix mentioned in the comment probably only worked for me because it made the Module not installed error very obvious, as the provider errors are no longer thrown after making that change. As the initial issue was me missing that first error, I don’t think this is really an issue, though it’s quite misleading that the providers throw that error even though my initial terraform init would have contained the correct provider for telmate/proxmox.

Run terraform init to initialize the new module, which also cleans up the provider errors