Unexpected(Unnecessary) provider resolution during `tofu test` using module in run
Community note
[!TIP] Hi there, OpenTofu community! The OpenTofu team prioritizes issues based on upvotes. Please make sure to upvote this issue and describe how it affects you in detail in the comments to show your support.
OpenTofu Version
main branchOpenTofu Configuration Files
# main.tf
terraform {
required_providers {
docker = {
source = "docker/docker"
version = "0.4.1"
}
}
}
data "docker_hub_repository" "example" {
namespace = "library"
name = "memcached"
}# main.tftest.hcl
provider "docker" {
username = "user"
password = "password"
}
run "setup" {
module {
source = "./mod"
}
}
# mod/main.tf
output "password" {
value = "pass"
}Debug Output
TF_LOG=trace tofu test 2025-03-17T12:54:57.806+0400 [INFO] OpenTofu version: 1.9.0 2025-03-17T12:54:57.807+0400 [DEBUG] using github.com/hashicorp/go-tfe v1.36.0 2025-03-17T12:54:57.807+0400 [DEBUG] using github.com/opentofu/hcl/v2 v2.0.0-20240814143621-8048794c5c52 2025-03-17T12:54:57.807+0400 [DEBUG] using github.com/hashicorp/terraform-svchost v0.1.1 2025-03-17T12:54:57.807+0400 [DEBUG] using github.com/zclconf/go-cty v1.14.4 2025-03-17T12:54:57.807+0400 [INFO] Go runtime version: go1.24.1 2025-03-17T12:54:57.807+0400 [INFO] CLI args: []string{"tofu", "test"} 2025-03-17T12:54:57.807+0400 [TRACE] Stdout is a terminal of width 133 2025-03-17T12:54:57.807+0400 [TRACE] Stderr is a terminal of width 133 2025-03-17T12:54:57.807+0400 [TRACE] Stdin is a terminal 2025-03-17T12:54:57.807+0400 [DEBUG] Attempting to open CLI config file: /home/gogotchuri/.tofurc 2025-03-17T12:54:57.807+0400 [DEBUG] File doesn't exist, but doesn't need to. Ignoring. 2025-03-17T12:54:57.807+0400 [DEBUG] ignoring non-existing provider search directory terraform.d/plugins 2025-03-17T12:54:57.807+0400 [DEBUG] ignoring non-existing provider search directory /home/gogotchuri/.terraform.d/plugins 2025-03-17T12:54:57.807+0400 [DEBUG] ignoring non-existing provider search directory /home/gogotchuri/.local/share/terraform/plugins 2025-03-17T12:54:57.807+0400 [DEBUG] ignoring non-existing provider search directory /home/gogotchuri/.local/share/flatpak/exports/share/terraform/plugins 2025-03-17T12:54:57.807+0400 [DEBUG] ignoring non-existing provider search directory /var/lib/flatpak/exports/share/terraform/plugins 2025-03-17T12:54:57.807+0400 [DEBUG] ignoring non-existing provider search directory /usr/local/share/terraform/plugins 2025-03-17T12:54:57.807+0400 [DEBUG] ignoring non-existing provider search directory /usr/share/terraform/plugins 2025-03-17T12:54:57.807+0400 [DEBUG] ignoring non-existing provider search directory /var/lib/snapd/desktop/terraform/plugins 2025-03-17T12:54:57.807+0400 [DEBUG] Found the config directory: /home/gogotchuri/.terraform.d 2025-03-17T12:54:57.807+0400 [INFO] CLI command args: []string{"test"} 2025-03-17T12:54:57.808+0400 [DEBUG] TestCommand: found 1 files with 1 run blocks 2025-03-17T12:54:57.808+0400 [TRACE] providercache.fillMetaCache: scanning directory .terraform/providers 2025-03-17T12:54:57.808+0400 [TRACE] getproviders.SearchLocalDirectory: found registry.opentofu.org/docker/docker v0.4.1 for linux_amd64 at .terraform/providers/registry.opentofu.org/docker/docker/0.4.1/linux_amd64 2025-03-17T12:54:57.808+0400 [TRACE] providercache.fillMetaCache: including .terraform/providers/registry.opentofu.org/docker/docker/0.4.1/linux_amd64 as a candidate package for registry.opentofu.org/docker/docker 0.4.1 2025-03-17T12:54:57.816+0400 [DEBUG] checking for provisioner in "." 2025-03-17T12:54:57.818+0400 [DEBUG] checking for provisioner in "/usr/bin" 2025-03-17T12:54:57.818+0400 [TRACE] TestFileRunner: executing test file main.tftest.hcl 2025-03-17T12:54:57.818+0400 [TRACE] TestFileRunner: executing run block main.tftest.hcl/setup 2025-03-17T12:54:57.818+0400 [TRACE] TestFileRunner: called validate for main.tftest.hcl/setup 2025-03-17T12:54:57.818+0400 [TRACE] tofu.NewContext: starting 2025-03-17T12:54:57.818+0400 [TRACE] tofu.NewContext: complete 2025-03-17T12:54:57.818+0400 [TRACE] TestFileRunner: waiting for execution during main.tftest.hcl/setup 2025-03-17T12:54:57.818+0400 [DEBUG] TestFileRunner: starting validate for main.tftest.hcl/setup 2025-03-17T12:54:57.818+0400 [DEBUG] TestFileRunner: completed validate for main.tftest.hcl/setup main.tftest.hcl... fail run "setup"... fail ╷ │ Error: Missing required provider │ │ This configuration requires provider registry.opentofu.org/hashicorp/docker, but that provider isn't available. You may be able to │ install it automatically by running: │ tofu init ╵ 2025-03-17T12:54:57.818+0400 [TRACE] TestStateManager: cleaning up state for main.tftest.hcl
Failure! 0 passed, 1 failed.
Expected Behavior
There are 2 possibilities here that I would expect:
- Since the
modmodule doesn't use the given provider (in this case docker) it should just ignore the provider config inside the test file during a test run - If we are resolving the provider address from the test file, we should resolve its address based on the module the test is defined in (in this case
./main.tfhas arequired_providersblock
Actual Behavior
During the test run, we are trying to resolve the provider based on the module's context that isn't using the provider. Hence, it doesn't define it in the required_providers block, and since it is the root during the test resolution we are assuming the provider address as the default address (registry.opentofu.org/hashicorp/docker instead of registry.opentofu.org/docker/docker) which doesn't exist.
Steps to Reproduce
- tofu init
- tofu test
Additional Context
No response
References
No response
Source: opentofu/opentofu