github.com/ewbankkit/terraform@v0.7.7/terraform/state_add_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestStateAdd(t *testing.T) {
     8  	cases := map[string]struct {
     9  		Err      bool
    10  		From, To string
    11  		Value    interface{}
    12  		One, Two *State
    13  	}{
    14  		"ModuleState => Module Addr (new)": {
    15  			false,
    16  			"",
    17  			"module.foo",
    18  			&ModuleState{
    19  				Path: rootModulePath,
    20  				Resources: map[string]*ResourceState{
    21  					"test_instance.foo": &ResourceState{
    22  						Type: "test_instance",
    23  						Primary: &InstanceState{
    24  							ID: "foo",
    25  						},
    26  					},
    27  
    28  					"test_instance.bar": &ResourceState{
    29  						Type: "test_instance",
    30  						Primary: &InstanceState{
    31  							ID: "foo",
    32  						},
    33  					},
    34  				},
    35  			},
    36  
    37  			&State{},
    38  			&State{
    39  				Modules: []*ModuleState{
    40  					&ModuleState{
    41  						Path: []string{"root", "foo"},
    42  						Resources: map[string]*ResourceState{
    43  							"test_instance.foo": &ResourceState{
    44  								Type: "test_instance",
    45  								Primary: &InstanceState{
    46  									ID: "foo",
    47  								},
    48  							},
    49  
    50  							"test_instance.bar": &ResourceState{
    51  								Type: "test_instance",
    52  								Primary: &InstanceState{
    53  									ID: "foo",
    54  								},
    55  							},
    56  						},
    57  					},
    58  				},
    59  			},
    60  		},
    61  
    62  		"ModuleState => Nested Module Addr (new)": {
    63  			false,
    64  			"",
    65  			"module.foo.module.bar",
    66  			&ModuleState{
    67  				Path: rootModulePath,
    68  				Resources: map[string]*ResourceState{
    69  					"test_instance.foo": &ResourceState{
    70  						Type: "test_instance",
    71  						Primary: &InstanceState{
    72  							ID: "foo",
    73  						},
    74  					},
    75  
    76  					"test_instance.bar": &ResourceState{
    77  						Type: "test_instance",
    78  						Primary: &InstanceState{
    79  							ID: "foo",
    80  						},
    81  					},
    82  				},
    83  			},
    84  
    85  			&State{},
    86  			&State{
    87  				Modules: []*ModuleState{
    88  					&ModuleState{
    89  						Path: []string{"root", "foo", "bar"},
    90  						Resources: map[string]*ResourceState{
    91  							"test_instance.foo": &ResourceState{
    92  								Type: "test_instance",
    93  								Primary: &InstanceState{
    94  									ID: "foo",
    95  								},
    96  							},
    97  
    98  							"test_instance.bar": &ResourceState{
    99  								Type: "test_instance",
   100  								Primary: &InstanceState{
   101  									ID: "foo",
   102  								},
   103  							},
   104  						},
   105  					},
   106  				},
   107  			},
   108  		},
   109  
   110  		"ModuleState w/ outputs and deps => Module Addr (new)": {
   111  			false,
   112  			"",
   113  			"module.foo",
   114  			&ModuleState{
   115  				Path: rootModulePath,
   116  				Outputs: map[string]*OutputState{
   117  					"foo": &OutputState{
   118  						Type:      "string",
   119  						Sensitive: false,
   120  						Value:     "bar",
   121  					},
   122  				},
   123  				Dependencies: []string{"foo"},
   124  				Resources: map[string]*ResourceState{
   125  					"test_instance.foo": &ResourceState{
   126  						Type: "test_instance",
   127  						Primary: &InstanceState{
   128  							ID: "foo",
   129  						},
   130  					},
   131  
   132  					"test_instance.bar": &ResourceState{
   133  						Type: "test_instance",
   134  						Primary: &InstanceState{
   135  							ID: "foo",
   136  						},
   137  					},
   138  				},
   139  			},
   140  
   141  			&State{},
   142  			&State{
   143  				Modules: []*ModuleState{
   144  					&ModuleState{
   145  						Path: []string{"root", "foo"},
   146  						Outputs: map[string]*OutputState{
   147  							"foo": &OutputState{
   148  								Type:      "string",
   149  								Sensitive: false,
   150  								Value:     "bar",
   151  							},
   152  						},
   153  						Dependencies: []string{"foo"},
   154  						Resources: map[string]*ResourceState{
   155  							"test_instance.foo": &ResourceState{
   156  								Type: "test_instance",
   157  								Primary: &InstanceState{
   158  									ID: "foo",
   159  								},
   160  							},
   161  
   162  							"test_instance.bar": &ResourceState{
   163  								Type: "test_instance",
   164  								Primary: &InstanceState{
   165  									ID: "foo",
   166  								},
   167  							},
   168  						},
   169  					},
   170  				},
   171  			},
   172  		},
   173  
   174  		"ModuleState => Module Addr (existing)": {
   175  			true,
   176  			"",
   177  			"module.foo",
   178  			&ModuleState{},
   179  			&State{
   180  				Modules: []*ModuleState{
   181  					&ModuleState{
   182  						Path: []string{"root", "foo"},
   183  						Resources: map[string]*ResourceState{
   184  							"test_instance.baz": &ResourceState{
   185  								Type: "test_instance",
   186  								Primary: &InstanceState{
   187  									ID: "foo",
   188  								},
   189  							},
   190  						},
   191  					},
   192  				},
   193  			},
   194  			nil,
   195  		},
   196  
   197  		"ModuleState with children => Module Addr (new)": {
   198  			false,
   199  			"module.foo",
   200  			"module.bar",
   201  
   202  			[]*ModuleState{
   203  				&ModuleState{
   204  					Path:      []string{"root", "foo"},
   205  					Resources: map[string]*ResourceState{},
   206  				},
   207  
   208  				&ModuleState{
   209  					Path: []string{"root", "foo", "child1"},
   210  					Resources: map[string]*ResourceState{
   211  						"test_instance.foo": &ResourceState{
   212  							Type: "test_instance",
   213  							Primary: &InstanceState{
   214  								ID: "foo",
   215  							},
   216  						},
   217  					},
   218  				},
   219  
   220  				&ModuleState{
   221  					Path: []string{"root", "foo", "child2"},
   222  					Resources: map[string]*ResourceState{
   223  						"test_instance.foo": &ResourceState{
   224  							Type: "test_instance",
   225  							Primary: &InstanceState{
   226  								ID: "foo",
   227  							},
   228  						},
   229  					},
   230  				},
   231  
   232  				// Should be ignored
   233  				&ModuleState{
   234  					Path: []string{"root", "baz", "child2"},
   235  					Resources: map[string]*ResourceState{
   236  						"test_instance.foo": &ResourceState{
   237  							Type: "test_instance",
   238  							Primary: &InstanceState{
   239  								ID: "foo",
   240  							},
   241  						},
   242  					},
   243  				},
   244  			},
   245  
   246  			&State{},
   247  			&State{
   248  				Modules: []*ModuleState{
   249  					&ModuleState{
   250  						Path:      []string{"root", "bar"},
   251  						Resources: map[string]*ResourceState{},
   252  					},
   253  
   254  					&ModuleState{
   255  						Path: []string{"root", "bar", "child1"},
   256  						Resources: map[string]*ResourceState{
   257  							"test_instance.foo": &ResourceState{
   258  								Type: "test_instance",
   259  								Primary: &InstanceState{
   260  									ID: "foo",
   261  								},
   262  							},
   263  						},
   264  					},
   265  
   266  					&ModuleState{
   267  						Path: []string{"root", "bar", "child2"},
   268  						Resources: map[string]*ResourceState{
   269  							"test_instance.foo": &ResourceState{
   270  								Type: "test_instance",
   271  								Primary: &InstanceState{
   272  									ID: "foo",
   273  								},
   274  							},
   275  						},
   276  					},
   277  				},
   278  			},
   279  		},
   280  
   281  		"ResourceState => Resource Addr (new)": {
   282  			false,
   283  			"aws_instance.bar",
   284  			"aws_instance.foo",
   285  			&ResourceState{
   286  				Type: "test_instance",
   287  				Primary: &InstanceState{
   288  					ID: "foo",
   289  				},
   290  			},
   291  
   292  			&State{},
   293  			&State{
   294  				Modules: []*ModuleState{
   295  					&ModuleState{
   296  						Path: []string{"root"},
   297  						Resources: map[string]*ResourceState{
   298  							"aws_instance.foo": &ResourceState{
   299  								Type: "test_instance",
   300  								Primary: &InstanceState{
   301  									ID: "foo",
   302  								},
   303  							},
   304  						},
   305  					},
   306  				},
   307  			},
   308  		},
   309  
   310  		"ResourceState w/ deps, provider => Resource Addr (new)": {
   311  			false,
   312  			"aws_instance.bar",
   313  			"aws_instance.foo",
   314  			&ResourceState{
   315  				Type:         "test_instance",
   316  				Provider:     "foo",
   317  				Dependencies: []string{"bar"},
   318  				Primary: &InstanceState{
   319  					ID: "foo",
   320  				},
   321  			},
   322  
   323  			&State{},
   324  			&State{
   325  				Modules: []*ModuleState{
   326  					&ModuleState{
   327  						Path: []string{"root"},
   328  						Resources: map[string]*ResourceState{
   329  							"aws_instance.foo": &ResourceState{
   330  								Type:         "test_instance",
   331  								Provider:     "foo",
   332  								Dependencies: []string{"bar"},
   333  								Primary: &InstanceState{
   334  									ID: "foo",
   335  								},
   336  							},
   337  						},
   338  					},
   339  				},
   340  			},
   341  		},
   342  
   343  		"ResourceState tainted => Resource Addr (new)": {
   344  			false,
   345  			"aws_instance.bar",
   346  			"aws_instance.foo",
   347  			&ResourceState{
   348  				Type: "test_instance",
   349  				Primary: &InstanceState{
   350  					ID:      "foo",
   351  					Tainted: true,
   352  				},
   353  			},
   354  
   355  			&State{},
   356  			&State{
   357  				Modules: []*ModuleState{
   358  					&ModuleState{
   359  						Path: []string{"root"},
   360  						Resources: map[string]*ResourceState{
   361  							"aws_instance.foo": &ResourceState{
   362  								Type: "test_instance",
   363  								Primary: &InstanceState{
   364  									ID:      "foo",
   365  									Tainted: true,
   366  								},
   367  							},
   368  						},
   369  					},
   370  				},
   371  			},
   372  		},
   373  
   374  		"ResourceState with count unspecified => Resource Addr (new)": {
   375  			false,
   376  			"aws_instance.bar",
   377  			"aws_instance.foo",
   378  			[]*ResourceState{
   379  				&ResourceState{
   380  					Type: "test_instance",
   381  					Primary: &InstanceState{
   382  						ID: "foo",
   383  					},
   384  				},
   385  
   386  				&ResourceState{
   387  					Type: "test_instance",
   388  					Primary: &InstanceState{
   389  						ID: "bar",
   390  					},
   391  				},
   392  			},
   393  
   394  			&State{},
   395  			&State{
   396  				Modules: []*ModuleState{
   397  					&ModuleState{
   398  						Path: []string{"root"},
   399  						Resources: map[string]*ResourceState{
   400  							"aws_instance.foo.0": &ResourceState{
   401  								Type: "test_instance",
   402  								Primary: &InstanceState{
   403  									ID: "foo",
   404  								},
   405  							},
   406  
   407  							"aws_instance.foo.1": &ResourceState{
   408  								Type: "test_instance",
   409  								Primary: &InstanceState{
   410  									ID: "bar",
   411  								},
   412  							},
   413  						},
   414  					},
   415  				},
   416  			},
   417  		},
   418  
   419  		"ResourceState with count unspecified => Resource Addr (new with count)": {
   420  			true,
   421  			"aws_instance.bar",
   422  			"aws_instance.foo[0]",
   423  			[]*ResourceState{
   424  				&ResourceState{
   425  					Type: "test_instance",
   426  					Primary: &InstanceState{
   427  						ID: "foo",
   428  					},
   429  				},
   430  
   431  				&ResourceState{
   432  					Type: "test_instance",
   433  					Primary: &InstanceState{
   434  						ID: "bar",
   435  					},
   436  				},
   437  			},
   438  
   439  			&State{},
   440  			nil,
   441  		},
   442  
   443  		"ResourceState with single count unspecified => Resource Addr (new with count)": {
   444  			false,
   445  			"aws_instance.bar",
   446  			"aws_instance.foo[0]",
   447  			[]*ResourceState{
   448  				&ResourceState{
   449  					Type: "test_instance",
   450  					Primary: &InstanceState{
   451  						ID: "foo",
   452  					},
   453  				},
   454  			},
   455  
   456  			&State{},
   457  			&State{
   458  				Modules: []*ModuleState{
   459  					&ModuleState{
   460  						Path: []string{"root"},
   461  						Resources: map[string]*ResourceState{
   462  							"aws_instance.foo.0": &ResourceState{
   463  								Type: "test_instance",
   464  								Primary: &InstanceState{
   465  									ID: "foo",
   466  								},
   467  							},
   468  						},
   469  					},
   470  				},
   471  			},
   472  		},
   473  
   474  		"ResourceState => Resource Addr (new with count)": {
   475  			false,
   476  			"aws_instance.bar",
   477  			"aws_instance.foo[0]",
   478  			&ResourceState{
   479  				Type: "test_instance",
   480  				Primary: &InstanceState{
   481  					ID: "foo",
   482  				},
   483  			},
   484  
   485  			&State{},
   486  			&State{
   487  				Modules: []*ModuleState{
   488  					&ModuleState{
   489  						Path: []string{"root"},
   490  						Resources: map[string]*ResourceState{
   491  							"aws_instance.foo.0": &ResourceState{
   492  								Type: "test_instance",
   493  								Primary: &InstanceState{
   494  									ID: "foo",
   495  								},
   496  							},
   497  						},
   498  					},
   499  				},
   500  			},
   501  		},
   502  
   503  		"ResourceState => Resource Addr (existing)": {
   504  			true,
   505  			"aws_instance.bar",
   506  			"aws_instance.foo",
   507  			&ResourceState{
   508  				Type: "test_instance",
   509  				Primary: &InstanceState{
   510  					ID: "foo",
   511  				},
   512  			},
   513  
   514  			&State{
   515  				Modules: []*ModuleState{
   516  					&ModuleState{
   517  						Path: []string{"root"},
   518  						Resources: map[string]*ResourceState{
   519  							"aws_instance.foo": &ResourceState{
   520  								Type: "test_instance",
   521  								Primary: &InstanceState{
   522  									ID: "foo",
   523  								},
   524  							},
   525  						},
   526  					},
   527  				},
   528  			},
   529  			nil,
   530  		},
   531  
   532  		"ResourceState => Module (new)": {
   533  			false,
   534  			"aws_instance.bar",
   535  			"module.foo",
   536  			&ResourceState{
   537  				Type: "test_instance",
   538  				Primary: &InstanceState{
   539  					ID: "foo",
   540  				},
   541  			},
   542  
   543  			&State{},
   544  			&State{
   545  				Modules: []*ModuleState{
   546  					&ModuleState{
   547  						Path: []string{"root", "foo"},
   548  						Resources: map[string]*ResourceState{
   549  							"aws_instance.bar": &ResourceState{
   550  								Type: "test_instance",
   551  								Primary: &InstanceState{
   552  									ID: "foo",
   553  								},
   554  							},
   555  						},
   556  					},
   557  				},
   558  			},
   559  		},
   560  
   561  		"InstanceState => Resource (new)": {
   562  			false,
   563  			"aws_instance.bar.primary",
   564  			"aws_instance.baz",
   565  			&InstanceState{
   566  				ID: "foo",
   567  			},
   568  
   569  			&State{},
   570  			&State{
   571  				Modules: []*ModuleState{
   572  					&ModuleState{
   573  						Path: []string{"root"},
   574  						Resources: map[string]*ResourceState{
   575  							"aws_instance.baz": &ResourceState{
   576  								Type: "aws_instance",
   577  								Primary: &InstanceState{
   578  									ID: "foo",
   579  								},
   580  							},
   581  						},
   582  					},
   583  				},
   584  			},
   585  		},
   586  
   587  		"InstanceState => Module (new)": {
   588  			false,
   589  			"aws_instance.bar.primary",
   590  			"module.foo",
   591  			&InstanceState{
   592  				ID: "foo",
   593  			},
   594  
   595  			&State{},
   596  			&State{
   597  				Modules: []*ModuleState{
   598  					&ModuleState{
   599  						Path: []string{"root", "foo"},
   600  						Resources: map[string]*ResourceState{
   601  							"aws_instance.bar": &ResourceState{
   602  								Type: "aws_instance",
   603  								Primary: &InstanceState{
   604  									ID: "foo",
   605  								},
   606  							},
   607  						},
   608  					},
   609  				},
   610  			},
   611  		},
   612  	}
   613  
   614  	for k, tc := range cases {
   615  		// Make sure they're both initialized as normal
   616  		tc.One.init()
   617  		if tc.Two != nil {
   618  			tc.Two.init()
   619  		}
   620  
   621  		// Add the value
   622  		err := tc.One.Add(tc.From, tc.To, tc.Value)
   623  		if (err != nil) != tc.Err {
   624  			t.Fatalf("bad: %s\n\n%s", k, err)
   625  		}
   626  		if tc.Err {
   627  			continue
   628  		}
   629  
   630  		// Prune them both to be sure
   631  		tc.One.prune()
   632  		tc.Two.prune()
   633  
   634  		// Verify equality
   635  		if !tc.One.Equal(tc.Two) {
   636  			//t.Fatalf("Bad: %s\n\n%#v\n\n%#v", k, tc.One, tc.Two)
   637  			t.Fatalf("Bad: %s\n\n%s\n\n%s", k, tc.One.String(), tc.Two.String())
   638  		}
   639  	}
   640  }