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