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