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

tests/provider: Ensure CheckDestroy functions continue rather than return nil #15847

Closed
bflad opened this issue Oct 27, 2020 · 2 comments
Closed
Labels
linter Pertains to changes to or issues with the various linters. stale Old or inactive issues managed by automation, if no further action taken these will get closed. technical-debt Addresses areas of the codebase that need refactoring or redesign. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.

Comments

@bflad
Copy link
Contributor

bflad commented Oct 27, 2020

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

CheckDestroy functions contain a for loop to check all resource states. If a resource matches and is not found (good in this case), the logic should continue the loop instead of immediately return nil, otherwise if multiple resources exist they will not all be checked.

Potential semgrep Implementation

  - id: CheckDestroy-for-loop-return-nil
    languages: [go]
    message: CheckDestroy functions should use `continue` inside `for` loop instead of `return nil`
    paths:
      include:
        - "*_test.go"
    patterns:
      - pattern: |
          func $FUNC($STATE *terraform.State) error {
            ...
            for _, $RESOURCESTATE := range $STATE.RootModule().Resources {
              ...
              return nil
              ...
            }
            ...
          }
    severity: WARNING

Example report (where line 481 should be updated):

aws/resource_aws_acmpca_certificate_authority_test.go
severity:warning rule:CheckDestroy-for-loop-return-nil: CheckDestroy functions should use `continue` inside `for` loop instead of `return nil`
465:func testAccCheckAwsAcmpcaCertificateAuthorityDestroy(s *terraform.State) error {
466:	conn := testAccProvider.Meta().(*AWSClient).acmpcaconn
467:
468:	for _, rs := range s.RootModule().Resources {
469:		if rs.Type != "aws_acmpca_certificate_authority" {
470:			continue
471:		}
472:
473:		input := &acmpca.DescribeCertificateAuthorityInput{
474:			CertificateAuthorityArn: aws.String(rs.Primary.ID),
475:		}
476:
477:		output, err := conn.DescribeCertificateAuthority(input)
478:
479:		if err != nil {
480:			if isAWSErr(err, acmpca.ErrCodeResourceNotFoundException, "") {
481:				return nil
482:			}
483:			return err
484:		}
485:
486:		if output != nil && output.CertificateAuthority != nil && aws.StringValue(output.CertificateAuthority.Arn) == rs.Primary.ID && aws.StringValue(output.CertificateAuthority.Status) != acmpca.CertificateAuthorityStatusDeleted {
487:			return fmt.Errorf("ACMPCA Certificate Authority %q still exists in non-DELETED state: %s", rs.Primary.ID, aws.StringValue(output.CertificateAuthority.Status))
488:		}
489:	}
490:
491:	return nil
492:
493:}
@bflad bflad added tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. technical-debt Addresses areas of the codebase that need refactoring or redesign. labels Oct 27, 2020
@bflad bflad added the linter Pertains to changes to or issues with the various linters. label Oct 28, 2020
@github-actions
Copy link

Marking this issue as stale due to inactivity. This helps our maintainers find and focus on the active issues. If this issue receives no comments in the next 30 days it will automatically be closed. Maintainers can also remove the stale label.

If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thank you!

@github-actions github-actions bot added the stale Old or inactive issues managed by automation, if no further action taken these will get closed. label Oct 18, 2022
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 18, 2022
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
linter Pertains to changes to or issues with the various linters. stale Old or inactive issues managed by automation, if no further action taken these will get closed. technical-debt Addresses areas of the codebase that need refactoring or redesign. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.
Projects
None yet
Development

No branches or pull requests

1 participant