2019-01-08 20:39:14 -06:00
|
|
|
package initwd
|
2018-02-14 22:13:35 -06:00
|
|
|
|
|
|
|
import (
|
2021-11-01 15:09:16 -05:00
|
|
|
"context"
|
2018-02-14 22:13:35 -06:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
Refactoring of module source addresses and module installation
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.
2021-05-27 21:24:59 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2018-02-14 22:13:35 -06:00
|
|
|
version "github.com/hashicorp/go-version"
|
2021-05-17 14:17:09 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/configs"
|
|
|
|
"github.com/hashicorp/terraform/internal/configs/configload"
|
2020-10-07 11:48:25 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/copy"
|
2021-05-17 11:45:36 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/registry"
|
2021-05-17 12:11:06 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
2018-02-14 22:13:35 -06:00
|
|
|
)
|
|
|
|
|
2019-01-08 20:39:14 -06:00
|
|
|
func TestDirFromModule_registry(t *testing.T) {
|
2018-02-14 22:13:35 -06:00
|
|
|
if os.Getenv("TF_ACC") == "" {
|
|
|
|
t.Skip("this test accesses registry.terraform.io and github.com; set TF_ACC=1 to run it")
|
|
|
|
}
|
|
|
|
|
2019-06-30 02:38:36 -05:00
|
|
|
fixtureDir := filepath.Clean("testdata/empty")
|
2019-09-25 15:27:17 -05:00
|
|
|
tmpDir, done := tempChdir(t, fixtureDir)
|
2020-06-17 12:24:56 -05:00
|
|
|
defer done()
|
2019-09-26 10:30:52 -05:00
|
|
|
|
|
|
|
// the module installer runs filepath.EvalSymlinks() on the destination
|
|
|
|
// directory before copying files, and the resultant directory is what is
|
|
|
|
// returned by the install hooks. Without this, tests could fail on machines
|
|
|
|
// where the default temp dir was a symlink.
|
2019-09-25 15:27:17 -05:00
|
|
|
dir, err := filepath.EvalSymlinks(tmpDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-01-08 20:39:14 -06:00
|
|
|
modsDir := filepath.Join(dir, ".terraform/modules")
|
2018-02-14 22:13:35 -06:00
|
|
|
|
|
|
|
hooks := &testInstallHooks{}
|
|
|
|
|
2019-01-08 20:39:14 -06:00
|
|
|
reg := registry.NewClient(nil, nil)
|
2023-02-17 05:40:47 -06:00
|
|
|
loader, cleanup := configload.NewLoaderForTests(t)
|
|
|
|
defer cleanup()
|
|
|
|
diags := DirFromModule(context.Background(), loader, dir, modsDir, "hashicorp/module-installer-acctest/aws//examples/main", reg, hooks)
|
2018-02-14 22:13:35 -06:00
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
|
2020-06-17 12:24:56 -05:00
|
|
|
v := version.Must(version.NewVersion("0.0.2"))
|
2018-02-14 22:13:35 -06:00
|
|
|
|
|
|
|
wantCalls := []testInstallHookCall{
|
|
|
|
// The module specified to populate the root directory is not mentioned
|
|
|
|
// here, because the hook mechanism is defined to talk about descendent
|
|
|
|
// modules only and so a caller to InitDirFromModule is expected to
|
|
|
|
// produce its own user-facing announcement about the root module being
|
|
|
|
// installed.
|
|
|
|
|
|
|
|
// Note that "root" in the following examples is, confusingly, the
|
|
|
|
// label on the module block in the example we've installed here:
|
|
|
|
// module "root" {
|
|
|
|
|
|
|
|
{
|
|
|
|
Name: "Download",
|
|
|
|
ModuleAddr: "root",
|
Refactoring of module source addresses and module installation
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.
2021-05-27 21:24:59 -05:00
|
|
|
PackageAddr: "registry.terraform.io/hashicorp/module-installer-acctest/aws",
|
2018-02-14 22:13:35 -06:00
|
|
|
Version: v,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Install",
|
|
|
|
ModuleAddr: "root",
|
|
|
|
Version: v,
|
2021-06-02 13:38:53 -05:00
|
|
|
// NOTE: This local path and the other paths derived from it below
|
|
|
|
// can vary depending on how the registry is implemented. At the
|
|
|
|
// time of writing this test, registry.terraform.io returns
|
|
|
|
// git repository source addresses and so this path refers to the
|
|
|
|
// root of the git clone, but historically the registry referred
|
|
|
|
// to GitHub-provided tar archives which meant that there was an
|
|
|
|
// extra level of subdirectory here for the typical directory
|
|
|
|
// nesting in tar archives, which would've been reflected as
|
|
|
|
// an extra segment on this path. If this test fails due to an
|
|
|
|
// additional path segment in future, then a change to the upstream
|
|
|
|
// registry might be the root cause.
|
|
|
|
LocalPath: filepath.Join(dir, ".terraform/modules/root"),
|
2018-02-14 22:13:35 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Install",
|
|
|
|
ModuleAddr: "root.child_a",
|
2021-06-02 13:38:53 -05:00
|
|
|
LocalPath: filepath.Join(dir, ".terraform/modules/root/modules/child_a"),
|
2018-02-14 22:13:35 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Install",
|
|
|
|
ModuleAddr: "root.child_a.child_b",
|
2021-06-02 13:38:53 -05:00
|
|
|
LocalPath: filepath.Join(dir, ".terraform/modules/root/modules/child_b"),
|
2018-02-14 22:13:35 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
Refactoring of module source addresses and module installation
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.
2021-05-27 21:24:59 -05:00
|
|
|
if diff := cmp.Diff(wantCalls, hooks.Calls); diff != "" {
|
|
|
|
t.Fatalf("wrong installer calls\n%s", diff)
|
2018-02-14 22:13:35 -06:00
|
|
|
}
|
|
|
|
|
2023-02-17 05:40:47 -06:00
|
|
|
loader, err = configload.NewLoader(&configload.Config{
|
2019-01-08 20:39:14 -06:00
|
|
|
ModulesDir: modsDir,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-02-14 22:13:35 -06:00
|
|
|
// Make sure the configuration is loadable now.
|
|
|
|
// (This ensures that correct information is recorded in the manifest.)
|
|
|
|
config, loadDiags := loader.LoadConfig(".")
|
2019-01-08 20:39:14 -06:00
|
|
|
if assertNoDiagnostics(t, tfdiags.Diagnostics{}.Append(loadDiags)) {
|
2018-02-14 22:13:35 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
wantTraces := map[string]string{
|
|
|
|
"": "in example",
|
|
|
|
"root": "in root module",
|
|
|
|
"root.child_a": "in child_a module",
|
|
|
|
"root.child_a.child_b": "in child_b module",
|
|
|
|
}
|
|
|
|
gotTraces := map[string]string{}
|
|
|
|
config.DeepEach(func(c *configs.Config) {
|
|
|
|
path := strings.Join(c.Path, ".")
|
|
|
|
if c.Module.Variables["v"] == nil {
|
|
|
|
gotTraces[path] = "<missing>"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
varDesc := c.Module.Variables["v"].Description
|
|
|
|
gotTraces[path] = varDesc
|
|
|
|
})
|
|
|
|
assertResultDeepEqual(t, gotTraces, wantTraces)
|
|
|
|
}
|
2020-06-17 12:24:56 -05:00
|
|
|
|
|
|
|
func TestDirFromModule_submodules(t *testing.T) {
|
|
|
|
fixtureDir := filepath.Clean("testdata/empty")
|
|
|
|
fromModuleDir, err := filepath.Abs("./testdata/local-modules")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-27 09:02:22 -05:00
|
|
|
// DirFromModule will expand ("canonicalize") the pathnames, so we must do
|
|
|
|
// the same for our "wantCalls" comparison values. Otherwise this test
|
|
|
|
// will fail when building in a source tree with symlinks in $PWD.
|
|
|
|
//
|
|
|
|
// See also: https://github.com/hashicorp/terraform/issues/26014
|
|
|
|
//
|
|
|
|
fromModuleDirRealpath, err := filepath.EvalSymlinks(fromModuleDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2020-06-17 12:24:56 -05:00
|
|
|
tmpDir, done := tempChdir(t, fixtureDir)
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
hooks := &testInstallHooks{}
|
|
|
|
dir, err := filepath.EvalSymlinks(tmpDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
modInstallDir := filepath.Join(dir, ".terraform/modules")
|
|
|
|
|
2023-02-17 05:40:47 -06:00
|
|
|
loader, cleanup := configload.NewLoaderForTests(t)
|
|
|
|
defer cleanup()
|
|
|
|
diags := DirFromModule(context.Background(), loader, dir, modInstallDir, fromModuleDir, nil, hooks)
|
2020-06-17 12:24:56 -05:00
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
wantCalls := []testInstallHookCall{
|
|
|
|
{
|
|
|
|
Name: "Install",
|
|
|
|
ModuleAddr: "child_a",
|
2020-08-27 09:02:22 -05:00
|
|
|
LocalPath: filepath.Join(fromModuleDirRealpath, "child_a"),
|
2020-06-17 12:24:56 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Install",
|
|
|
|
ModuleAddr: "child_a.child_b",
|
2020-08-27 09:02:22 -05:00
|
|
|
LocalPath: filepath.Join(fromModuleDirRealpath, "child_a/child_b"),
|
2020-06-17 12:24:56 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if assertResultDeepEqual(t, hooks.Calls, wantCalls) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-17 05:40:47 -06:00
|
|
|
loader, err = configload.NewLoader(&configload.Config{
|
2020-06-17 12:24:56 -05:00
|
|
|
ModulesDir: modInstallDir,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the configuration is loadable now.
|
|
|
|
// (This ensures that correct information is recorded in the manifest.)
|
|
|
|
config, loadDiags := loader.LoadConfig(".")
|
|
|
|
if assertNoDiagnostics(t, tfdiags.Diagnostics{}.Append(loadDiags)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wantTraces := map[string]string{
|
|
|
|
"": "in root module",
|
|
|
|
"child_a": "in child_a module",
|
|
|
|
"child_a.child_b": "in child_b module",
|
|
|
|
}
|
|
|
|
gotTraces := map[string]string{}
|
|
|
|
|
|
|
|
config.DeepEach(func(c *configs.Config) {
|
|
|
|
path := strings.Join(c.Path, ".")
|
|
|
|
if c.Module.Variables["v"] == nil {
|
|
|
|
gotTraces[path] = "<missing>"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
varDesc := c.Module.Variables["v"].Description
|
|
|
|
gotTraces[path] = varDesc
|
|
|
|
})
|
|
|
|
assertResultDeepEqual(t, gotTraces, wantTraces)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestDirFromModule_rel_submodules is similar to the test above, but the
|
|
|
|
// from-module is relative to the install dir ("../"):
|
|
|
|
// https://github.com/hashicorp/terraform/issues/23010
|
|
|
|
func TestDirFromModule_rel_submodules(t *testing.T) {
|
|
|
|
// This test creates a tmpdir with the following directory structure:
|
|
|
|
// - tmpdir/local-modules (with contents of testdata/local-modules)
|
|
|
|
// - tmpdir/empty: the workDir we CD into for the test
|
|
|
|
// - tmpdir/empty/target (target, the destination for init -from-module)
|
2022-04-08 11:34:16 -05:00
|
|
|
tmpDir := t.TempDir()
|
2020-06-17 12:24:56 -05:00
|
|
|
fromModuleDir := filepath.Join(tmpDir, "local-modules")
|
|
|
|
workDir := filepath.Join(tmpDir, "empty")
|
|
|
|
if err := os.Mkdir(fromModuleDir, os.ModePerm); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-10-07 11:48:25 -05:00
|
|
|
if err := copy.CopyDir(fromModuleDir, "testdata/local-modules"); err != nil {
|
2020-06-17 12:24:56 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := os.Mkdir(workDir, os.ModePerm); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
targetDir := filepath.Join(tmpDir, "target")
|
|
|
|
if err := os.Mkdir(targetDir, os.ModePerm); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
oldDir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = os.Chdir(targetDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to switch to temp dir %s: %s", tmpDir, err)
|
|
|
|
}
|
2022-04-08 11:34:16 -05:00
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Chdir(oldDir)
|
|
|
|
})
|
2020-06-17 12:24:56 -05:00
|
|
|
|
|
|
|
hooks := &testInstallHooks{}
|
|
|
|
|
|
|
|
modInstallDir := ".terraform/modules"
|
|
|
|
sourceDir := "../local-modules"
|
2023-02-17 05:40:47 -06:00
|
|
|
loader, cleanup := configload.NewLoaderForTests(t)
|
|
|
|
defer cleanup()
|
|
|
|
diags := DirFromModule(context.Background(), loader, ".", modInstallDir, sourceDir, nil, hooks)
|
2020-06-17 12:24:56 -05:00
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
wantCalls := []testInstallHookCall{
|
|
|
|
{
|
|
|
|
Name: "Install",
|
|
|
|
ModuleAddr: "child_a",
|
|
|
|
LocalPath: filepath.Join(sourceDir, "child_a"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Install",
|
|
|
|
ModuleAddr: "child_a.child_b",
|
|
|
|
LocalPath: filepath.Join(sourceDir, "child_a/child_b"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if assertResultDeepEqual(t, hooks.Calls, wantCalls) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-17 05:40:47 -06:00
|
|
|
loader, err = configload.NewLoader(&configload.Config{
|
2020-06-17 12:24:56 -05:00
|
|
|
ModulesDir: modInstallDir,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the configuration is loadable now.
|
|
|
|
// (This ensures that correct information is recorded in the manifest.)
|
|
|
|
config, loadDiags := loader.LoadConfig(".")
|
|
|
|
if assertNoDiagnostics(t, tfdiags.Diagnostics{}.Append(loadDiags)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wantTraces := map[string]string{
|
|
|
|
"": "in root module",
|
|
|
|
"child_a": "in child_a module",
|
|
|
|
"child_a.child_b": "in child_b module",
|
|
|
|
}
|
|
|
|
gotTraces := map[string]string{}
|
|
|
|
|
|
|
|
config.DeepEach(func(c *configs.Config) {
|
|
|
|
path := strings.Join(c.Path, ".")
|
|
|
|
if c.Module.Variables["v"] == nil {
|
|
|
|
gotTraces[path] = "<missing>"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
varDesc := c.Module.Variables["v"].Description
|
|
|
|
gotTraces[path] = varDesc
|
|
|
|
})
|
|
|
|
assertResultDeepEqual(t, gotTraces, wantTraces)
|
|
|
|
}
|