Skip to content

Add additional validation that the backend used in a run is a supported type #36648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
26 changes: 21 additions & 5 deletions internal/command/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,37 @@ func (c *TestCommand) Run(rawArgs []string) int {
return 1
}

// Per file, ensure backends aren't reused
var duplicateBackendDiags tfdiags.Diagnostics
// Per file, ensure backends:
// * aren't reused
// * are valid types
var backendDiags tfdiags.Diagnostics
for _, tf := range config.Module.Tests {
bucketHashes := make(map[int]string)
// Use an ordered list of backends, so that errors are raised by 2nd+ time
// that a backend config is used in a file.
for _, bc := range orderBackendsByDeclarationLine(tf.BackendConfigs) {
f := backendInit.Backend(bc.Backend.Type)
if f == nil {
detail := fmt.Sprintf("There is no backend type named %q.", bc.Backend.Type)
if msg, removed := backendInit.RemovedBackends[bc.Backend.Type]; removed {
Copy link
Member Author

Choose a reason for hiding this comment

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

This validation needs to be here because it also depends on internal/backend/init, which is hard to import elsewhere

detail = msg
}
backendDiags = backendDiags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unsupported backend type",
Detail: detail,
Subject: &bc.Backend.TypeRange,
})
continue
}

b := f()
schema := b.ConfigSchema()
hash := bc.Backend.Hash(schema)

if runName, exists := bucketHashes[hash]; exists {
// This backend's been encountered before
duplicateBackendDiags = duplicateBackendDiags.Append(
backendDiags = backendDiags.Append(
&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Repeat use of the same backend block",
Expand All @@ -157,8 +173,8 @@ func (c *TestCommand) Run(rawArgs []string) int {
bucketHashes[bc.Backend.Hash(schema)] = bc.Run.Name
}
}
diags = diags.Append(duplicateBackendDiags)
if duplicateBackendDiags.HasErrors() {
diags = diags.Append(backendDiags)
if backendDiags.HasErrors() {
view.Diagnostics(nil, nil, diags)
return 1
}
Expand Down
33 changes: 28 additions & 5 deletions internal/command/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2564,12 +2564,13 @@ Failure! 0 passed, 1 failed.
}
}

// TestTest_ReusedBackendConfiguration asserts that it's not valid to re-use the same backend config (i.e the same state file)
// in parallel runs. This would result in multiple actions attempting to set state, potentially with different resource configurations.
// TestTest_ValidateBackendConfiguration tests validation of how backends are declared in test files:
// * it's not valid to re-use the same backend config (i.e the same state file)
// * it's not valid to use a deprecated backend type
// * it's not valid to use a non-existent backend type
//
// Note - this test is written to assert that diagnostics are returned about re-used backend blocks between run blocks, but it allows either
// of the conflicting run blocks to cause the error to be raised. This is because run blocks without matching state keys aren't run in a
// deterministic order.
// Backend validaton performed in the command package is dependent on the internal/backend/init package,
// which cannot be imported in configuration parsing packages without creating an import cycle.
func TestTest_ReusedBackendConfiguration(t *testing.T) {

testCases := map[string]struct {
Expand Down Expand Up @@ -2600,6 +2601,28 @@ Error: Repeat use of the same backend block
The run "test_2" contains a backend configuration that's already been used in
run "test_1". Sharing the same backend configuration between separate runs
will result in conflicting state updates.
`,
},
"validation detects when a deprecated backend type is used": {
dirName: "removed-backend-type",
expectErr: `
Error: Unsupported backend type

on main.tftest.hcl line 7, in run "test_removed_backend":
7: backend "etcd" {

The "etcd" backend is not supported in Terraform v1.3 or later.
`,
},
"validation detects when a non-existent backend type": {
dirName: "non-existent-backend-type",
expectErr: `
Error: Unsupported backend type

on main.tftest.hcl line 7, in run "test_invalid_backend":
7: backend "foobar" {

There is no backend type named "foobar".
`,
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

variable "input" {
type = string
}

resource "test_resource" "a" {
value = var.input
}

resource "test_resource" "c" {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The "foobar" backend does not exist and isn't a removed backend either
run "test_invalid_backend" {
variables {
input = "foobar"
}

backend "foobar" {
}
}
10 changes: 10 additions & 0 deletions internal/command/testdata/test/removed-backend-type/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

variable "input" {
type = string
}

resource "test_resource" "a" {
value = var.input
}

resource "test_resource" "c" {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The "etcd" backend has been removed from Terraform versions 1.3+
run "test_removed_backend" {
variables {
input = "foobar"
}

backend "etcd" {
}
}
Loading