add state an context tests

Make sure duplicate depends_on entries are pruned from existing states
on read.

Make sure new state built from configs with multiple references to the
same resource only add it once to the Dependencies.
This commit is contained in:
James Bardin 2017-04-08 15:15:18 -04:00
parent bd983f6cba
commit 3f49227b72
3 changed files with 93 additions and 0 deletions

View File

@ -8100,3 +8100,32 @@ func TestContext2Apply_terraformEnv(t *testing.T) {
t.Fatalf("bad: \n%s", actual)
}
}
// verify that multiple config references only create a single depends_on entry
func TestContext2Apply_multiRef(t *testing.T) {
m := testModule(t, "apply-multi-ref")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
if _, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
deps := state.Modules[0].Resources["aws_instance.other"].Dependencies
if len(deps) > 1 || deps[0] != "aws_instance.create" {
t.Fatalf("expected 1 depends_on entry for aws_instance.create, got %q", deps)
}
}

View File

@ -1898,6 +1898,62 @@ func TestReadState_prune(t *testing.T) {
}
}
func TestReadState_pruneDependencies(t *testing.T) {
state := &State{
Serial: 9,
Lineage: "5d1ad1a1-4027-4665-a908-dbe6adff11d8",
Remote: &RemoteState{
Type: "http",
Config: map[string]string{
"url": "http://my-cool-server.com/",
},
},
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Dependencies: []string{
"aws_instance.bar",
"aws_instance.bar",
},
Resources: map[string]*ResourceState{
"foo": &ResourceState{
Dependencies: []string{
"aws_instance.baz",
"aws_instance.baz",
},
Primary: &InstanceState{
ID: "bar",
},
},
},
},
},
}
state.init()
buf := new(bytes.Buffer)
if err := WriteState(state, buf); err != nil {
t.Fatalf("err: %s", err)
}
actual, err := ReadState(buf)
if err != nil {
t.Fatalf("err: %s", err)
}
// make sure the duplicate Dependencies are filtered
modDeps := actual.Modules[0].Dependencies
resourceDeps := actual.Modules[0].Resources["foo"].Dependencies
if len(modDeps) > 1 || modDeps[0] != "aws_instance.bar" {
t.Fatalf("expected 1 module depends_on entry, got %q", modDeps)
}
if len(resourceDeps) > 1 || resourceDeps[0] != "aws_instance.baz" {
t.Fatalf("expected 1 resource depends_on entry, got %q", resourceDeps)
}
}
func TestResourceNameSort(t *testing.T) {
names := []string{
"a",

View File

@ -0,0 +1,8 @@
resource "aws_instance" "create" {
bar = "abc"
}
resource "aws_instance" "other" {
var = "${aws_instance.create.id}"
foo = "${aws_instance.create.bar}"
}