-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40854 from hashicorp/td-testcheckresourceattrset-…
…arn-bc testing: Reduce uses of `resource.TestCheckResourceAttrSet` with ARN attributes: `b` and `c` services
- Loading branch information
Showing
17 changed files
with
257 additions
and
76 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,40 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/YakDriver/regexache" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-provider-aws/internal/acctest" | ||
"github.com/hashicorp/terraform-provider-aws/names" | ||
) | ||
|
||
func test1(t *testing.T) { | ||
ctx := acctest.Context(t) | ||
|
||
const resourceName = "aws_prometheus_scraper.test" | ||
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.PreCheck(ctx, t) }, | ||
ErrorCheck: acctest.ErrorCheck(t, names.AMPServiceID), | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, | ||
CheckDestroy: testAccCheckScraperDestroy(ctx), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccScraperConfig_basic(rName), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
// ruleid: arn-resourceattrset | ||
resource.TestCheckResourceAttrSet(resourceName, names.AttrARN), | ||
// ok: arn-resourceattrset | ||
acctest.MatchResourceAttrRegionalARN(ctx, resourceName, names.AttrARN, "aps", regexache.MustCompile(`scraper/\w+$`)), | ||
// todoruleid: arn-resourceattrset | ||
resource.TestCheckResourceAttrSet(resourceName, names.AttrRoleARN), | ||
// todoruleid: arn-resourceattrset | ||
resource.TestCheckResourceAttrSet(resourceName, "some_other_arn"), | ||
), | ||
}, | ||
}, | ||
}) | ||
|
||
} |
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,60 @@ | ||
rules: | ||
- id: arn-resourceattrset | ||
languages: [go] | ||
message: Use one of the `acctest` ARN value checks or TestCheckResourceAttrPair | ||
paths: | ||
exclude: | ||
- "internal/service/connect" | ||
- "internal/service/controltower" | ||
- "internal/service/datazone" | ||
- "internal/service/deploy" | ||
- "internal/service/directconnect" | ||
- "internal/service/dms" | ||
- "internal/service/docdb" | ||
- "internal/service/docdbelastic" | ||
- "internal/service/drs" | ||
- "internal/service/ec2" | ||
- "internal/service/elasticache" | ||
- "internal/service/elasticbeanstalk" | ||
- "internal/service/elb" | ||
- "internal/service/elbv2" | ||
- "internal/service/events" | ||
- "internal/service/firehose" | ||
- "internal/service/globalaccelerator" | ||
- "internal/service/guardduty" | ||
- "internal/service/iam" | ||
- "internal/service/iot" | ||
- "internal/service/kafka" | ||
- "internal/service/kafkaconnect" | ||
- "internal/service/kendra" | ||
- "internal/service/kinesis" | ||
- "internal/service/kinesisvideo" | ||
- "internal/service/lexmodels" | ||
- "internal/service/lightsail" | ||
- "internal/service/medialive" | ||
- "internal/service/memorydb" | ||
- "internal/service/networkmanager" | ||
- "internal/service/networkmonitor" | ||
- "internal/service/oam" | ||
- "internal/service/organizations" | ||
- "internal/service/pinpoint" | ||
- "internal/service/redshift" | ||
- "internal/service/rekognition" | ||
- "internal/service/resourcegroups" | ||
- "internal/service/route53resolver" | ||
- "internal/service/s3" | ||
- "internal/service/servicediscovery" | ||
- "internal/service/ses" | ||
- "internal/service/signer" | ||
- "internal/service/transcribe" | ||
- "internal/service/transfer" | ||
patterns: | ||
- pattern: | | ||
resource.TestCheckResourceAttrSet($NAME, $ATTR) | ||
- metavariable-pattern: | ||
metavariable: $ATTR | ||
patterns: | ||
- pattern-either: | ||
- pattern: names.AttrARN | ||
# - pattern-regex: arn | ||
severity: ERROR |
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
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,68 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package acctest | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/terraform" | ||
) | ||
|
||
func CheckResourceAttrGlobalARNFormat(ctx context.Context, resourceName, attributeName, arnService, arnFormat string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
resource, err := populateARNFormat(s, resourceName, arnFormat) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return CheckResourceAttrGlobalARN(ctx, resourceName, attributeName, arnService, resource)(s) | ||
} | ||
} | ||
|
||
func CheckResourceAttrRegionalARNFormat(ctx context.Context, resourceName, attributeName, arnService, arnFormat string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
resource, err := populateARNFormat(s, resourceName, arnFormat) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return CheckResourceAttrRegionalARN(ctx, resourceName, attributeName, arnService, resource)(s) | ||
} | ||
} | ||
|
||
func populateARNFormat(s *terraform.State, resourceName, arnFormat string) (string, error) { | ||
is, err := PrimaryInstanceState(s, resourceName) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var buf strings.Builder | ||
str := arnFormat | ||
for str != "" { | ||
var ( | ||
stuff string | ||
found bool | ||
) | ||
stuff, str, found = strings.Cut(str, "{") | ||
buf.WriteString(stuff) | ||
if found { | ||
var param string | ||
param, str, found = strings.Cut(str, "}") | ||
if !found { | ||
return "", fmt.Errorf("missing closing '}' in ARN format %q", arnFormat) | ||
} | ||
|
||
attr, ok := is.Attributes[param] | ||
if !ok { | ||
return "", fmt.Errorf("attribute %q not found in resource %q, referenced in ARN format %q", param, resourceName, arnFormat) | ||
} | ||
buf.WriteString(attr) | ||
} | ||
} | ||
|
||
return buf.String(), 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
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
Oops, something went wrong.