github.com/wikibal01/hashicorp-terraform@v0.11.12-beta1/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 providerName := ResolveProviderName("foo", nil) 94 n := &EvalCloseProvider{Name: providerName} 95 provider := &MockResourceProvider{} 96 ctx := &MockEvalContext{CloseProviderProvider: provider} 97 if _, err := n.Eval(ctx); err != nil { 98 t.Fatalf("err: %s", err) 99 } 100 101 if !ctx.CloseProviderCalled { 102 t.Fatal("should be called") 103 } 104 if ctx.CloseProviderName != providerName { 105 t.Fatalf("bad: %#v", ctx.CloseProviderName) 106 } 107 } 108 109 func TestEvalGetProvider_impl(t *testing.T) { 110 var _ EvalNode = new(EvalGetProvider) 111 } 112 113 func TestEvalGetProvider(t *testing.T) { 114 var actual ResourceProvider 115 n := &EvalGetProvider{Name: "foo", Output: &actual} 116 provider := &MockResourceProvider{} 117 ctx := &MockEvalContext{ProviderProvider: provider} 118 if _, err := n.Eval(ctx); err != nil { 119 t.Fatalf("err: %s", err) 120 } 121 if actual != provider { 122 t.Fatalf("bad: %#v", actual) 123 } 124 125 if !ctx.ProviderCalled { 126 t.Fatal("should be called") 127 } 128 if ctx.ProviderName != "foo" { 129 t.Fatalf("bad: %#v", ctx.ProviderName) 130 } 131 } 132 133 func TestEvalInputProvider(t *testing.T) { 134 var provider ResourceProvider = &MockResourceProvider{ 135 InputFn: func(ui UIInput, c *ResourceConfig) (*ResourceConfig, error) { 136 if c.Config["mock_config"] != "mock" { 137 t.Fatalf("original config not passed to provider.Input") 138 } 139 140 rawConfig, err := config.NewRawConfig(map[string]interface{}{ 141 "set_by_input": "input", 142 }) 143 if err != nil { 144 return nil, err 145 } 146 config := NewResourceConfig(rawConfig) 147 config.ComputedKeys = []string{"computed"} // fake computed key 148 149 return config, nil 150 }, 151 } 152 ctx := &MockEvalContext{ProviderProvider: provider} 153 rawConfig, err := config.NewRawConfig(map[string]interface{}{ 154 "mock_config": "mock", 155 "set_in_config": "input", 156 }) 157 if err != nil { 158 t.Fatalf("NewRawConfig failed: %s", err) 159 } 160 config := NewResourceConfig(rawConfig) 161 162 n := &EvalInputProvider{ 163 Name: "mock", 164 Provider: &provider, 165 Config: &config, 166 } 167 168 result, err := n.Eval(ctx) 169 if err != nil { 170 t.Fatalf("Eval failed: %s", err) 171 } 172 if result != nil { 173 t.Fatalf("Eval returned non-nil result %#v", result) 174 } 175 176 if !ctx.SetProviderInputCalled { 177 t.Fatalf("ctx.SetProviderInput wasn't called") 178 } 179 180 if got, want := ctx.SetProviderInputName, "mock"; got != want { 181 t.Errorf("wrong provider name %q; want %q", got, want) 182 } 183 184 inputCfg := ctx.SetProviderInputConfig 185 186 // we should only have the value that was set during Input 187 want := map[string]interface{}{ 188 "set_by_input": "input", 189 } 190 if !reflect.DeepEqual(inputCfg, want) { 191 t.Errorf("got incorrect input config:\n%#v\nwant:\n%#v", inputCfg, want) 192 } 193 }