2019-01-09 03:39:14 +01:00
|
|
|
package initwd
|
2018-02-15 05:13:35 +01:00
|
|
|
|
|
|
|
import (
|
2020-06-17 19:24:56 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-02-15 05:13:35 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
version "github.com/hashicorp/go-version"
|
|
|
|
"github.com/hashicorp/terraform/configs"
|
2019-01-09 03:39:14 +01:00
|
|
|
"github.com/hashicorp/terraform/configs/configload"
|
2020-10-07 18:48:25 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/copy"
|
2019-01-09 03:39:14 +01:00
|
|
|
"github.com/hashicorp/terraform/registry"
|
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2018-02-15 05:13:35 +01:00
|
|
|
)
|
|
|
|
|
2019-01-09 03:39:14 +01:00
|
|
|
func TestDirFromModule_registry(t *testing.T) {
|
2018-02-15 05:13:35 +01: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 09:38:36 +02:00
|
|
|
fixtureDir := filepath.Clean("testdata/empty")
|
2019-09-25 22:27:17 +02:00
|
|
|
tmpDir, done := tempChdir(t, fixtureDir)
|
2020-06-17 19:24:56 +02:00
|
|
|
defer done()
|
2019-09-26 17:30:52 +02: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 22:27:17 +02:00
|
|
|
dir, err := filepath.EvalSymlinks(tmpDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-01-09 03:39:14 +01:00
|
|
|
modsDir := filepath.Join(dir, ".terraform/modules")
|
2018-02-15 05:13:35 +01:00
|
|
|
|
|
|
|
hooks := &testInstallHooks{}
|
|
|
|
|
2019-01-09 03:39:14 +01:00
|
|
|
reg := registry.NewClient(nil, nil)
|
|
|
|
diags := DirFromModule(dir, modsDir, "hashicorp/module-installer-acctest/aws//examples/main", reg, hooks)
|
2018-02-15 05:13:35 +01:00
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
|
2020-06-17 19:24:56 +02:00
|
|
|
v := version.Must(version.NewVersion("0.0.2"))
|
2018-02-15 05:13:35 +01: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",
|
|
|
|
PackageAddr: "hashicorp/module-installer-acctest/aws",
|
|
|
|
Version: v,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Install",
|
|
|
|
ModuleAddr: "root",
|
|
|
|
Version: v,
|
2020-06-17 19:24:56 +02:00
|
|
|
LocalPath: filepath.Join(dir, fmt.Sprintf(".terraform/modules/root/terraform-aws-module-installer-acctest-%s", v)),
|
2018-02-15 05:13:35 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Install",
|
|
|
|
ModuleAddr: "root.child_a",
|
2020-06-17 19:24:56 +02:00
|
|
|
LocalPath: filepath.Join(dir, fmt.Sprintf(".terraform/modules/root/terraform-aws-module-installer-acctest-%s/modules/child_a", v)),
|
2018-02-15 05:13:35 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Install",
|
|
|
|
ModuleAddr: "root.child_a.child_b",
|
2020-06-17 19:24:56 +02:00
|
|
|
LocalPath: filepath.Join(dir, fmt.Sprintf(".terraform/modules/root/terraform-aws-module-installer-acctest-%s/modules/child_b", v)),
|
2018-02-15 05:13:35 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if assertResultDeepEqual(t, hooks.Calls, wantCalls) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-09 03:39:14 +01:00
|
|
|
loader, err := configload.NewLoader(&configload.Config{
|
|
|
|
ModulesDir: modsDir,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-02-15 05:13:35 +01:00
|
|
|
// Make sure the configuration is loadable now.
|
|
|
|
// (This ensures that correct information is recorded in the manifest.)
|
|
|
|
config, loadDiags := loader.LoadConfig(".")
|
2019-01-09 03:39:14 +01:00
|
|
|
if assertNoDiagnostics(t, tfdiags.Diagnostics{}.Append(loadDiags)) {
|
2018-02-15 05:13:35 +01: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 19:24:56 +02: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 16:02:22 +02: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 19:24:56 +02: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")
|
|
|
|
|
|
|
|
diags := DirFromModule(dir, modInstallDir, fromModuleDir, nil, hooks)
|
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
wantCalls := []testInstallHookCall{
|
|
|
|
{
|
|
|
|
Name: "Install",
|
|
|
|
ModuleAddr: "child_a",
|
2020-08-27 16:02:22 +02:00
|
|
|
LocalPath: filepath.Join(fromModuleDirRealpath, "child_a"),
|
2020-06-17 19:24:56 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Install",
|
|
|
|
ModuleAddr: "child_a.child_b",
|
2020-08-27 16:02:22 +02:00
|
|
|
LocalPath: filepath.Join(fromModuleDirRealpath, "child_a/child_b"),
|
2020-06-17 19:24:56 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if assertResultDeepEqual(t, hooks.Calls, wantCalls) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
loader, err := configload.NewLoader(&configload.Config{
|
|
|
|
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)
|
|
|
|
tmpDir, err := ioutil.TempDir("", "terraform-configload")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
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 18:48:25 +02:00
|
|
|
if err := copy.CopyDir(fromModuleDir, "testdata/local-modules"); err != nil {
|
2020-06-17 19:24:56 +02: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)
|
|
|
|
}
|
|
|
|
defer os.Chdir(oldDir)
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
hooks := &testInstallHooks{}
|
|
|
|
|
|
|
|
modInstallDir := ".terraform/modules"
|
|
|
|
sourceDir := "../local-modules"
|
|
|
|
diags := DirFromModule(".", modInstallDir, sourceDir, nil, hooks)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
loader, err := configload.NewLoader(&configload.Config{
|
|
|
|
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)
|
|
|
|
}
|