github.com/hugorut/terraform@v1.1.3/src/terraform/node_resource_apply_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hugorut/terraform/src/addrs"
     7  	"github.com/hugorut/terraform/src/configs"
     8  	"github.com/hugorut/terraform/src/instances"
     9  	"github.com/hugorut/terraform/src/states"
    10  )
    11  
    12  func TestNodeApplyableResourceExecute(t *testing.T) {
    13  	state := states.NewState()
    14  	ctx := &MockEvalContext{
    15  		StateState:               state.SyncWrapper(),
    16  		InstanceExpanderExpander: instances.NewExpander(),
    17  	}
    18  
    19  	t.Run("no config", func(t *testing.T) {
    20  		node := NodeApplyableResource{
    21  			NodeAbstractResource: &NodeAbstractResource{
    22  				Config: nil,
    23  			},
    24  			Addr: mustAbsResourceAddr("test_instance.foo"),
    25  		}
    26  		diags := node.Execute(ctx, walkApply)
    27  		if diags.HasErrors() {
    28  			t.Fatalf("unexpected error: %s", diags.Err())
    29  		}
    30  		if !state.Empty() {
    31  			t.Fatalf("expected no state, got:\n %s", state.String())
    32  		}
    33  	})
    34  
    35  	t.Run("simple", func(t *testing.T) {
    36  
    37  		node := NodeApplyableResource{
    38  			NodeAbstractResource: &NodeAbstractResource{
    39  				Config: &configs.Resource{
    40  					Mode: addrs.ManagedResourceMode,
    41  					Type: "test_instance",
    42  					Name: "foo",
    43  				},
    44  				ResolvedProvider: addrs.AbsProviderConfig{
    45  					Provider: addrs.NewDefaultProvider("test"),
    46  					Module:   addrs.RootModule,
    47  				},
    48  			},
    49  			Addr: mustAbsResourceAddr("test_instance.foo"),
    50  		}
    51  		diags := node.Execute(ctx, walkApply)
    52  		if diags.HasErrors() {
    53  			t.Fatalf("unexpected error: %s", diags.Err())
    54  		}
    55  		if state.Empty() {
    56  			t.Fatal("expected resources in state, got empty state")
    57  		}
    58  		r := state.Resource(mustAbsResourceAddr("test_instance.foo"))
    59  		if r == nil {
    60  			t.Fatal("test_instance.foo not found in state")
    61  		}
    62  	})
    63  }