kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/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  	"kubeform.dev/terraform-backend-sdk/configs"
    14  )
    15  
    16  func TestLoaderLoadConfig_okay(t *testing.T) {
    17  	fixtureDir := filepath.Clean("testdata/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  }
    61  
    62  func TestLoaderLoadConfig_addVersion(t *testing.T) {
    63  	// This test is for what happens when there is a version constraint added
    64  	// to a module that previously didn't have one.
    65  	fixtureDir := filepath.Clean("testdata/add-version-constraint")
    66  	loader, err := NewLoader(&Config{
    67  		ModulesDir: filepath.Join(fixtureDir, ".terraform/modules"),
    68  	})
    69  	if err != nil {
    70  		t.Fatalf("unexpected error from NewLoader: %s", err)
    71  	}
    72  
    73  	_, diags := loader.LoadConfig(fixtureDir)
    74  	if !diags.HasErrors() {
    75  		t.Fatalf("success; want error")
    76  	}
    77  	got := diags.Error()
    78  	want := "Module version requirements have changed"
    79  	if !strings.Contains(got, want) {
    80  		t.Fatalf("wrong error\ngot:\n%s\n\nwant: containing %q", got, want)
    81  	}
    82  }
    83  
    84  func TestLoaderLoadConfig_loadDiags(t *testing.T) {
    85  	// building a config which didn't load correctly may cause configs to panic
    86  	fixtureDir := filepath.Clean("testdata/invalid-names")
    87  	loader, err := NewLoader(&Config{
    88  		ModulesDir: filepath.Join(fixtureDir, ".terraform/modules"),
    89  	})
    90  	if err != nil {
    91  		t.Fatalf("unexpected error from NewLoader: %s", err)
    92  	}
    93  
    94  	_, diags := loader.LoadConfig(fixtureDir)
    95  	if !diags.HasErrors() {
    96  		t.Fatalf("success; want error")
    97  	}
    98  }