github.com/salme4/terraform@v0.11.12-beta1/configs/config_build_test.go (about)

     1  package configs
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"reflect"
     7  	"sort"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/davecgh/go-spew/spew"
    12  
    13  	version "github.com/hashicorp/go-version"
    14  	"github.com/hashicorp/hcl2/hcl"
    15  )
    16  
    17  func TestBuildConfig(t *testing.T) {
    18  	parser := NewParser(nil)
    19  	mod, diags := parser.LoadConfigDir("test-fixtures/config-build")
    20  	assertNoDiagnostics(t, diags)
    21  	if mod == nil {
    22  		t.Fatal("got nil root module; want non-nil")
    23  	}
    24  
    25  	versionI := 0
    26  	cfg, diags := BuildConfig(mod, ModuleWalkerFunc(
    27  		func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) {
    28  			// For the sake of this test we're going to just treat our
    29  			// SourceAddr as a path relative to our fixture directory.
    30  			// A "real" implementation of ModuleWalker should accept the
    31  			// various different source address syntaxes Terraform supports.
    32  			sourcePath := filepath.Join("test-fixtures/config-build", req.SourceAddr)
    33  
    34  			mod, diags := parser.LoadConfigDir(sourcePath)
    35  			version, _ := version.NewVersion(fmt.Sprintf("1.0.%d", versionI))
    36  			versionI++
    37  			return mod, version, diags
    38  		},
    39  	))
    40  	assertNoDiagnostics(t, diags)
    41  	if cfg == nil {
    42  		t.Fatal("got nil config; want non-nil")
    43  	}
    44  
    45  	var got []string
    46  	cfg.DeepEach(func(c *Config) {
    47  		got = append(got, fmt.Sprintf("%s %s", strings.Join(c.Path, "."), c.Version))
    48  	})
    49  	sort.Strings(got)
    50  	want := []string{
    51  		" <nil>",
    52  		"child_a 1.0.0",
    53  		"child_a.child_c 1.0.1",
    54  		"child_b 1.0.2",
    55  		"child_b.child_c 1.0.3",
    56  	}
    57  
    58  	if !reflect.DeepEqual(got, want) {
    59  		t.Fatalf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want))
    60  	}
    61  
    62  	if _, exists := cfg.Children["child_a"].Children["child_c"].Module.Outputs["hello"]; !exists {
    63  		t.Fatalf("missing output 'hello' in child_a.child_c")
    64  	}
    65  	if _, exists := cfg.Children["child_b"].Children["child_c"].Module.Outputs["hello"]; !exists {
    66  		t.Fatalf("missing output 'hello' in child_b.child_c")
    67  	}
    68  	if cfg.Children["child_a"].Children["child_c"].Module == cfg.Children["child_b"].Children["child_c"].Module {
    69  		t.Fatalf("child_a.child_c is same object as child_b.child_c; should not be")
    70  	}
    71  }