-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testing: Add SkipTestOnError to test steps
- Loading branch information
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package acctest | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
// SkipOnErrorContains helps skip tests based on a portion of an error message | ||
// which is contained in a complete error message. | ||
func SkipOnErrorContains(s string) resource.SkipOnErrorFunc { | ||
return func(err error) bool { | ||
if err == nil { | ||
return false | ||
} | ||
|
||
return strings.Contains(err.Error(), s) | ||
} | ||
} | ||
|
||
// SkipOnErrorRegexp helps skip tests based on a regexp that matches an error | ||
// message. | ||
func SkipOnErrorRegexp(re *regexp.Regexp) resource.SkipOnErrorFunc { | ||
return func(err error) bool { | ||
if err == nil { | ||
return false | ||
} | ||
|
||
return re.MatchString(err.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package acctest | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func TestSkipOnErrorContains(t *testing.T) { | ||
testCases := []struct { | ||
s string | ||
err error | ||
expected bool | ||
}{ | ||
{ | ||
s: "Operations related to PublicDNS are not supported in this aws partition", | ||
err: fmt.Errorf("Error: error creating Route53 Hosted Zone: InvalidInput: Operations related to PublicDNS are not supported in this aws partition.\nstatus code: 400, request id: 395ef7ef-be89-48a1-98ec-0bcb0a517825"), | ||
expected: true, | ||
}, | ||
{ | ||
s: "Operations related to PublicDNS are not supported in this aws partition", | ||
err: fmt.Errorf("Error: unrelated error: Smog Patrol. Had your emissions checked?\nstatus code: 400, request id: 395ef7ef-be89-48a1-98ec-0bcb0a517825"), | ||
expected: false, | ||
}, | ||
{ | ||
s: "Operations related to PublicDNS are not supported in this aws partition", | ||
expected: false, | ||
}, | ||
} | ||
|
||
for i, tc := range testCases { | ||
f := SkipOnErrorContains(tc.s) | ||
if f(tc.err) != tc.expected { | ||
t.Fatalf("expected test case %d to be %v but was %v (error portion %s, error message %s)", i, tc.expected, f(tc.err), tc.s, tc.err) | ||
} | ||
} | ||
} | ||
|
||
func TestSkipOnErrorRegexp(t *testing.T) { | ||
testCases := []struct { | ||
re *regexp.Regexp | ||
err error | ||
expected bool | ||
}{ | ||
{ | ||
re: regexp.MustCompile(`Operations related to [a-zA-Z]+ are not supported in this aws partition`), | ||
err: fmt.Errorf("Error: error creating Route53 Hosted Zone: InvalidInput: Operations related to PublicDNS are not supported in this aws partition.\nstatus code: 400, request id: 395ef7ef-be89-48a1-98ec-0bcb0a517825"), | ||
expected: true, | ||
}, | ||
{ | ||
re: regexp.MustCompile(`Operations related to [a-zA-Z]+ are not supported in this aws partition`), | ||
err: fmt.Errorf("Error: unrelated error, You on a scavenger hunt, or did I forget to pay my dinner check?\nstatus code: 400, request id: 395ef7ef-be89-48a1-98ec-0bcb0a517825"), | ||
expected: false, | ||
}, | ||
{ | ||
re: regexp.MustCompile(`Operations related to [a-zA-Z]+ are not supported in this aws partition`), | ||
expected: false, | ||
}, | ||
} | ||
|
||
for i, tc := range testCases { | ||
f := SkipOnErrorRegexp(tc.re) | ||
if f(tc.err) != tc.expected { | ||
t.Fatalf("expected test case %d to be %v but was %v (regexp %s, error message %s)", i, tc.expected, f(tc.err), tc.re, tc.err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters