2015-02-03 18:06:12 -06:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2015-02-03 18:19:18 -06:00
|
|
|
"reflect"
|
2015-02-03 18:06:12 -06:00
|
|
|
"testing"
|
2017-04-03 14:23:50 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
2015-02-03 18:06:12 -06:00
|
|
|
)
|
|
|
|
|
2015-03-25 18:28:52 -05:00
|
|
|
func TestEvalBuildProviderConfig_impl(t *testing.T) {
|
|
|
|
var _ EvalNode = new(EvalBuildProviderConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvalBuildProviderConfig(t *testing.T) {
|
2017-04-03 14:23:50 -05:00
|
|
|
config := testResourceConfig(t, map[string]interface{}{
|
|
|
|
"set_in_config": "config",
|
|
|
|
"set_in_config_and_parent": "config",
|
|
|
|
"computed_in_config": "config",
|
|
|
|
})
|
2015-03-25 18:28:52 -05:00
|
|
|
provider := "foo"
|
|
|
|
|
|
|
|
n := &EvalBuildProviderConfig{
|
|
|
|
Provider: provider,
|
|
|
|
Config: &config,
|
|
|
|
Output: &config,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := &MockEvalContext{
|
|
|
|
ProviderInputConfig: map[string]interface{}{
|
2017-04-03 14:23:50 -05:00
|
|
|
"set_in_config": "input",
|
|
|
|
"set_by_input": "input",
|
2015-03-25 18:28:52 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if _, err := n.Eval(ctx); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-10-13 09:11:10 -05:00
|
|
|
// We expect the provider config with the added input value
|
2015-03-25 18:28:52 -05:00
|
|
|
expected := map[string]interface{}{
|
2017-10-16 12:42:55 -05:00
|
|
|
"set_in_config": "config",
|
2017-10-13 09:11:10 -05:00
|
|
|
"set_in_config_and_parent": "config",
|
2017-10-13 09:34:03 -05:00
|
|
|
"computed_in_config": "config",
|
|
|
|
"set_by_input": "input",
|
2015-03-25 18:28:52 -05:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(config.Raw, expected) {
|
2017-10-13 09:11:10 -05:00
|
|
|
t.Fatalf("incorrect merged config:\n%#v\nwanted:\n%#v", config.Raw, expected)
|
2015-03-25 18:28:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 18:19:18 -06:00
|
|
|
func TestEvalConfigProvider_impl(t *testing.T) {
|
|
|
|
var _ EvalNode = new(EvalConfigProvider)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvalConfigProvider(t *testing.T) {
|
|
|
|
config := testResourceConfig(t, map[string]interface{}{})
|
|
|
|
provider := &MockResourceProvider{}
|
2015-02-14 00:58:41 -06:00
|
|
|
n := &EvalConfigProvider{Config: &config}
|
2015-02-03 18:19:18 -06:00
|
|
|
|
|
|
|
ctx := &MockEvalContext{ProviderProvider: provider}
|
2015-02-14 00:58:41 -06:00
|
|
|
if _, err := n.Eval(ctx); err != nil {
|
2015-02-03 18:19:18 -06:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-02-10 01:33:48 -06:00
|
|
|
if !ctx.ConfigureProviderCalled {
|
2015-02-03 18:19:18 -06:00
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
2015-02-10 01:33:48 -06:00
|
|
|
if !reflect.DeepEqual(ctx.ConfigureProviderConfig, config) {
|
|
|
|
t.Fatalf("bad: %#v", ctx.ConfigureProviderConfig)
|
2015-02-03 18:19:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 18:06:12 -06:00
|
|
|
func TestEvalInitProvider_impl(t *testing.T) {
|
|
|
|
var _ EvalNode = new(EvalInitProvider)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvalInitProvider(t *testing.T) {
|
|
|
|
n := &EvalInitProvider{Name: "foo"}
|
|
|
|
provider := &MockResourceProvider{}
|
|
|
|
ctx := &MockEvalContext{InitProviderProvider: provider}
|
2015-02-14 00:58:41 -06:00
|
|
|
if _, err := n.Eval(ctx); err != nil {
|
2015-02-03 18:06:12 -06:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.InitProviderCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
|
|
|
if ctx.InitProviderName != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", ctx.InitProviderName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 14:52:50 -05:00
|
|
|
func TestEvalCloseProvider(t *testing.T) {
|
2018-01-04 14:00:09 -06:00
|
|
|
providerName := ResolveProviderName("foo", nil)
|
|
|
|
n := &EvalCloseProvider{Name: providerName}
|
2015-06-19 14:52:50 -05:00
|
|
|
provider := &MockResourceProvider{}
|
|
|
|
ctx := &MockEvalContext{CloseProviderProvider: provider}
|
|
|
|
if _, err := n.Eval(ctx); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.CloseProviderCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
2018-01-04 14:00:09 -06:00
|
|
|
if ctx.CloseProviderName != providerName {
|
2015-06-19 14:52:50 -05:00
|
|
|
t.Fatalf("bad: %#v", ctx.CloseProviderName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 18:06:12 -06:00
|
|
|
func TestEvalGetProvider_impl(t *testing.T) {
|
|
|
|
var _ EvalNode = new(EvalGetProvider)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvalGetProvider(t *testing.T) {
|
2015-02-14 00:58:41 -06:00
|
|
|
var actual ResourceProvider
|
|
|
|
n := &EvalGetProvider{Name: "foo", Output: &actual}
|
2015-02-03 18:06:12 -06:00
|
|
|
provider := &MockResourceProvider{}
|
|
|
|
ctx := &MockEvalContext{ProviderProvider: provider}
|
2015-02-14 00:58:41 -06:00
|
|
|
if _, err := n.Eval(ctx); err != nil {
|
2015-02-03 18:06:12 -06:00
|
|
|
t.Fatalf("err: %s", err)
|
2015-02-14 00:58:41 -06:00
|
|
|
}
|
|
|
|
if actual != provider {
|
2015-02-03 18:06:12 -06:00
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.ProviderCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
|
|
|
if ctx.ProviderName != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", ctx.ProviderName)
|
|
|
|
}
|
|
|
|
}
|
2017-04-03 14:23:50 -05:00
|
|
|
|
|
|
|
func TestEvalInputProvider(t *testing.T) {
|
|
|
|
var provider ResourceProvider = &MockResourceProvider{
|
|
|
|
InputFn: func(ui UIInput, c *ResourceConfig) (*ResourceConfig, error) {
|
|
|
|
if c.Config["mock_config"] != "mock" {
|
|
|
|
t.Fatalf("original config not passed to provider.Input")
|
|
|
|
}
|
|
|
|
|
|
|
|
rawConfig, err := config.NewRawConfig(map[string]interface{}{
|
2017-10-16 15:14:55 -05:00
|
|
|
"set_by_input": "input",
|
2017-04-03 14:23:50 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config := NewResourceConfig(rawConfig)
|
|
|
|
config.ComputedKeys = []string{"computed"} // fake computed key
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := &MockEvalContext{ProviderProvider: provider}
|
|
|
|
rawConfig, err := config.NewRawConfig(map[string]interface{}{
|
2017-10-16 15:14:55 -05:00
|
|
|
"mock_config": "mock",
|
|
|
|
"set_in_config": "input",
|
2017-04-03 14:23:50 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("NewRawConfig failed: %s", err)
|
|
|
|
}
|
|
|
|
config := NewResourceConfig(rawConfig)
|
|
|
|
|
|
|
|
n := &EvalInputProvider{
|
|
|
|
Name: "mock",
|
|
|
|
Provider: &provider,
|
|
|
|
Config: &config,
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := n.Eval(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Eval failed: %s", err)
|
|
|
|
}
|
|
|
|
if result != nil {
|
|
|
|
t.Fatalf("Eval returned non-nil result %#v", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.SetProviderInputCalled {
|
|
|
|
t.Fatalf("ctx.SetProviderInput wasn't called")
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := ctx.SetProviderInputName, "mock"; got != want {
|
|
|
|
t.Errorf("wrong provider name %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
inputCfg := ctx.SetProviderInputConfig
|
2017-10-16 15:14:55 -05:00
|
|
|
|
|
|
|
// we should only have the value that was set during Input
|
2017-04-03 14:23:50 -05:00
|
|
|
want := map[string]interface{}{
|
2017-10-16 15:14:55 -05:00
|
|
|
"set_by_input": "input",
|
2017-04-03 14:23:50 -05:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(inputCfg, want) {
|
2017-10-16 15:14:55 -05:00
|
|
|
t.Errorf("got incorrect input config:\n%#v\nwant:\n%#v", inputCfg, want)
|
2017-04-03 14:23:50 -05:00
|
|
|
}
|
|
|
|
}
|