kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/configs/configload/loader_snapshot_test.go (about)

     1  package configload
     2  
     3  import (
     4  	"path/filepath"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/davecgh/go-spew/spew"
     9  	"github.com/go-test/deep"
    10  )
    11  
    12  func TestLoadConfigWithSnapshot(t *testing.T) {
    13  	fixtureDir := filepath.Clean("testdata/already-installed")
    14  	loader, err := NewLoader(&Config{
    15  		ModulesDir: filepath.Join(fixtureDir, ".terraform/modules"),
    16  	})
    17  	if err != nil {
    18  		t.Fatalf("unexpected error from NewLoader: %s", err)
    19  	}
    20  
    21  	_, got, diags := loader.LoadConfigWithSnapshot(fixtureDir)
    22  	assertNoDiagnostics(t, diags)
    23  	if got == nil {
    24  		t.Fatalf("snapshot is nil; want non-nil")
    25  	}
    26  
    27  	t.Log(spew.Sdump(got))
    28  
    29  	{
    30  		gotModuleDirs := map[string]string{}
    31  		for k, m := range got.Modules {
    32  			gotModuleDirs[k] = m.Dir
    33  		}
    34  		wantModuleDirs := map[string]string{
    35  			"":                "testdata/already-installed",
    36  			"child_a":         "testdata/already-installed/.terraform/modules/child_a",
    37  			"child_a.child_c": "testdata/already-installed/.terraform/modules/child_a/child_c",
    38  			"child_b":         "testdata/already-installed/.terraform/modules/child_b",
    39  			"child_b.child_d": "testdata/already-installed/.terraform/modules/child_b.child_d",
    40  		}
    41  
    42  		problems := deep.Equal(wantModuleDirs, gotModuleDirs)
    43  		for _, problem := range problems {
    44  			t.Errorf(problem)
    45  		}
    46  		if len(problems) > 0 {
    47  			return
    48  		}
    49  	}
    50  
    51  	gotRoot := got.Modules[""]
    52  	wantRoot := &SnapshotModule{
    53  		Dir: "testdata/already-installed",
    54  		Files: map[string][]byte{
    55  			"root.tf": []byte(`
    56  module "child_a" {
    57    source  = "example.com/foo/bar_a/baz"
    58    version = ">= 1.0.0"
    59  }
    60  
    61  module "child_b" {
    62    source = "example.com/foo/bar_b/baz"
    63    version = ">= 1.0.0"
    64  }
    65  `),
    66  		},
    67  	}
    68  	if !reflect.DeepEqual(gotRoot, wantRoot) {
    69  		t.Errorf("wrong root module snapshot\ngot: %swant: %s", spew.Sdump(gotRoot), spew.Sdump(wantRoot))
    70  	}
    71  
    72  }
    73  
    74  func TestSnapshotRoundtrip(t *testing.T) {
    75  	fixtureDir := filepath.Clean("testdata/already-installed")
    76  	loader, err := NewLoader(&Config{
    77  		ModulesDir: filepath.Join(fixtureDir, ".terraform/modules"),
    78  	})
    79  	if err != nil {
    80  		t.Fatalf("unexpected error from NewLoader: %s", err)
    81  	}
    82  
    83  	_, snap, diags := loader.LoadConfigWithSnapshot(fixtureDir)
    84  	assertNoDiagnostics(t, diags)
    85  	if snap == nil {
    86  		t.Fatalf("snapshot is nil; want non-nil")
    87  	}
    88  
    89  	snapLoader := NewLoaderFromSnapshot(snap)
    90  	if loader == nil {
    91  		t.Fatalf("loader is nil; want non-nil")
    92  	}
    93  
    94  	config, diags := snapLoader.LoadConfig(fixtureDir)
    95  	assertNoDiagnostics(t, diags)
    96  	if config == nil {
    97  		t.Fatalf("config is nil; want non-nil")
    98  	}
    99  	if config.Module == nil {
   100  		t.Fatalf("config has no root module")
   101  	}
   102  	if got, want := config.Module.SourceDir, "testdata/already-installed"; got != want {
   103  		t.Errorf("wrong root module sourcedir %q; want %q", got, want)
   104  	}
   105  	if got, want := len(config.Module.ModuleCalls), 2; got != want {
   106  		t.Errorf("wrong number of module calls in root module %d; want %d", got, want)
   107  	}
   108  	childA := config.Children["child_a"]
   109  	if childA == nil {
   110  		t.Fatalf("child_a config is nil; want non-nil")
   111  	}
   112  	if childA.Module == nil {
   113  		t.Fatalf("child_a config has no module")
   114  	}
   115  	if got, want := childA.Module.SourceDir, "testdata/already-installed/.terraform/modules/child_a"; got != want {
   116  		t.Errorf("wrong child_a sourcedir %q; want %q", got, want)
   117  	}
   118  	if got, want := len(childA.Module.ModuleCalls), 1; got != want {
   119  		t.Errorf("wrong number of module calls in child_a %d; want %d", got, want)
   120  	}
   121  }