diff --git a/terraform/state_add_test.go b/terraform/state_add_test.go new file mode 100644 index 0000000000..50596af1f0 --- /dev/null +++ b/terraform/state_add_test.go @@ -0,0 +1,165 @@ +package terraform + +import ( + "testing" +) + +func TestStateAdd(t *testing.T) { + cases := map[string]struct { + Err bool + Address string + Value interface{} + One, Two *State + }{ + "ModuleState => Module Addr (new)": { + false, + "module.foo", + &ModuleState{ + Path: rootModulePath, + Resources: map[string]*ResourceState{ + "test_instance.foo": &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + + "test_instance.bar": &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + }, + }, + + &State{}, + &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: []string{"root", "foo"}, + Resources: map[string]*ResourceState{ + "test_instance.foo": &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + + "test_instance.bar": &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + }, + }, + }, + }, + }, + + "ModuleState w/ outputs and deps => Module Addr (new)": { + false, + "module.foo", + &ModuleState{ + Path: rootModulePath, + Outputs: map[string]interface{}{ + "foo": "bar", + }, + Dependencies: []string{"foo"}, + Resources: map[string]*ResourceState{ + "test_instance.foo": &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + + "test_instance.bar": &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + }, + }, + + &State{}, + &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: []string{"root", "foo"}, + Outputs: map[string]interface{}{ + "foo": "bar", + }, + Dependencies: []string{"foo"}, + Resources: map[string]*ResourceState{ + "test_instance.foo": &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + + "test_instance.bar": &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + }, + }, + }, + }, + }, + + "ModuleState => Module Addr (existing)": { + true, + "module.foo", + &ModuleState{}, + &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: []string{"root", "foo"}, + Resources: map[string]*ResourceState{ + "test_instance.baz": &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + }, + }, + }, + }, + nil, + }, + } + + for k, tc := range cases { + // Make sure they're both initialized as normal + tc.One.init() + if tc.Two != nil { + tc.Two.init() + } + + // Add the value + err := tc.One.Add(tc.Address, tc.Value) + if (err != nil) != tc.Err { + t.Fatalf("bad: %s\n\n%s", k, err) + } + if tc.Err { + continue + } + + // Prune them both to be sure + tc.One.prune() + tc.Two.prune() + + // Verify equality + if !tc.One.Equal(tc.Two) { + //t.Fatalf("Bad: %s\n\n%#v\n\n%#v", k, tc.One, tc.Two) + t.Fatalf("Bad: %s\n\n%s\n\n%s", k, tc.One.String(), tc.Two.String()) + } + } +} diff --git a/terraform/state_test.go b/terraform/state_test.go index fdfd7a90a5..a51b670b7d 100644 --- a/terraform/state_test.go +++ b/terraform/state_test.go @@ -424,166 +424,6 @@ func TestStateIncrementSerialMaybe(t *testing.T) { } } -func TestStateAdd(t *testing.T) { - cases := map[string]struct { - Err bool - Address string - Value interface{} - One, Two *State - }{ - "ModuleState => Module Addr (new)": { - false, - "module.foo", - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "test_instance.foo": &ResourceState{ - Type: "test_instance", - Primary: &InstanceState{ - ID: "foo", - }, - }, - - "test_instance.bar": &ResourceState{ - Type: "test_instance", - Primary: &InstanceState{ - ID: "foo", - }, - }, - }, - }, - - &State{}, - &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: []string{"root", "foo"}, - Resources: map[string]*ResourceState{ - "test_instance.foo": &ResourceState{ - Type: "test_instance", - Primary: &InstanceState{ - ID: "foo", - }, - }, - - "test_instance.bar": &ResourceState{ - Type: "test_instance", - Primary: &InstanceState{ - ID: "foo", - }, - }, - }, - }, - }, - }, - }, - - "ModuleState w/ outputs and deps => Module Addr (new)": { - false, - "module.foo", - &ModuleState{ - Path: rootModulePath, - Outputs: map[string]interface{}{ - "foo": "bar", - }, - Dependencies: []string{"foo"}, - Resources: map[string]*ResourceState{ - "test_instance.foo": &ResourceState{ - Type: "test_instance", - Primary: &InstanceState{ - ID: "foo", - }, - }, - - "test_instance.bar": &ResourceState{ - Type: "test_instance", - Primary: &InstanceState{ - ID: "foo", - }, - }, - }, - }, - - &State{}, - &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: []string{"root", "foo"}, - Outputs: map[string]interface{}{ - "foo": "bar", - }, - Dependencies: []string{"foo"}, - Resources: map[string]*ResourceState{ - "test_instance.foo": &ResourceState{ - Type: "test_instance", - Primary: &InstanceState{ - ID: "foo", - }, - }, - - "test_instance.bar": &ResourceState{ - Type: "test_instance", - Primary: &InstanceState{ - ID: "foo", - }, - }, - }, - }, - }, - }, - }, - - "ModuleState => Module Addr (existing)": { - true, - "module.foo", - &ModuleState{}, - &State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: []string{"root", "foo"}, - Resources: map[string]*ResourceState{ - "test_instance.baz": &ResourceState{ - Type: "test_instance", - Primary: &InstanceState{ - ID: "foo", - }, - }, - }, - }, - }, - }, - nil, - }, - } - - for k, tc := range cases { - // Make sure they're both initialized as normal - tc.One.init() - if tc.Two != nil { - tc.Two.init() - } - - // Add the value - err := tc.One.Add(tc.Address, tc.Value) - if (err != nil) != tc.Err { - t.Fatalf("bad: %s\n\n%s", k, err) - } - if tc.Err { - continue - } - - // Prune them both to be sure - tc.One.prune() - tc.Two.prune() - - // Verify equality - if !tc.One.Equal(tc.Two) { - //t.Fatalf("Bad: %s\n\n%#v\n\n%#v", k, tc.One, tc.Two) - t.Fatalf("Bad: %s\n\n%s\n\n%s", k, tc.One.String(), tc.Two.String()) - } - } -} - func TestStateRemove(t *testing.T) { cases := map[string]struct { Address string