2014-09-14 23:46:45 +02:00
|
|
|
package module
|
|
|
|
|
|
|
|
import (
|
2016-12-17 01:47:32 +01:00
|
|
|
"fmt"
|
2017-09-22 22:49:58 +02:00
|
|
|
"io/ioutil"
|
2017-10-12 19:43:04 +02:00
|
|
|
"log"
|
2016-09-30 02:47:47 +02:00
|
|
|
"os"
|
2017-09-22 22:49:58 +02:00
|
|
|
"path/filepath"
|
2014-09-14 23:46:45 +02:00
|
|
|
"reflect"
|
2014-09-15 05:14:37 +02:00
|
|
|
"strings"
|
2014-09-14 23:46:45 +02:00
|
|
|
"testing"
|
2016-09-30 02:47:47 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/helper/copy"
|
2014-09-14 23:46:45 +02:00
|
|
|
)
|
|
|
|
|
2014-10-08 05:00:36 +02:00
|
|
|
func TestTreeChild(t *testing.T) {
|
2016-09-14 22:35:54 +02:00
|
|
|
var nilTree *Tree
|
|
|
|
if nilTree.Child(nil) != nil {
|
|
|
|
t.Fatal("child should be nil")
|
|
|
|
}
|
|
|
|
|
2014-10-08 05:00:36 +02:00
|
|
|
storage := testStorage(t)
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeGet
|
2014-10-08 05:00:36 +02:00
|
|
|
tree := NewTree("", testConfig(t, "child"))
|
2017-10-27 17:29:29 +02:00
|
|
|
if err := tree.Load(storage); err != nil {
|
2014-10-08 05:00:36 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-10-08 05:02:18 +02:00
|
|
|
// Should be able to get the root child
|
|
|
|
if c := tree.Child([]string{}); c == nil {
|
|
|
|
t.Fatal("should not be nil")
|
|
|
|
} else if c.Name() != "root" {
|
|
|
|
t.Fatalf("bad: %#v", c.Name())
|
2015-04-08 01:37:46 +02:00
|
|
|
} else if !reflect.DeepEqual(c.Path(), []string(nil)) {
|
|
|
|
t.Fatalf("bad: %#v", c.Path())
|
2014-10-08 05:02:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Should be able to get the root child
|
|
|
|
if c := tree.Child(nil); c == nil {
|
|
|
|
t.Fatal("should not be nil")
|
|
|
|
} else if c.Name() != "root" {
|
|
|
|
t.Fatalf("bad: %#v", c.Name())
|
2015-04-08 01:37:46 +02:00
|
|
|
} else if !reflect.DeepEqual(c.Path(), []string(nil)) {
|
|
|
|
t.Fatalf("bad: %#v", c.Path())
|
2014-10-08 05:02:18 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 05:00:36 +02:00
|
|
|
// Should be able to get the foo child
|
|
|
|
if c := tree.Child([]string{"foo"}); c == nil {
|
|
|
|
t.Fatal("should not be nil")
|
|
|
|
} else if c.Name() != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", c.Name())
|
2015-04-08 01:37:46 +02:00
|
|
|
} else if !reflect.DeepEqual(c.Path(), []string{"foo"}) {
|
|
|
|
t.Fatalf("bad: %#v", c.Path())
|
2014-10-08 05:00:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Should be able to get the nested child
|
|
|
|
if c := tree.Child([]string{"foo", "bar"}); c == nil {
|
|
|
|
t.Fatal("should not be nil")
|
|
|
|
} else if c.Name() != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", c.Name())
|
2015-04-08 01:37:46 +02:00
|
|
|
} else if !reflect.DeepEqual(c.Path(), []string{"foo", "bar"}) {
|
|
|
|
t.Fatalf("bad: %#v", c.Path())
|
2014-10-08 05:00:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
func TestTreeLoad(t *testing.T) {
|
2014-09-15 05:14:37 +02:00
|
|
|
storage := testStorage(t)
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "basic"))
|
2014-09-15 05:14:37 +02:00
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
if tree.Loaded() {
|
|
|
|
t.Fatal("should not be loaded")
|
|
|
|
}
|
|
|
|
|
2014-09-15 05:14:37 +02:00
|
|
|
// This should error because we haven't gotten things yet
|
2017-10-27 17:29:29 +02:00
|
|
|
if err := tree.Load(storage); err == nil {
|
2014-09-15 05:14:37 +02:00
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
if tree.Loaded() {
|
|
|
|
t.Fatal("should not be loaded")
|
|
|
|
}
|
|
|
|
|
2014-09-15 05:14:37 +02:00
|
|
|
// This should get things
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2014-09-15 05:14:37 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
2014-09-15 05:14:37 +02:00
|
|
|
// This should no longer error
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeNone
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2014-09-15 01:17:29 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-09-15 05:14:37 +02:00
|
|
|
|
|
|
|
actual := strings.TrimSpace(tree.String())
|
|
|
|
expected := strings.TrimSpace(treeLoadStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n\n%s", actual)
|
|
|
|
}
|
2014-09-15 01:17:29 +02:00
|
|
|
}
|
|
|
|
|
2014-09-15 18:53:29 +02:00
|
|
|
func TestTreeLoad_duplicate(t *testing.T) {
|
|
|
|
storage := testStorage(t)
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "dup"))
|
2014-09-15 18:53:29 +02:00
|
|
|
|
|
|
|
if tree.Loaded() {
|
|
|
|
t.Fatal("should not be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should get things
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err == nil {
|
2014-09-15 18:53:29 +02:00
|
|
|
t.Fatalf("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 02:47:47 +02:00
|
|
|
func TestTreeLoad_copyable(t *testing.T) {
|
|
|
|
dir := tempDir(t)
|
2017-10-27 17:29:29 +02:00
|
|
|
storage := &ModuleStorage{
|
|
|
|
StorageDir: dir,
|
|
|
|
Mode: GetModeGet,
|
|
|
|
}
|
2016-09-30 02:47:47 +02:00
|
|
|
cfg := testConfig(t, "basic")
|
|
|
|
tree := NewTree("", cfg)
|
|
|
|
|
|
|
|
// This should get things
|
2017-10-27 17:29:29 +02:00
|
|
|
if err := tree.Load(storage); err != nil {
|
2016-09-30 02:47:47 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should no longer error
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeNone
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2016-09-30 02:47:47 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we copy the directory, this COPIES symlink values, and
|
|
|
|
// doesn't create symlinks themselves. That is important.
|
|
|
|
dir2 := tempDir(t)
|
|
|
|
os.RemoveAll(dir2)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
if err := copy.CopyDir(dir, dir2); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now copy the configuration
|
|
|
|
cfgDir := tempDir(t)
|
|
|
|
os.RemoveAll(cfgDir)
|
|
|
|
defer os.RemoveAll(cfgDir)
|
|
|
|
if err := copy.CopyDir(cfg.Dir, cfgDir); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
cfg, err := config.LoadDir(cfgDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tree := NewTree("", cfg)
|
2017-10-27 17:29:29 +02:00
|
|
|
storage := &ModuleStorage{
|
|
|
|
StorageDir: dir2,
|
|
|
|
Mode: GetModeNone,
|
|
|
|
}
|
2016-09-30 02:47:47 +02:00
|
|
|
|
|
|
|
// This should not error since we already got it!
|
2017-10-27 17:29:29 +02:00
|
|
|
if err := tree.Load(storage); err != nil {
|
2016-09-30 02:47:47 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 17:11:32 +01:00
|
|
|
func TestTreeLoad_parentRef(t *testing.T) {
|
|
|
|
storage := testStorage(t)
|
|
|
|
tree := NewTree("", testConfig(t, "basic-parent"))
|
|
|
|
|
|
|
|
if tree.Loaded() {
|
|
|
|
t.Fatal("should not be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should error because we haven't gotten things yet
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeNone
|
|
|
|
if err := tree.Load(storage); err == nil {
|
2015-03-26 17:11:32 +01:00
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if tree.Loaded() {
|
|
|
|
t.Fatal("should not be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should get things
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2015-03-26 17:11:32 +01:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should no longer error
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeNone
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2015-03-26 17:11:32 +01:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(tree.String())
|
2015-03-26 17:30:32 +01:00
|
|
|
expected := strings.TrimSpace(treeLoadParentStr)
|
2015-03-26 17:11:32 +01:00
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-26 23:56:04 +02:00
|
|
|
func TestTreeLoad_subdir(t *testing.T) {
|
2017-09-21 20:06:47 +02:00
|
|
|
fixtures := []string{
|
|
|
|
"basic-subdir",
|
|
|
|
"basic-tar-subdir",
|
2017-09-21 22:32:55 +02:00
|
|
|
|
|
|
|
// Passing a subpath to go getter extracts only this subpath. The old
|
|
|
|
// internal code would keep the entire directory structure, allowing a
|
|
|
|
// top-level module to reference others through its parent directory.
|
|
|
|
// TODO: this can be removed as a breaking change in a major release.
|
|
|
|
"tar-subdir-to-parent",
|
2017-09-21 20:06:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range fixtures {
|
|
|
|
t.Run(tc, func(t *testing.T) {
|
|
|
|
storage := testStorage(t)
|
|
|
|
tree := NewTree("", testConfig(t, tc))
|
|
|
|
|
|
|
|
if tree.Loaded() {
|
|
|
|
t.Fatal("should not be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should error because we haven't gotten things yet
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeNone
|
|
|
|
if err := tree.Load(storage); err == nil {
|
2017-09-21 20:06:47 +02:00
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if tree.Loaded() {
|
|
|
|
t.Fatal("should not be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should get things
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-09-21 20:06:47 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should no longer error
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeNone
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-09-21 20:06:47 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(tree.String())
|
|
|
|
expected := strings.TrimSpace(treeLoadSubdirStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: \n\n%s", actual)
|
|
|
|
}
|
|
|
|
})
|
2014-09-26 23:56:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-21 17:56:53 +02:00
|
|
|
func TestTree_recordManifest(t *testing.T) {
|
2017-09-22 22:49:58 +02:00
|
|
|
td, err := ioutil.TempDir("", "tf-module")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage := ModuleStorage{StorageDir: td}
|
2017-10-23 17:19:24 +02:00
|
|
|
|
2017-09-22 22:49:58 +02:00
|
|
|
dir := filepath.Join(td, "0131bf0fef686e090b16bdbab4910ddf")
|
|
|
|
|
|
|
|
subDir := "subDirName"
|
|
|
|
|
|
|
|
// record and read the subdir path
|
2017-10-23 17:19:24 +02:00
|
|
|
if err := storage.recordModuleRoot(dir, subDir); err != nil {
|
2017-09-22 22:49:58 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-10-23 17:19:24 +02:00
|
|
|
actual, err := storage.getModuleRoot(dir)
|
2017-09-22 22:49:58 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != subDir {
|
|
|
|
t.Fatalf("expected subDir %q, got %q", subDir, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwrite the path, and nmake sure we get the new one
|
|
|
|
subDir = "newSubDir"
|
2017-10-23 17:19:24 +02:00
|
|
|
if err := storage.recordModuleRoot(dir, subDir); err != nil {
|
2017-09-22 22:49:58 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-10-23 17:19:24 +02:00
|
|
|
actual, err = storage.getModuleRoot(dir)
|
2017-09-22 22:49:58 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != subDir {
|
|
|
|
t.Fatalf("expected subDir %q, got %q", subDir, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a fake entry
|
2017-10-23 17:19:24 +02:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(td, manifestName), []byte("BAD DATA"), 0644); err != nil {
|
2017-09-22 22:49:58 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// this should fail because there aare now 2 entries
|
2017-10-23 17:19:24 +02:00
|
|
|
actual, err = storage.getModuleRoot(dir)
|
2017-09-22 22:49:58 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected multiple subdir entries")
|
|
|
|
}
|
|
|
|
|
|
|
|
// writing the subdir entry should remove the incorrect value
|
2017-10-23 17:19:24 +02:00
|
|
|
if err := storage.recordModuleRoot(dir, subDir); err != nil {
|
2017-09-22 22:49:58 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-10-23 17:19:24 +02:00
|
|
|
actual, err = storage.getModuleRoot(dir)
|
2017-09-22 22:49:58 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != subDir {
|
|
|
|
t.Fatalf("expected subDir %q, got %q", subDir, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
func TestTreeModules(t *testing.T) {
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "basic"))
|
2014-09-14 23:46:45 +02:00
|
|
|
actual := tree.Modules()
|
|
|
|
|
|
|
|
expected := []*Module{
|
|
|
|
&Module{Name: "foo", Source: "./foo"},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
2014-09-15 05:14:37 +02:00
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
func TestTreeName(t *testing.T) {
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "basic"))
|
2014-09-15 05:14:37 +02:00
|
|
|
actual := tree.Name()
|
|
|
|
|
2014-09-23 01:39:01 +02:00
|
|
|
if actual != RootName {
|
2014-09-15 05:14:37 +02:00
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 01:47:32 +01:00
|
|
|
// This is a table-driven test for tree validation. This is the preferred
|
|
|
|
// way to test Validate. Non table-driven tests exist historically but
|
|
|
|
// that style shouldn't be done anymore.
|
|
|
|
func TestTreeValidate_table(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Fixture string
|
|
|
|
Err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"provider alias in child",
|
|
|
|
"validate-alias-good",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"undefined provider alias in child",
|
|
|
|
"validate-alias-bad",
|
|
|
|
"alias must be defined",
|
|
|
|
},
|
2017-01-09 00:39:57 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
"root module named root",
|
|
|
|
"validate-module-root",
|
|
|
|
"cannot contain module",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"grandchild module named root",
|
|
|
|
"validate-module-root-grandchild",
|
|
|
|
"",
|
|
|
|
},
|
2016-12-17 01:47:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
|
|
|
|
tree := NewTree("", testConfig(t, tc.Fixture))
|
2017-10-27 17:29:29 +02:00
|
|
|
storage := testStorage(t)
|
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2016-12-17 01:47:32 +01:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := tree.Validate()
|
|
|
|
if (err != nil) != (tc.Err != "") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), tc.Err) {
|
|
|
|
t.Fatalf("err should contain %q: %s", tc.Err, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
func TestTreeValidate_badChild(t *testing.T) {
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "validate-child-bad"))
|
2014-09-15 18:37:40 +02:00
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage := testStorage(t)
|
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2014-09-15 18:37:40 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-09-15 22:57:07 +02:00
|
|
|
if err := tree.Validate(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTreeValidate_badChildOutput(t *testing.T) {
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "validate-bad-output"))
|
2014-09-15 22:57:07 +02:00
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage := testStorage(t)
|
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2014-09-15 22:57:07 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
if err := tree.Validate(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 02:12:28 +02:00
|
|
|
func TestTreeValidate_badChildOutputToModule(t *testing.T) {
|
|
|
|
tree := NewTree("", testConfig(t, "validate-bad-output-to-module"))
|
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage := testStorage(t)
|
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2015-04-30 02:12:28 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tree.Validate(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 19:32:41 +02:00
|
|
|
func TestTreeValidate_badChildVar(t *testing.T) {
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "validate-bad-var"))
|
2014-09-15 19:32:41 +02:00
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage := testStorage(t)
|
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2014-09-15 19:32:41 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tree.Validate(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
func TestTreeValidate_badRoot(t *testing.T) {
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "validate-root-bad"))
|
2014-09-15 18:37:40 +02:00
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage := testStorage(t)
|
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2014-09-15 18:37:40 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tree.Validate(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTreeValidate_good(t *testing.T) {
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "validate-child-good"))
|
2014-09-15 18:37:40 +02:00
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage := testStorage(t)
|
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2014-09-15 18:37:40 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tree.Validate(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTreeValidate_notLoaded(t *testing.T) {
|
2014-09-16 00:49:07 +02:00
|
|
|
tree := NewTree("", testConfig(t, "basic"))
|
2014-09-15 18:37:40 +02:00
|
|
|
|
|
|
|
if err := tree.Validate(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 04:40:06 +02:00
|
|
|
func TestTreeValidate_requiredChildVar(t *testing.T) {
|
|
|
|
tree := NewTree("", testConfig(t, "validate-required-var"))
|
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage := testStorage(t)
|
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2014-09-25 04:40:06 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-03-02 20:10:41 +01:00
|
|
|
err := tree.Validate()
|
|
|
|
if err == nil {
|
2014-09-25 04:40:06 +02:00
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
2017-03-02 20:10:41 +01:00
|
|
|
|
|
|
|
// ensure both variables are mentioned in the output
|
|
|
|
errMsg := err.Error()
|
|
|
|
for _, v := range []string{"feature", "memory"} {
|
|
|
|
if !strings.Contains(errMsg, v) {
|
|
|
|
t.Fatalf("no mention of missing variable %q", v)
|
|
|
|
}
|
|
|
|
}
|
2014-09-25 04:40:06 +02:00
|
|
|
}
|
2014-09-16 00:49:07 +02:00
|
|
|
|
2017-03-17 04:14:20 +01:00
|
|
|
func TestTreeValidate_unknownModule(t *testing.T) {
|
|
|
|
tree := NewTree("", testConfig(t, "validate-module-unknown"))
|
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage := testStorage(t)
|
|
|
|
storage.Mode = GetModeNone
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-03-17 04:14:20 +01:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tree.Validate(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 19:43:04 +02:00
|
|
|
func TestTreeProviders_basic(t *testing.T) {
|
|
|
|
storage := testStorage(t)
|
|
|
|
tree := NewTree("", testConfig(t, "basic-parent-providers"))
|
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-10-12 19:43:04 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var a, b *Tree
|
|
|
|
for _, child := range tree.Children() {
|
|
|
|
if child.Name() == "a" {
|
|
|
|
a = child
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rootProviders := tree.config.ProviderConfigsByFullName()
|
|
|
|
topRaw := rootProviders["top.foo"]
|
|
|
|
|
|
|
|
if a == nil {
|
|
|
|
t.Fatal("could not find module 'a'")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, child := range a.Children() {
|
2017-10-18 00:55:53 +02:00
|
|
|
if child.Name() == "c" {
|
2017-10-12 19:43:04 +02:00
|
|
|
b = child
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if b == nil {
|
2017-10-18 00:55:53 +02:00
|
|
|
t.Fatal("could not find module 'c'")
|
2017-10-12 19:43:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
aProviders := a.config.ProviderConfigsByFullName()
|
|
|
|
bottomRaw := aProviders["bottom.foo"]
|
|
|
|
bProviders := b.config.ProviderConfigsByFullName()
|
|
|
|
bBottom := bProviders["bottom"]
|
|
|
|
|
|
|
|
// compare the configs
|
|
|
|
// top.foo should have been copied to a.top
|
|
|
|
aTop := aProviders["top"]
|
|
|
|
if !reflect.DeepEqual(aTop.RawConfig.RawMap(), topRaw.RawConfig.RawMap()) {
|
|
|
|
log.Fatalf("expected config %#v, got %#v",
|
|
|
|
topRaw.RawConfig.RawMap(),
|
|
|
|
aTop.RawConfig.RawMap(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:44:04 +02:00
|
|
|
if !reflect.DeepEqual(aTop.Path, []string{RootName}) {
|
|
|
|
log.Fatalf(`expected scope for "top": {"root"}, got %#v`, aTop.Path)
|
2017-10-12 19:43:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(bBottom.RawConfig.RawMap(), bottomRaw.RawConfig.RawMap()) {
|
|
|
|
t.Fatalf("expected config %#v, got %#v",
|
|
|
|
bottomRaw.RawConfig.RawMap(),
|
|
|
|
bBottom.RawConfig.RawMap(),
|
|
|
|
)
|
|
|
|
}
|
2017-10-18 00:44:04 +02:00
|
|
|
if !reflect.DeepEqual(bBottom.Path, []string{RootName, "a"}) {
|
|
|
|
t.Fatalf(`expected scope for "bottom": {"root", "a"}, got %#v`, bBottom.Path)
|
2017-10-12 19:43:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTreeProviders_implicit(t *testing.T) {
|
|
|
|
storage := testStorage(t)
|
|
|
|
tree := NewTree("", testConfig(t, "implicit-parent-providers"))
|
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-10-12 19:43:04 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var child *Tree
|
|
|
|
for _, c := range tree.Children() {
|
|
|
|
if c.Name() == "child" {
|
|
|
|
child = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if child == nil {
|
|
|
|
t.Fatal("could not find module 'child'")
|
|
|
|
}
|
|
|
|
|
|
|
|
// child should have inherited foo
|
|
|
|
providers := child.config.ProviderConfigsByFullName()
|
|
|
|
foo := providers["foo"]
|
|
|
|
|
|
|
|
if foo == nil {
|
|
|
|
t.Fatal("could not find provider 'foo' in child module")
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:44:04 +02:00
|
|
|
if !reflect.DeepEqual([]string{RootName}, foo.Path) {
|
|
|
|
t.Fatalf(`expected foo scope of {"root"}, got %#v`, foo.Path)
|
2017-10-12 19:43:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"value": "from root",
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, foo.RawConfig.RawMap()) {
|
|
|
|
t.Fatalf(`expected "foo" config %#v, got: %#v`, expected, foo.RawConfig.RawMap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTreeProviders_implicitMultiLevel(t *testing.T) {
|
|
|
|
storage := testStorage(t)
|
|
|
|
tree := NewTree("", testConfig(t, "implicit-grandparent-providers"))
|
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-10-12 19:43:04 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var child, grandchild *Tree
|
|
|
|
for _, c := range tree.Children() {
|
|
|
|
if c.Name() == "child" {
|
|
|
|
child = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if child == nil {
|
|
|
|
t.Fatal("could not find module 'child'")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range child.Children() {
|
|
|
|
if c.Name() == "grandchild" {
|
|
|
|
grandchild = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if grandchild == nil {
|
|
|
|
t.Fatal("could not find module 'grandchild'")
|
|
|
|
}
|
|
|
|
|
|
|
|
// child should have inherited foo
|
|
|
|
providers := child.config.ProviderConfigsByFullName()
|
|
|
|
foo := providers["foo"]
|
|
|
|
|
|
|
|
if foo == nil {
|
|
|
|
t.Fatal("could not find provider 'foo' in child module")
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:44:04 +02:00
|
|
|
if !reflect.DeepEqual([]string{RootName}, foo.Path) {
|
|
|
|
t.Fatalf(`expected foo scope of {"root"}, got %#v`, foo.Path)
|
2017-10-12 19:43:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"value": "from root",
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, foo.RawConfig.RawMap()) {
|
|
|
|
t.Fatalf(`expected "foo" config %#v, got: %#v`, expected, foo.RawConfig.RawMap())
|
|
|
|
}
|
|
|
|
|
|
|
|
// grandchild should have inherited bar
|
|
|
|
providers = grandchild.config.ProviderConfigsByFullName()
|
|
|
|
bar := providers["bar"]
|
|
|
|
|
|
|
|
if bar == nil {
|
|
|
|
t.Fatal("could not find provider 'bar' in grandchild module")
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:44:04 +02:00
|
|
|
if !reflect.DeepEqual([]string{RootName, "child"}, bar.Path) {
|
|
|
|
t.Fatalf(`expected bar scope of {"root", "child"}, got %#v`, bar.Path)
|
2017-10-12 19:43:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
expected = map[string]interface{}{
|
|
|
|
"value": "from child",
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, bar.RawConfig.RawMap()) {
|
|
|
|
t.Fatalf(`expected "bar" config %#v, got: %#v`, expected, bar.RawConfig.RawMap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 17:58:44 +02:00
|
|
|
func TestTreeLoad_conflictingSubmoduleNames(t *testing.T) {
|
|
|
|
storage := testStorage(t)
|
|
|
|
tree := NewTree("", testConfig(t, "conficting-submodule-names"))
|
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-10-23 17:58:44 +02:00
|
|
|
t.Fatalf("load failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to reload
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeNone
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-10-23 17:58:44 +02:00
|
|
|
t.Fatalf("reload failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify that the grand-children are correctly loaded
|
|
|
|
for _, c := range tree.Children() {
|
|
|
|
for _, gc := range c.Children() {
|
|
|
|
if len(gc.config.Resources) != 1 {
|
|
|
|
t.Fatalf("expected 1 resource in %s, got %d", gc.name, len(gc.config.Resources))
|
|
|
|
}
|
|
|
|
res := gc.config.Resources[0]
|
|
|
|
switch gc.path[0] {
|
|
|
|
case "a":
|
|
|
|
if res.Name != "a-c" {
|
|
|
|
t.Fatal("found wrong resource in a/c:", res.Name)
|
|
|
|
}
|
|
|
|
case "b":
|
|
|
|
if res.Name != "b-c" {
|
|
|
|
t.Fatal("found wrong resource in b/c:", res.Name)
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 22:00:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// changing the source for a module but not the module "path"
|
|
|
|
func TestTreeLoad_changeIntermediateSource(t *testing.T) {
|
|
|
|
// copy the config to our tempdir this time, since we're going to edit it
|
|
|
|
td, err := ioutil.TempDir("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
|
|
|
if err := copyDir(td, filepath.Join(fixtureDir, "change-intermediate-source")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(td); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(wd)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(".terraform/modules", 0777); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-10-27 17:29:29 +02:00
|
|
|
storage := &ModuleStorage{StorageDir: ".terraform/modules"}
|
2017-10-24 22:00:42 +02:00
|
|
|
cfg, err := config.LoadDir("./")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tree := NewTree("", cfg)
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-10-24 22:00:42 +02:00
|
|
|
t.Fatalf("load failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we change the source of our module, without changing its path
|
|
|
|
if err := os.Rename("main.tf.disabled", "main.tf"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// reload the tree
|
|
|
|
cfg, err = config.LoadDir("./")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tree = NewTree("", cfg)
|
2017-10-27 17:29:29 +02:00
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-10-24 22:00:42 +02:00
|
|
|
t.Fatalf("load failed: %s", err)
|
|
|
|
}
|
2017-10-23 17:58:44 +02:00
|
|
|
|
2017-10-24 22:00:42 +02:00
|
|
|
// check for our resource in b
|
|
|
|
for _, c := range tree.Children() {
|
|
|
|
for _, gc := range c.Children() {
|
|
|
|
if len(gc.config.Resources) != 1 {
|
|
|
|
t.Fatalf("expected 1 resource in %s, got %d", gc.name, len(gc.config.Resources))
|
|
|
|
}
|
|
|
|
res := gc.config.Resources[0]
|
|
|
|
expected := "c-b"
|
|
|
|
if res.Name != expected {
|
|
|
|
t.Fatalf("expexted resource %q, got %q", expected, res.Name)
|
|
|
|
}
|
2017-10-23 17:58:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 05:14:37 +02:00
|
|
|
const treeLoadStr = `
|
2014-09-23 01:39:01 +02:00
|
|
|
root
|
2015-04-08 01:37:46 +02:00
|
|
|
foo (path: foo)
|
2014-09-15 05:14:37 +02:00
|
|
|
`
|
2014-09-26 23:56:04 +02:00
|
|
|
|
2015-03-26 17:30:32 +01:00
|
|
|
const treeLoadParentStr = `
|
|
|
|
root
|
2015-04-08 01:37:46 +02:00
|
|
|
a (path: a)
|
|
|
|
b (path: a, b)
|
2015-03-26 17:30:32 +01:00
|
|
|
`
|
2014-09-26 23:56:04 +02:00
|
|
|
const treeLoadSubdirStr = `
|
|
|
|
root
|
2015-04-08 01:37:46 +02:00
|
|
|
foo (path: foo)
|
|
|
|
bar (path: foo, bar)
|
2014-09-26 23:56:04 +02:00
|
|
|
`
|
2017-10-18 00:01:34 +02:00
|
|
|
|
|
|
|
const treeLoadRegistrySubdirStr = `
|
|
|
|
root
|
|
|
|
foo (path: foo)
|
|
|
|
`
|