github.com/paybyphone/terraform@v0.9.5-0.20170613192930-9706042ddd51/terraform/transform_orphan_resource_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/dag"
     9  )
    10  
    11  func TestOrphanResourceTransformer(t *testing.T) {
    12  	mod := testModule(t, "transform-orphan-basic")
    13  	state := &State{
    14  		Modules: []*ModuleState{
    15  			&ModuleState{
    16  				Path: RootModulePath,
    17  				Resources: map[string]*ResourceState{
    18  					"aws_instance.web": &ResourceState{
    19  						Type: "aws_instance",
    20  						Primary: &InstanceState{
    21  							ID: "foo",
    22  						},
    23  					},
    24  
    25  					// The orphan
    26  					"aws_instance.db": &ResourceState{
    27  						Type: "aws_instance",
    28  						Primary: &InstanceState{
    29  							ID: "foo",
    30  						},
    31  					},
    32  				},
    33  			},
    34  		},
    35  	}
    36  
    37  	g := Graph{Path: RootModulePath}
    38  	{
    39  		tf := &ConfigTransformer{Module: mod}
    40  		if err := tf.Transform(&g); err != nil {
    41  			t.Fatalf("err: %s", err)
    42  		}
    43  	}
    44  
    45  	{
    46  		tf := &OrphanResourceTransformer{
    47  			Concrete: testOrphanResourceConcreteFunc,
    48  			State:    state, Module: mod,
    49  		}
    50  		if err := tf.Transform(&g); err != nil {
    51  			t.Fatalf("err: %s", err)
    52  		}
    53  	}
    54  
    55  	actual := strings.TrimSpace(g.String())
    56  	expected := strings.TrimSpace(testTransformOrphanResourceBasicStr)
    57  	if actual != expected {
    58  		t.Fatalf("bad:\n\n%s", actual)
    59  	}
    60  }
    61  
    62  func TestOrphanResourceTransformer_nilModule(t *testing.T) {
    63  	mod := testModule(t, "transform-orphan-basic")
    64  	state := &State{
    65  		Modules: []*ModuleState{nil},
    66  	}
    67  
    68  	g := Graph{Path: RootModulePath}
    69  	{
    70  		tf := &ConfigTransformer{Module: mod}
    71  		if err := tf.Transform(&g); err != nil {
    72  			t.Fatalf("err: %s", err)
    73  		}
    74  	}
    75  
    76  	{
    77  		tf := &OrphanResourceTransformer{
    78  			Concrete: testOrphanResourceConcreteFunc,
    79  			State:    state, Module: mod,
    80  		}
    81  		if err := tf.Transform(&g); err != nil {
    82  			t.Fatalf("err: %s", err)
    83  		}
    84  	}
    85  }
    86  
    87  func TestOrphanResourceTransformer_countGood(t *testing.T) {
    88  	mod := testModule(t, "transform-orphan-count")
    89  	state := &State{
    90  		Modules: []*ModuleState{
    91  			&ModuleState{
    92  				Path: RootModulePath,
    93  				Resources: map[string]*ResourceState{
    94  					"aws_instance.foo.0": &ResourceState{
    95  						Type: "aws_instance",
    96  						Primary: &InstanceState{
    97  							ID: "foo",
    98  						},
    99  					},
   100  
   101  					"aws_instance.foo.1": &ResourceState{
   102  						Type: "aws_instance",
   103  						Primary: &InstanceState{
   104  							ID: "foo",
   105  						},
   106  					},
   107  				},
   108  			},
   109  		},
   110  	}
   111  
   112  	g := Graph{Path: RootModulePath}
   113  	{
   114  		tf := &ConfigTransformer{Module: mod}
   115  		if err := tf.Transform(&g); err != nil {
   116  			t.Fatalf("err: %s", err)
   117  		}
   118  	}
   119  
   120  	{
   121  		tf := &OrphanResourceTransformer{
   122  			Concrete: testOrphanResourceConcreteFunc,
   123  			State:    state, Module: mod,
   124  		}
   125  		if err := tf.Transform(&g); err != nil {
   126  			t.Fatalf("err: %s", err)
   127  		}
   128  	}
   129  
   130  	actual := strings.TrimSpace(g.String())
   131  	expected := strings.TrimSpace(testTransformOrphanResourceCountStr)
   132  	if actual != expected {
   133  		t.Fatalf("bad:\n\n%s", actual)
   134  	}
   135  }
   136  
   137  func TestOrphanResourceTransformer_countBad(t *testing.T) {
   138  	mod := testModule(t, "transform-orphan-count-empty")
   139  	state := &State{
   140  		Modules: []*ModuleState{
   141  			&ModuleState{
   142  				Path: RootModulePath,
   143  				Resources: map[string]*ResourceState{
   144  					"aws_instance.foo.0": &ResourceState{
   145  						Type: "aws_instance",
   146  						Primary: &InstanceState{
   147  							ID: "foo",
   148  						},
   149  					},
   150  
   151  					"aws_instance.foo.1": &ResourceState{
   152  						Type: "aws_instance",
   153  						Primary: &InstanceState{
   154  							ID: "foo",
   155  						},
   156  					},
   157  				},
   158  			},
   159  		},
   160  	}
   161  
   162  	g := Graph{Path: RootModulePath}
   163  	{
   164  		tf := &ConfigTransformer{Module: mod}
   165  		if err := tf.Transform(&g); err != nil {
   166  			t.Fatalf("err: %s", err)
   167  		}
   168  	}
   169  
   170  	{
   171  		tf := &OrphanResourceTransformer{
   172  			Concrete: testOrphanResourceConcreteFunc,
   173  			State:    state, Module: mod,
   174  		}
   175  		if err := tf.Transform(&g); err != nil {
   176  			t.Fatalf("err: %s", err)
   177  		}
   178  	}
   179  
   180  	actual := strings.TrimSpace(g.String())
   181  	expected := strings.TrimSpace(testTransformOrphanResourceCountBadStr)
   182  	if actual != expected {
   183  		t.Fatalf("bad:\n\n%s", actual)
   184  	}
   185  }
   186  
   187  func TestOrphanResourceTransformer_modules(t *testing.T) {
   188  	mod := testModule(t, "transform-orphan-modules")
   189  	state := &State{
   190  		Modules: []*ModuleState{
   191  			&ModuleState{
   192  				Path: RootModulePath,
   193  				Resources: map[string]*ResourceState{
   194  					"aws_instance.foo": &ResourceState{
   195  						Type: "aws_instance",
   196  						Primary: &InstanceState{
   197  							ID: "foo",
   198  						},
   199  					},
   200  				},
   201  			},
   202  
   203  			&ModuleState{
   204  				Path: []string{"root", "child"},
   205  				Resources: map[string]*ResourceState{
   206  					"aws_instance.web": &ResourceState{
   207  						Type: "aws_instance",
   208  						Primary: &InstanceState{
   209  							ID: "foo",
   210  						},
   211  					},
   212  				},
   213  			},
   214  		},
   215  	}
   216  
   217  	g := Graph{Path: RootModulePath}
   218  	{
   219  		tf := &ConfigTransformer{Module: mod}
   220  		if err := tf.Transform(&g); err != nil {
   221  			t.Fatalf("err: %s", err)
   222  		}
   223  	}
   224  
   225  	{
   226  		tf := &OrphanResourceTransformer{
   227  			Concrete: testOrphanResourceConcreteFunc,
   228  			State:    state, Module: mod,
   229  		}
   230  		if err := tf.Transform(&g); err != nil {
   231  			t.Fatalf("err: %s", err)
   232  		}
   233  	}
   234  
   235  	actual := strings.TrimSpace(g.String())
   236  	expected := strings.TrimSpace(testTransformOrphanResourceModulesStr)
   237  	if actual != expected {
   238  		t.Fatalf("bad:\n\n%s", actual)
   239  	}
   240  }
   241  
   242  const testTransformOrphanResourceBasicStr = `
   243  aws_instance.db (orphan)
   244  aws_instance.web
   245  `
   246  
   247  const testTransformOrphanResourceCountStr = `
   248  aws_instance.foo
   249  `
   250  
   251  const testTransformOrphanResourceCountBadStr = `
   252  aws_instance.foo[0] (orphan)
   253  aws_instance.foo[1] (orphan)
   254  `
   255  
   256  const testTransformOrphanResourceModulesStr = `
   257  aws_instance.foo
   258  module.child.aws_instance.web (orphan)
   259  `
   260  
   261  func testOrphanResourceConcreteFunc(a *NodeAbstractResource) dag.Vertex {
   262  	return &testOrphanResourceConcrete{a}
   263  }
   264  
   265  type testOrphanResourceConcrete struct {
   266  	*NodeAbstractResource
   267  }
   268  
   269  func (n *testOrphanResourceConcrete) Name() string {
   270  	return fmt.Sprintf("%s (orphan)", n.NodeAbstractResource.Name())
   271  }