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

     1  package terraform
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/zclconf/go-cty/cty"
     7  
     8  	"github.com/hashicorp/terraform-plugin-sdk/internal/addrs"
     9  )
    10  
    11  func TestNodeRefreshableDataResourceDynamicExpand_scaleOut(t *testing.T) {
    12  	m := testModule(t, "refresh-data-scale-inout")
    13  
    14  	state := MustShimLegacyState(&State{
    15  		Modules: []*ModuleState{
    16  			{
    17  				Path: rootModulePath,
    18  				Resources: map[string]*ResourceState{
    19  					"data.aws_instance.foo.0": {
    20  						Type: "aws_instance",
    21  						Deposed: []*InstanceState{
    22  							{
    23  								ID: "foo",
    24  							},
    25  						},
    26  					},
    27  					"data.aws_instance.foo.1": {
    28  						Type: "aws_instance",
    29  						Deposed: []*InstanceState{
    30  							{
    31  								ID: "bar",
    32  							},
    33  						},
    34  					},
    35  				},
    36  			},
    37  		},
    38  	})
    39  
    40  	n := &NodeRefreshableDataResource{
    41  		NodeAbstractResource: &NodeAbstractResource{
    42  			Addr: addrs.RootModuleInstance.Resource(
    43  				addrs.DataResourceMode,
    44  				"aws_instance",
    45  				"foo",
    46  			),
    47  			Config: m.Module.DataResources["data.aws_instance.foo"],
    48  		},
    49  	}
    50  
    51  	g, err := n.DynamicExpand(&MockEvalContext{
    52  		PathPath:   addrs.RootModuleInstance,
    53  		StateState: state.SyncWrapper(),
    54  
    55  		// DynamicExpand will call EvaluateExpr to evaluate the "count"
    56  		// expression, which is just a literal number 3 in the fixture config
    57  		// and so we'll just hard-code this here too.
    58  		EvaluateExprResult: cty.NumberIntVal(3),
    59  	})
    60  	if err != nil {
    61  		t.Fatalf("error on DynamicExpand: %s", err)
    62  	}
    63  
    64  	actual := g.StringWithNodeTypes()
    65  	expected := `data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance
    66  data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance
    67  data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance
    68  root - terraform.graphNodeRoot
    69    data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance
    70    data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance
    71    data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance
    72  `
    73  	if expected != actual {
    74  		t.Fatalf("Expected:\n%s\nGot:\n%s", expected, actual)
    75  	}
    76  }
    77  
    78  func TestNodeRefreshableDataResourceDynamicExpand_scaleIn(t *testing.T) {
    79  	m := testModule(t, "refresh-data-scale-inout")
    80  
    81  	state := MustShimLegacyState(&State{
    82  		Modules: []*ModuleState{
    83  			{
    84  				Path: rootModulePath,
    85  				Resources: map[string]*ResourceState{
    86  					"data.aws_instance.foo.0": {
    87  						Type: "aws_instance",
    88  						Deposed: []*InstanceState{
    89  							{
    90  								ID: "foo",
    91  							},
    92  						},
    93  					},
    94  					"data.aws_instance.foo.1": {
    95  						Type: "aws_instance",
    96  						Deposed: []*InstanceState{
    97  							{
    98  								ID: "bar",
    99  							},
   100  						},
   101  					},
   102  					"data.aws_instance.foo.2": {
   103  						Type: "aws_instance",
   104  						Deposed: []*InstanceState{
   105  							{
   106  								ID: "baz",
   107  							},
   108  						},
   109  					},
   110  					"data.aws_instance.foo.3": {
   111  						Type: "aws_instance",
   112  						Deposed: []*InstanceState{
   113  							{
   114  								ID: "qux",
   115  							},
   116  						},
   117  					},
   118  				},
   119  			},
   120  		},
   121  	})
   122  
   123  	n := &NodeRefreshableDataResource{
   124  		NodeAbstractResource: &NodeAbstractResource{
   125  			Addr: addrs.RootModuleInstance.Resource(
   126  				addrs.DataResourceMode,
   127  				"aws_instance",
   128  				"foo",
   129  			),
   130  			Config: m.Module.DataResources["data.aws_instance.foo"],
   131  			ResolvedProvider: addrs.AbsProviderConfig{
   132  				ProviderConfig: addrs.ProviderConfig{
   133  					Type: "aws",
   134  				},
   135  			},
   136  		},
   137  	}
   138  
   139  	g, err := n.DynamicExpand(&MockEvalContext{
   140  		PathPath:   addrs.RootModuleInstance,
   141  		StateState: state.SyncWrapper(),
   142  
   143  		// DynamicExpand will call EvaluateExpr to evaluate the "count"
   144  		// expression, which is just a literal number 3 in the fixture config
   145  		// and so we'll just hard-code this here too.
   146  		EvaluateExprResult: cty.NumberIntVal(3),
   147  	})
   148  	if err != nil {
   149  		t.Fatalf("error on DynamicExpand: %s", err)
   150  	}
   151  	actual := g.StringWithNodeTypes()
   152  	expected := `data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance
   153  data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance
   154  data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance
   155  data.aws_instance.foo[3] - *terraform.NodeDestroyableDataResourceInstance
   156  root - terraform.graphNodeRoot
   157    data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance
   158    data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance
   159    data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance
   160    data.aws_instance.foo[3] - *terraform.NodeDestroyableDataResourceInstance
   161  `
   162  	if expected != actual {
   163  		t.Fatalf("Expected:\n%s\nGot:\n%s", expected, actual)
   164  	}
   165  
   166  	var destroyableDataResource *NodeDestroyableDataResourceInstance
   167  	for _, v := range g.Vertices() {
   168  		if r, ok := v.(*NodeDestroyableDataResourceInstance); ok {
   169  			destroyableDataResource = r
   170  		}
   171  	}
   172  
   173  	if destroyableDataResource == nil {
   174  		t.Fatal("failed to find a destroyableDataResource")
   175  	}
   176  
   177  	if destroyableDataResource.ResolvedProvider.ProviderConfig.Type == "" {
   178  		t.Fatal("NodeDestroyableDataResourceInstance missing provider config")
   179  	}
   180  }