|
1 | 1 | package terraform |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "log" |
4 | 6 | "testing" |
5 | 7 |
|
6 | 8 | "github.com/apparentlymart/go-dump/dump" |
7 | 9 | "github.com/hashicorp/terraform/backend" |
| 10 | + "github.com/hashicorp/terraform/configs/configschema" |
| 11 | + "github.com/hashicorp/terraform/states/statemgr" |
8 | 12 | "github.com/hashicorp/terraform/tfdiags" |
9 | 13 | "github.com/zclconf/go-cty/cty" |
10 | 14 | ) |
@@ -283,3 +287,64 @@ func TestState_basic(t *testing.T) { |
283 | 287 | }) |
284 | 288 | } |
285 | 289 | } |
| 290 | + |
| 291 | +func TestState_validation(t *testing.T) { |
| 292 | + // The main test TestState_basic covers both validation and reading of |
| 293 | + // state snapshots, so this additional test is here only to verify that |
| 294 | + // the validation step in isolation does not attempt to configure |
| 295 | + // the backend. |
| 296 | + overrideBackendFactories = map[string]backend.InitFn{ |
| 297 | + "failsconfigure": func() backend.Backend { |
| 298 | + return backendFailsConfigure{} |
| 299 | + }, |
| 300 | + } |
| 301 | + defer func() { |
| 302 | + // undo our overrides so we won't affect other tests |
| 303 | + overrideBackendFactories = nil |
| 304 | + }() |
| 305 | + |
| 306 | + schema := dataSourceRemoteStateGetSchema().Block |
| 307 | + config, err := schema.CoerceValue(cty.ObjectVal(map[string]cty.Value{ |
| 308 | + "backend": cty.StringVal("failsconfigure"), |
| 309 | + "config": cty.EmptyObjectVal, |
| 310 | + })) |
| 311 | + if err != nil { |
| 312 | + t.Fatalf("unexpected error: %s", err) |
| 313 | + } |
| 314 | + |
| 315 | + diags := dataSourceRemoteStateValidate(config) |
| 316 | + if diags.HasErrors() { |
| 317 | + t.Fatalf("unexpected errors\n%s", diags.Err().Error()) |
| 318 | + } |
| 319 | +} |
| 320 | + |
| 321 | +type backendFailsConfigure struct{} |
| 322 | + |
| 323 | +func (b backendFailsConfigure) ConfigSchema() *configschema.Block { |
| 324 | + log.Printf("[TRACE] backendFailsConfigure.ConfigSchema") |
| 325 | + return &configschema.Block{} // intentionally empty configuration schema |
| 326 | +} |
| 327 | + |
| 328 | +func (b backendFailsConfigure) PrepareConfig(given cty.Value) (cty.Value, tfdiags.Diagnostics) { |
| 329 | + // No special actions to take here |
| 330 | + return given, nil |
| 331 | +} |
| 332 | + |
| 333 | +func (b backendFailsConfigure) Configure(config cty.Value) tfdiags.Diagnostics { |
| 334 | + log.Printf("[TRACE] backendFailsConfigure.Configure(%#v)", config) |
| 335 | + var diags tfdiags.Diagnostics |
| 336 | + diags = diags.Append(fmt.Errorf("Configure should never be called")) |
| 337 | + return diags |
| 338 | +} |
| 339 | + |
| 340 | +func (b backendFailsConfigure) StateMgr(workspace string) (statemgr.Full, error) { |
| 341 | + return nil, fmt.Errorf("StateMgr not implemented") |
| 342 | +} |
| 343 | + |
| 344 | +func (b backendFailsConfigure) DeleteWorkspace(name string) error { |
| 345 | + return fmt.Errorf("DeleteWorkspace not implemented") |
| 346 | +} |
| 347 | + |
| 348 | +func (b backendFailsConfigure) Workspaces() ([]string, error) { |
| 349 | + return nil, fmt.Errorf("Workspaces not implemented") |
| 350 | +} |
0 commit comments