github.com/burmuley/terraform@v0.11.12-beta1/terraform/eval_local_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/davecgh/go-spew/spew"
     9  	"github.com/hashicorp/terraform/config"
    10  )
    11  
    12  func TestEvalLocal_impl(t *testing.T) {
    13  	var _ EvalNode = new(EvalLocal)
    14  }
    15  
    16  func TestEvalLocal(t *testing.T) {
    17  	tests := []struct {
    18  		Value string
    19  		Want  interface{}
    20  		Err   bool
    21  	}{
    22  		{
    23  			"hello!",
    24  			"hello!",
    25  			false,
    26  		},
    27  		{
    28  			"",
    29  			"",
    30  			false,
    31  		},
    32  	}
    33  
    34  	for _, test := range tests {
    35  		t.Run(test.Value, func(t *testing.T) {
    36  			rawConfig, err := config.NewRawConfig(map[string]interface{}{
    37  				"value": test.Value,
    38  			})
    39  			if err != nil {
    40  				t.Fatal(err)
    41  			}
    42  
    43  			n := &EvalLocal{
    44  				Name:  "foo",
    45  				Value: rawConfig,
    46  			}
    47  			ctx := &MockEvalContext{
    48  				StateState: &State{},
    49  				StateLock:  &sync.RWMutex{},
    50  
    51  				InterpolateConfigResult: testResourceConfig(t, map[string]interface{}{
    52  					"value": test.Want,
    53  				}),
    54  			}
    55  
    56  			_, err = n.Eval(ctx)
    57  			if (err != nil) != test.Err {
    58  				if err != nil {
    59  					t.Errorf("unexpected error: %s", err)
    60  				} else {
    61  					t.Errorf("successful Eval; want error")
    62  				}
    63  			}
    64  
    65  			ms := ctx.StateState.ModuleByPath([]string{})
    66  			gotLocals := ms.Locals
    67  			wantLocals := map[string]interface{}{
    68  				"foo": test.Want,
    69  			}
    70  
    71  			if !reflect.DeepEqual(gotLocals, wantLocals) {
    72  				t.Errorf(
    73  					"wrong locals after Eval\ngot:  %swant: %s",
    74  					spew.Sdump(gotLocals), spew.Sdump(wantLocals),
    75  				)
    76  			}
    77  		})
    78  	}
    79  
    80  }