terraform/terraform/transform_provisioner_test.go

203 lines
4.7 KiB
Go
Raw Normal View History

2015-02-09 19:14:09 +01:00
package terraform
import (
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
2015-02-09 19:14:09 +01:00
"github.com/hashicorp/terraform/dag"
"github.com/hashicorp/terraform/states"
2015-02-09 19:14:09 +01:00
)
func TestMissingProvisionerTransformer(t *testing.T) {
mod := testModule(t, "transform-provisioner-basic")
g := Graph{Path: addrs.RootModuleInstance}
2015-02-09 19:14:09 +01:00
{
tf := &ConfigTransformer{Config: mod}
2015-02-09 19:14:09 +01:00
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &AttachResourceConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &ProvisionerTransformer{}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
2015-02-09 19:14:09 +01:00
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTransformMissingProvisionerBasicStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
}
}
func TestMissingProvisionerTransformer_module(t *testing.T) {
mod := testModule(t, "transform-provisioner-module")
g := Graph{Path: addrs.RootModuleInstance}
{
concreteResource := func(a *NodeAbstractResourceInstance) dag.Vertex {
return a
}
state := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
&states.ResourceInstanceObjectSrc{
AttrsFlat: map[string]string{
"id": "foo",
},
Status: states.ObjectReady,
},
addrs.ProviderConfig{
Type: "aws",
}.Absolute(addrs.RootModuleInstance),
)
s.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance.Child("child", addrs.NoKey)),
&states.ResourceInstanceObjectSrc{
AttrsFlat: map[string]string{
"id": "foo",
},
Status: states.ObjectReady,
},
addrs.ProviderConfig{
Type: "aws",
}.Absolute(addrs.RootModuleInstance),
)
})
tf := &StateTransformer{
core: Be more explicit in how we handle create_before_destroy Previously our handling of create_before_destroy -- and of deposed objects in particular -- was rather "implicit" and spread over various different subsystems. We'd quietly just destroy every deposed object during a destroy operation, without any user-visible plan to do so. Here we make things more explicit by tracking each deposed object individually by its pseudorandomly-allocated key. There are two different mechanisms at play here, building on the same concepts: - During a replace operation with create_before_destroy, we *pre-allocate* a DeposedKey to use for the prior object in the "apply" node and then pass that exact id to the destroy node, ensuring that we only destroy the single object we planned to destroy. In the happy path here the user never actually sees the allocated deposed key because we use it and then immediately destroy it within the same operation. However, that destroy may fail, which brings us to the second mechanism: - If any deposed objects are already present in state during _plan_, we insert a destroy change for them into the plan so that it's explicit to the user that we are going to destroy these additional objects, and then create an individual graph node for each one in DiffTransformer. The main motivation here is to be more careful in how we handle these destroys so that from a user's standpoint we never destroy something without the user knowing about it ahead of time. However, this new organization also hopefully makes the code itself a little easier to follow because the connection between the create and destroy steps of a Replace is reprseented in a single place (in DiffTransformer) and deposed instances each have their own explicit graph node rather than being secretly handled as part of the main instance-level graph node.
2018-09-20 21:30:52 +02:00
ConcreteCurrent: concreteResource,
State: state,
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
t.Logf("graph after StateTransformer:\n%s", g.StringWithNodeTypes())
}
{
transform := &AttachResourceConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
t.Logf("graph after MissingProvisionerTransformer:\n%s", g.StringWithNodeTypes())
}
{
transform := &ProvisionerTransformer{}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
t.Logf("graph after ProvisionerTransformer:\n%s", g.StringWithNodeTypes())
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTransformMissingProvisionerModuleStr)
if actual != expected {
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
}
}
func TestCloseProvisionerTransformer(t *testing.T) {
mod := testModule(t, "transform-provisioner-basic")
2015-02-09 19:14:09 +01:00
g := Graph{Path: addrs.RootModuleInstance}
2015-02-09 19:14:09 +01:00
{
tf := &ConfigTransformer{Config: mod}
2015-02-09 19:14:09 +01:00
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &AttachResourceConfigTransformer{Config: mod}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
2015-02-09 19:14:09 +01:00
{
transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}}
2015-02-09 19:14:09 +01:00
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &ProvisionerTransformer{}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
transform := &CloseProvisionerTransformer{}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
2015-02-09 19:14:09 +01:00
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTransformCloseProvisionerBasicStr)
2015-02-09 19:14:09 +01:00
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
}
}
const testTransformMissingProvisionerBasicStr = `
aws_instance.web
provisioner.shell
provisioner.shell
2015-02-09 19:14:09 +01:00
`
const testTransformMissingProvisionerModuleStr = `
aws_instance.foo
provisioner.shell
module.child.aws_instance.foo
module.child.provisioner.shell
module.child.provisioner.shell
provisioner.shell
`
const testTransformCloseProvisionerBasicStr = `
2015-02-09 19:14:09 +01:00
aws_instance.web
provisioner.shell
provisioner.shell
provisioner.shell (close)
aws_instance.web
2015-02-09 19:14:09 +01:00
`