github.com/sathiyas/terraform@v0.6.9-0.20151210233947-3330da00b997/config/merge_test.go (about)

     1  package config
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestMerge(t *testing.T) {
     9  	cases := []struct {
    10  		c1, c2, result *Config
    11  		err            bool
    12  	}{
    13  		// Normal good case.
    14  		{
    15  			&Config{
    16  				Atlas: &AtlasConfig{
    17  					Name: "foo",
    18  				},
    19  				Modules: []*Module{
    20  					&Module{Name: "foo"},
    21  				},
    22  				Outputs: []*Output{
    23  					&Output{Name: "foo"},
    24  				},
    25  				ProviderConfigs: []*ProviderConfig{
    26  					&ProviderConfig{Name: "foo"},
    27  				},
    28  				Resources: []*Resource{
    29  					&Resource{Name: "foo"},
    30  				},
    31  				Variables: []*Variable{
    32  					&Variable{Name: "foo"},
    33  				},
    34  
    35  				unknownKeys: []string{"foo"},
    36  			},
    37  
    38  			&Config{
    39  				Atlas: &AtlasConfig{
    40  					Name: "bar",
    41  				},
    42  				Modules: []*Module{
    43  					&Module{Name: "bar"},
    44  				},
    45  				Outputs: []*Output{
    46  					&Output{Name: "bar"},
    47  				},
    48  				ProviderConfigs: []*ProviderConfig{
    49  					&ProviderConfig{Name: "bar"},
    50  				},
    51  				Resources: []*Resource{
    52  					&Resource{Name: "bar"},
    53  				},
    54  				Variables: []*Variable{
    55  					&Variable{Name: "bar"},
    56  				},
    57  
    58  				unknownKeys: []string{"bar"},
    59  			},
    60  
    61  			&Config{
    62  				Atlas: &AtlasConfig{
    63  					Name: "bar",
    64  				},
    65  				Modules: []*Module{
    66  					&Module{Name: "foo"},
    67  					&Module{Name: "bar"},
    68  				},
    69  				Outputs: []*Output{
    70  					&Output{Name: "foo"},
    71  					&Output{Name: "bar"},
    72  				},
    73  				ProviderConfigs: []*ProviderConfig{
    74  					&ProviderConfig{Name: "foo"},
    75  					&ProviderConfig{Name: "bar"},
    76  				},
    77  				Resources: []*Resource{
    78  					&Resource{Name: "foo"},
    79  					&Resource{Name: "bar"},
    80  				},
    81  				Variables: []*Variable{
    82  					&Variable{Name: "foo"},
    83  					&Variable{Name: "bar"},
    84  				},
    85  
    86  				unknownKeys: []string{"foo", "bar"},
    87  			},
    88  
    89  			false,
    90  		},
    91  
    92  		// Test that when merging duplicates, it merges into the
    93  		// first, but keeps the duplicates so that errors still
    94  		// happen.
    95  		{
    96  			&Config{
    97  				Outputs: []*Output{
    98  					&Output{Name: "foo"},
    99  				},
   100  				ProviderConfigs: []*ProviderConfig{
   101  					&ProviderConfig{Name: "foo"},
   102  				},
   103  				Resources: []*Resource{
   104  					&Resource{Name: "foo"},
   105  				},
   106  				Variables: []*Variable{
   107  					&Variable{Name: "foo", Default: "foo"},
   108  					&Variable{Name: "foo"},
   109  				},
   110  
   111  				unknownKeys: []string{"foo"},
   112  			},
   113  
   114  			&Config{
   115  				Outputs: []*Output{
   116  					&Output{Name: "bar"},
   117  				},
   118  				ProviderConfigs: []*ProviderConfig{
   119  					&ProviderConfig{Name: "bar"},
   120  				},
   121  				Resources: []*Resource{
   122  					&Resource{Name: "bar"},
   123  				},
   124  				Variables: []*Variable{
   125  					&Variable{Name: "foo", Default: "bar"},
   126  					&Variable{Name: "bar"},
   127  				},
   128  
   129  				unknownKeys: []string{"bar"},
   130  			},
   131  
   132  			&Config{
   133  				Outputs: []*Output{
   134  					&Output{Name: "foo"},
   135  					&Output{Name: "bar"},
   136  				},
   137  				ProviderConfigs: []*ProviderConfig{
   138  					&ProviderConfig{Name: "foo"},
   139  					&ProviderConfig{Name: "bar"},
   140  				},
   141  				Resources: []*Resource{
   142  					&Resource{Name: "foo"},
   143  					&Resource{Name: "bar"},
   144  				},
   145  				Variables: []*Variable{
   146  					&Variable{Name: "foo", Default: "bar"},
   147  					&Variable{Name: "foo"},
   148  					&Variable{Name: "bar"},
   149  				},
   150  
   151  				unknownKeys: []string{"foo", "bar"},
   152  			},
   153  
   154  			false,
   155  		},
   156  	}
   157  
   158  	for i, tc := range cases {
   159  		actual, err := Merge(tc.c1, tc.c2)
   160  		if err != nil != tc.err {
   161  			t.Fatalf("%d: error fail", i)
   162  		}
   163  
   164  		if !reflect.DeepEqual(actual, tc.result) {
   165  			t.Fatalf("%d: bad:\n\n%#v", i, actual)
   166  		}
   167  	}
   168  }