Tf 1.8.3 test: I'm not able to select the right provider for sub module

Hello,

I don’t manage to configure my simple test case with the non hashicorp provider microsoft/azuredevops in the following way:

My context:

$ terraform version
Terraform v1.8.3
on windows_amd64

module_to_test/main.tf

terraform {
  required_providers {
    restapi = {
      source  = "Mastercard/restapi"
      version = "~> 1.18.2"
    }
  }
}

module_to_test/tests/unit.tftest.hcl

provider "restapi" {
  [...]
}
provider "azuredevops" {
  org_service_url = "https://dev.azure.com/foobar"
}

run "setup_azdevops" {
  command = apply

  providers = {
    azuredevops = azuredevops
  }

  variables {
    [...]
  }

  module {
    source = "../azdevops"
  }
}

# Passing test
run "azdevops_test" {
  [...]
}

azdevops/main.tf

terraform {
  required_providers {
    azuredevops = {
      source  = "microsoft/azuredevops"
      version = "~> 1.0.1"
    }
  }
}
[...]

When running the terraform init, there is provider conflict between hashicorp/azuredevops (which does not exist) and the right one microsoft/azuredevops

$ terraform init

Initializing the backend...
Initializing modules...

Initializing provider plugins...
- Finding microsoft/azuredevops versions matching "~> 1.0.1"...
- Finding mastercard/restapi versions matching "~> 1.18.2"...
- Finding latest version of hashicorp/azuredevops...
- Installing microsoft/azuredevops v1.0.1...
- Installed microsoft/azuredevops v1.0.1 (signed by a HashiCorp partner, key ID 6F0B91BDE98478CF)
- Installing mastercard/restapi v1.18.2...
- Installed mastercard/restapi v1.18.2 (self-signed, key ID DCB8C431D71C30AB)

Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html
β•·
β”‚ Error: Failed to query available provider packages
β”‚
β”‚ Could not retrieve the list of available versions for provider hashicorp/azuredevops: provider registry registry.terraform.io does not have a provider named
β”‚ registry.terraform.io/hashicorp/azuredevops
β”‚
β”‚ Did you intend to use microsoft/azuredevops? If so, you must specify that source address in each module which requires that provider. To see which modules are currently
β”‚ depending on hashicorp/azuredevops, run the following command:
β”‚     terraform providers

and when running terraform providers:

$ terraform providers

Providers required by configuration:
.
β”œβ”€β”€ provider[registry.terraform.io/mastercard/restapi] ~> 1.18.2
└── test.tests\unit
    β”œβ”€β”€ provider[registry.terraform.io/hashicorp/azuredevops]
    └── run.setup_azdevops
        └── provider[registry.terraform.io/microsoft/azuredevops] ~> 1.0.1

Any idea on how to overcome this configuration issue?

Hi @sebastien.latre, are you able to file this as a bug in Issues Β· hashicorp/terraform Β· GitHub?

I think Terraform isn’t correctly connecting the provider definition you have for Azure DevOps to the source you provide in the alternate module, which I can investigate and fix once we have the bug report.

In the meantime and as a workaround, if you place the azuredevops definition into the required_providers block in your main configuration (within module_to_test/main.tf) does that resolve the conflict? I will test that myself shortly to confirm, but you might get to it before I do.

Hello @liamcervante,

I did try your workaround with this new configuration (even if it’s not something I would like to have):

module_to_test/main.tf

terraform {
  required_providers {
    restapi = {
      source  = "Mastercard/restapi"
      version = "~> 1.18.2"
    }
  }
}

provider "azuredevops" {
  org_service_url = "https://dev.azure.com/foobar"
}

However this didn’t worked:

$ terraform init

Initializing the backend...
Initializing modules...
β•·
β”‚ Error: Provider type mismatch
β”‚
β”‚   on tests\unit.tftest.hcl line 2:
β”‚    2: provider "azuredevops" {
β”‚
β”‚ The provider "azuredevops" in tests\unit.tftest.hcl represents provider "registry.terraform.io/microsoft/azuredevops", but "azuredevops" in the root module represents
β”‚ "registry.terraform.io/hashicorp/azuredevops".
β”‚
β”‚ This means the provider definition for "azuredevops" within tests\unit.tftest.hcl has been referenced by multiple run blocks and assigned to different provider types.
$ terraform providers
β•·
β”‚ Error: Provider type mismatch
β”‚
β”‚   on tests\unit.tftest.hcl line 2:
β”‚    2: provider "azuredevops" {
β”‚
β”‚ The provider "azuredevops" in tests\unit.tftest.hcl represents provider "registry.terraform.io/microsoft/azuredevops", but "azuredevops" in the root module represents
β”‚ "registry.terraform.io/hashicorp/azuredevops".
β”‚
β”‚ This means the provider definition for "azuredevops" within tests\unit.tftest.hcl has been referenced by multiple run blocks and assigned to different provider types.

I’ll fill up a new issue on the tracker soon and let you know here.

Hi @sebastien.latre, apologies - I meant within the required_providers block, leaving the actual provider definition within the test file:

terraform {
  required_providers {
    restapi = {
      source  = "Mastercard/restapi"
      version = "~> 1.18.2"
    }
    azuredevops = {
      source  = "microsoft/azuredevops"
      version = "~> 1.0.1"
    }
  }
}

My bad, I read too quickly your first answer, sorry!

It’s indeed working but does not map correctly the context. The azuredevops provider is required by a side module which output is used as input of the current one (the current having no need at all for the azuredevops provider).
But this is a good workaround, thanks!

$ terraform providers

Providers required by configuration:
.
β”œβ”€β”€ provider[registry.terraform.io/microsoft/azuredevops] ~> 1.0.1
β”œβ”€β”€ provider[registry.terraform.io/mastercard/restapi] ~> 1.18.2
└── test.tests\unit
    β”œβ”€β”€ provider[registry.terraform.io/mastercard/restapi]
    β”œβ”€β”€ provider[registry.terraform.io/microsoft/azuredevops]
    └── run.setup_azdevops_ci
        └── provider[registry.terraform.io/microsoft/azuredevops] ~> 1.0.1

Yes, I’d still consider this a bug but it’s good we have a workaround. If you have time it’d be great if you could file an issue - they have more impact coming from external users. It’ll also be easier for you to get notified once a fix is in place.

Otherwise, I can create one later to track a fix for this if you don’t have time.

Thanks!

Here is the issue: Tf test 1.8.3 - Cannot resolve properly provider definition "Provider type mismatch" Β· Issue #35160 Β· hashicorp/terraform Β· GitHub

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.