github.com/magodo/terraform@v0.11.12-beta1/configs/module_merge_test.go (about) 1 package configs 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/hcl2/gohcl" 7 "github.com/hashicorp/hcl2/hcl" 8 "github.com/zclconf/go-cty/cty" 9 ) 10 11 func TestModuleOverrideVariable(t *testing.T) { 12 mod, diags := testModuleFromDir("test-fixtures/valid-modules/override-variable") 13 assertNoDiagnostics(t, diags) 14 if mod == nil { 15 t.Fatalf("module is nil") 16 } 17 18 got := mod.Variables 19 want := map[string]*Variable{ 20 "fully_overridden": { 21 Name: "fully_overridden", 22 Description: "b_override description", 23 DescriptionSet: true, 24 Default: cty.StringVal("b_override"), 25 Type: cty.String, 26 ParsingMode: VariableParseLiteral, 27 DeclRange: hcl.Range{ 28 Filename: "test-fixtures/valid-modules/override-variable/primary.tf", 29 Start: hcl.Pos{ 30 Line: 1, 31 Column: 1, 32 Byte: 0, 33 }, 34 End: hcl.Pos{ 35 Line: 1, 36 Column: 28, 37 Byte: 27, 38 }, 39 }, 40 }, 41 "partially_overridden": { 42 Name: "partially_overridden", 43 Description: "base description", 44 DescriptionSet: true, 45 Default: cty.StringVal("b_override partial"), 46 Type: cty.String, 47 ParsingMode: VariableParseLiteral, 48 DeclRange: hcl.Range{ 49 Filename: "test-fixtures/valid-modules/override-variable/primary.tf", 50 Start: hcl.Pos{ 51 Line: 7, 52 Column: 1, 53 Byte: 103, 54 }, 55 End: hcl.Pos{ 56 Line: 7, 57 Column: 32, 58 Byte: 134, 59 }, 60 }, 61 }, 62 } 63 assertResultDeepEqual(t, got, want) 64 } 65 66 func TestModuleOverrideModule(t *testing.T) { 67 mod, diags := testModuleFromDir("test-fixtures/valid-modules/override-module") 68 assertNoDiagnostics(t, diags) 69 if mod == nil { 70 t.Fatalf("module is nil") 71 } 72 73 if _, exists := mod.ModuleCalls["example"]; !exists { 74 t.Fatalf("no module 'example'") 75 } 76 if len(mod.ModuleCalls) != 1 { 77 t.Fatalf("wrong number of module calls in result %d; want 1", len(mod.ModuleCalls)) 78 } 79 80 got := mod.ModuleCalls["example"] 81 want := &ModuleCall{ 82 Name: "example", 83 SourceAddr: "./example2-a_override", 84 SourceAddrRange: hcl.Range{ 85 Filename: "test-fixtures/valid-modules/override-module/a_override.tf", 86 Start: hcl.Pos{ 87 Line: 3, 88 Column: 12, 89 Byte: 31, 90 }, 91 End: hcl.Pos{ 92 Line: 3, 93 Column: 35, 94 Byte: 54, 95 }, 96 }, 97 SourceSet: true, 98 DeclRange: hcl.Range{ 99 Filename: "test-fixtures/valid-modules/override-module/primary.tf", 100 Start: hcl.Pos{ 101 Line: 2, 102 Column: 1, 103 Byte: 1, 104 }, 105 End: hcl.Pos{ 106 Line: 2, 107 Column: 17, 108 Byte: 17, 109 }, 110 }, 111 } 112 113 // We're going to extract and nil out our hcl.Body here because DeepEqual 114 // is not a useful way to assert on that. 115 gotConfig := got.Config 116 got.Config = nil 117 118 assertResultDeepEqual(t, got, want) 119 120 type content struct { 121 Kept *string `hcl:"kept"` 122 Foo *string `hcl:"foo"` 123 New *string `hcl:"new"` 124 Newer *string `hcl:"newer"` 125 } 126 var gotArgs content 127 diags = gohcl.DecodeBody(gotConfig, nil, &gotArgs) 128 assertNoDiagnostics(t, diags) 129 130 wantArgs := content{ 131 Kept: stringPtr("primary kept"), 132 Foo: stringPtr("a_override foo"), 133 New: stringPtr("b_override new"), 134 Newer: stringPtr("b_override newer"), 135 } 136 137 assertResultDeepEqual(t, gotArgs, wantArgs) 138 }