github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/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 ParentProviderConfigConfig: testResourceConfig(t, map[string]interface{}{ 30 "inherited_from_parent": "parent", 31 "set_in_config_and_parent": "parent", 32 }), 33 ProviderInputConfig: map[string]interface{}{ 34 "set_in_config": "input", 35 "set_by_input": "input", 36 }, 37 } 38 if _, err := n.Eval(ctx); err != nil { 39 t.Fatalf("err: %s", err) 40 } 41 42 // This is a merger of the following, with later items taking precedence: 43 // - "config" (the config as written in the current module, with all 44 // interpolation expressions resolved) 45 // - ProviderInputConfig (mock of config produced by the input walk, after 46 // prompting the user interactively for values unspecified in config) 47 // - ParentProviderConfigConfig (mock of config inherited from a parent module) 48 expected := map[string]interface{}{ 49 "set_in_config": "input", // in practice, input map contains identical literals from config 50 "set_in_config_and_parent": "parent", 51 "inherited_from_parent": "parent", 52 "computed_in_config": "config", 53 "set_by_input": "input", 54 } 55 if !reflect.DeepEqual(config.Raw, expected) { 56 t.Fatalf("incorrect merged config %#v; want %#v", config.Raw, expected) 57 } 58 } 59 60 func TestEvalBuildProviderConfig_parentPriority(t *testing.T) { 61 config := testResourceConfig(t, map[string]interface{}{}) 62 provider := "foo" 63 64 n := &EvalBuildProviderConfig{ 65 Provider: provider, 66 Config: &config, 67 Output: &config, 68 } 69 70 ctx := &MockEvalContext{ 71 ParentProviderConfigConfig: testResourceConfig(t, map[string]interface{}{ 72 "foo": "bar", 73 }), 74 ProviderInputConfig: map[string]interface{}{ 75 "foo": "baz", 76 }, 77 } 78 if _, err := n.Eval(ctx); err != nil { 79 t.Fatalf("err: %s", err) 80 } 81 82 expected := map[string]interface{}{ 83 "foo": "bar", 84 } 85 if !reflect.DeepEqual(config.Raw, expected) { 86 t.Fatalf("bad: %#v", config.Raw) 87 } 88 } 89 90 func TestEvalConfigProvider_impl(t *testing.T) { 91 var _ EvalNode = new(EvalConfigProvider) 92 } 93 94 func TestEvalConfigProvider(t *testing.T) { 95 config := testResourceConfig(t, map[string]interface{}{}) 96 provider := &MockResourceProvider{} 97 n := &EvalConfigProvider{Config: &config} 98 99 ctx := &MockEvalContext{ProviderProvider: provider} 100 if _, err := n.Eval(ctx); err != nil { 101 t.Fatalf("err: %s", err) 102 } 103 104 if !ctx.ConfigureProviderCalled { 105 t.Fatal("should be called") 106 } 107 if !reflect.DeepEqual(ctx.ConfigureProviderConfig, config) { 108 t.Fatalf("bad: %#v", ctx.ConfigureProviderConfig) 109 } 110 } 111 112 func TestEvalInitProvider_impl(t *testing.T) { 113 var _ EvalNode = new(EvalInitProvider) 114 } 115 116 func TestEvalInitProvider(t *testing.T) { 117 n := &EvalInitProvider{Name: "foo"} 118 provider := &MockResourceProvider{} 119 ctx := &MockEvalContext{InitProviderProvider: provider} 120 if _, err := n.Eval(ctx); err != nil { 121 t.Fatalf("err: %s", err) 122 } 123 124 if !ctx.InitProviderCalled { 125 t.Fatal("should be called") 126 } 127 if ctx.InitProviderName != "foo" { 128 t.Fatalf("bad: %#v", ctx.InitProviderName) 129 } 130 } 131 132 func TestEvalCloseProvider(t *testing.T) { 133 n := &EvalCloseProvider{Name: "foo"} 134 provider := &MockResourceProvider{} 135 ctx := &MockEvalContext{CloseProviderProvider: provider} 136 if _, err := n.Eval(ctx); err != nil { 137 t.Fatalf("err: %s", err) 138 } 139 140 if !ctx.CloseProviderCalled { 141 t.Fatal("should be called") 142 } 143 if ctx.CloseProviderName != "foo" { 144 t.Fatalf("bad: %#v", ctx.CloseProviderName) 145 } 146 } 147 148 func TestEvalGetProvider_impl(t *testing.T) { 149 var _ EvalNode = new(EvalGetProvider) 150 } 151 152 func TestEvalGetProvider(t *testing.T) { 153 var actual ResourceProvider 154 n := &EvalGetProvider{Name: "foo", Output: &actual} 155 provider := &MockResourceProvider{} 156 ctx := &MockEvalContext{ProviderProvider: provider} 157 if _, err := n.Eval(ctx); err != nil { 158 t.Fatalf("err: %s", err) 159 } 160 if actual != provider { 161 t.Fatalf("bad: %#v", actual) 162 } 163 164 if !ctx.ProviderCalled { 165 t.Fatal("should be called") 166 } 167 if ctx.ProviderName != "foo" { 168 t.Fatalf("bad: %#v", ctx.ProviderName) 169 } 170 } 171 172 func TestEvalInputProvider(t *testing.T) { 173 var provider ResourceProvider = &MockResourceProvider{ 174 InputFn: func(ui UIInput, c *ResourceConfig) (*ResourceConfig, error) { 175 if c.Config["mock_config"] != "mock" { 176 t.Fatalf("original config not passed to provider.Input") 177 } 178 179 rawConfig, err := config.NewRawConfig(map[string]interface{}{ 180 "set_in_config": "input", 181 "set_by_input": "input", 182 "computed": "fake_computed", 183 }) 184 if err != nil { 185 return nil, err 186 } 187 config := NewResourceConfig(rawConfig) 188 config.ComputedKeys = []string{"computed"} // fake computed key 189 190 return config, nil 191 }, 192 } 193 ctx := &MockEvalContext{ProviderProvider: provider} 194 rawConfig, err := config.NewRawConfig(map[string]interface{}{ 195 "mock_config": "mock", 196 }) 197 if err != nil { 198 t.Fatalf("NewRawConfig failed: %s", err) 199 } 200 config := NewResourceConfig(rawConfig) 201 202 n := &EvalInputProvider{ 203 Name: "mock", 204 Provider: &provider, 205 Config: &config, 206 } 207 208 result, err := n.Eval(ctx) 209 if err != nil { 210 t.Fatalf("Eval failed: %s", err) 211 } 212 if result != nil { 213 t.Fatalf("Eval returned non-nil result %#v", result) 214 } 215 216 if !ctx.SetProviderInputCalled { 217 t.Fatalf("ctx.SetProviderInput wasn't called") 218 } 219 220 if got, want := ctx.SetProviderInputName, "mock"; got != want { 221 t.Errorf("wrong provider name %q; want %q", got, want) 222 } 223 224 inputCfg := ctx.SetProviderInputConfig 225 want := map[string]interface{}{ 226 "set_in_config": "input", 227 "set_by_input": "input", 228 // "computed" is omitted because it value isn't known at input time 229 } 230 if !reflect.DeepEqual(inputCfg, want) { 231 t.Errorf("got incorrect input config %#v; want %#v", inputCfg, want) 232 } 233 }