2015-06-24 18:34:21 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
2018-04-19 23:47:36 +02:00
|
|
|
|
2018-05-05 04:24:06 +02:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
2015-06-24 18:34:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuiltinEvalContextProviderInput(t *testing.T) {
|
|
|
|
var lock sync.Mutex
|
2018-05-05 04:24:06 +02:00
|
|
|
cache := make(map[string]map[string]cty.Value)
|
2015-06-24 18:34:21 +02:00
|
|
|
|
|
|
|
ctx1 := testBuiltinEvalContext(t)
|
2018-05-05 04:24:06 +02:00
|
|
|
ctx1.PathValue = addrs.RootModuleInstance
|
2015-06-24 18:34:21 +02:00
|
|
|
ctx1.ProviderInputConfig = cache
|
|
|
|
ctx1.ProviderLock = &lock
|
|
|
|
|
|
|
|
ctx2 := testBuiltinEvalContext(t)
|
2018-05-05 04:24:06 +02:00
|
|
|
ctx2.PathValue = addrs.RootModuleInstance.Child("child", addrs.NoKey)
|
2015-06-24 18:34:21 +02:00
|
|
|
ctx2.ProviderInputConfig = cache
|
|
|
|
ctx2.ProviderLock = &lock
|
|
|
|
|
2018-05-05 04:24:06 +02:00
|
|
|
providerAddr := addrs.ProviderConfig{Type: "foo"}
|
|
|
|
|
|
|
|
expected1 := map[string]cty.Value{"value": cty.StringVal("foo")}
|
|
|
|
ctx1.SetProviderInput(providerAddr, expected1)
|
2015-06-24 18:34:21 +02:00
|
|
|
|
2018-05-05 04:24:06 +02:00
|
|
|
expected2 := map[string]cty.Value{"value": cty.StringVal("bar")}
|
|
|
|
ctx2.SetProviderInput(providerAddr, expected2)
|
2015-06-24 18:34:21 +02:00
|
|
|
|
2018-05-05 04:24:06 +02:00
|
|
|
actual1 := ctx1.ProviderInput(providerAddr)
|
|
|
|
actual2 := ctx2.ProviderInput(providerAddr)
|
2015-06-24 18:34:21 +02:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual1, expected1) {
|
|
|
|
t.Fatalf("bad: %#v %#v", actual1, expected1)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual2, expected2) {
|
|
|
|
t.Fatalf("bad: %#v %#v", actual2, expected2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 23:47:36 +02:00
|
|
|
func TestBuildingEvalContextInitProvider(t *testing.T) {
|
|
|
|
var lock sync.Mutex
|
|
|
|
|
|
|
|
testP := &MockResourceProvider{
|
|
|
|
ResourcesReturn: []ResourceType{
|
|
|
|
{
|
|
|
|
Name: "test_thing",
|
|
|
|
SchemaAvailable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DataSourcesReturn: []DataSource{
|
|
|
|
{
|
|
|
|
Name: "test_thing",
|
|
|
|
SchemaAvailable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := testBuiltinEvalContext(t)
|
|
|
|
ctx.ProviderLock = &lock
|
|
|
|
ctx.ProviderCache = make(map[string]ResourceProvider)
|
|
|
|
ctx.Components = &basicComponentFactory{
|
|
|
|
providers: map[string]ResourceProviderFactory{
|
|
|
|
"test": ResourceProviderFactoryFixed(testP),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-05-05 04:24:06 +02:00
|
|
|
providerAddrDefault := addrs.ProviderConfig{Type: "test"}
|
|
|
|
providerAddrAlias := addrs.ProviderConfig{Type: "test", Alias: "foo"}
|
|
|
|
|
|
|
|
_, err := ctx.InitProvider("test", providerAddrDefault)
|
2018-04-19 23:47:36 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error initializing provider test: %s", err)
|
|
|
|
}
|
2018-05-05 04:24:06 +02:00
|
|
|
_, err = ctx.InitProvider("test", providerAddrAlias)
|
2018-04-19 23:47:36 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error initializing provider test.foo: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 18:34:21 +02:00
|
|
|
func testBuiltinEvalContext(t *testing.T) *BuiltinEvalContext {
|
|
|
|
return &BuiltinEvalContext{}
|
|
|
|
}
|