mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
1a8da65314
It's been a long while since we gave close attention to the codepaths for module source address parsing and external module package installation. Due to their age, these codepaths often diverged from our modern practices such as representing address types in the addrs package, and encapsulating package installation details only in a particular location. In particular, this refactor makes source address parsing a separate step from module installation, which therefore makes the result of that parsing available to other Terraform subsystems which work with the configuration representation objects. This also presented the opportunity to better encapsulate our use of go-getter into a new package "getmodules" (echoing "getproviders"), which is intended to be the only part of Terraform that directly interacts with go-getter. This is largely just a refactor of the existing functionality into a new code organization, but there is one notable change in behavior here: the source address parsing now happens during configuration loading rather than module installation, which may cause errors about invalid addresses to be returned in different situations than before. That counts as backward compatible because we only promise to remain compatible with configurations that are _valid_, which means that they can be initialized, planned, and applied without any errors. This doesn't introduce any new error cases, and instead just makes a pre-existing error case be detected earlier. Our module registry client is still using its own special module address type from registry/regsrc for now, with a small shim from the new addrs.ModuleSourceRegistry type. Hopefully in a later commit we'll also rework the registry client to work with the new address type, but this commit is already big enough as it is.
85 lines
2.6 KiB
Go
85 lines
2.6 KiB
Go
package earlyconfig
|
|
|
|
import (
|
|
"log"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
version "github.com/hashicorp/go-version"
|
|
"github.com/hashicorp/terraform-config-inspect/tfconfig"
|
|
svchost "github.com/hashicorp/terraform-svchost"
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
|
"github.com/hashicorp/terraform/internal/getproviders"
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
|
)
|
|
|
|
func TestConfigProviderRequirements(t *testing.T) {
|
|
cfg := testConfig(t, "testdata/provider-reqs")
|
|
|
|
impliedProvider := addrs.NewProvider(
|
|
addrs.DefaultProviderRegistryHost,
|
|
"hashicorp", "implied",
|
|
)
|
|
nullProvider := addrs.NewProvider(
|
|
addrs.DefaultProviderRegistryHost,
|
|
"hashicorp", "null",
|
|
)
|
|
randomProvider := addrs.NewProvider(
|
|
addrs.DefaultProviderRegistryHost,
|
|
"hashicorp", "random",
|
|
)
|
|
tlsProvider := addrs.NewProvider(
|
|
addrs.DefaultProviderRegistryHost,
|
|
"hashicorp", "tls",
|
|
)
|
|
happycloudProvider := addrs.NewProvider(
|
|
svchost.Hostname("tf.example.com"),
|
|
"awesomecorp", "happycloud",
|
|
)
|
|
|
|
got, diags := cfg.ProviderRequirements()
|
|
if diags.HasErrors() {
|
|
t.Fatalf("unexpected diagnostics: %s", diags.Err().Error())
|
|
}
|
|
want := getproviders.Requirements{
|
|
// the nullProvider constraints from the two modules are merged
|
|
nullProvider: getproviders.MustParseVersionConstraints("~> 2.0.0, 2.0.1"),
|
|
randomProvider: getproviders.MustParseVersionConstraints("~> 1.2.0"),
|
|
tlsProvider: getproviders.MustParseVersionConstraints("~> 3.0"),
|
|
impliedProvider: nil,
|
|
happycloudProvider: nil,
|
|
}
|
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
t.Errorf("wrong result\n%s", diff)
|
|
}
|
|
}
|
|
|
|
func testConfig(t *testing.T, baseDir string) *Config {
|
|
rootMod, diags := LoadModule(baseDir)
|
|
if diags.HasErrors() {
|
|
t.Fatalf("unexpected diagnostics: %s", diags.Err().Error())
|
|
}
|
|
|
|
cfg, diags := BuildConfig(rootMod, ModuleWalkerFunc(testModuleWalkerFunc))
|
|
if diags.HasErrors() {
|
|
t.Fatalf("unexpected diagnostics: %s", diags.Err().Error())
|
|
}
|
|
|
|
return cfg
|
|
}
|
|
|
|
// testModuleWalkerFunc is a simple implementation of ModuleWalkerFunc that
|
|
// only understands how to resolve relative filesystem paths, using source
|
|
// location information from the call.
|
|
func testModuleWalkerFunc(req *ModuleRequest) (*tfconfig.Module, *version.Version, tfdiags.Diagnostics) {
|
|
callFilename := req.CallPos.Filename
|
|
sourcePath := req.SourceAddr.String()
|
|
finalPath := filepath.Join(filepath.Dir(callFilename), sourcePath)
|
|
log.Printf("[TRACE] %s in %s -> %s", sourcePath, callFilename, finalPath)
|
|
|
|
newMod, diags := LoadModule(finalPath)
|
|
return newMod, version.Must(version.NewVersion("0.0.0")), diags
|
|
}
|