2015-02-09 12:14:09 -06:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
|
2015-02-09 12:14:09 -06:00
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMissingProvisionerTransformer(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-provisioner-basic")
|
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2015-02-09 12:14:09 -06:00
|
|
|
{
|
2018-05-04 21:24:06 -05:00
|
|
|
tf := &ConfigTransformer{Config: mod}
|
2015-02-09 12:14:09 -06:00
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 21:56:16 -06:00
|
|
|
{
|
2018-05-04 21:24:06 -05:00
|
|
|
transform := &AttachResourceConfigTransformer{Config: mod}
|
2017-01-26 21:56:16 -06:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 14:52:50 -05:00
|
|
|
{
|
2016-02-04 06:46:45 -06:00
|
|
|
transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}}
|
2015-06-19 14:52:50 -05:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-02-04 06:46:45 -06:00
|
|
|
transform := &ProvisionerTransformer{}
|
2015-06-19 14:52:50 -05:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-02-09 12:14:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformMissingProvisionerBasicStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-03 12:25:11 -05:00
|
|
|
func TestMissingProvisionerTransformer_module(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-provisioner-module")
|
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2016-11-03 12:25:11 -05:00
|
|
|
{
|
2018-05-04 21:24:06 -05:00
|
|
|
concreteResource := func(a *NodeAbstractResourceInstance) dag.Vertex {
|
2016-11-03 12:25:11 -05:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
var state State
|
|
|
|
state.init()
|
|
|
|
state.Modules = []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: []string{"root"},
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.foo": &ResourceState{
|
|
|
|
Primary: &InstanceState{ID: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
&ModuleState{
|
|
|
|
Path: []string{"root", "child"},
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.foo": &ResourceState{
|
|
|
|
Primary: &InstanceState{ID: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tf := &StateTransformer{
|
|
|
|
Concrete: concreteResource,
|
|
|
|
State: &state,
|
|
|
|
}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-05-04 21:24:06 -05:00
|
|
|
transform := &AttachResourceConfigTransformer{Config: mod}
|
2016-11-03 12:25:11 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformMissingProvisionerModuleStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 06:46:45 -06:00
|
|
|
func TestCloseProvisionerTransformer(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-provisioner-basic")
|
2015-02-09 12:14:09 -06:00
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2015-02-09 12:14:09 -06:00
|
|
|
{
|
2018-05-04 21:24:06 -05:00
|
|
|
tf := &ConfigTransformer{Config: mod}
|
2015-02-09 12:14:09 -06:00
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 21:56:16 -06:00
|
|
|
{
|
2018-05-04 21:24:06 -05:00
|
|
|
transform := &AttachResourceConfigTransformer{Config: mod}
|
2017-01-26 21:56:16 -06:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-09 12:14:09 -06:00
|
|
|
{
|
2016-02-04 06:46:45 -06:00
|
|
|
transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}}
|
2015-02-09 12:14:09 -06: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 14:52:50 -05:00
|
|
|
{
|
|
|
|
transform := &CloseProvisionerTransformer{}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-09 12:14:09 -06:00
|
|
|
actual := strings.TrimSpace(g.String())
|
2016-02-04 06:46:45 -06:00
|
|
|
expected := strings.TrimSpace(testTransformCloseProvisionerBasicStr)
|
2015-02-09 12:14:09 -06:00
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testTransformMissingProvisionerBasicStr = `
|
|
|
|
aws_instance.web
|
2016-02-04 06:46:45 -06:00
|
|
|
provisioner.shell
|
|
|
|
provisioner.shell
|
2015-02-09 12:14:09 -06:00
|
|
|
`
|
|
|
|
|
2016-11-03 12:25:11 -05:00
|
|
|
const testTransformMissingProvisionerModuleStr = `
|
|
|
|
aws_instance.foo
|
|
|
|
provisioner.shell
|
|
|
|
module.child.aws_instance.foo
|
|
|
|
module.child.provisioner.shell
|
|
|
|
module.child.provisioner.shell
|
|
|
|
provisioner.shell
|
|
|
|
`
|
|
|
|
|
2016-02-04 06:46:45 -06:00
|
|
|
const testTransformCloseProvisionerBasicStr = `
|
2015-02-09 12:14:09 -06:00
|
|
|
aws_instance.web
|
2016-02-04 06:46:45 -06:00
|
|
|
provisioner.shell
|
|
|
|
provisioner.shell
|
|
|
|
provisioner.shell (close)
|
2015-06-19 14:52:50 -05:00
|
|
|
aws_instance.web
|
2015-02-09 12:14:09 -06:00
|
|
|
`
|