github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/terraform/state_add_test.go (about)

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