github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/terraform/transform_provider_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/dag"
     8  )
     9  
    10  func TestProviderTransformer(t *testing.T) {
    11  	mod := testModule(t, "transform-provider-basic")
    12  
    13  	g := Graph{Path: RootModulePath}
    14  	{
    15  		tf := &ConfigTransformerOld{Module: mod}
    16  		if err := tf.Transform(&g); err != nil {
    17  			t.Fatalf("err: %s", err)
    18  		}
    19  	}
    20  
    21  	transform := &ProviderTransformer{}
    22  	if err := transform.Transform(&g); err != nil {
    23  		t.Fatalf("err: %s", err)
    24  	}
    25  
    26  	actual := strings.TrimSpace(g.String())
    27  	expected := strings.TrimSpace(testTransformProviderBasicStr)
    28  	if actual != expected {
    29  		t.Fatalf("bad:\n\n%s", actual)
    30  	}
    31  }
    32  
    33  func TestProviderTransformer_moduleChild(t *testing.T) {
    34  	g := Graph{Path: RootModulePath}
    35  
    36  	{
    37  		tf := &ImportStateTransformer{
    38  			Targets: []*ImportTarget{
    39  				&ImportTarget{
    40  					Addr: "module.moo.foo_instance.qux",
    41  					ID:   "bar",
    42  				},
    43  			},
    44  		}
    45  		if err := tf.Transform(&g); err != nil {
    46  			t.Fatalf("err: %s", err)
    47  		}
    48  	}
    49  
    50  	{
    51  		tf := &MissingProviderTransformer{Providers: []string{"foo", "bar"}}
    52  		if err := tf.Transform(&g); err != nil {
    53  			t.Fatalf("err: %s", err)
    54  		}
    55  	}
    56  
    57  	{
    58  		tf := &ProviderTransformer{}
    59  		if err := tf.Transform(&g); err != nil {
    60  			t.Fatalf("err: %s", err)
    61  		}
    62  	}
    63  
    64  	actual := strings.TrimSpace(g.String())
    65  	expected := strings.TrimSpace(testTransformProviderModuleChildStr)
    66  	if actual != expected {
    67  		t.Fatalf("bad:\n\n%s", actual)
    68  	}
    69  }
    70  
    71  func TestCloseProviderTransformer(t *testing.T) {
    72  	mod := testModule(t, "transform-provider-basic")
    73  
    74  	g := Graph{Path: RootModulePath}
    75  	{
    76  		tf := &ConfigTransformerOld{Module: mod}
    77  		if err := tf.Transform(&g); err != nil {
    78  			t.Fatalf("err: %s", err)
    79  		}
    80  	}
    81  
    82  	{
    83  		transform := &ProviderTransformer{}
    84  		if err := transform.Transform(&g); err != nil {
    85  			t.Fatalf("err: %s", err)
    86  		}
    87  	}
    88  
    89  	{
    90  		transform := &CloseProviderTransformer{}
    91  		if err := transform.Transform(&g); err != nil {
    92  			t.Fatalf("err: %s", err)
    93  		}
    94  	}
    95  
    96  	actual := strings.TrimSpace(g.String())
    97  	expected := strings.TrimSpace(testTransformCloseProviderBasicStr)
    98  	if actual != expected {
    99  		t.Fatalf("bad:\n\n%s", actual)
   100  	}
   101  }
   102  
   103  func TestCloseProviderTransformer_withTargets(t *testing.T) {
   104  	mod := testModule(t, "transform-provider-basic")
   105  
   106  	g := Graph{Path: RootModulePath}
   107  	transforms := []GraphTransformer{
   108  		&ConfigTransformerOld{Module: mod},
   109  		&ProviderTransformer{},
   110  		&CloseProviderTransformer{},
   111  		&TargetsTransformer{
   112  			Targets: []string{"something.else"},
   113  		},
   114  	}
   115  
   116  	for _, tr := range transforms {
   117  		if err := tr.Transform(&g); err != nil {
   118  			t.Fatalf("err: %s", err)
   119  		}
   120  	}
   121  
   122  	actual := strings.TrimSpace(g.String())
   123  	expected := strings.TrimSpace(`
   124  provider.aws
   125  provider.aws (close)
   126    provider.aws
   127  	`)
   128  	if actual != expected {
   129  		t.Fatalf("expected:%s\n\ngot:\n\n%s", expected, actual)
   130  	}
   131  }
   132  
   133  func TestMissingProviderTransformer(t *testing.T) {
   134  	mod := testModule(t, "transform-provider-missing")
   135  
   136  	g := Graph{Path: RootModulePath}
   137  	{
   138  		tf := &ConfigTransformerOld{Module: mod}
   139  		if err := tf.Transform(&g); err != nil {
   140  			t.Fatalf("err: %s", err)
   141  		}
   142  	}
   143  
   144  	{
   145  		transform := &MissingProviderTransformer{Providers: []string{"foo", "bar"}}
   146  		if err := transform.Transform(&g); err != nil {
   147  			t.Fatalf("err: %s", err)
   148  		}
   149  	}
   150  
   151  	{
   152  		transform := &CloseProviderTransformer{}
   153  		if err := transform.Transform(&g); err != nil {
   154  			t.Fatalf("err: %s", err)
   155  		}
   156  	}
   157  
   158  	actual := strings.TrimSpace(g.String())
   159  	expected := strings.TrimSpace(testTransformMissingProviderBasicStr)
   160  	if actual != expected {
   161  		t.Fatalf("bad:\n\n%s", actual)
   162  	}
   163  }
   164  
   165  func TestMissingProviderTransformer_moduleChild(t *testing.T) {
   166  	g := Graph{Path: RootModulePath}
   167  
   168  	// We use the import state transformer since at the time of writing
   169  	// this test it is the first and only transformer that will introduce
   170  	// multiple module-path nodes at a single go.
   171  	{
   172  		tf := &ImportStateTransformer{
   173  			Targets: []*ImportTarget{
   174  				&ImportTarget{
   175  					Addr: "module.moo.foo_instance.qux",
   176  					ID:   "bar",
   177  				},
   178  			},
   179  		}
   180  		if err := tf.Transform(&g); err != nil {
   181  			t.Fatalf("err: %s", err)
   182  		}
   183  	}
   184  
   185  	{
   186  		tf := &MissingProviderTransformer{Providers: []string{"foo", "bar"}}
   187  		if err := tf.Transform(&g); err != nil {
   188  			t.Fatalf("err: %s", err)
   189  		}
   190  	}
   191  
   192  	actual := strings.TrimSpace(g.String())
   193  	expected := strings.TrimSpace(testTransformMissingProviderModuleChildStr)
   194  	if actual != expected {
   195  		t.Fatalf("bad:\n\n%s", actual)
   196  	}
   197  }
   198  
   199  func TestMissingProviderTransformer_moduleGrandchild(t *testing.T) {
   200  	g := Graph{Path: RootModulePath}
   201  
   202  	// We use the import state transformer since at the time of writing
   203  	// this test it is the first and only transformer that will introduce
   204  	// multiple module-path nodes at a single go.
   205  	{
   206  		tf := &ImportStateTransformer{
   207  			Targets: []*ImportTarget{
   208  				&ImportTarget{
   209  					Addr: "module.a.module.b.foo_instance.qux",
   210  					ID:   "bar",
   211  				},
   212  			},
   213  		}
   214  		if err := tf.Transform(&g); err != nil {
   215  			t.Fatalf("err: %s", err)
   216  		}
   217  	}
   218  
   219  	{
   220  		tf := &MissingProviderTransformer{Providers: []string{"foo", "bar"}}
   221  		if err := tf.Transform(&g); err != nil {
   222  			t.Fatalf("err: %s", err)
   223  		}
   224  	}
   225  
   226  	actual := strings.TrimSpace(g.String())
   227  	expected := strings.TrimSpace(testTransformMissingProviderModuleGrandchildStr)
   228  	if actual != expected {
   229  		t.Fatalf("bad:\n\n%s", actual)
   230  	}
   231  }
   232  
   233  func TestParentProviderTransformer(t *testing.T) {
   234  	g := Graph{Path: RootModulePath}
   235  
   236  	// Introduce a cihld module
   237  	{
   238  		tf := &ImportStateTransformer{
   239  			Targets: []*ImportTarget{
   240  				&ImportTarget{
   241  					Addr: "module.moo.foo_instance.qux",
   242  					ID:   "bar",
   243  				},
   244  			},
   245  		}
   246  		if err := tf.Transform(&g); err != nil {
   247  			t.Fatalf("err: %s", err)
   248  		}
   249  	}
   250  
   251  	// Add the missing modules
   252  	{
   253  		tf := &MissingProviderTransformer{Providers: []string{"foo", "bar"}}
   254  		if err := tf.Transform(&g); err != nil {
   255  			t.Fatalf("err: %s", err)
   256  		}
   257  	}
   258  
   259  	// Connect parents
   260  	{
   261  		tf := &ParentProviderTransformer{}
   262  		if err := tf.Transform(&g); err != nil {
   263  			t.Fatalf("err: %s", err)
   264  		}
   265  	}
   266  
   267  	actual := strings.TrimSpace(g.String())
   268  	expected := strings.TrimSpace(testTransformParentProviderStr)
   269  	if actual != expected {
   270  		t.Fatalf("bad:\n\n%s", actual)
   271  	}
   272  }
   273  
   274  func TestParentProviderTransformer_moduleGrandchild(t *testing.T) {
   275  	g := Graph{Path: RootModulePath}
   276  
   277  	// We use the import state transformer since at the time of writing
   278  	// this test it is the first and only transformer that will introduce
   279  	// multiple module-path nodes at a single go.
   280  	{
   281  		tf := &ImportStateTransformer{
   282  			Targets: []*ImportTarget{
   283  				&ImportTarget{
   284  					Addr: "module.a.module.b.foo_instance.qux",
   285  					ID:   "bar",
   286  				},
   287  			},
   288  		}
   289  		if err := tf.Transform(&g); err != nil {
   290  			t.Fatalf("err: %s", err)
   291  		}
   292  	}
   293  
   294  	{
   295  		tf := &MissingProviderTransformer{Providers: []string{"foo", "bar"}}
   296  		if err := tf.Transform(&g); err != nil {
   297  			t.Fatalf("err: %s", err)
   298  		}
   299  	}
   300  
   301  	// Connect parents
   302  	{
   303  		tf := &ParentProviderTransformer{}
   304  		if err := tf.Transform(&g); err != nil {
   305  			t.Fatalf("err: %s", err)
   306  		}
   307  	}
   308  
   309  	actual := strings.TrimSpace(g.String())
   310  	expected := strings.TrimSpace(testTransformParentProviderModuleGrandchildStr)
   311  	if actual != expected {
   312  		t.Fatalf("bad:\n\n%s", actual)
   313  	}
   314  }
   315  
   316  func TestPruneProviderTransformer(t *testing.T) {
   317  	mod := testModule(t, "transform-provider-prune")
   318  
   319  	g := Graph{Path: RootModulePath}
   320  	{
   321  		tf := &ConfigTransformerOld{Module: mod}
   322  		if err := tf.Transform(&g); err != nil {
   323  			t.Fatalf("err: %s", err)
   324  		}
   325  	}
   326  
   327  	{
   328  		transform := &MissingProviderTransformer{Providers: []string{"foo"}}
   329  		if err := transform.Transform(&g); err != nil {
   330  			t.Fatalf("err: %s", err)
   331  		}
   332  	}
   333  
   334  	{
   335  		transform := &ProviderTransformer{}
   336  		if err := transform.Transform(&g); err != nil {
   337  			t.Fatalf("err: %s", err)
   338  		}
   339  	}
   340  
   341  	{
   342  		transform := &CloseProviderTransformer{}
   343  		if err := transform.Transform(&g); err != nil {
   344  			t.Fatalf("err: %s", err)
   345  		}
   346  	}
   347  
   348  	{
   349  		transform := &PruneProviderTransformer{}
   350  		if err := transform.Transform(&g); err != nil {
   351  			t.Fatalf("err: %s", err)
   352  		}
   353  	}
   354  
   355  	actual := strings.TrimSpace(g.String())
   356  	expected := strings.TrimSpace(testTransformPruneProviderBasicStr)
   357  	if actual != expected {
   358  		t.Fatalf("bad:\n\n%s", actual)
   359  	}
   360  }
   361  
   362  func TestDisableProviderTransformer(t *testing.T) {
   363  	mod := testModule(t, "transform-provider-disable")
   364  
   365  	g := Graph{Path: RootModulePath}
   366  	transforms := []GraphTransformer{
   367  		&ConfigTransformerOld{Module: mod},
   368  		&MissingProviderTransformer{Providers: []string{"aws"}},
   369  		&ProviderTransformer{},
   370  		&DisableProviderTransformerOld{},
   371  		&CloseProviderTransformer{},
   372  		&PruneProviderTransformer{},
   373  	}
   374  
   375  	for _, tr := range transforms {
   376  		if err := tr.Transform(&g); err != nil {
   377  			t.Fatalf("err: %s", err)
   378  		}
   379  	}
   380  
   381  	actual := strings.TrimSpace(g.String())
   382  	expected := strings.TrimSpace(testTransformDisableProviderBasicStr)
   383  	if actual != expected {
   384  		t.Fatalf("expected:\n%s\n\ngot:\n%s\n", expected, actual)
   385  	}
   386  }
   387  
   388  func TestDisableProviderTransformer_keep(t *testing.T) {
   389  	mod := testModule(t, "transform-provider-disable-keep")
   390  
   391  	g := Graph{Path: RootModulePath}
   392  	transforms := []GraphTransformer{
   393  		&ConfigTransformerOld{Module: mod},
   394  		&MissingProviderTransformer{Providers: []string{"aws"}},
   395  		&ProviderTransformer{},
   396  		&DisableProviderTransformerOld{},
   397  		&CloseProviderTransformer{},
   398  		&PruneProviderTransformer{},
   399  	}
   400  
   401  	for _, tr := range transforms {
   402  		if err := tr.Transform(&g); err != nil {
   403  			t.Fatalf("err: %s", err)
   404  		}
   405  	}
   406  
   407  	actual := strings.TrimSpace(g.String())
   408  	expected := strings.TrimSpace(testTransformDisableProviderKeepStr)
   409  	if actual != expected {
   410  		t.Fatalf("expected:\n%s\n\ngot:\n%s\n", expected, actual)
   411  	}
   412  }
   413  
   414  func TestGraphNodeProvider_impl(t *testing.T) {
   415  	var _ dag.Vertex = new(graphNodeProvider)
   416  	var _ dag.NamedVertex = new(graphNodeProvider)
   417  	var _ GraphNodeProvider = new(graphNodeProvider)
   418  }
   419  
   420  func TestGraphNodeProvider_ProviderName(t *testing.T) {
   421  	n := &graphNodeProvider{ProviderNameValue: "foo"}
   422  	if v := n.ProviderName(); v != "foo" {
   423  		t.Fatalf("bad: %#v", v)
   424  	}
   425  }
   426  
   427  const testTransformProviderBasicStr = `
   428  aws_instance.web
   429    provider.aws
   430  provider.aws
   431  `
   432  
   433  const testTransformCloseProviderBasicStr = `
   434  aws_instance.web
   435    provider.aws
   436  provider.aws
   437  provider.aws (close)
   438    aws_instance.web
   439    provider.aws
   440  `
   441  
   442  const testTransformMissingProviderBasicStr = `
   443  aws_instance.web
   444  foo_instance.web
   445  provider.aws
   446  provider.aws (close)
   447    aws_instance.web
   448    provider.aws
   449  provider.foo
   450  provider.foo (close)
   451    foo_instance.web
   452    provider.foo
   453  `
   454  
   455  const testTransformMissingProviderModuleChildStr = `
   456  module.moo.foo_instance.qux (import id: bar)
   457  module.moo.provider.foo
   458  provider.foo
   459  `
   460  
   461  const testTransformMissingProviderModuleGrandchildStr = `
   462  module.a.module.b.foo_instance.qux (import id: bar)
   463  module.a.module.b.provider.foo
   464  module.a.provider.foo
   465  provider.foo
   466  `
   467  
   468  const testTransformParentProviderStr = `
   469  module.moo.foo_instance.qux (import id: bar)
   470  module.moo.provider.foo
   471    provider.foo
   472  provider.foo
   473  `
   474  
   475  const testTransformParentProviderModuleGrandchildStr = `
   476  module.a.module.b.foo_instance.qux (import id: bar)
   477  module.a.module.b.provider.foo
   478    module.a.provider.foo
   479  module.a.provider.foo
   480    provider.foo
   481  provider.foo
   482  `
   483  
   484  const testTransformProviderModuleChildStr = `
   485  module.moo.foo_instance.qux (import id: bar)
   486    module.moo.provider.foo
   487  module.moo.provider.foo
   488  provider.foo
   489  `
   490  
   491  const testTransformPruneProviderBasicStr = `
   492  foo_instance.web
   493    provider.foo
   494  provider.foo
   495  provider.foo (close)
   496    foo_instance.web
   497    provider.foo
   498  `
   499  
   500  const testTransformDisableProviderBasicStr = `
   501  module.child
   502    provider.aws (disabled)
   503    var.foo
   504  provider.aws (close)
   505    module.child
   506    provider.aws (disabled)
   507  provider.aws (disabled)
   508  var.foo
   509  `
   510  
   511  const testTransformDisableProviderKeepStr = `
   512  aws_instance.foo
   513    provider.aws
   514  module.child
   515    provider.aws
   516    var.foo
   517  provider.aws
   518  provider.aws (close)
   519    aws_instance.foo
   520    module.child
   521    provider.aws
   522  var.foo
   523  `