Skip to content

Commit

Permalink
common checks
Browse files Browse the repository at this point in the history
  • Loading branch information
lantoli committed Mar 23, 2024
1 parent 666be6d commit 127f169
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ func TestMigProjectIPAccessList_settingIPAddress(t *testing.T) {
{
ExternalProviders: mig.ExternalProviders(),
Config: config,
Check: resource.ComposeTestCheckFunc(
acc.CheckProjectIPAccessListExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "ip_address"),
resource.TestCheckResourceAttrSet(resourceName, "comment"),
resource.TestCheckResourceAttr(resourceName, "ip_address", ipAddress),
resource.TestCheckResourceAttr(resourceName, "comment", comment),
),
Check: resource.ComposeTestCheckFunc(commonChecks(ipAddress, "", "", comment)...),
},
mig.TestStepCheckEmptyPlan(config),
},
Expand All @@ -56,14 +49,7 @@ func TestMigProjectIPAccessList_settingCIDRBlock(t *testing.T) {
{
ExternalProviders: mig.ExternalProviders(),
Config: config,
Check: resource.ComposeTestCheckFunc(
acc.CheckProjectIPAccessListExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "cidr_block"),
resource.TestCheckResourceAttrSet(resourceName, "comment"),
resource.TestCheckResourceAttr(resourceName, "cidr_block", cidrBlock),
resource.TestCheckResourceAttr(resourceName, "comment", comment),
),
Check: resource.ComposeTestCheckFunc(commonChecks("", cidrBlock, "", comment)...),
},
mig.TestStepCheckEmptyPlan(config),
},
Expand Down Expand Up @@ -91,12 +77,7 @@ func TestMigProjectIPAccessList_settingAWSSecurityGroup(t *testing.T) {
{
ExternalProviders: mig.ExternalProviders(),
Config: config,
Check: resource.ComposeTestCheckFunc(
acc.CheckProjectIPAccessListExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttr(resourceName, "aws_security_group", awsSGroup),
resource.TestCheckResourceAttr(resourceName, "comment", comment),
),
Check: resource.ComposeTestCheckFunc(commonChecks("", "", awsSGroup, comment)...),
},
mig.TestStepCheckEmptyPlan(config),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,11 @@ func TestAccProjectIPAccesslist_settingIPAddress(t *testing.T) {
Steps: []resource.TestStep{
{
Config: acc.ConfigProjectIPAccessListWithIPAddress(orgID, projectName, ipAddress, comment),
Check: resource.ComposeTestCheckFunc(
acc.CheckProjectIPAccessListExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "ip_address"),
resource.TestCheckResourceAttrSet(resourceName, "comment"),
resource.TestCheckResourceAttr(resourceName, "ip_address", ipAddress),
resource.TestCheckResourceAttr(resourceName, "comment", comment),

acc.CheckProjectIPAccessListExists(dataSourceName),
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "ip_address"),
resource.TestCheckResourceAttrSet(dataSourceName, "comment"),
resource.TestCheckResourceAttr(dataSourceName, "ip_address", ipAddress),
resource.TestCheckResourceAttr(dataSourceName, "comment", comment),
),
Check: resource.ComposeTestCheckFunc(commonChecks(ipAddress, "", "", comment)...),
},
{
Config: acc.ConfigProjectIPAccessListWithIPAddress(orgID, projectName, updatedIPAddress, updatedComment),
Check: resource.ComposeTestCheckFunc(
acc.CheckProjectIPAccessListExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "ip_address"),
resource.TestCheckResourceAttrSet(resourceName, "comment"),
resource.TestCheckResourceAttr(resourceName, "ip_address", updatedIPAddress),
resource.TestCheckResourceAttr(resourceName, "comment", updatedComment),
),
Check: resource.ComposeTestCheckFunc(commonChecks(updatedIPAddress, "", "", updatedComment)...),
},
},
})
Expand All @@ -80,32 +59,11 @@ func TestAccProjectIPAccessList_settingCIDRBlock(t *testing.T) {
Steps: []resource.TestStep{
{
Config: acc.ConfigProjectIPAccessListWithCIDRBlock(orgID, projectName, cidrBlock, comment),
Check: resource.ComposeTestCheckFunc(
acc.CheckProjectIPAccessListExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "cidr_block"),
resource.TestCheckResourceAttrSet(resourceName, "comment"),
resource.TestCheckResourceAttr(resourceName, "cidr_block", cidrBlock),
resource.TestCheckResourceAttr(resourceName, "comment", comment),

acc.CheckProjectIPAccessListExists(dataSourceName),
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "cidr_block"),
resource.TestCheckResourceAttrSet(dataSourceName, "comment"),
resource.TestCheckResourceAttr(dataSourceName, "cidr_block", cidrBlock),
resource.TestCheckResourceAttr(dataSourceName, "comment", comment),
),
Check: resource.ComposeTestCheckFunc(commonChecks("", cidrBlock, "", comment)...),
},
{
Config: acc.ConfigProjectIPAccessListWithCIDRBlock(orgID, projectName, updatedCIDRBlock, updatedComment),
Check: resource.ComposeTestCheckFunc(
acc.CheckProjectIPAccessListExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "cidr_block"),
resource.TestCheckResourceAttrSet(resourceName, "comment"),
resource.TestCheckResourceAttr(resourceName, "cidr_block", updatedCIDRBlock),
resource.TestCheckResourceAttr(resourceName, "comment", updatedComment),
),
Check: resource.ComposeTestCheckFunc(commonChecks("", updatedCIDRBlock, "", updatedComment)...),
},
},
})
Expand Down Expand Up @@ -133,25 +91,11 @@ func TestAccProjectIPAccessList_settingAWSSecurityGroup(t *testing.T) {
Steps: []resource.TestStep{
{
Config: acc.ConfigProjectIPAccessListWithAWSSecurityGroup(orgID, projectName, providerName, vpcID, awsAccountID, vpcCIDRBlock, awsRegion, awsSGroup, comment),
Check: resource.ComposeTestCheckFunc(
acc.CheckProjectIPAccessListExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttr(resourceName, "aws_security_group", awsSGroup),
resource.TestCheckResourceAttr(resourceName, "comment", comment),

acc.CheckProjectIPAccessListExists(dataSourceName),
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
resource.TestCheckResourceAttr(dataSourceName, "aws_security_group", awsSGroup),
resource.TestCheckResourceAttr(dataSourceName, "comment", comment),
),
Check: resource.ComposeTestCheckFunc(commonChecks("", "", awsSGroup, comment)...),
},
{
Config: acc.ConfigProjectIPAccessListWithAWSSecurityGroup(orgID, projectName, providerName, vpcID, awsAccountID, vpcCIDRBlock, awsRegion, updatedAWSSgroup, updatedComment),
Check: resource.ComposeTestCheckFunc(
acc.CheckProjectIPAccessListExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "aws_security_group", updatedAWSSgroup),
resource.TestCheckResourceAttr(resourceName, "comment", updatedComment),
),
Check: resource.ComposeTestCheckFunc(commonChecks("", "", updatedAWSSgroup, updatedComment)...),
},
},
})
Expand Down Expand Up @@ -261,3 +205,30 @@ func TestAccProjectIPAccessList_importIncorrectId(t *testing.T) {
},
})
}

func commonChecks(ipAddress, cidrBlock, awsSGroup, comment string) []resource.TestCheckFunc {
checks := []resource.TestCheckFunc{
acc.CheckProjectIPAccessListExists(resourceName),
acc.CheckProjectIPAccessListExists(dataSourceName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
resource.TestCheckResourceAttr(resourceName, "comment", comment),
resource.TestCheckResourceAttr(dataSourceName, "comment", comment),
}
if ipAddress != "" {
checks = append(checks,
resource.TestCheckResourceAttr(resourceName, "ip_address", ipAddress),
resource.TestCheckResourceAttr(dataSourceName, "ip_address", ipAddress))
}
if cidrBlock != "" {
checks = append(checks,
resource.TestCheckResourceAttr(resourceName, "cidr_block", cidrBlock),
resource.TestCheckResourceAttr(dataSourceName, "cidr_block", cidrBlock))
}
if awsSGroup != "" {
checks = append(checks,
resource.TestCheckResourceAttr(resourceName, "aws_security_group", awsSGroup),
resource.TestCheckResourceAttr(dataSourceName, "aws_security_group", awsSGroup))
}
return checks
}

0 comments on commit 127f169

Please sign in to comment.