github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/transform_provisioner_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform-plugin-sdk/internal/addrs"
     8  	"github.com/hashicorp/terraform-plugin-sdk/internal/dag"
     9  	"github.com/hashicorp/terraform-plugin-sdk/internal/states"
    10  )
    11  
    12  func TestMissingProvisionerTransformer(t *testing.T) {
    13  	mod := testModule(t, "transform-provisioner-basic")
    14  
    15  	g := Graph{Path: addrs.RootModuleInstance}
    16  	{
    17  		tf := &ConfigTransformer{Config: mod}
    18  		if err := tf.Transform(&g); err != nil {
    19  			t.Fatalf("err: %s", err)
    20  		}
    21  	}
    22  
    23  	{
    24  		transform := &AttachResourceConfigTransformer{Config: mod}
    25  		if err := transform.Transform(&g); err != nil {
    26  			t.Fatalf("err: %s", err)
    27  		}
    28  	}
    29  
    30  	{
    31  		transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}}
    32  		if err := transform.Transform(&g); err != nil {
    33  			t.Fatalf("err: %s", err)
    34  		}
    35  	}
    36  
    37  	{
    38  		transform := &ProvisionerTransformer{}
    39  		if err := transform.Transform(&g); err != nil {
    40  			t.Fatalf("err: %s", err)
    41  		}
    42  	}
    43  
    44  	actual := strings.TrimSpace(g.String())
    45  	expected := strings.TrimSpace(testTransformMissingProvisionerBasicStr)
    46  	if actual != expected {
    47  		t.Fatalf("bad:\n\n%s", actual)
    48  	}
    49  }
    50  
    51  func TestMissingProvisionerTransformer_module(t *testing.T) {
    52  	mod := testModule(t, "transform-provisioner-module")
    53  
    54  	g := Graph{Path: addrs.RootModuleInstance}
    55  	{
    56  		concreteResource := func(a *NodeAbstractResourceInstance) dag.Vertex {
    57  			return a
    58  		}
    59  
    60  		state := states.BuildState(func(s *states.SyncState) {
    61  			s.SetResourceInstanceCurrent(
    62  				addrs.Resource{
    63  					Mode: addrs.ManagedResourceMode,
    64  					Type: "aws_instance",
    65  					Name: "foo",
    66  				}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
    67  				&states.ResourceInstanceObjectSrc{
    68  					AttrsFlat: map[string]string{
    69  						"id": "foo",
    70  					},
    71  					Status: states.ObjectReady,
    72  				},
    73  				addrs.ProviderConfig{
    74  					Type: "aws",
    75  				}.Absolute(addrs.RootModuleInstance),
    76  			)
    77  			s.SetResourceInstanceCurrent(
    78  				addrs.Resource{
    79  					Mode: addrs.ManagedResourceMode,
    80  					Type: "aws_instance",
    81  					Name: "foo",
    82  				}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance.Child("child", addrs.NoKey)),
    83  				&states.ResourceInstanceObjectSrc{
    84  					AttrsFlat: map[string]string{
    85  						"id": "foo",
    86  					},
    87  					Status: states.ObjectReady,
    88  				},
    89  				addrs.ProviderConfig{
    90  					Type: "aws",
    91  				}.Absolute(addrs.RootModuleInstance),
    92  			)
    93  		})
    94  
    95  		tf := &StateTransformer{
    96  			ConcreteCurrent: concreteResource,
    97  			State:           state,
    98  		}
    99  		if err := tf.Transform(&g); err != nil {
   100  			t.Fatalf("err: %s", err)
   101  		}
   102  		t.Logf("graph after StateTransformer:\n%s", g.StringWithNodeTypes())
   103  	}
   104  
   105  	{
   106  		transform := &AttachResourceConfigTransformer{Config: mod}
   107  		if err := transform.Transform(&g); err != nil {
   108  			t.Fatalf("err: %s", err)
   109  		}
   110  	}
   111  
   112  	{
   113  		transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}}
   114  		if err := transform.Transform(&g); err != nil {
   115  			t.Fatalf("err: %s", err)
   116  		}
   117  		t.Logf("graph after MissingProvisionerTransformer:\n%s", g.StringWithNodeTypes())
   118  	}
   119  
   120  	{
   121  		transform := &ProvisionerTransformer{}
   122  		if err := transform.Transform(&g); err != nil {
   123  			t.Fatalf("err: %s", err)
   124  		}
   125  		t.Logf("graph after ProvisionerTransformer:\n%s", g.StringWithNodeTypes())
   126  	}
   127  
   128  	actual := strings.TrimSpace(g.String())
   129  	expected := strings.TrimSpace(testTransformMissingProvisionerModuleStr)
   130  	if actual != expected {
   131  		t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
   132  	}
   133  }
   134  
   135  func TestCloseProvisionerTransformer(t *testing.T) {
   136  	mod := testModule(t, "transform-provisioner-basic")
   137  
   138  	g := Graph{Path: addrs.RootModuleInstance}
   139  	{
   140  		tf := &ConfigTransformer{Config: mod}
   141  		if err := tf.Transform(&g); err != nil {
   142  			t.Fatalf("err: %s", err)
   143  		}
   144  	}
   145  
   146  	{
   147  		transform := &AttachResourceConfigTransformer{Config: mod}
   148  		if err := transform.Transform(&g); err != nil {
   149  			t.Fatalf("err: %s", err)
   150  		}
   151  	}
   152  
   153  	{
   154  		transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}}
   155  		if err := transform.Transform(&g); err != nil {
   156  			t.Fatalf("err: %s", err)
   157  		}
   158  	}
   159  
   160  	{
   161  		transform := &ProvisionerTransformer{}
   162  		if err := transform.Transform(&g); err != nil {
   163  			t.Fatalf("err: %s", err)
   164  		}
   165  	}
   166  
   167  	{
   168  		transform := &CloseProvisionerTransformer{}
   169  		if err := transform.Transform(&g); err != nil {
   170  			t.Fatalf("err: %s", err)
   171  		}
   172  	}
   173  
   174  	actual := strings.TrimSpace(g.String())
   175  	expected := strings.TrimSpace(testTransformCloseProvisionerBasicStr)
   176  	if actual != expected {
   177  		t.Fatalf("bad:\n\n%s", actual)
   178  	}
   179  }
   180  
   181  const testTransformMissingProvisionerBasicStr = `
   182  aws_instance.web
   183    provisioner.shell
   184  provisioner.shell
   185  `
   186  
   187  const testTransformMissingProvisionerModuleStr = `
   188  aws_instance.foo
   189    provisioner.shell
   190  module.child.aws_instance.foo
   191    module.child.provisioner.shell
   192  module.child.provisioner.shell
   193  provisioner.shell
   194  `
   195  
   196  const testTransformCloseProvisionerBasicStr = `
   197  aws_instance.web
   198    provisioner.shell
   199  provisioner.shell
   200  provisioner.shell (close)
   201    aws_instance.web
   202  `