github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/configs/configload/loader_load_test.go (about)

     1  package configload
     2  
     3  import (
     4  	"path/filepath"
     5  	"reflect"
     6  	"sort"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/davecgh/go-spew/spew"
    11  	"github.com/zclconf/go-cty/cty"
    12  
    13  	"github.com/hashicorp/terraform/configs"
    14  )
    15  
    16  func TestLoaderLoadConfig_okay(t *testing.T) {
    17  	fixtureDir := filepath.Clean("test-fixtures/already-installed")
    18  	loader, err := NewLoader(&Config{
    19  		ModulesDir: filepath.Join(fixtureDir, ".terraform/modules"),
    20  	})
    21  	if err != nil {
    22  		t.Fatalf("unexpected error from NewLoader: %s", err)
    23  	}
    24  
    25  	cfg, diags := loader.LoadConfig(fixtureDir)
    26  	assertNoDiagnostics(t, diags)
    27  	if cfg == nil {
    28  		t.Fatalf("config is nil; want non-nil")
    29  	}
    30  
    31  	var gotPaths []string
    32  	cfg.DeepEach(func(c *configs.Config) {
    33  		gotPaths = append(gotPaths, strings.Join(c.Path, "."))
    34  	})
    35  	sort.Strings(gotPaths)
    36  	wantPaths := []string{
    37  		"", // root module
    38  		"child_a",
    39  		"child_a.child_c",
    40  		"child_b",
    41  		"child_b.child_d",
    42  	}
    43  
    44  	if !reflect.DeepEqual(gotPaths, wantPaths) {
    45  		t.Fatalf("wrong module paths\ngot: %swant %s", spew.Sdump(gotPaths), spew.Sdump(wantPaths))
    46  	}
    47  
    48  	t.Run("child_a.child_c output", func(t *testing.T) {
    49  		output := cfg.Children["child_a"].Children["child_c"].Module.Outputs["hello"]
    50  		got, diags := output.Expr.Value(nil)
    51  		assertNoDiagnostics(t, diags)
    52  		assertResultCtyEqual(t, got, cty.StringVal("Hello from child_c"))
    53  	})
    54  	t.Run("child_b.child_d output", func(t *testing.T) {
    55  		output := cfg.Children["child_b"].Children["child_d"].Module.Outputs["hello"]
    56  		got, diags := output.Expr.Value(nil)
    57  		assertNoDiagnostics(t, diags)
    58  		assertResultCtyEqual(t, got, cty.StringVal("Hello from child_d"))
    59  	})
    60  }