From 127f169af19ff27d18b01108ab9fb398231f58e3 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Sat, 23 Mar 2024 05:56:43 +0100 Subject: [PATCH] common checks --- ...e_project_ip_access_list_migration_test.go | 25 +---- .../resource_project_ip_access_list_test.go | 95 +++++++------------ 2 files changed, 36 insertions(+), 84 deletions(-) diff --git a/internal/service/projectipaccesslist/resource_project_ip_access_list_migration_test.go b/internal/service/projectipaccesslist/resource_project_ip_access_list_migration_test.go index 4e2222f2b5..f6e17a8956 100644 --- a/internal/service/projectipaccesslist/resource_project_ip_access_list_migration_test.go +++ b/internal/service/projectipaccesslist/resource_project_ip_access_list_migration_test.go @@ -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), }, @@ -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), }, @@ -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), }, diff --git a/internal/service/projectipaccesslist/resource_project_ip_access_list_test.go b/internal/service/projectipaccesslist/resource_project_ip_access_list_test.go index b6bde041d3..8554d4eaff 100644 --- a/internal/service/projectipaccesslist/resource_project_ip_access_list_test.go +++ b/internal/service/projectipaccesslist/resource_project_ip_access_list_test.go @@ -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)...), }, }, }) @@ -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)...), }, }, }) @@ -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)...), }, }, }) @@ -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 +}