Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions internal/command/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"path/filepath"
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/terraform/internal/addrs"
backendInit "github.com/hashicorp/terraform/internal/backend/init"
"github.com/hashicorp/terraform/internal/command/arguments"
"github.com/hashicorp/terraform/internal/command/views"
"github.com/hashicorp/terraform/internal/configs"
Expand Down Expand Up @@ -87,6 +90,13 @@ func (c *ValidateCommand) validate(dir string) tfdiags.Diagnostics {

diags = diags.Append(c.validateConfig(cfg))

// Validation of backend block, if present
// Backend blocks live outside the Terraform graph so we have to do this separately.
backend := cfg.Module.Backend
if backend != nil {
diags = diags.Append(c.validateBackend(backend))
}

// Unless excluded, we'll also do a quick validation of the Terraform test files. These live
// outside the Terraform graph so we have to do this separately.
if !c.ParsedArgs.NoTests {
Expand Down Expand Up @@ -157,6 +167,48 @@ func (c *ValidateCommand) validateTestFiles(cfg *configs.Config) tfdiags.Diagnos
return diags
}

// We validate the backend in an offline manner, so we use PrepareConfig to validate the configuration (and ENVs present),
// but we never use the Configure method, as that will interact with third-party systems.
//
// The code in this method is very similar to the `backendInitFromConfig` method, expect it doesn't configure the backend.
func (c *ValidateCommand) validateBackend(cfg *configs.Backend) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics

bf := backendInit.Backend(cfg.Type)
if bf == nil {
detail := fmt.Sprintf("There is no backend type named %q.", cfg.Type)
if msg, removed := backendInit.RemovedBackends[cfg.Type]; removed {
detail = msg
}

diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unsupported backend type",
Detail: detail,
Subject: &cfg.TypeRange,
})
return diags
}

b := bf()
backendSchema := b.ConfigSchema()

decSpec := backendSchema.DecoderSpec()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need test coverage of detecting a missing required attr, and need to confirm there aren't any quirks with remote-state backends that affect the assumptions here.

configVal, hclDiags := hcldec.Decode(cfg.Config, decSpec, nil)
diags = diags.Append(hclDiags)
if hclDiags.HasErrors() {
return diags
}

_, validateDiags := b.PrepareConfig(configVal)
diags = diags.Append(validateDiags)
if validateDiags.HasErrors() {
return diags
}

return diags
}

func (c *ValidateCommand) Synopsis() string {
return "Check whether the configuration is valid"
}
Expand Down
34 changes: 16 additions & 18 deletions internal/command/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,33 +547,31 @@ func TestValidate_backendBlocks(t *testing.T) {
}
})

// TODO: Should this validation be added?
t.Run("NOT invalid when the backend type is unknown", func(t *testing.T) {
t.Run("invalid when the backend type is unknown", func(t *testing.T) {
output, code := setupTest(t, "invalid-backend-configuration/unknown-backend-type")
if code != 0 {
t.Fatalf("expected a successful exit code %d\n\n%s", code, output.Stderr())
if code != 1 {
t.Fatalf("expected an unsuccessful exit code %d\n\n%s", code, output.Stderr())
}
expectedMsg := "Success! The configuration is valid."
if !strings.Contains(output.Stdout(), expectedMsg) {
t.Fatalf("unexpected output content: wanted %q, got: %s",
expectedMsg,
output.Stdout(),
expectedErr := "Error: Unsupported backend type"
if !strings.Contains(output.Stderr(), expectedErr) {
t.Fatalf("unexpected error content: wanted %q, got: %s",
expectedErr,
output.Stderr(),
)
}
})

// Backend blocks aren't validated using their schemas currently.
// TODO: Should this validation be added?
t.Run("NOT invalid when there's an unknown attribute present", func(t *testing.T) {
t.Run("invalid when there's an unknown attribute present", func(t *testing.T) {
output, code := setupTest(t, "invalid-backend-configuration/unknown-attr")
if code != 0 {
t.Fatalf("expected a successful exit code %d\n\n%s", code, output.Stderr())
if code != 1 {
t.Fatalf("expected an unsuccessful exit code %d\n\n%s", code, output.Stdout())
}
expectedMsg := "Success! The configuration is valid."
if !strings.Contains(output.Stdout(), expectedMsg) {
t.Fatalf("unexpected output content: wanted %q, got: %s",
expectedMsg,
output.Stdout(),
expectedErr := "Error: Unsupported argument"
if !strings.Contains(output.Stderr(), expectedErr) {
t.Fatalf("unexpected error content: wanted %q, got: %s",
expectedErr,
output.Stderr(),
)
}
})
Expand Down
Loading