From 03493f7d465e757fd22d796917b9c049d60fbafe Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 2 Mar 2017 14:07:49 -0800 Subject: [PATCH] command: validate backend config The validation itself was added a couple weeks ago but I forgot to actually call it. :sad: --- command/meta_backend.go | 6 ++++++ command/meta_backend_test.go | 18 ++++++++++++++++++ .../test-fixtures/backend-new-interp/main.tf | 7 +++++++ 3 files changed, 31 insertions(+) create mode 100644 command/test-fixtures/backend-new-interp/main.tf diff --git a/command/meta_backend.go b/command/meta_backend.go index ab39348a52..ea96e7455f 100644 --- a/command/meta_backend.go +++ b/command/meta_backend.go @@ -237,6 +237,12 @@ func (m *Meta) backendConfig(opts *BackendOpts) (*config.Backend, error) { backend.RawConfig = backend.RawConfig.Merge(rc) } + // Validate the backend early. We have to do this before the normal + // config validation pass since backend loading happens earlier. + if errs := backend.Validate(); len(errs) > 0 { + return nil, multierror.Append(nil, errs...) + } + // Return the configuration which may or may not be set return backend, nil } diff --git a/command/meta_backend_test.go b/command/meta_backend_test.go index 02a1d2c40a..dda5db644c 100644 --- a/command/meta_backend_test.go +++ b/command/meta_backend_test.go @@ -267,6 +267,24 @@ func TestMetaBackend_emptyLegacyRemote(t *testing.T) { } } +// Verify that interpolations result in an error +func TestMetaBackend_configureInterpolation(t *testing.T) { + // Create a temporary working directory that is empty + td := tempDir(t) + copy.CopyDir(testFixturePath("backend-new-interp"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + // Setup the meta + m := testMetaBackend(t, nil) + + // Get the backend + _, err := m.Backend(&BackendOpts{Init: true}) + if err == nil { + t.Fatal("should error") + } +} + // Newly configured backend func TestMetaBackend_configureNew(t *testing.T) { // Create a temporary working directory that is empty diff --git a/command/test-fixtures/backend-new-interp/main.tf b/command/test-fixtures/backend-new-interp/main.tf new file mode 100644 index 0000000000..136d0f3baa --- /dev/null +++ b/command/test-fixtures/backend-new-interp/main.tf @@ -0,0 +1,7 @@ +variable "foo" { default = "bar" } + +terraform { + backend "local" { + path = "${var.foo}" + } +}