github.com/trawler/terraform@v0.10.8-0.20171106022149-4b1c7a1d9b48/terraform/eval_provider_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/config"
     8  )
     9  
    10  func TestEvalBuildProviderConfig_impl(t *testing.T) {
    11  	var _ EvalNode = new(EvalBuildProviderConfig)
    12  }
    13  
    14  func TestEvalBuildProviderConfig(t *testing.T) {
    15  	config := testResourceConfig(t, map[string]interface{}{
    16  		"set_in_config":            "config",
    17  		"set_in_config_and_parent": "config",
    18  		"computed_in_config":       "config",
    19  	})
    20  	provider := "foo"
    21  
    22  	n := &EvalBuildProviderConfig{
    23  		Provider: provider,
    24  		Config:   &config,
    25  		Output:   &config,
    26  	}
    27  
    28  	ctx := &MockEvalContext{
    29  		ProviderInputConfig: map[string]interface{}{
    30  			"set_in_config": "input",
    31  			"set_by_input":  "input",
    32  		},
    33  	}
    34  	if _, err := n.Eval(ctx); err != nil {
    35  		t.Fatalf("err: %s", err)
    36  	}
    37  
    38  	// We expect the provider config with the added input value
    39  	expected := map[string]interface{}{
    40  		"set_in_config":            "config",
    41  		"set_in_config_and_parent": "config",
    42  		"computed_in_config":       "config",
    43  		"set_by_input":             "input",
    44  	}
    45  	if !reflect.DeepEqual(config.Raw, expected) {
    46  		t.Fatalf("incorrect merged config:\n%#v\nwanted:\n%#v", config.Raw, expected)
    47  	}
    48  }
    49  
    50  func TestEvalConfigProvider_impl(t *testing.T) {
    51  	var _ EvalNode = new(EvalConfigProvider)
    52  }
    53  
    54  func TestEvalConfigProvider(t *testing.T) {
    55  	config := testResourceConfig(t, map[string]interface{}{})
    56  	provider := &MockResourceProvider{}
    57  	n := &EvalConfigProvider{Config: &config}
    58  
    59  	ctx := &MockEvalContext{ProviderProvider: provider}
    60  	if _, err := n.Eval(ctx); err != nil {
    61  		t.Fatalf("err: %s", err)
    62  	}
    63  
    64  	if !ctx.ConfigureProviderCalled {
    65  		t.Fatal("should be called")
    66  	}
    67  	if !reflect.DeepEqual(ctx.ConfigureProviderConfig, config) {
    68  		t.Fatalf("bad: %#v", ctx.ConfigureProviderConfig)
    69  	}
    70  }
    71  
    72  func TestEvalInitProvider_impl(t *testing.T) {
    73  	var _ EvalNode = new(EvalInitProvider)
    74  }
    75  
    76  func TestEvalInitProvider(t *testing.T) {
    77  	n := &EvalInitProvider{Name: "foo"}
    78  	provider := &MockResourceProvider{}
    79  	ctx := &MockEvalContext{InitProviderProvider: provider}
    80  	if _, err := n.Eval(ctx); err != nil {
    81  		t.Fatalf("err: %s", err)
    82  	}
    83  
    84  	if !ctx.InitProviderCalled {
    85  		t.Fatal("should be called")
    86  	}
    87  	if ctx.InitProviderName != "foo" {
    88  		t.Fatalf("bad: %#v", ctx.InitProviderName)
    89  	}
    90  }
    91  
    92  func TestEvalCloseProvider(t *testing.T) {
    93  	n := &EvalCloseProvider{Name: "foo"}
    94  	provider := &MockResourceProvider{}
    95  	ctx := &MockEvalContext{CloseProviderProvider: provider}
    96  	if _, err := n.Eval(ctx); err != nil {
    97  		t.Fatalf("err: %s", err)
    98  	}
    99  
   100  	if !ctx.CloseProviderCalled {
   101  		t.Fatal("should be called")
   102  	}
   103  	if ctx.CloseProviderName != "foo" {
   104  		t.Fatalf("bad: %#v", ctx.CloseProviderName)
   105  	}
   106  }
   107  
   108  func TestEvalGetProvider_impl(t *testing.T) {
   109  	var _ EvalNode = new(EvalGetProvider)
   110  }
   111  
   112  func TestEvalGetProvider(t *testing.T) {
   113  	var actual ResourceProvider
   114  	n := &EvalGetProvider{Name: "foo", Output: &actual}
   115  	provider := &MockResourceProvider{}
   116  	ctx := &MockEvalContext{ProviderProvider: provider}
   117  	if _, err := n.Eval(ctx); err != nil {
   118  		t.Fatalf("err: %s", err)
   119  	}
   120  	if actual != provider {
   121  		t.Fatalf("bad: %#v", actual)
   122  	}
   123  
   124  	if !ctx.ProviderCalled {
   125  		t.Fatal("should be called")
   126  	}
   127  	if ctx.ProviderName != "foo" {
   128  		t.Fatalf("bad: %#v", ctx.ProviderName)
   129  	}
   130  }
   131  
   132  func TestEvalInputProvider(t *testing.T) {
   133  	var provider ResourceProvider = &MockResourceProvider{
   134  		InputFn: func(ui UIInput, c *ResourceConfig) (*ResourceConfig, error) {
   135  			if c.Config["mock_config"] != "mock" {
   136  				t.Fatalf("original config not passed to provider.Input")
   137  			}
   138  
   139  			rawConfig, err := config.NewRawConfig(map[string]interface{}{
   140  				"set_by_input": "input",
   141  			})
   142  			if err != nil {
   143  				return nil, err
   144  			}
   145  			config := NewResourceConfig(rawConfig)
   146  			config.ComputedKeys = []string{"computed"} // fake computed key
   147  
   148  			return config, nil
   149  		},
   150  	}
   151  	ctx := &MockEvalContext{ProviderProvider: provider}
   152  	rawConfig, err := config.NewRawConfig(map[string]interface{}{
   153  		"mock_config":   "mock",
   154  		"set_in_config": "input",
   155  	})
   156  	if err != nil {
   157  		t.Fatalf("NewRawConfig failed: %s", err)
   158  	}
   159  	config := NewResourceConfig(rawConfig)
   160  
   161  	n := &EvalInputProvider{
   162  		Name:     "mock",
   163  		Provider: &provider,
   164  		Config:   &config,
   165  	}
   166  
   167  	result, err := n.Eval(ctx)
   168  	if err != nil {
   169  		t.Fatalf("Eval failed: %s", err)
   170  	}
   171  	if result != nil {
   172  		t.Fatalf("Eval returned non-nil result %#v", result)
   173  	}
   174  
   175  	if !ctx.SetProviderInputCalled {
   176  		t.Fatalf("ctx.SetProviderInput wasn't called")
   177  	}
   178  
   179  	if got, want := ctx.SetProviderInputName, "mock"; got != want {
   180  		t.Errorf("wrong provider name %q; want %q", got, want)
   181  	}
   182  
   183  	inputCfg := ctx.SetProviderInputConfig
   184  
   185  	// we should only have the value that was set during Input
   186  	want := map[string]interface{}{
   187  		"set_by_input": "input",
   188  	}
   189  	if !reflect.DeepEqual(inputCfg, want) {
   190  		t.Errorf("got incorrect input config:\n%#v\nwant:\n%#v", inputCfg, want)
   191  	}
   192  }