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

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform-plugin-sdk/internal/addrs"
     9  	"github.com/hashicorp/terraform-plugin-sdk/internal/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.PathValue = addrs.RootModuleInstance
    19  	ctx1.ProviderInputConfig = cache
    20  	ctx1.ProviderLock = &lock
    21  
    22  	ctx2 := testBuiltinEvalContext(t)
    23  	ctx2.PathValue = addrs.RootModuleInstance.Child("child", addrs.NoKey)
    24  	ctx2.ProviderInputConfig = cache
    25  	ctx2.ProviderLock = &lock
    26  
    27  	providerAddr := addrs.ProviderConfig{Type: "foo"}
    28  
    29  	expected1 := map[string]cty.Value{"value": cty.StringVal("foo")}
    30  	ctx1.SetProviderInput(providerAddr, expected1)
    31  
    32  	try2 := map[string]cty.Value{"value": cty.StringVal("bar")}
    33  	ctx2.SetProviderInput(providerAddr, try2) // ignored because not a root module
    34  
    35  	actual1 := ctx1.ProviderInput(providerAddr)
    36  	actual2 := ctx2.ProviderInput(providerAddr)
    37  
    38  	if !reflect.DeepEqual(actual1, expected1) {
    39  		t.Errorf("wrong result 1\ngot:  %#v\nwant: %#v", actual1, expected1)
    40  	}
    41  	if actual2 != nil {
    42  		t.Errorf("wrong result 2\ngot:  %#v\nwant: %#v", actual2, nil)
    43  	}
    44  }
    45  
    46  func TestBuildingEvalContextInitProvider(t *testing.T) {
    47  	var lock sync.Mutex
    48  
    49  	testP := &MockProvider{}
    50  
    51  	ctx := testBuiltinEvalContext(t)
    52  	ctx.ProviderLock = &lock
    53  	ctx.ProviderCache = make(map[string]providers.Interface)
    54  	ctx.Components = &basicComponentFactory{
    55  		providers: map[string]providers.Factory{
    56  			"test": providers.FactoryFixed(testP),
    57  		},
    58  	}
    59  
    60  	providerAddrDefault := addrs.ProviderConfig{Type: "test"}
    61  	providerAddrAlias := addrs.ProviderConfig{Type: "test", Alias: "foo"}
    62  
    63  	_, err := ctx.InitProvider("test", providerAddrDefault)
    64  	if err != nil {
    65  		t.Fatalf("error initializing provider test: %s", err)
    66  	}
    67  	_, err = ctx.InitProvider("test", providerAddrAlias)
    68  	if err != nil {
    69  		t.Fatalf("error initializing provider test.foo: %s", err)
    70  	}
    71  }
    72  
    73  func testBuiltinEvalContext(t *testing.T) *BuiltinEvalContext {
    74  	return &BuiltinEvalContext{}
    75  }