github.com/kevinklinger/open_terraform@v1.3.6/noninternal/terraform/eval_context_builtin_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/kevinklinger/open_terraform/noninternal/addrs"
     9  	"github.com/kevinklinger/open_terraform/noninternal/providers"
    10  	"github.com/zclconf/go-cty/cty"
    11  )
    12  
    13  func TestBuiltinEvalContextProviderInput(t *testing.T) {
    14  	var lock sync.Mutex
    15  	cache := make(map[string]map[string]cty.Value)
    16  
    17  	ctx1 := testBuiltinEvalContext(t)
    18  	ctx1 = ctx1.WithPath(addrs.RootModuleInstance).(*BuiltinEvalContext)
    19  	ctx1.ProviderInputConfig = cache
    20  	ctx1.ProviderLock = &lock
    21  
    22  	ctx2 := testBuiltinEvalContext(t)
    23  	ctx2 = ctx2.WithPath(addrs.RootModuleInstance.Child("child", addrs.NoKey)).(*BuiltinEvalContext)
    24  	ctx2.ProviderInputConfig = cache
    25  	ctx2.ProviderLock = &lock
    26  
    27  	providerAddr1 := addrs.AbsProviderConfig{
    28  		Module:   addrs.RootModule,
    29  		Provider: addrs.NewDefaultProvider("foo"),
    30  	}
    31  	providerAddr2 := addrs.AbsProviderConfig{
    32  		Module:   addrs.RootModule.Child("child"),
    33  		Provider: addrs.NewDefaultProvider("foo"),
    34  	}
    35  
    36  	expected1 := map[string]cty.Value{"value": cty.StringVal("foo")}
    37  	ctx1.SetProviderInput(providerAddr1, expected1)
    38  
    39  	try2 := map[string]cty.Value{"value": cty.StringVal("bar")}
    40  	ctx2.SetProviderInput(providerAddr2, try2) // ignored because not a root module
    41  
    42  	actual1 := ctx1.ProviderInput(providerAddr1)
    43  	actual2 := ctx2.ProviderInput(providerAddr2)
    44  
    45  	if !reflect.DeepEqual(actual1, expected1) {
    46  		t.Errorf("wrong result 1\ngot:  %#v\nwant: %#v", actual1, expected1)
    47  	}
    48  	if actual2 != nil {
    49  		t.Errorf("wrong result 2\ngot:  %#v\nwant: %#v", actual2, nil)
    50  	}
    51  }
    52  
    53  func TestBuildingEvalContextInitProvider(t *testing.T) {
    54  	var lock sync.Mutex
    55  
    56  	testP := &MockProvider{}
    57  
    58  	ctx := testBuiltinEvalContext(t)
    59  	ctx = ctx.WithPath(addrs.RootModuleInstance).(*BuiltinEvalContext)
    60  	ctx.ProviderLock = &lock
    61  	ctx.ProviderCache = make(map[string]providers.Interface)
    62  	ctx.Plugins = newContextPlugins(map[addrs.Provider]providers.Factory{
    63  		addrs.NewDefaultProvider("test"): providers.FactoryFixed(testP),
    64  	}, nil)
    65  
    66  	providerAddrDefault := addrs.AbsProviderConfig{
    67  		Module:   addrs.RootModule,
    68  		Provider: addrs.NewDefaultProvider("test"),
    69  	}
    70  	providerAddrAlias := addrs.AbsProviderConfig{
    71  		Module:   addrs.RootModule,
    72  		Provider: addrs.NewDefaultProvider("test"),
    73  		Alias:    "foo",
    74  	}
    75  
    76  	_, err := ctx.InitProvider(providerAddrDefault)
    77  	if err != nil {
    78  		t.Fatalf("error initializing provider test: %s", err)
    79  	}
    80  	_, err = ctx.InitProvider(providerAddrAlias)
    81  	if err != nil {
    82  		t.Fatalf("error initializing provider test.foo: %s", err)
    83  	}
    84  }
    85  
    86  func testBuiltinEvalContext(t *testing.T) *BuiltinEvalContext {
    87  	return &BuiltinEvalContext{}
    88  }