add destroy provisioner test with locals, outputs

Add a complex destroy provisioner testcase using locals, outputs and
variables.

Add that pesky "id" attribute to the instance states for interpolation.
This commit is contained in:
James Bardin 2018-01-29 18:00:18 -05:00
parent 7da1a39480
commit 7ac0a46981
5 changed files with 100 additions and 1 deletions

View File

@ -7623,7 +7623,7 @@ func TestContext2Apply_destroyProvisionerWithLocals(t *testing.T) {
State: &State{ State: &State{
Modules: []*ModuleState{ Modules: []*ModuleState{
&ModuleState{ &ModuleState{
Path: rootModulePath, Path: []string{"root", "mod"},
Resources: map[string]*ResourceState{ Resources: map[string]*ResourceState{
"aws_instance.foo": resourceState("aws_instance", "1234"), "aws_instance.foo": resourceState("aws_instance", "1234"),
}, },
@ -7646,6 +7646,68 @@ func TestContext2Apply_destroyProvisionerWithLocals(t *testing.T) {
} }
} }
func TestContext2Apply_destroyProvisionerWithOutput(t *testing.T) {
m := testModule(t, "apply-provisioner-destroy-outputs")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
pr := testProvisioner()
pr.ApplyFn = func(is *InstanceState, rc *ResourceConfig) error {
cmd, ok := rc.Get("command")
if !ok || cmd != "3" {
fmt.Printf("%#v\n", rc.Config)
return fmt.Errorf("provisioner for %s got %v:%s", is.ID, ok, cmd)
}
return nil
}
ctx := testContext2(t, &ContextOpts{
Module: m,
ProviderResolver: ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
),
Provisioners: map[string]ResourceProvisionerFactory{
"shell": testProvisionerFuncFixed(pr),
},
State: &State{
Modules: []*ModuleState{
&ModuleState{
Path: []string{"root"},
Resources: map[string]*ResourceState{
"aws_instance.foo": resourceState("aws_instance", "1"),
},
},
&ModuleState{
Path: []string{"root", "mod"},
Resources: map[string]*ResourceState{
"aws_instance.baz": resourceState("aws_instance", "3"),
},
// state needs to be properly initialized
Outputs: map[string]*OutputState{},
},
&ModuleState{
Path: []string{"root", "mod2"},
Resources: map[string]*ResourceState{
"aws_instance.bar": resourceState("aws_instance", "2"),
},
},
},
},
Destroy: true,
})
if _, err := ctx.Plan(); err != nil {
t.Fatal(err)
}
if _, err := ctx.Apply(); err != nil {
t.Fatal(err)
}
}
func TestContext2Apply_targetedDestroyCountDeps(t *testing.T) { func TestContext2Apply_targetedDestroyCountDeps(t *testing.T) {
m := testModule(t, "apply-destroy-targeted-count") m := testModule(t, "apply-destroy-targeted-count")
p := testProvider("aws") p := testProvider("aws")

View File

@ -375,6 +375,9 @@ func resourceState(resourceType, resourceID string) *ResourceState {
Type: resourceType, Type: resourceType,
Primary: &InstanceState{ Primary: &InstanceState{
ID: resourceID, ID: resourceID,
Attributes: map[string]string{
"id": resourceID,
},
}, },
} }
} }

View File

@ -0,0 +1,19 @@
module "mod" {
source = "./mod"
}
locals {
value = "${module.mod.value}"
}
resource "aws_instance" "foo" {
provisioner "shell" {
command = "${local.value}"
when = "destroy"
}
}
module "mod2" {
source = "./mod2"
value = "${module.mod.value}"
}

View File

@ -0,0 +1,5 @@
output "value" {
value = "${aws_instance.baz.id}"
}
resource "aws_instance" "baz" {}

View File

@ -0,0 +1,10 @@
variable "value" {
}
resource "aws_instance" "bar" {
provisioner "shell" {
command = "${var.value}"
when = "destroy"
}
}