2018-02-10 00:32:49 +01:00
|
|
|
package configload
|
|
|
|
|
|
|
|
import (
|
2020-05-08 17:35:28 +02:00
|
|
|
"fmt"
|
2018-02-10 00:32:49 +01:00
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/configs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoaderLoadConfig_okay(t *testing.T) {
|
2019-06-30 09:38:36 +02:00
|
|
|
fixtureDir := filepath.Clean("testdata/already-installed")
|
2018-02-10 00:32:49 +01:00
|
|
|
loader, err := NewLoader(&Config{
|
|
|
|
ModulesDir: filepath.Join(fixtureDir, ".terraform/modules"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error from NewLoader: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, diags := loader.LoadConfig(fixtureDir)
|
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
if cfg == nil {
|
|
|
|
t.Fatalf("config is nil; want non-nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
var gotPaths []string
|
|
|
|
cfg.DeepEach(func(c *configs.Config) {
|
|
|
|
gotPaths = append(gotPaths, strings.Join(c.Path, "."))
|
|
|
|
})
|
|
|
|
sort.Strings(gotPaths)
|
|
|
|
wantPaths := []string{
|
|
|
|
"", // root module
|
|
|
|
"child_a",
|
|
|
|
"child_a.child_c",
|
|
|
|
"child_b",
|
|
|
|
"child_b.child_d",
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(gotPaths, wantPaths) {
|
|
|
|
t.Fatalf("wrong module paths\ngot: %swant %s", spew.Sdump(gotPaths), spew.Sdump(wantPaths))
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("child_a.child_c output", func(t *testing.T) {
|
|
|
|
output := cfg.Children["child_a"].Children["child_c"].Module.Outputs["hello"]
|
|
|
|
got, diags := output.Expr.Value(nil)
|
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
assertResultCtyEqual(t, got, cty.StringVal("Hello from child_c"))
|
|
|
|
})
|
|
|
|
t.Run("child_b.child_d output", func(t *testing.T) {
|
|
|
|
output := cfg.Children["child_b"].Children["child_d"].Module.Outputs["hello"]
|
|
|
|
got, diags := output.Expr.Value(nil)
|
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
assertResultCtyEqual(t, got, cty.StringVal("Hello from child_d"))
|
|
|
|
})
|
|
|
|
}
|
2019-06-03 17:55:43 +02:00
|
|
|
|
|
|
|
func TestLoaderLoadConfig_addVersion(t *testing.T) {
|
|
|
|
// This test is for what happens when there is a version constraint added
|
|
|
|
// to a module that previously didn't have one.
|
2019-06-30 09:38:36 +02:00
|
|
|
fixtureDir := filepath.Clean("testdata/add-version-constraint")
|
2019-06-03 17:55:43 +02:00
|
|
|
loader, err := NewLoader(&Config{
|
|
|
|
ModulesDir: filepath.Join(fixtureDir, ".terraform/modules"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error from NewLoader: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, diags := loader.LoadConfig(fixtureDir)
|
|
|
|
if !diags.HasErrors() {
|
|
|
|
t.Fatalf("success; want error")
|
|
|
|
}
|
|
|
|
got := diags.Error()
|
2020-05-08 17:35:28 +02:00
|
|
|
want := "Module version requirements have changed"
|
|
|
|
if !strings.Contains(got, want) {
|
2019-06-03 17:55:43 +02:00
|
|
|
t.Fatalf("wrong error\ngot:\n%s\n\nwant: containing %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 17:35:28 +02:00
|
|
|
|
|
|
|
func TestLoaderLoadConfig_moduleExpand(t *testing.T) {
|
|
|
|
// We do not allow providers to be configured in expanding modules
|
|
|
|
// In addition, if a provider is present but an empty block, it is allowed,
|
|
|
|
// but IFF a provider is passed through the module call
|
2020-06-16 19:52:41 +02:00
|
|
|
paths := []string{"provider-configured", "no-provider-passed", "nested-provider", "more-nested-provider"}
|
2020-05-08 17:35:28 +02:00
|
|
|
for _, p := range paths {
|
|
|
|
fixtureDir := filepath.Clean(fmt.Sprintf("testdata/expand-modules/%s", p))
|
|
|
|
loader, err := NewLoader(&Config{
|
|
|
|
ModulesDir: filepath.Join(fixtureDir, ".terraform/modules"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-06-16 19:52:41 +02:00
|
|
|
t.Fatalf("unexpected error from NewLoader at path %s: %s", p, err)
|
2020-05-08 17:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, diags := loader.LoadConfig(fixtureDir)
|
|
|
|
if !diags.HasErrors() {
|
2020-06-16 19:52:41 +02:00
|
|
|
t.Fatalf("success; want error at path %s", p)
|
2020-05-08 17:35:28 +02:00
|
|
|
}
|
|
|
|
got := diags.Error()
|
|
|
|
want := "Module does not support count"
|
|
|
|
if !strings.Contains(got, want) {
|
2020-06-16 19:52:41 +02:00
|
|
|
t.Fatalf("wrong error at path %s \ngot:\n%s\n\nwant: containing %q", p, got, want)
|
2020-05-08 17:35:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoaderLoadConfig_moduleExpandValid(t *testing.T) {
|
|
|
|
// This tests for when valid configs are passing a provider through as a proxy,
|
|
|
|
// either with or without an alias present.
|
|
|
|
fixtureDir := filepath.Clean("testdata/expand-modules/valid")
|
|
|
|
loader, err := NewLoader(&Config{
|
|
|
|
ModulesDir: filepath.Join(fixtureDir, ".terraform/modules"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error from NewLoader: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, diags := loader.LoadConfig(fixtureDir)
|
|
|
|
assertNoDiagnostics(t, diags)
|
|
|
|
}
|
2020-06-23 03:04:19 +02:00
|
|
|
|
|
|
|
func TestLoaderLoadConfig_moduleDependsOnProviders(t *testing.T) {
|
|
|
|
// We do not allow providers to be configured in module using depends_on.
|
|
|
|
fixtureDir := filepath.Clean("testdata/module-depends-on")
|
|
|
|
loader, err := NewLoader(&Config{
|
|
|
|
ModulesDir: filepath.Join(fixtureDir, ".terraform/modules"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error from NewLoader: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, diags := loader.LoadConfig(fixtureDir)
|
|
|
|
if !diags.HasErrors() {
|
|
|
|
t.Fatal("success; want error")
|
|
|
|
}
|
|
|
|
got := diags.Error()
|
|
|
|
want := "Module does not support depends_on"
|
|
|
|
if !strings.Contains(got, want) {
|
|
|
|
t.Fatalf("wrong error\ngot:\n%s\n\nwant: containing %q", got, want)
|
|
|
|
}
|
|
|
|
}
|