github.com/jaredpalmer/terraform@v1.1.0-alpha20210908.0.20210911170307-88705c943a03/internal/terraform/node_count_boundary_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/hcl/v2/hcltest"
     7  	"github.com/hashicorp/terraform/internal/addrs"
     8  	"github.com/hashicorp/terraform/internal/configs"
     9  	"github.com/hashicorp/terraform/internal/states"
    10  	"github.com/zclconf/go-cty/cty"
    11  )
    12  
    13  func TestNodeCountBoundaryExecute(t *testing.T) {
    14  
    15  	// Create a state with a single instance (addrs.NoKey) of test_instance.foo
    16  	state := states.NewState()
    17  	state.Module(addrs.RootModuleInstance).SetResourceInstanceCurrent(
    18  		addrs.Resource{
    19  			Mode: addrs.ManagedResourceMode,
    20  			Type: "test_instance",
    21  			Name: "foo",
    22  		}.Instance(addrs.NoKey),
    23  		&states.ResourceInstanceObjectSrc{
    24  			Status:    states.ObjectReady,
    25  			AttrsJSON: []byte(`{"type":"string","value":"hello"}`),
    26  		},
    27  		addrs.AbsProviderConfig{
    28  			Provider: addrs.NewDefaultProvider("test"),
    29  			Module:   addrs.RootModule,
    30  		},
    31  	)
    32  
    33  	// Create a config that uses count to create 2 instances of test_instance.foo
    34  	rc := &configs.Resource{
    35  		Mode:  addrs.ManagedResourceMode,
    36  		Type:  "test_instance",
    37  		Name:  "foo",
    38  		Count: hcltest.MockExprLiteral(cty.NumberIntVal(2)),
    39  		Config: configs.SynthBody("", map[string]cty.Value{
    40  			"test_string": cty.StringVal("hello"),
    41  		}),
    42  	}
    43  	config := &configs.Config{
    44  		Module: &configs.Module{
    45  			ManagedResources: map[string]*configs.Resource{
    46  				"test_instance.foo": rc,
    47  			},
    48  		},
    49  	}
    50  
    51  	ctx := &MockEvalContext{
    52  		StateState: state.SyncWrapper(),
    53  	}
    54  	node := NodeCountBoundary{Config: config}
    55  
    56  	diags := node.Execute(ctx, walkApply)
    57  	if diags.HasErrors() {
    58  		t.Fatalf("unexpected error: %s", diags.Err())
    59  	}
    60  	if !state.HasResources() {
    61  		t.Fatal("resources missing from state")
    62  	}
    63  
    64  	// verify that the resource changed from test_instance.foo to
    65  	// test_instance.foo.0 in the state
    66  	actual := state.String()
    67  	expected := "test_instance.foo.0:\n  ID = \n  provider = provider[\"registry.terraform.io/hashicorp/test\"]\n  type = string\n  value = hello"
    68  
    69  	if actual != expected {
    70  		t.Fatalf("wrong result: %s", actual)
    71  	}
    72  }