2015-01-26 22:17:52 -06:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-01-28 00:00:56 -06:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
2015-01-26 22:17:52 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestOrphanTransformer(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-orphan-basic")
|
2015-01-26 23:32:32 -06:00
|
|
|
state := &State{
|
|
|
|
Modules: []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: RootModulePath,
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.web": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
},
|
2015-01-26 22:17:52 -06:00
|
|
|
|
2015-01-26 23:32:32 -06:00
|
|
|
// The orphan
|
|
|
|
"aws_instance.db": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
},
|
2015-01-26 22:17:52 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-01-26 23:32:32 -06:00
|
|
|
g := Graph{Path: RootModulePath}
|
|
|
|
{
|
2016-11-05 18:26:12 -05:00
|
|
|
tf := &ConfigTransformerOld{Module: mod}
|
2015-01-26 23:32:32 -06:00
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-01-26 22:17:52 -06:00
|
|
|
}
|
|
|
|
|
2015-01-30 15:31:39 -06:00
|
|
|
transform := &OrphanTransformer{State: state, Module: mod}
|
2015-01-26 23:32:32 -06:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
2015-01-26 22:17:52 -06:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformOrphanBasicStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 13:00:24 -06:00
|
|
|
func TestOrphanTransformer_modules(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-orphan-modules")
|
|
|
|
state := &State{
|
|
|
|
Modules: []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: RootModulePath,
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.foo": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Orphan module
|
|
|
|
&ModuleState{
|
|
|
|
Path: []string{RootModuleName, "foo"},
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.web": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
|
|
{
|
2016-11-05 18:26:12 -05:00
|
|
|
tf := &ConfigTransformerOld{Module: mod}
|
2015-01-27 13:00:24 -06:00
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 15:31:39 -06:00
|
|
|
transform := &OrphanTransformer{State: state, Module: mod}
|
2015-01-27 13:00:24 -06:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformOrphanModulesStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 13:10:51 -06:00
|
|
|
func TestOrphanTransformer_modulesDeps(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-orphan-modules")
|
|
|
|
state := &State{
|
|
|
|
Modules: []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: RootModulePath,
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.foo": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Orphan module
|
|
|
|
&ModuleState{
|
|
|
|
Path: []string{RootModuleName, "foo"},
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.web": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Dependencies: []string{
|
|
|
|
"aws_instance.foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
|
|
{
|
2016-11-05 18:26:12 -05:00
|
|
|
tf := &ConfigTransformerOld{Module: mod}
|
2015-01-27 13:10:51 -06:00
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 15:31:39 -06:00
|
|
|
transform := &OrphanTransformer{State: state, Module: mod}
|
2015-01-27 13:10:51 -06:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformOrphanModulesDepsStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 15:44:27 -06:00
|
|
|
func TestOrphanTransformer_modulesDepsOrphan(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-orphan-modules")
|
|
|
|
state := &State{
|
|
|
|
Modules: []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: RootModulePath,
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.web": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Orphan module
|
|
|
|
&ModuleState{
|
|
|
|
Path: []string{RootModuleName, "foo"},
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.web": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Dependencies: []string{
|
|
|
|
"aws_instance.web",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
|
|
{
|
2016-11-05 18:26:12 -05:00
|
|
|
tf := &ConfigTransformerOld{Module: mod}
|
2015-01-27 15:44:27 -06:00
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 15:31:39 -06:00
|
|
|
transform := &OrphanTransformer{State: state, Module: mod}
|
2015-01-27 15:44:27 -06:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformOrphanModulesDepsOrphanStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 19:42:17 -06:00
|
|
|
func TestOrphanTransformer_modulesNoRoot(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-orphan-modules")
|
|
|
|
state := &State{
|
|
|
|
Modules: []*ModuleState{
|
|
|
|
// Orphan module
|
|
|
|
&ModuleState{
|
|
|
|
Path: []string{RootModuleName, "foo"},
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.web": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
|
|
{
|
2016-11-05 18:26:12 -05:00
|
|
|
tf := &ConfigTransformerOld{Module: mod}
|
2015-02-11 19:42:17 -06:00
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
transform := &OrphanTransformer{State: state, Module: mod}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformOrphanModulesNoRootStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 22:17:52 -06:00
|
|
|
func TestOrphanTransformer_resourceDepends(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-orphan-basic")
|
2015-01-26 23:32:32 -06:00
|
|
|
state := &State{
|
|
|
|
Modules: []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: RootModulePath,
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.web": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
},
|
2015-01-26 22:17:52 -06:00
|
|
|
|
2015-01-26 23:32:32 -06:00
|
|
|
// The orphan
|
|
|
|
"aws_instance.db": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
Dependencies: []string{
|
|
|
|
"aws_instance.web",
|
|
|
|
},
|
|
|
|
},
|
2015-01-26 22:17:52 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-01-26 23:32:32 -06:00
|
|
|
g := Graph{Path: RootModulePath}
|
|
|
|
{
|
2016-11-05 18:26:12 -05:00
|
|
|
tf := &ConfigTransformerOld{Module: mod}
|
2015-01-26 23:32:32 -06:00
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-01-26 22:17:52 -06:00
|
|
|
}
|
|
|
|
|
2015-01-30 15:31:39 -06:00
|
|
|
transform := &OrphanTransformer{State: state, Module: mod}
|
2015-01-26 23:32:32 -06:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
2015-01-26 22:17:52 -06:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformOrphanResourceDependsStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 19:50:07 -06:00
|
|
|
func TestOrphanTransformer_nilState(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-orphan-basic")
|
|
|
|
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
|
|
{
|
2016-11-05 18:26:12 -05:00
|
|
|
tf := &ConfigTransformerOld{Module: mod}
|
2015-02-11 19:50:07 -06:00
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
transform := &OrphanTransformer{State: nil, Module: mod}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformOrphanNilStateStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 19:42:17 -06:00
|
|
|
func TestGraphNodeOrphanModule_impl(t *testing.T) {
|
|
|
|
var _ dag.Vertex = new(graphNodeOrphanModule)
|
|
|
|
var _ dag.NamedVertex = new(graphNodeOrphanModule)
|
|
|
|
var _ GraphNodeExpandable = new(graphNodeOrphanModule)
|
|
|
|
}
|
|
|
|
|
2015-01-28 00:00:56 -06:00
|
|
|
func TestGraphNodeOrphanResource_impl(t *testing.T) {
|
|
|
|
var _ dag.Vertex = new(graphNodeOrphanResource)
|
|
|
|
var _ dag.NamedVertex = new(graphNodeOrphanResource)
|
|
|
|
var _ GraphNodeProviderConsumer = new(graphNodeOrphanResource)
|
2016-01-07 14:43:43 -06:00
|
|
|
var _ GraphNodeAddressable = new(graphNodeOrphanResource)
|
2015-01-28 00:00:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGraphNodeOrphanResource_ProvidedBy(t *testing.T) {
|
2016-01-07 14:43:43 -06:00
|
|
|
n := &graphNodeOrphanResource{ResourceKey: &ResourceStateKey{Type: "aws_instance"}}
|
2015-02-10 01:32:28 -06:00
|
|
|
if v := n.ProvidedBy(); v[0] != "aws" {
|
2015-01-28 00:00:56 -06:00
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-23 17:36:53 -05:00
|
|
|
func TestGraphNodeOrphanResource_ProvidedBy_alias(t *testing.T) {
|
2016-01-07 14:43:43 -06:00
|
|
|
n := &graphNodeOrphanResource{ResourceKey: &ResourceStateKey{Type: "aws_instance"}, Provider: "aws.bar"}
|
2015-03-23 17:36:53 -05:00
|
|
|
if v := n.ProvidedBy(); v[0] != "aws.bar" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 22:17:52 -06:00
|
|
|
const testTransformOrphanBasicStr = `
|
|
|
|
aws_instance.db (orphan)
|
|
|
|
aws_instance.web
|
|
|
|
`
|
|
|
|
|
2015-01-27 13:00:24 -06:00
|
|
|
const testTransformOrphanModulesStr = `
|
|
|
|
aws_instance.foo
|
|
|
|
module.foo (orphan)
|
|
|
|
`
|
|
|
|
|
2015-01-27 13:10:51 -06:00
|
|
|
const testTransformOrphanModulesDepsStr = `
|
|
|
|
aws_instance.foo
|
|
|
|
module.foo (orphan)
|
|
|
|
aws_instance.foo
|
|
|
|
`
|
|
|
|
|
2015-01-27 15:44:27 -06:00
|
|
|
const testTransformOrphanModulesDepsOrphanStr = `
|
|
|
|
aws_instance.foo
|
|
|
|
aws_instance.web (orphan)
|
|
|
|
module.foo (orphan)
|
|
|
|
aws_instance.web (orphan)
|
|
|
|
`
|
|
|
|
|
2015-02-11 19:50:07 -06:00
|
|
|
const testTransformOrphanNilStateStr = `
|
|
|
|
aws_instance.web
|
|
|
|
`
|
|
|
|
|
2015-01-26 22:17:52 -06:00
|
|
|
const testTransformOrphanResourceDependsStr = `
|
|
|
|
aws_instance.db (orphan)
|
|
|
|
aws_instance.web
|
|
|
|
aws_instance.web
|
|
|
|
`
|
2015-02-11 19:42:17 -06:00
|
|
|
|
|
|
|
const testTransformOrphanModulesNoRootStr = `
|
|
|
|
aws_instance.foo
|
|
|
|
module.foo (orphan)
|
|
|
|
`
|