forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UPSTREAM: Add and use 'internal/tfresource' package. (hashicorp#15477)
- Loading branch information
Showing
2 changed files
with
118 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,21 @@ | ||
package tfresource | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
// NotFound returns true if the error represents a "resource not found" condition. | ||
// Specifically, NotFound returns true if the error is of type resource.NotFoundError. | ||
func NotFound(err error) bool { | ||
_, ok := err.(*resource.NotFoundError) | ||
return ok | ||
} | ||
|
||
// TimedOut returns true if the error represents a "wait timed out" condition. | ||
// Specifically, TimedOut returns true if the error matches all these conditions: | ||
// * err is of type resource.TimeoutError | ||
// * TimeoutError.LastError is nil | ||
func TimedOut(err error) bool { | ||
timeoutErr, ok := err.(*resource.TimeoutError) | ||
return ok && timeoutErr.LastError == nil | ||
} |
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,97 @@ | ||
package tfresource | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestNotFound(t *testing.T) { | ||
testCases := []struct { | ||
Name string | ||
Err error | ||
Expected bool | ||
}{ | ||
{ | ||
Name: "nil error", | ||
Err: nil, | ||
}, | ||
{ | ||
Name: "other error", | ||
Err: errors.New("test"), | ||
}, | ||
{ | ||
Name: "not found error", | ||
Err: &resource.NotFoundError{LastError: errors.New("test")}, | ||
Expected: true, | ||
}, | ||
{ | ||
Name: "wrapped other error", | ||
Err: fmt.Errorf("test: %w", errors.New("test")), | ||
}, | ||
{ | ||
Name: "wrapped not found error", | ||
Err: fmt.Errorf("test: %w", &resource.NotFoundError{LastError: errors.New("test")}), | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.Name, func(t *testing.T) { | ||
got := NotFound(testCase.Err) | ||
|
||
if got != testCase.Expected { | ||
t.Errorf("got %t, expected %t", got, testCase.Expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestTimedOut(t *testing.T) { | ||
testCases := []struct { | ||
Name string | ||
Err error | ||
Expected bool | ||
}{ | ||
{ | ||
Name: "nil error", | ||
Err: nil, | ||
}, | ||
{ | ||
Name: "other error", | ||
Err: errors.New("test"), | ||
}, | ||
{ | ||
Name: "timeout error", | ||
Err: &resource.TimeoutError{}, | ||
Expected: true, | ||
}, | ||
{ | ||
Name: "timeout error non-nil last error", | ||
Err: &resource.TimeoutError{LastError: errors.New("test")}, | ||
}, | ||
{ | ||
Name: "wrapped other error", | ||
Err: fmt.Errorf("test: %w", errors.New("test")), | ||
}, | ||
{ | ||
Name: "wrapped timeout error", | ||
Err: fmt.Errorf("test: %w", &resource.TimeoutError{}), | ||
}, | ||
{ | ||
Name: "wrapped timeout error non-nil last error", | ||
Err: fmt.Errorf("test: %w", &resource.TimeoutError{LastError: errors.New("test")}), | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.Name, func(t *testing.T) { | ||
got := TimedOut(testCase.Err) | ||
|
||
if got != testCase.Expected { | ||
t.Errorf("got %t, expected %t", got, testCase.Expected) | ||
} | ||
}) | ||
} | ||
} |