-
Notifications
You must be signed in to change notification settings - Fork 232
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
testing: Provider handling of errors, Add ErrorCheck to TestCase #592
Conversation
Since this is intended to be a test-level functionality rather than step-level, I think it would make more sense for the |
da9abaf
to
4bab928
Compare
Super stoked about this! I'm wondering if we should use a custom Go type for the field value, e.g. SkipOnError SkipOnErrorFunc
// ...
type SkipOnErrorFunc func() *regexp.Regexp
// ... or likely better ...
type SkipOnErrorFunc func(error) bool Which would allow for providers to more easily inject custom logic? For example in the AWS Provider, we have this helper that we use in a lot of the // Check service API call error for reasons to skip acceptance testing
// These include missing API endpoints and unsupported API calls
func testAccPreCheckSkipError(err error) bool {
// GovCloud has endpoints that respond with (no message provided after the error code):
// AccessDeniedException:
// Ignore these API endpoints that exist but are not officially enabled
if isAWSErr(err, "AccessDeniedException", "") {
return true
}
// Ignore missing API endpoints
if isAWSErr(err, "RequestError", "send request failed") {
return true
}
// Ignore unsupported API calls
if isAWSErr(err, "UnknownOperationException", "") {
return true
}
if isAWSErr(err, "UnsupportedOperation", "") {
return true
}
return false
} Being able to use something like that directly: resource.TestCase{
// ...
SkipOnError: testAccPreCheckSkipError,
// ...
} Or potentially with some additional helpers to still make the regular expression case easy, e.g. func SkipOnErrorRegexp(re *regexp.Regexp) SkipOnErrorFunc {
return func(err error) bool {
if err == nil {
return false
}
return re.MatchString(err.Error())
}
} And: resource.TestCase{
// ...
SkipOnError: acctest.SkipOnErrorRegexp(regexp.MustCompile(`Operations related to PublicDNS are not supported in this aws partition`)),
// ...
} Sounds amazing! |
91a2493
to
239332a
Compare
@bflad I made most of your suggested changes. The one variation is creating |
239332a
to
45df75d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-binding approval from me. 👍 Whenever the maintainers can take a look it would be greatly appreciated.
45df75d
to
1c3fa94
Compare
2ce34b1
to
7541318
Compare
7541318
to
6b1fbfd
Compare
@paultyng This is the implementation on the AWS Provider side: func testAccAWSRoute53RecordPublicDNSErrorCheck(err error, t *testing.T) error {
re := regexp.MustCompile(`Operations related to PublicDNS are not supported in this aws partition`)
if re.MatchString(err.Error()) {
t.Skipf("skipping test; this partition %s does not support PublicDNS operations", testAccGetPartition())
}
return err
}
func TestAccAWSRoute53Record_basic(t *testing.T) {
var record1 route53.ResourceRecordSet
resourceName := "aws_route53_record.default"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: func(err error) error { return testAccAWSRoute53RecordPublicDNSErrorCheck(err, t) },
IDRefreshName: resourceName,
Providers: testAccProviders,
CheckDestroy: testAccCheckRoute53RecordDestroy,
Steps: []resource.TestStep{
{
Config: testAccRoute53RecordConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53RecordExists(resourceName, &record1),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"allow_overwrite", "weight"},
},
},
})
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM, will also need an @paddycarver or @kmoe sign off as well.
We were just talking about this! If possible, I'd like to wait until early November for some other things to fall in place, but I think we should be able to review and merge this that week, if that timing works out. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this. Looks good to me.
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
Closes #568
Relates hashicorp/terraform-provider-aws#14188
This PR includes
TestCase
field calledErrorCheck
, which takes anErrorCheckFunc
function. The function is called when an error occurs allowing providers the option of handling errors.See #568 for design considerations and an example of using this PR.
In GovCloud, the output from the AWS provider acceptance test above with this PR:
On the standard/commercial partition, the output from the test with this PR (unaffected by this change):