Skip to content

Commit be14669

Browse files
YakDriverkmoe
authored andcommitted
testing: Add SkipTestOnError to test steps
1 parent 7c7b5ec commit be14669

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

helper/acctest/skip_on_error.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package acctest
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
// SkipOnErrorContains helps skip tests based on a portion of an error message
11+
// which is contained in a complete error message.
12+
func SkipOnErrorContains(s string) resource.SkipOnErrorFunc {
13+
return func(err error) bool {
14+
if err == nil {
15+
return false
16+
}
17+
18+
return strings.Contains(err.Error(), s)
19+
}
20+
}
21+
22+
// SkipOnErrorRegexp helps skip tests based on a regexp that matches an error
23+
// message.
24+
func SkipOnErrorRegexp(re *regexp.Regexp) resource.SkipOnErrorFunc {
25+
return func(err error) bool {
26+
if err == nil {
27+
return false
28+
}
29+
30+
return re.MatchString(err.Error())
31+
}
32+
}

helper/acctest/skip_on_error_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package acctest
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"testing"
7+
)
8+
9+
func TestSkipOnErrorContains(t *testing.T) {
10+
testCases := []struct {
11+
s string
12+
err error
13+
expected bool
14+
}{
15+
{
16+
s: "Operations related to PublicDNS are not supported in this aws partition",
17+
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"),
18+
expected: true,
19+
},
20+
{
21+
s: "Operations related to PublicDNS are not supported in this aws partition",
22+
err: fmt.Errorf("Error: unrelated error: Smog Patrol. Had your emissions checked?\nstatus code: 400, request id: 395ef7ef-be89-48a1-98ec-0bcb0a517825"),
23+
expected: false,
24+
},
25+
{
26+
s: "Operations related to PublicDNS are not supported in this aws partition",
27+
expected: false,
28+
},
29+
}
30+
31+
for i, tc := range testCases {
32+
f := SkipOnErrorContains(tc.s)
33+
if f(tc.err) != tc.expected {
34+
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)
35+
}
36+
}
37+
}
38+
39+
func TestSkipOnErrorRegexp(t *testing.T) {
40+
testCases := []struct {
41+
re *regexp.Regexp
42+
err error
43+
expected bool
44+
}{
45+
{
46+
re: regexp.MustCompile(`Operations related to [a-zA-Z]+ are not supported in this aws partition`),
47+
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"),
48+
expected: true,
49+
},
50+
{
51+
re: regexp.MustCompile(`Operations related to [a-zA-Z]+ are not supported in this aws partition`),
52+
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"),
53+
expected: false,
54+
},
55+
{
56+
re: regexp.MustCompile(`Operations related to [a-zA-Z]+ are not supported in this aws partition`),
57+
expected: false,
58+
},
59+
}
60+
61+
for i, tc := range testCases {
62+
f := SkipOnErrorRegexp(tc.re)
63+
if f(tc.err) != tc.expected {
64+
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)
65+
}
66+
}
67+
}

helper/resource/testing.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ type ImportStateCheckFunc func([]*terraform.InstanceState) error
258258
// generation for ImportState tests.
259259
type ImportStateIdFunc func(*terraform.State) (string, error)
260260

261+
// SkipOnErrorFunc is a function to determine whether an error should cause a
262+
// test to be skipped.
263+
type SkipOnErrorFunc func(error) bool
264+
261265
// TestCase is a single acceptance test case used to test the apply/destroy
262266
// lifecycle of a resource in a specific configuration.
263267
//
@@ -323,6 +327,11 @@ type TestCase struct {
323327
// to allow the tester to test that the resource is truly gone.
324328
CheckDestroy TestCheckFunc
325329

330+
// SkipOnError allows the construction of tests that we want to skip if
331+
// they fail with particular errors. The error is passed to a function that
332+
// determines whether to skip the test.
333+
SkipOnError SkipOnErrorFunc
334+
326335
// Steps are the apply sequences done within the context of the
327336
// same state. Each step can have its own check to verify correctness.
328337
Steps []TestStep

helper/resource/testing_new.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ func runNewTest(t testing.T, c TestCase, helper *plugintest.Helper) {
113113
if !step.ExpectError.MatchString(err.Error()) {
114114
t.Fatalf("Step %d/%d error running import, expected an error with pattern (%s), no match on: %s", i+1, len(c.Steps), step.ExpectError.String(), err)
115115
}
116+
} else if err != nil && c.SkipOnError != nil && c.SkipOnError(err) {
117+
t.Skipf("[WARN] Skipping test, step %d/%d error passed SkipOnError: %s", i+1, len(c.Steps), err)
116118
} else {
117119
if err != nil {
118120
t.Fatalf("Step %d/%d error running import: %s", i+1, len(c.Steps), err)
@@ -130,6 +132,8 @@ func runNewTest(t testing.T, c TestCase, helper *plugintest.Helper) {
130132
if !step.ExpectError.MatchString(err.Error()) {
131133
t.Fatalf("Step %d/%d, expected an error with pattern, no match on: %s", i+1, len(c.Steps), err)
132134
}
135+
} else if err != nil && c.SkipOnError != nil && c.SkipOnError(err) {
136+
t.Skipf("[WARN] Skipping test, step %d/%d error passed SkipOnError: %s", i+1, len(c.Steps), err)
133137
} else {
134138
if err != nil {
135139
t.Fatalf("Step %d/%d error: %s", i+1, len(c.Steps), err)

0 commit comments

Comments
 (0)