github.com/federicobaldo/terraform@v0.6.15-0.20160323222747-b20f680cbf05/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 := &ConfigTransformer{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 TestCloseProviderTransformer(t *testing.T) {
    34  	mod := testModule(t, "transform-provider-basic")
    35  
    36  	g := Graph{Path: RootModulePath}
    37  	{
    38  		tf := &ConfigTransformer{Module: mod}
    39  		if err := tf.Transform(&g); err != nil {
    40  			t.Fatalf("err: %s", err)
    41  		}
    42  	}
    43  
    44  	{
    45  		transform := &ProviderTransformer{}
    46  		if err := transform.Transform(&g); err != nil {
    47  			t.Fatalf("err: %s", err)
    48  		}
    49  	}
    50  
    51  	{
    52  		transform := &CloseProviderTransformer{}
    53  		if err := transform.Transform(&g); err != nil {
    54  			t.Fatalf("err: %s", err)
    55  		}
    56  	}
    57  
    58  	actual := strings.TrimSpace(g.String())
    59  	expected := strings.TrimSpace(testTransformCloseProviderBasicStr)
    60  	if actual != expected {
    61  		t.Fatalf("bad:\n\n%s", actual)
    62  	}
    63  }
    64  
    65  func TestCloseProviderTransformer_withTargets(t *testing.T) {
    66  	mod := testModule(t, "transform-provider-basic")
    67  
    68  	g := Graph{Path: RootModulePath}
    69  	transforms := []GraphTransformer{
    70  		&ConfigTransformer{Module: mod},
    71  		&ProviderTransformer{},
    72  		&CloseProviderTransformer{},
    73  		&TargetsTransformer{
    74  			Targets: []string{"something.else"},
    75  		},
    76  	}
    77  
    78  	for _, tr := range transforms {
    79  		if err := tr.Transform(&g); err != nil {
    80  			t.Fatalf("err: %s", err)
    81  		}
    82  	}
    83  
    84  	actual := strings.TrimSpace(g.String())
    85  	expected := strings.TrimSpace(`
    86  provider.aws
    87  provider.aws (close)
    88    provider.aws
    89  	`)
    90  	if actual != expected {
    91  		t.Fatalf("expected:%s\n\ngot:\n\n%s", expected, actual)
    92  	}
    93  }
    94  
    95  func TestMissingProviderTransformer(t *testing.T) {
    96  	mod := testModule(t, "transform-provider-missing")
    97  
    98  	g := Graph{Path: RootModulePath}
    99  	{
   100  		tf := &ConfigTransformer{Module: mod}
   101  		if err := tf.Transform(&g); err != nil {
   102  			t.Fatalf("err: %s", err)
   103  		}
   104  	}
   105  
   106  	{
   107  		transform := &MissingProviderTransformer{Providers: []string{"foo", "bar"}}
   108  		if err := transform.Transform(&g); err != nil {
   109  			t.Fatalf("err: %s", err)
   110  		}
   111  	}
   112  
   113  	{
   114  		transform := &CloseProviderTransformer{}
   115  		if err := transform.Transform(&g); err != nil {
   116  			t.Fatalf("err: %s", err)
   117  		}
   118  	}
   119  
   120  	actual := strings.TrimSpace(g.String())
   121  	expected := strings.TrimSpace(testTransformMissingProviderBasicStr)
   122  	if actual != expected {
   123  		t.Fatalf("bad:\n\n%s", actual)
   124  	}
   125  }
   126  
   127  func TestPruneProviderTransformer(t *testing.T) {
   128  	mod := testModule(t, "transform-provider-prune")
   129  
   130  	g := Graph{Path: RootModulePath}
   131  	{
   132  		tf := &ConfigTransformer{Module: mod}
   133  		if err := tf.Transform(&g); err != nil {
   134  			t.Fatalf("err: %s", err)
   135  		}
   136  	}
   137  
   138  	{
   139  		transform := &MissingProviderTransformer{Providers: []string{"foo"}}
   140  		if err := transform.Transform(&g); err != nil {
   141  			t.Fatalf("err: %s", err)
   142  		}
   143  	}
   144  
   145  	{
   146  		transform := &ProviderTransformer{}
   147  		if err := transform.Transform(&g); err != nil {
   148  			t.Fatalf("err: %s", err)
   149  		}
   150  	}
   151  
   152  	{
   153  		transform := &CloseProviderTransformer{}
   154  		if err := transform.Transform(&g); err != nil {
   155  			t.Fatalf("err: %s", err)
   156  		}
   157  	}
   158  
   159  	{
   160  		transform := &PruneProviderTransformer{}
   161  		if err := transform.Transform(&g); err != nil {
   162  			t.Fatalf("err: %s", err)
   163  		}
   164  	}
   165  
   166  	actual := strings.TrimSpace(g.String())
   167  	expected := strings.TrimSpace(testTransformPruneProviderBasicStr)
   168  	if actual != expected {
   169  		t.Fatalf("bad:\n\n%s", actual)
   170  	}
   171  }
   172  
   173  func TestDisableProviderTransformer(t *testing.T) {
   174  	mod := testModule(t, "transform-provider-disable")
   175  
   176  	g := Graph{Path: RootModulePath}
   177  	transforms := []GraphTransformer{
   178  		&ConfigTransformer{Module: mod},
   179  		&MissingProviderTransformer{Providers: []string{"aws"}},
   180  		&ProviderTransformer{},
   181  		&DisableProviderTransformer{},
   182  		&CloseProviderTransformer{},
   183  		&PruneProviderTransformer{},
   184  	}
   185  
   186  	for _, tr := range transforms {
   187  		if err := tr.Transform(&g); err != nil {
   188  			t.Fatalf("err: %s", err)
   189  		}
   190  	}
   191  
   192  	actual := strings.TrimSpace(g.String())
   193  	expected := strings.TrimSpace(testTransformDisableProviderBasicStr)
   194  	if actual != expected {
   195  		t.Fatalf("expected:\n%s\n\ngot:\n%s\n", expected, actual)
   196  	}
   197  }
   198  
   199  func TestDisableProviderTransformer_keep(t *testing.T) {
   200  	mod := testModule(t, "transform-provider-disable-keep")
   201  
   202  	g := Graph{Path: RootModulePath}
   203  	transforms := []GraphTransformer{
   204  		&ConfigTransformer{Module: mod},
   205  		&MissingProviderTransformer{Providers: []string{"aws"}},
   206  		&ProviderTransformer{},
   207  		&DisableProviderTransformer{},
   208  		&CloseProviderTransformer{},
   209  		&PruneProviderTransformer{},
   210  	}
   211  
   212  	for _, tr := range transforms {
   213  		if err := tr.Transform(&g); err != nil {
   214  			t.Fatalf("err: %s", err)
   215  		}
   216  	}
   217  
   218  	actual := strings.TrimSpace(g.String())
   219  	expected := strings.TrimSpace(testTransformDisableProviderKeepStr)
   220  	if actual != expected {
   221  		t.Fatalf("expected:\n%s\n\ngot:\n%s\n", expected, actual)
   222  	}
   223  }
   224  
   225  func TestGraphNodeProvider_impl(t *testing.T) {
   226  	var _ dag.Vertex = new(graphNodeProvider)
   227  	var _ dag.NamedVertex = new(graphNodeProvider)
   228  	var _ GraphNodeProvider = new(graphNodeProvider)
   229  }
   230  
   231  func TestGraphNodeProvider_ProviderName(t *testing.T) {
   232  	n := &graphNodeProvider{ProviderNameValue: "foo"}
   233  	if v := n.ProviderName(); v != "foo" {
   234  		t.Fatalf("bad: %#v", v)
   235  	}
   236  }
   237  
   238  const testTransformProviderBasicStr = `
   239  aws_instance.web
   240    provider.aws
   241  provider.aws
   242  `
   243  
   244  const testTransformCloseProviderBasicStr = `
   245  aws_instance.web
   246    provider.aws
   247  provider.aws
   248  provider.aws (close)
   249    aws_instance.web
   250    provider.aws
   251  `
   252  
   253  const testTransformMissingProviderBasicStr = `
   254  aws_instance.web
   255  foo_instance.web
   256  provider.aws
   257  provider.aws (close)
   258    aws_instance.web
   259    provider.aws
   260  provider.foo
   261  provider.foo (close)
   262    foo_instance.web
   263    provider.foo
   264  `
   265  
   266  const testTransformPruneProviderBasicStr = `
   267  foo_instance.web
   268    provider.foo
   269  provider.foo
   270  provider.foo (close)
   271    foo_instance.web
   272    provider.foo
   273  `
   274  
   275  const testTransformDisableProviderBasicStr = `
   276  module.child
   277    provider.aws (disabled)
   278    var.foo
   279  provider.aws (close)
   280    module.child
   281    provider.aws (disabled)
   282  provider.aws (disabled)
   283  var.foo
   284  `
   285  
   286  const testTransformDisableProviderKeepStr = `
   287  aws_instance.foo
   288    provider.aws
   289  module.child
   290    provider.aws
   291    var.foo
   292  provider.aws
   293  provider.aws (close)
   294    aws_instance.foo
   295    module.child
   296    provider.aws
   297  var.foo
   298  `