github.com/adrian-bl/terraform@v0.7.0-rc2.0.20160705220747-de0a34fc3517/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  		"ResourceState => Resource Addr (new)": {
   198  			false,
   199  			"aws_instance.bar",
   200  			"aws_instance.foo",
   201  			&ResourceState{
   202  				Type: "test_instance",
   203  				Primary: &InstanceState{
   204  					ID: "foo",
   205  				},
   206  			},
   207  
   208  			&State{},
   209  			&State{
   210  				Modules: []*ModuleState{
   211  					&ModuleState{
   212  						Path: []string{"root"},
   213  						Resources: map[string]*ResourceState{
   214  							"aws_instance.foo": &ResourceState{
   215  								Type: "test_instance",
   216  								Primary: &InstanceState{
   217  									ID: "foo",
   218  								},
   219  							},
   220  						},
   221  					},
   222  				},
   223  			},
   224  		},
   225  
   226  		"ResourceState w/ deps, provider => Resource Addr (new)": {
   227  			false,
   228  			"aws_instance.bar",
   229  			"aws_instance.foo",
   230  			&ResourceState{
   231  				Type:         "test_instance",
   232  				Provider:     "foo",
   233  				Dependencies: []string{"bar"},
   234  				Primary: &InstanceState{
   235  					ID: "foo",
   236  				},
   237  			},
   238  
   239  			&State{},
   240  			&State{
   241  				Modules: []*ModuleState{
   242  					&ModuleState{
   243  						Path: []string{"root"},
   244  						Resources: map[string]*ResourceState{
   245  							"aws_instance.foo": &ResourceState{
   246  								Type:         "test_instance",
   247  								Provider:     "foo",
   248  								Dependencies: []string{"bar"},
   249  								Primary: &InstanceState{
   250  									ID: "foo",
   251  								},
   252  							},
   253  						},
   254  					},
   255  				},
   256  			},
   257  		},
   258  
   259  		"ResourceState tainted => Resource Addr (new)": {
   260  			false,
   261  			"aws_instance.bar",
   262  			"aws_instance.foo",
   263  			&ResourceState{
   264  				Type: "test_instance",
   265  				Primary: &InstanceState{
   266  					ID:      "foo",
   267  					Tainted: true,
   268  				},
   269  			},
   270  
   271  			&State{},
   272  			&State{
   273  				Modules: []*ModuleState{
   274  					&ModuleState{
   275  						Path: []string{"root"},
   276  						Resources: map[string]*ResourceState{
   277  							"aws_instance.foo": &ResourceState{
   278  								Type: "test_instance",
   279  								Primary: &InstanceState{
   280  									ID:      "foo",
   281  									Tainted: true,
   282  								},
   283  							},
   284  						},
   285  					},
   286  				},
   287  			},
   288  		},
   289  
   290  		"ResourceState => Resource Addr (existing)": {
   291  			true,
   292  			"aws_instance.bar",
   293  			"aws_instance.foo",
   294  			&ResourceState{
   295  				Type: "test_instance",
   296  				Primary: &InstanceState{
   297  					ID: "foo",
   298  				},
   299  			},
   300  
   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  			nil,
   317  		},
   318  
   319  		"ResourceState => Module (new)": {
   320  			false,
   321  			"aws_instance.bar",
   322  			"module.foo",
   323  			&ResourceState{
   324  				Type: "test_instance",
   325  				Primary: &InstanceState{
   326  					ID: "foo",
   327  				},
   328  			},
   329  
   330  			&State{},
   331  			&State{
   332  				Modules: []*ModuleState{
   333  					&ModuleState{
   334  						Path: []string{"root", "foo"},
   335  						Resources: map[string]*ResourceState{
   336  							"aws_instance.bar": &ResourceState{
   337  								Type: "test_instance",
   338  								Primary: &InstanceState{
   339  									ID: "foo",
   340  								},
   341  							},
   342  						},
   343  					},
   344  				},
   345  			},
   346  		},
   347  
   348  		"InstanceState => Resource (new)": {
   349  			false,
   350  			"aws_instance.bar.primary",
   351  			"aws_instance.baz",
   352  			&InstanceState{
   353  				ID: "foo",
   354  			},
   355  
   356  			&State{},
   357  			&State{
   358  				Modules: []*ModuleState{
   359  					&ModuleState{
   360  						Path: []string{"root"},
   361  						Resources: map[string]*ResourceState{
   362  							"aws_instance.baz": &ResourceState{
   363  								Type: "aws_instance",
   364  								Primary: &InstanceState{
   365  									ID: "foo",
   366  								},
   367  							},
   368  						},
   369  					},
   370  				},
   371  			},
   372  		},
   373  
   374  		"InstanceState => Module (new)": {
   375  			false,
   376  			"aws_instance.bar.primary",
   377  			"module.foo",
   378  			&InstanceState{
   379  				ID: "foo",
   380  			},
   381  
   382  			&State{},
   383  			&State{
   384  				Modules: []*ModuleState{
   385  					&ModuleState{
   386  						Path: []string{"root", "foo"},
   387  						Resources: map[string]*ResourceState{
   388  							"aws_instance.bar": &ResourceState{
   389  								Type: "aws_instance",
   390  								Primary: &InstanceState{
   391  									ID: "foo",
   392  								},
   393  							},
   394  						},
   395  					},
   396  				},
   397  			},
   398  		},
   399  	}
   400  
   401  	for k, tc := range cases {
   402  		// Make sure they're both initialized as normal
   403  		tc.One.init()
   404  		if tc.Two != nil {
   405  			tc.Two.init()
   406  		}
   407  
   408  		// Add the value
   409  		err := tc.One.Add(tc.From, tc.To, tc.Value)
   410  		if (err != nil) != tc.Err {
   411  			t.Fatalf("bad: %s\n\n%s", k, err)
   412  		}
   413  		if tc.Err {
   414  			continue
   415  		}
   416  
   417  		// Prune them both to be sure
   418  		tc.One.prune()
   419  		tc.Two.prune()
   420  
   421  		// Verify equality
   422  		if !tc.One.Equal(tc.Two) {
   423  			t.Fatalf("Bad: %s\n\n%#v\n\n%#v", k, tc.One, tc.Two)
   424  			//t.Fatalf("Bad: %s\n\n%s\n\n%s", k, tc.One.String(), tc.Two.String())
   425  		}
   426  	}
   427  }