github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/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  		// Terraform block
   158  		{
   159  			&Config{
   160  				Terraform: &Terraform{
   161  					RequiredVersion: "A",
   162  				},
   163  			},
   164  			&Config{},
   165  			&Config{
   166  				Terraform: &Terraform{
   167  					RequiredVersion: "A",
   168  				},
   169  			},
   170  			false,
   171  		},
   172  
   173  		{
   174  			&Config{},
   175  			&Config{
   176  				Terraform: &Terraform{
   177  					RequiredVersion: "A",
   178  				},
   179  			},
   180  			&Config{
   181  				Terraform: &Terraform{
   182  					RequiredVersion: "A",
   183  				},
   184  			},
   185  			false,
   186  		},
   187  
   188  		// Provider alias
   189  		{
   190  			&Config{
   191  				ProviderConfigs: []*ProviderConfig{
   192  					&ProviderConfig{Alias: "foo"},
   193  				},
   194  			},
   195  			&Config{},
   196  			&Config{
   197  				ProviderConfigs: []*ProviderConfig{
   198  					&ProviderConfig{Alias: "foo"},
   199  				},
   200  			},
   201  			false,
   202  		},
   203  
   204  		{
   205  			&Config{},
   206  			&Config{
   207  				ProviderConfigs: []*ProviderConfig{
   208  					&ProviderConfig{Alias: "foo"},
   209  				},
   210  			},
   211  			&Config{
   212  				ProviderConfigs: []*ProviderConfig{
   213  					&ProviderConfig{Alias: "foo"},
   214  				},
   215  			},
   216  			false,
   217  		},
   218  
   219  		{
   220  			&Config{
   221  				ProviderConfigs: []*ProviderConfig{
   222  					&ProviderConfig{Alias: "bar"},
   223  				},
   224  			},
   225  			&Config{
   226  				ProviderConfigs: []*ProviderConfig{
   227  					&ProviderConfig{Alias: "foo"},
   228  				},
   229  			},
   230  			&Config{
   231  				ProviderConfigs: []*ProviderConfig{
   232  					&ProviderConfig{Alias: "foo"},
   233  				},
   234  			},
   235  			false,
   236  		},
   237  
   238  		// Variable type
   239  		{
   240  			&Config{
   241  				Variables: []*Variable{
   242  					&Variable{DeclaredType: "foo"},
   243  				},
   244  			},
   245  			&Config{},
   246  			&Config{
   247  				Variables: []*Variable{
   248  					&Variable{DeclaredType: "foo"},
   249  				},
   250  			},
   251  			false,
   252  		},
   253  
   254  		{
   255  			&Config{},
   256  			&Config{
   257  				Variables: []*Variable{
   258  					&Variable{DeclaredType: "foo"},
   259  				},
   260  			},
   261  			&Config{
   262  				Variables: []*Variable{
   263  					&Variable{DeclaredType: "foo"},
   264  				},
   265  			},
   266  			false,
   267  		},
   268  
   269  		{
   270  			&Config{
   271  				Variables: []*Variable{
   272  					&Variable{DeclaredType: "bar"},
   273  				},
   274  			},
   275  			&Config{
   276  				Variables: []*Variable{
   277  					&Variable{DeclaredType: "foo"},
   278  				},
   279  			},
   280  			&Config{
   281  				Variables: []*Variable{
   282  					&Variable{DeclaredType: "foo"},
   283  				},
   284  			},
   285  			false,
   286  		},
   287  
   288  		// Output description
   289  		{
   290  			&Config{
   291  				Outputs: []*Output{
   292  					&Output{Description: "foo"},
   293  				},
   294  			},
   295  			&Config{},
   296  			&Config{
   297  				Outputs: []*Output{
   298  					&Output{Description: "foo"},
   299  				},
   300  			},
   301  			false,
   302  		},
   303  
   304  		{
   305  			&Config{},
   306  			&Config{
   307  				Outputs: []*Output{
   308  					&Output{Description: "foo"},
   309  				},
   310  			},
   311  			&Config{
   312  				Outputs: []*Output{
   313  					&Output{Description: "foo"},
   314  				},
   315  			},
   316  			false,
   317  		},
   318  
   319  		{
   320  			&Config{
   321  				Outputs: []*Output{
   322  					&Output{Description: "bar"},
   323  				},
   324  			},
   325  			&Config{
   326  				Outputs: []*Output{
   327  					&Output{Description: "foo"},
   328  				},
   329  			},
   330  			&Config{
   331  				Outputs: []*Output{
   332  					&Output{Description: "foo"},
   333  				},
   334  			},
   335  			false,
   336  		},
   337  
   338  		// Output depends_on
   339  		{
   340  			&Config{
   341  				Outputs: []*Output{
   342  					&Output{DependsOn: []string{"foo"}},
   343  				},
   344  			},
   345  			&Config{},
   346  			&Config{
   347  				Outputs: []*Output{
   348  					&Output{DependsOn: []string{"foo"}},
   349  				},
   350  			},
   351  			false,
   352  		},
   353  
   354  		{
   355  			&Config{},
   356  			&Config{
   357  				Outputs: []*Output{
   358  					&Output{DependsOn: []string{"foo"}},
   359  				},
   360  			},
   361  			&Config{
   362  				Outputs: []*Output{
   363  					&Output{DependsOn: []string{"foo"}},
   364  				},
   365  			},
   366  			false,
   367  		},
   368  
   369  		{
   370  			&Config{
   371  				Outputs: []*Output{
   372  					&Output{DependsOn: []string{"bar"}},
   373  				},
   374  			},
   375  			&Config{
   376  				Outputs: []*Output{
   377  					&Output{DependsOn: []string{"foo"}},
   378  				},
   379  			},
   380  			&Config{
   381  				Outputs: []*Output{
   382  					&Output{DependsOn: []string{"foo"}},
   383  				},
   384  			},
   385  			false,
   386  		},
   387  
   388  		// Output sensitive
   389  		{
   390  			&Config{
   391  				Outputs: []*Output{
   392  					&Output{Sensitive: true},
   393  				},
   394  			},
   395  			&Config{},
   396  			&Config{
   397  				Outputs: []*Output{
   398  					&Output{Sensitive: true},
   399  				},
   400  			},
   401  			false,
   402  		},
   403  
   404  		{
   405  			&Config{},
   406  			&Config{
   407  				Outputs: []*Output{
   408  					&Output{Sensitive: true},
   409  				},
   410  			},
   411  			&Config{
   412  				Outputs: []*Output{
   413  					&Output{Sensitive: true},
   414  				},
   415  			},
   416  			false,
   417  		},
   418  
   419  		{
   420  			&Config{
   421  				Outputs: []*Output{
   422  					&Output{Sensitive: false},
   423  				},
   424  			},
   425  			&Config{
   426  				Outputs: []*Output{
   427  					&Output{Sensitive: true},
   428  				},
   429  			},
   430  			&Config{
   431  				Outputs: []*Output{
   432  					&Output{Sensitive: true},
   433  				},
   434  			},
   435  			false,
   436  		},
   437  
   438  		// terraform blocks are merged, not overwritten
   439  		{
   440  			&Config{
   441  				Terraform: &Terraform{
   442  					RequiredVersion: "A",
   443  				},
   444  			},
   445  			&Config{
   446  				Terraform: &Terraform{
   447  					Backend: &Backend{
   448  						Type: "test",
   449  					},
   450  				},
   451  			},
   452  			&Config{
   453  				Terraform: &Terraform{
   454  					RequiredVersion: "A",
   455  					Backend: &Backend{
   456  						Type: "test",
   457  					},
   458  				},
   459  			},
   460  			false,
   461  		},
   462  	}
   463  
   464  	for i, tc := range cases {
   465  		actual, err := Merge(tc.c1, tc.c2)
   466  		if err != nil != tc.err {
   467  			t.Fatalf("%d: error fail", i)
   468  		}
   469  
   470  		if !reflect.DeepEqual(actual, tc.result) {
   471  			t.Fatalf("%d: bad:\n\n%#v", i, actual)
   472  		}
   473  	}
   474  }