core: fill out context tests for targeted ops

This commit is contained in:
Paul Hinze 2015-03-30 15:17:28 -05:00
parent 23d005b0e8
commit 40ebfb5ccc
4 changed files with 186 additions and 0 deletions

View File

@ -1339,6 +1339,40 @@ func TestContext2Plan_multiple_taint(t *testing.T) {
} }
} }
func TestContext2Plan_targeted(t *testing.T) {
m := testModule(t, "plan-targeted")
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
Targets: []string{"aws_instance.foo"},
})
plan, err := ctx.Plan()
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(`
DIFF:
CREATE: aws_instance.foo
num: "" => "2"
type: "" => "aws_instance"
STATE:
<no state>
`)
if actual != expected {
t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual)
}
}
func TestContext2Plan_provider(t *testing.T) { func TestContext2Plan_provider(t *testing.T) {
m := testModule(t, "plan-provider") m := testModule(t, "plan-provider")
p := testProvider("aws") p := testProvider("aws")
@ -1460,6 +1494,47 @@ func TestContext2Refresh(t *testing.T) {
} }
} }
func TestContext2Refresh_targeted(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "refresh-targeted")
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
State: &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"aws_vpc.metoo": resourceState("aws_vpc", "vpc-abc123"),
"aws_instance.notme": resourceState("aws_instance", "i-bcd345"),
"aws_instance.me": resourceState("aws_instance", "i-abc123"),
"aws_elb.meneither": resourceState("aws_elb", "lb-abc123"),
},
},
},
},
Targets: []string{"aws_instance.me"},
})
refreshedResources := make([]string, 0, 2)
p.RefreshFn = func(i *InstanceInfo, is *InstanceState) (*InstanceState, error) {
refreshedResources = append(refreshedResources, i.Id)
return is, nil
}
_, err := ctx.Refresh()
if err != nil {
t.Fatalf("err: %s", err)
}
expected := []string{"aws_vpc.metoo", "aws_instance.me"}
if !reflect.DeepEqual(refreshedResources, expected) {
t.Fatalf("expected: %#v, got: %#v", expected, refreshedResources)
}
}
func TestContext2Refresh_delete(t *testing.T) { func TestContext2Refresh_delete(t *testing.T) {
p := testProvider("aws") p := testProvider("aws")
m := testModule(t, "refresh-basic") m := testModule(t, "refresh-basic")
@ -5164,6 +5239,86 @@ func TestContext2Apply_taintDepRequiresNew(t *testing.T) {
} }
} }
func TestContext2Apply_targeted(t *testing.T) {
m := testModule(t, "apply-targeted")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
Targets: []string{"aws_instance.foo"},
})
if _, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
mod := state.RootModule()
if len(mod.Resources) != 1 {
t.Fatalf("expected 1 resource, got: %#v", mod.Resources)
}
checkStateString(t, state, `
aws_instance.foo:
ID = foo
num = 2
type = aws_instance
`)
}
func TestContext2Apply_targetedDestroy(t *testing.T) {
m := testModule(t, "apply-targeted")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
State: &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.foo": resourceState("aws_instance", "i-bcd345"),
"aws_instance.bar": resourceState("aws_instance", "i-abc123"),
},
},
},
},
Targets: []string{"aws_instance.foo"},
Destroy: true,
})
if _, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
mod := state.RootModule()
if len(mod.Resources) != 1 {
t.Fatalf("expected 1 resource, got: %#v", mod.Resources)
}
checkStateString(t, state, `
aws_instance.bar:
ID = i-abc123
`)
}
func TestContext2Apply_unknownAttribute(t *testing.T) { func TestContext2Apply_unknownAttribute(t *testing.T) {
m := testModule(t, "apply-unknown") m := testModule(t, "apply-unknown")
p := testProvider("aws") p := testProvider("aws")
@ -5553,6 +5708,15 @@ func checkStateString(t *testing.T, state *State, expected string) {
} }
} }
func resourceState(resourceType, resourceID string) *ResourceState {
return &ResourceState{
Type: resourceType,
Primary: &InstanceState{
ID: resourceID,
},
}
}
const testContextGraph = ` const testContextGraph = `
root: root root: root
aws_instance.bar aws_instance.bar

View File

@ -0,0 +1,7 @@
resource "aws_instance" "foo" {
num = "2"
}
resource "aws_instance" "bar" {
foo = "bar"
}

View File

@ -0,0 +1,7 @@
resource "aws_instance" "foo" {
num = "2"
}
resource "aws_instance" "bar" {
foo = "${aws_instance.foo.num}"
}

View File

@ -0,0 +1,8 @@
resource "aws_vpc" "metoo" {}
resource "aws_instance" "notme" { }
resource "aws_instance" "me" {
vpc_id = "${aws_vpc.metoo.id}"
}
resource "aws_elb" "meneither" {
instances = ["${aws_instance.me.*.id}"]
}