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: Fix compilation-time randomization in test configuration, enable semgrep rule to catch coding issue #18220

Merged
merged 2 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .semgrep.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,42 @@ rules:
- pattern: '*aws.Time($VALUE)'
severity: WARNING

- id: helper-acctest-RandInt-compiled
languages: [go]
message: Using `acctest.RandInt()` in constant or variable declaration will execute during compilation and not randomize, pass into string generating function instead
paths:
include:
- aws/
patterns:
- pattern-either:
- pattern: const $CONST = fmt.Sprintf(..., <... acctest.RandInt() ...>, ...)
- pattern: var $VAR = fmt.Sprintf(..., <... acctest.RandInt() ...>, ...)
severity: WARNING

- id: helper-acctest-RandString-compiled
languages: [go]
message: Using `acctest.RandString()` in constant or variable declaration will execute during compilation and not randomize, pass into string generating function instead
paths:
include:
- aws/
patterns:
- pattern-either:
- pattern: const $CONST = fmt.Sprintf(..., <... acctest.RandString(...) ...>, ...)
- pattern: var $VAR = fmt.Sprintf(..., <... acctest.RandString(...) ...>, ...)
severity: WARNING

- id: helper-acctest-RandomWithPrefix-compiled
languages: [go]
message: Using `acctest.RandomWithPrefix()` in constant or variable declaration will execute during compilation and not randomize, pass into string generating function instead
paths:
include:
- aws/
patterns:
- pattern-either:
- pattern: const $CONST = fmt.Sprintf(..., <... acctest.RandomWithPrefix(...) ...>, ...)
- pattern: var $VAR = fmt.Sprintf(..., <... acctest.RandomWithPrefix(...) ...>, ...)
severity: WARNING

- id: helper-schema-Set-extraneous-NewSet-with-flattenStringList
languages: [go]
message: Prefer `flattenStringSet()` function for casting a list of string pointers to a set
Expand Down
19 changes: 11 additions & 8 deletions aws/data_source_aws_cloudfront_distribution_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aws

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/cloudfront"
Expand All @@ -12,13 +11,15 @@ import (
func TestAccAWSDataSourceCloudFrontDistribution_basic(t *testing.T) {
dataSourceName := "data.aws_cloudfront_distribution.test"
resourceName := "aws_cloudfront_distribution.s3_distribution"
rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(cloudfront.EndpointsID, t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(cloudfront.EndpointsID, t) },
ErrorCheck: testAccErrorCheck(t, cloudfront.EndpointsID),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudFrontDistributionData,
Config: testAccAWSCloudFrontDistributionDataConfig(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "domain_name", resourceName, "domain_name"),
Expand All @@ -33,10 +34,12 @@ func TestAccAWSDataSourceCloudFrontDistribution_basic(t *testing.T) {
})
}

var testAccAWSCloudFrontDistributionData = fmt.Sprintf(`
%s

func testAccAWSCloudFrontDistributionDataConfig(rInt int) string {
return composeConfig(
testAccAWSCloudFrontDistributionS3ConfigWithTags(rInt),
`
data "aws_cloudfront_distribution" "test" {
id = aws_cloudfront_distribution.s3_distribution.id
}
`, fmt.Sprintf(testAccAWSCloudFrontDistributionS3ConfigWithTags, acctest.RandInt(), originBucket, logBucket, testAccAWSCloudFrontDistributionRetainConfig()))
`)
}
62 changes: 37 additions & 25 deletions aws/data_source_aws_ecs_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,79 @@ import (
)

func TestAccAWSEcsDataSource_ecsCluster(t *testing.T) {
dataSourceName := "data.aws_ecs_cluster.test"
resourceName := "aws_ecs_cluster.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, ecs.EndpointsID),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsEcsClusterDataSourceConfig,
Config: testAccCheckAwsEcsClusterDataSourceConfig(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_ecs_cluster.default", "status", "ACTIVE"),
resource.TestCheckResourceAttr("data.aws_ecs_cluster.default", "pending_tasks_count", "0"),
resource.TestCheckResourceAttr("data.aws_ecs_cluster.default", "running_tasks_count", "0"),
resource.TestCheckResourceAttr("data.aws_ecs_cluster.default", "registered_container_instances_count", "0"),
resource.TestCheckResourceAttrSet("data.aws_ecs_cluster.default", "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttr(dataSourceName, "pending_tasks_count", "0"),
resource.TestCheckResourceAttr(dataSourceName, "registered_container_instances_count", "0"),
resource.TestCheckResourceAttr(dataSourceName, "running_tasks_count", "0"),
resource.TestCheckResourceAttr(dataSourceName, "status", "ACTIVE"),
),
},
},
})
}

func TestAccAWSEcsDataSource_ecsClusterContainerInsights(t *testing.T) {
dataSourceName := "data.aws_ecs_cluster.test"
resourceName := "aws_ecs_cluster.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, ecs.EndpointsID),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsEcsClusterDataSourceConfigContainerInsights,
Config: testAccCheckAwsEcsClusterDataSourceConfigContainerInsights(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_ecs_cluster.default", "status", "ACTIVE"),
resource.TestCheckResourceAttr("data.aws_ecs_cluster.default", "pending_tasks_count", "0"),
resource.TestCheckResourceAttr("data.aws_ecs_cluster.default", "running_tasks_count", "0"),
resource.TestCheckResourceAttr("data.aws_ecs_cluster.default", "registered_container_instances_count", "0"),
resource.TestCheckResourceAttrSet("data.aws_ecs_cluster.default", "arn"),
resource.TestCheckResourceAttrPair("data.aws_ecs_cluster.default", "setting.#", "aws_ecs_cluster.default", "setting.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttr(dataSourceName, "pending_tasks_count", "0"),
resource.TestCheckResourceAttr(dataSourceName, "registered_container_instances_count", "0"),
resource.TestCheckResourceAttr(dataSourceName, "running_tasks_count", "0"),
resource.TestCheckResourceAttr(dataSourceName, "status", "ACTIVE"),
resource.TestCheckResourceAttrPair(dataSourceName, "setting.#", resourceName, "setting.#"),
),
},
},
})
}

var testAccCheckAwsEcsClusterDataSourceConfig = fmt.Sprintf(`
resource "aws_ecs_cluster" "default" {
name = "default-%d"
func testAccCheckAwsEcsClusterDataSourceConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_ecs_cluster" "test" {
name = %[1]q
}

data "aws_ecs_cluster" "default" {
cluster_name = aws_ecs_cluster.default.name
data "aws_ecs_cluster" "test" {
cluster_name = aws_ecs_cluster.test.name
}
`, rName)
}
`, acctest.RandInt())

var testAccCheckAwsEcsClusterDataSourceConfigContainerInsights = fmt.Sprintf(`
resource "aws_ecs_cluster" "default" {
name = "default-%d"
func testAccCheckAwsEcsClusterDataSourceConfigContainerInsights(rName string) string {
return fmt.Sprintf(`
resource "aws_ecs_cluster" "test" {
name = %[1]q

setting {
name = "containerInsights"
value = "enabled"
}
}

data "aws_ecs_cluster" "default" {
cluster_name = aws_ecs_cluster.default.name
data "aws_ecs_cluster" "test" {
cluster_name = aws_ecs_cluster.test.name
}
`, rName)
}
`, acctest.RandInt())
13 changes: 8 additions & 5 deletions aws/data_source_aws_ecs_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (
func TestAccAWSEcsServiceDataSource_basic(t *testing.T) {
dataSourceName := "data.aws_ecs_service.test"
resourceName := "aws_ecs_service.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, ecs.EndpointsID),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsEcsServiceDataSourceConfig,
Config: testAccCheckAwsEcsServiceDataSourceConfig(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "id", dataSourceName, "arn"),
resource.TestCheckResourceAttrPair(resourceName, "desired_count", dataSourceName, "desired_count"),
Expand All @@ -33,13 +34,14 @@ func TestAccAWSEcsServiceDataSource_basic(t *testing.T) {
})
}

var testAccCheckAwsEcsServiceDataSourceConfig = fmt.Sprintf(`
func testAccCheckAwsEcsServiceDataSourceConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_ecs_cluster" "test" {
name = "tf-acc-%d"
name = %[1]q
}

resource "aws_ecs_task_definition" "test" {
family = "mongodb"
family = %[1]q

container_definitions = <<DEFINITION
[
Expand All @@ -66,4 +68,5 @@ data "aws_ecs_service" "test" {
service_name = aws_ecs_service.test.name
cluster_arn = aws_ecs_cluster.test.arn
}
`, acctest.RandInt())
`, rName)
}
Loading