github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/terraform/transform_orphan_count_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestOrphanResourceCountTransformer(t *testing.T) {
     9  	addr, err := parseResourceAddressInternal("aws_instance.foo")
    10  	if err != nil {
    11  		t.Fatalf("err: %s", err)
    12  	}
    13  
    14  	state := &State{
    15  		Modules: []*ModuleState{
    16  			&ModuleState{
    17  				Path: RootModulePath,
    18  				Resources: map[string]*ResourceState{
    19  					"aws_instance.web": &ResourceState{
    20  						Type: "aws_instance",
    21  						Primary: &InstanceState{
    22  							ID: "foo",
    23  						},
    24  					},
    25  
    26  					"aws_instance.foo": &ResourceState{
    27  						Type: "aws_instance",
    28  						Primary: &InstanceState{
    29  							ID: "foo",
    30  						},
    31  					},
    32  
    33  					"aws_instance.foo.2": &ResourceState{
    34  						Type: "aws_instance",
    35  						Primary: &InstanceState{
    36  							ID: "foo",
    37  						},
    38  					},
    39  				},
    40  			},
    41  		},
    42  	}
    43  
    44  	g := Graph{Path: RootModulePath}
    45  
    46  	{
    47  		tf := &OrphanResourceCountTransformer{
    48  			Concrete: testOrphanResourceConcreteFunc,
    49  			Count:    1,
    50  			Addr:     addr,
    51  			State:    state,
    52  		}
    53  		if err := tf.Transform(&g); err != nil {
    54  			t.Fatalf("err: %s", err)
    55  		}
    56  	}
    57  
    58  	actual := strings.TrimSpace(g.String())
    59  	expected := strings.TrimSpace(testTransformOrphanResourceCountBasicStr)
    60  	if actual != expected {
    61  		t.Fatalf("bad:\n\n%s", actual)
    62  	}
    63  }
    64  
    65  func TestOrphanResourceCountTransformer_zero(t *testing.T) {
    66  	addr, err := parseResourceAddressInternal("aws_instance.foo")
    67  	if err != nil {
    68  		t.Fatalf("err: %s", err)
    69  	}
    70  
    71  	state := &State{
    72  		Modules: []*ModuleState{
    73  			&ModuleState{
    74  				Path: RootModulePath,
    75  				Resources: map[string]*ResourceState{
    76  					"aws_instance.web": &ResourceState{
    77  						Type: "aws_instance",
    78  						Primary: &InstanceState{
    79  							ID: "foo",
    80  						},
    81  					},
    82  
    83  					"aws_instance.foo": &ResourceState{
    84  						Type: "aws_instance",
    85  						Primary: &InstanceState{
    86  							ID: "foo",
    87  						},
    88  					},
    89  
    90  					"aws_instance.foo.2": &ResourceState{
    91  						Type: "aws_instance",
    92  						Primary: &InstanceState{
    93  							ID: "foo",
    94  						},
    95  					},
    96  				},
    97  			},
    98  		},
    99  	}
   100  
   101  	g := Graph{Path: RootModulePath}
   102  
   103  	{
   104  		tf := &OrphanResourceCountTransformer{
   105  			Concrete: testOrphanResourceConcreteFunc,
   106  			Count:    0,
   107  			Addr:     addr,
   108  			State:    state,
   109  		}
   110  		if err := tf.Transform(&g); err != nil {
   111  			t.Fatalf("err: %s", err)
   112  		}
   113  	}
   114  
   115  	actual := strings.TrimSpace(g.String())
   116  	expected := strings.TrimSpace(testTransformOrphanResourceCountZeroStr)
   117  	if actual != expected {
   118  		t.Fatalf("bad:\n\n%s", actual)
   119  	}
   120  }
   121  
   122  func TestOrphanResourceCountTransformer_oneNoIndex(t *testing.T) {
   123  	addr, err := parseResourceAddressInternal("aws_instance.foo")
   124  	if err != nil {
   125  		t.Fatalf("err: %s", err)
   126  	}
   127  
   128  	state := &State{
   129  		Modules: []*ModuleState{
   130  			&ModuleState{
   131  				Path: RootModulePath,
   132  				Resources: map[string]*ResourceState{
   133  					"aws_instance.web": &ResourceState{
   134  						Type: "aws_instance",
   135  						Primary: &InstanceState{
   136  							ID: "foo",
   137  						},
   138  					},
   139  
   140  					"aws_instance.foo": &ResourceState{
   141  						Type: "aws_instance",
   142  						Primary: &InstanceState{
   143  							ID: "foo",
   144  						},
   145  					},
   146  
   147  					"aws_instance.foo.2": &ResourceState{
   148  						Type: "aws_instance",
   149  						Primary: &InstanceState{
   150  							ID: "foo",
   151  						},
   152  					},
   153  				},
   154  			},
   155  		},
   156  	}
   157  
   158  	g := Graph{Path: RootModulePath}
   159  
   160  	{
   161  		tf := &OrphanResourceCountTransformer{
   162  			Concrete: testOrphanResourceConcreteFunc,
   163  			Count:    1,
   164  			Addr:     addr,
   165  			State:    state,
   166  		}
   167  		if err := tf.Transform(&g); err != nil {
   168  			t.Fatalf("err: %s", err)
   169  		}
   170  	}
   171  
   172  	actual := strings.TrimSpace(g.String())
   173  	expected := strings.TrimSpace(testTransformOrphanResourceCountOneNoIndexStr)
   174  	if actual != expected {
   175  		t.Fatalf("bad:\n\n%s", actual)
   176  	}
   177  }
   178  
   179  func TestOrphanResourceCountTransformer_oneIndex(t *testing.T) {
   180  	addr, err := parseResourceAddressInternal("aws_instance.foo")
   181  	if err != nil {
   182  		t.Fatalf("err: %s", err)
   183  	}
   184  
   185  	state := &State{
   186  		Modules: []*ModuleState{
   187  			&ModuleState{
   188  				Path: RootModulePath,
   189  				Resources: map[string]*ResourceState{
   190  					"aws_instance.web": &ResourceState{
   191  						Type: "aws_instance",
   192  						Primary: &InstanceState{
   193  							ID: "foo",
   194  						},
   195  					},
   196  
   197  					"aws_instance.foo.0": &ResourceState{
   198  						Type: "aws_instance",
   199  						Primary: &InstanceState{
   200  							ID: "foo",
   201  						},
   202  					},
   203  
   204  					"aws_instance.foo.1": &ResourceState{
   205  						Type: "aws_instance",
   206  						Primary: &InstanceState{
   207  							ID: "foo",
   208  						},
   209  					},
   210  				},
   211  			},
   212  		},
   213  	}
   214  
   215  	g := Graph{Path: RootModulePath}
   216  
   217  	{
   218  		tf := &OrphanResourceCountTransformer{
   219  			Concrete: testOrphanResourceConcreteFunc,
   220  			Count:    1,
   221  			Addr:     addr,
   222  			State:    state,
   223  		}
   224  		if err := tf.Transform(&g); err != nil {
   225  			t.Fatalf("err: %s", err)
   226  		}
   227  	}
   228  
   229  	actual := strings.TrimSpace(g.String())
   230  	expected := strings.TrimSpace(testTransformOrphanResourceCountOneIndexStr)
   231  	if actual != expected {
   232  		t.Fatalf("bad:\n\n%s", actual)
   233  	}
   234  }
   235  
   236  func TestOrphanResourceCountTransformer_zeroAndNone(t *testing.T) {
   237  	addr, err := parseResourceAddressInternal("aws_instance.foo")
   238  	if err != nil {
   239  		t.Fatalf("err: %s", err)
   240  	}
   241  
   242  	state := &State{
   243  		Modules: []*ModuleState{
   244  			&ModuleState{
   245  				Path: RootModulePath,
   246  				Resources: map[string]*ResourceState{
   247  					"aws_instance.web": &ResourceState{
   248  						Type: "aws_instance",
   249  						Primary: &InstanceState{
   250  							ID: "foo",
   251  						},
   252  					},
   253  
   254  					"aws_instance.foo": &ResourceState{
   255  						Type: "aws_instance",
   256  						Primary: &InstanceState{
   257  							ID: "foo",
   258  						},
   259  					},
   260  
   261  					"aws_instance.foo.0": &ResourceState{
   262  						Type: "aws_instance",
   263  						Primary: &InstanceState{
   264  							ID: "foo",
   265  						},
   266  					},
   267  				},
   268  			},
   269  		},
   270  	}
   271  
   272  	g := Graph{Path: RootModulePath}
   273  
   274  	{
   275  		tf := &OrphanResourceCountTransformer{
   276  			Concrete: testOrphanResourceConcreteFunc,
   277  			Count:    1,
   278  			Addr:     addr,
   279  			State:    state,
   280  		}
   281  		if err := tf.Transform(&g); err != nil {
   282  			t.Fatalf("err: %s", err)
   283  		}
   284  	}
   285  
   286  	actual := strings.TrimSpace(g.String())
   287  	expected := strings.TrimSpace(testTransformOrphanResourceCountZeroAndNoneStr)
   288  	if actual != expected {
   289  		t.Fatalf("bad:\n\n%s", actual)
   290  	}
   291  }
   292  
   293  func TestOrphanResourceCountTransformer_zeroAndNoneCount(t *testing.T) {
   294  	addr, err := parseResourceAddressInternal("aws_instance.foo")
   295  	if err != nil {
   296  		t.Fatalf("err: %s", err)
   297  	}
   298  
   299  	state := &State{
   300  		Modules: []*ModuleState{
   301  			&ModuleState{
   302  				Path: RootModulePath,
   303  				Resources: map[string]*ResourceState{
   304  					"aws_instance.web": &ResourceState{
   305  						Type: "aws_instance",
   306  						Primary: &InstanceState{
   307  							ID: "foo",
   308  						},
   309  					},
   310  
   311  					"aws_instance.foo": &ResourceState{
   312  						Type: "aws_instance",
   313  						Primary: &InstanceState{
   314  							ID: "foo",
   315  						},
   316  					},
   317  
   318  					"aws_instance.foo.0": &ResourceState{
   319  						Type: "aws_instance",
   320  						Primary: &InstanceState{
   321  							ID: "foo",
   322  						},
   323  					},
   324  				},
   325  			},
   326  		},
   327  	}
   328  
   329  	g := Graph{Path: RootModulePath}
   330  
   331  	{
   332  		tf := &OrphanResourceCountTransformer{
   333  			Concrete: testOrphanResourceConcreteFunc,
   334  			Count:    2,
   335  			Addr:     addr,
   336  			State:    state,
   337  		}
   338  		if err := tf.Transform(&g); err != nil {
   339  			t.Fatalf("err: %s", err)
   340  		}
   341  	}
   342  
   343  	actual := strings.TrimSpace(g.String())
   344  	expected := strings.TrimSpace(testTransformOrphanResourceCountZeroAndNoneCountStr)
   345  	if actual != expected {
   346  		t.Fatalf("bad:\n\n%s", actual)
   347  	}
   348  }
   349  
   350  const testTransformOrphanResourceCountBasicStr = `
   351  aws_instance.foo[2] (orphan)
   352  `
   353  
   354  const testTransformOrphanResourceCountZeroStr = `
   355  aws_instance.foo (orphan)
   356  aws_instance.foo[2] (orphan)
   357  `
   358  
   359  const testTransformOrphanResourceCountOneNoIndexStr = `
   360  aws_instance.foo[2] (orphan)
   361  `
   362  
   363  const testTransformOrphanResourceCountOneIndexStr = `
   364  aws_instance.foo[1] (orphan)
   365  `
   366  
   367  const testTransformOrphanResourceCountZeroAndNoneStr = `
   368  aws_instance.foo[0] (orphan)
   369  `
   370  
   371  const testTransformOrphanResourceCountZeroAndNoneCountStr = `
   372  aws_instance.foo (orphan)
   373  `