Skip to content

Commit b086287

Browse files
providers/terraform: test that validation does not configure backend
1 parent 4b7dd00 commit b086287

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

builtin/providers/terraform/data_source_state.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func getBackend(cfg cty.Value) (backend.Backend, cty.Value, tfdiags.Diagnostics)
171171

172172
// Create the client to access our remote state
173173
log.Printf("[DEBUG] Initializing remote state backend: %s", backendType)
174-
f := backendInit.Backend(backendType)
174+
f := getBackendFactory(backendType)
175175
if f == nil {
176176
diags = diags.Append(tfdiags.AttributeValue(
177177
tfdiags.Error,
@@ -216,3 +216,17 @@ func getBackend(cfg cty.Value) (backend.Backend, cty.Value, tfdiags.Diagnostics)
216216

217217
return b, newVal, diags
218218
}
219+
220+
// overrideBackendFactories allows test cases to control the set of available
221+
// backends to allow for more self-contained tests. This should never be set
222+
// in non-test code.
223+
var overrideBackendFactories map[string]backend.InitFn
224+
225+
func getBackendFactory(backendType string) backend.InitFn {
226+
if len(overrideBackendFactories) > 0 {
227+
// Tests may override the set of backend factories.
228+
return overrideBackendFactories[backendType]
229+
}
230+
231+
return backendInit.Backend(backendType)
232+
}

builtin/providers/terraform/data_source_state_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package terraform
22

33
import (
4+
"fmt"
5+
"log"
46
"testing"
57

68
"github.com/apparentlymart/go-dump/dump"
79
"github.com/hashicorp/terraform/backend"
10+
"github.com/hashicorp/terraform/configs/configschema"
11+
"github.com/hashicorp/terraform/states/statemgr"
812
"github.com/hashicorp/terraform/tfdiags"
913
"github.com/zclconf/go-cty/cty"
1014
)
@@ -283,3 +287,64 @@ func TestState_basic(t *testing.T) {
283287
})
284288
}
285289
}
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

Comments
 (0)