Skip to content

Commit

Permalink
Fix issue in switch back test error messages changed in Terraform v1.6
Browse files Browse the repository at this point in the history
SwitchBackToRemoteFuncError tests verify error messages, but the error
message for missing bucket key in the s3 backend differs depending on
the Terraform version.
Define a helper function to hide the difference.
  • Loading branch information
minamijoyo committed Oct 5, 2023
1 parent 7591205 commit ae31322
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
10 changes: 4 additions & 6 deletions tfmigrate/multi_state_migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,9 +1010,8 @@ resource "null_resource" "qux" {}
t.Fatalf("expected migrator plan error")
}

expected := "Error: \"bucket\": required field is not set"
if !strings.Contains(err.Error(), expected) {
t.Fatalf("expected migrator plan error to contain %s, got: %s", expected, err.Error())
if !containsBucketRequiredError(err) {
t.Fatalf("expected migrator plan error to contain bucket required error: %s", err.Error())
}
}

Expand Down Expand Up @@ -1130,8 +1129,7 @@ resource "null_resource" "qux" {}
t.Fatalf("expected migrator plan error to contain %s, got: %s", expected, err.Error())
}

expected = "Error: \"bucket\": required field is not set"
if !strings.Contains(err.Error(), expected) {
t.Fatalf("expected migrator plan error to contain %s, got: %s", expected, err.Error())
if !containsBucketRequiredError(err) {
t.Fatalf("expected migrator plan error to contain bucket required error: %s", err.Error())
}
}
10 changes: 4 additions & 6 deletions tfmigrate/state_migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,8 @@ resource "null_resource" "bar" {}
t.Fatalf("expected migrator plan error")
}

expected := "Error: \"bucket\": required field is not set"
if !strings.Contains(err.Error(), expected) {
t.Fatalf("expected migrator plan error to contain %s, got: %s", expected, err.Error())
if !containsBucketRequiredError(err) {
t.Fatalf("expected migrator plan error to contain bucket required error: %s", err.Error())
}
}

Expand Down Expand Up @@ -618,8 +617,7 @@ resource "null_resource" "bar" {}
t.Fatalf("expected migrator plan error to contain %s, got: %s", expected, err.Error())
}

expected = "Error: \"bucket\": required field is not set"
if !strings.Contains(err.Error(), expected) {
t.Fatalf("expected migrator plan error to contain %s, got: %s", expected, err.Error())
if !containsBucketRequiredError(err) {
t.Fatalf("expected migrator plan error to contain bucket required error: %s", err.Error())
}
}
38 changes: 38 additions & 0 deletions tfmigrate/test_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package tfmigrate

import "regexp"

// SwitchBackToRemoteFuncError tests verify error messages, but the
// error message for missing bucket key in the s3 backend differs
// depending on the Terraform version.
// Define a helper function to hide the difference.
//
// # Terraform v1.5
//
// ```
// Error: "bucket": required field is not set
// ```
//
// # Terraform v1.6
//
// ```
//
// Error: Missing Required Value
//
// on main.tf line 4, in terraform:
// 4: backend "s3" {
//
// The attribute "bucket" is required by the backend.
//
// Refer to the backend documentation for additional information which
// attributes are required.
//
// ```
const testBucketRequiredErrorLegacyTF = `Error: "bucket": required field is not set`
const testBucketRequiredErrorTF16 = `The attribute "bucket" is required by the backend`

var testBucketRequiredErrorRE = regexp.MustCompile(testBucketRequiredErrorLegacyTF + `|` + testBucketRequiredErrorTF16)

func containsBucketRequiredError(err error) bool {
return testBucketRequiredErrorRE.MatchString(err.Error())
}

0 comments on commit ae31322

Please sign in to comment.