Skip to content
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

Merged
merged 2 commits into from
Nov 4, 2020

Conversation

YakDriver
Copy link
Member

@YakDriver YakDriver commented Sep 25, 2020

Closes #568
Relates hashicorp/terraform-provider-aws#14188

This PR includes

  1. A new TestCase field called ErrorCheck, which takes an ErrorCheckFunc 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:

    resource_aws_route53_record_test.go:113: skipping test; this partition aws-us-gov does not support
PublicDNS operations
--- SKIP: TestAccAWSRoute53Record_basic (6.64s)

On the standard/commercial partition, the output from the test with this PR (unaffected by this change):

--- PASS: TestAccAWSRoute53Record_basic (110.87s)

@YakDriver
Copy link
Member Author

Since this is intended to be a test-level functionality rather than step-level, I think it would make more sense for the SkipTestOnError field to be part of TestCase rather than TestStep. Thoughts?

@YakDriver YakDriver changed the title testing: Add SkipTestOnError to test steps testing: Add SkipOnError to test steps Sep 25, 2020
@YakDriver YakDriver changed the title testing: Add SkipOnError to test steps testing: Add SkipOnError to TestCase Sep 25, 2020
@bflad
Copy link
Contributor

bflad commented Sep 25, 2020

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 PreCheck functions:

// 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!

@YakDriver YakDriver force-pushed the f-skip-regex-error branch 2 times, most recently from 91a2493 to 239332a Compare September 28, 2020 19:48
@YakDriver
Copy link
Member Author

@bflad I made most of your suggested changes. The one variation is creating acctest.SkipOnErrorContains() rather than acctest.SkipOnErrorRegexp() to avoid all the regexp.MustCompile() boilerplate everywhere.

helper/acctest/skip_on_error.go Outdated Show resolved Hide resolved
helper/acctest/skip_on_error_test.go Outdated Show resolved Hide resolved
helper/acctest/skip_on_error_test.go Outdated Show resolved Hide resolved
@bflad bflad added the enhancement New feature or request label Sep 29, 2020
Copy link
Contributor

@bflad bflad left a 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.

@YakDriver YakDriver force-pushed the f-skip-regex-error branch 2 times, most recently from 2ce34b1 to 7541318 Compare October 23, 2020 17:13
@YakDriver
Copy link
Member Author

@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"},
			},
		},
	})
}

Copy link
Contributor

@paultyng paultyng left a 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.

@paddycarver
Copy link
Contributor

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.

@paddycarver paddycarver requested a review from kmoe October 27, 2020 17:08
Copy link
Member

@kmoe kmoe left a 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.

@ghost
Copy link

ghost commented Dec 5, 2020

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.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Give providers option to handle testing errors (e.g., skip tests based on error)
5 participants