Skip to content

Commit

Permalink
update data source tests that compare lists of fields to compare all …
Browse files Browse the repository at this point in the history
…fields (#720)

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and danawillow committed May 20, 2019
1 parent ab3f8a8 commit 3e46ad8
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 309 deletions.
36 changes: 1 addition & 35 deletions google-beta/data_source_dns_managed_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourceDnsManagedZone_basic(t *testing.T) {
Expand All @@ -19,45 +18,12 @@ func TestAccDataSourceDnsManagedZone_basic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccDataSourceDnsManagedZone_basic(),
Check: testAccDataSourceDnsManagedZoneCheck("data.google_dns_managed_zone.qa", "google_dns_managed_zone.foo"),
Check: checkDataSourceStateMatchesResourceState("data.google_dns_managed_zone.qa", "google_dns_managed_zone.foo"),
},
},
})
}

func testAccDataSourceDnsManagedZoneCheck(dsName, rsName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ds, ok := s.RootModule().Resources[rsName]
if !ok {
return fmt.Errorf("can't find resource called %s in state", rsName)
}

rs, ok := s.RootModule().Resources[dsName]
if !ok {
return fmt.Errorf("can't find data source called %s in state", dsName)
}

dsAttr := ds.Primary.Attributes
rsAttr := rs.Primary.Attributes

attrsToTest := []string{
"id",
"name",
"description",
"dns_name",
"name_servers",
}

for _, attrToTest := range attrsToTest {
if dsAttr[attrToTest] != rsAttr[attrToTest] {
return fmt.Errorf("%s is %s; want %s", attrToTest, dsAttr[attrToTest], rsAttr[attrToTest])
}
}

return nil
}
}

func testAccDataSourceDnsManagedZone_basic() string {
return fmt.Sprintf(`
resource "google_dns_managed_zone" "foo" {
Expand Down
45 changes: 1 addition & 44 deletions google-beta/data_source_google_cloudfunctions_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourceGoogleCloudFunctionsFunction_basic(t *testing.T) {
Expand All @@ -31,56 +30,14 @@ func TestAccDataSourceGoogleCloudFunctionsFunction_basic(t *testing.T) {
Config: testAccDataSourceGoogleCloudFunctionsFunctionConfig(functionName,
bucketName, zipFilePath),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceGoogleCloudFunctionsFunctionCheck(funcDataNameHttp,
checkDataSourceStateMatchesResourceState(funcDataNameHttp,
"google_cloudfunctions_function.function_http"),
),
},
},
})
}

func testAccDataSourceGoogleCloudFunctionsFunctionCheck(dataSourceName string, resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ds, ok := s.RootModule().Resources[dataSourceName]
if !ok {
return fmt.Errorf("root module has no resource called %s", dataSourceName)
}

rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("can't find %s in state", resourceName)
}

dsAttr := ds.Primary.Attributes
rsAttr := rs.Primary.Attributes

cloudFuncAttrToCheck := []string{
"name",
"region",
"description",
"available_memory_mb",
"timeout",
"storage_bucket",
"storage_object",
"entry_point",
"trigger_http",
}

for _, attr := range cloudFuncAttrToCheck {
if dsAttr[attr] != rsAttr[attr] {
return fmt.Errorf(
"%s is %s; want %s",
attr,
dsAttr[attr],
rsAttr[attr],
)
}
}

return nil
}
}

func testAccDataSourceGoogleCloudFunctionsFunctionConfig(functionName, bucketName, zipFilePath string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
Expand Down
38 changes: 1 addition & 37 deletions google-beta/data_source_google_compute_backend_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourceComputeBackendService_basic(t *testing.T) {
Expand All @@ -22,47 +21,12 @@ func TestAccDataSourceComputeBackendService_basic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccDataSourceComputeBackendService_basic(serviceName, checkName),
Check: testAccDataSourceComputeBackendServiceCheck("data.google_compute_backend_service.baz", "google_compute_backend_service.foobar"),
Check: checkDataSourceStateMatchesResourceState("data.google_compute_backend_service.baz", "google_compute_backend_service.foobar"),
},
},
})
}

func testAccDataSourceComputeBackendServiceCheck(dsName, rsName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[rsName]
if !ok {
return fmt.Errorf("can't find resource called %s in state", rsName)
}

ds, ok := s.RootModule().Resources[dsName]
if !ok {
return fmt.Errorf("can't find data source called %s in state", dsName)
}

dsAttr := ds.Primary.Attributes
rsAttr := rs.Primary.Attributes

attrsToTest := []string{
"id",
"name",
"description",
"self_link",
"fingerprint",
"port_name",
"protocol",
}

for _, attrToTest := range attrsToTest {
if dsAttr[attrToTest] != rsAttr[attrToTest] {
return fmt.Errorf("%s is %s; want %s", attrToTest, dsAttr[attrToTest], rsAttr[attrToTest])
}
}

return nil
}
}

func testAccDataSourceComputeBackendService_basic(serviceName, checkName string) string {
return fmt.Sprintf(`
resource "google_compute_backend_service" "foobar" {
Expand Down
86 changes: 20 additions & 66 deletions google-beta/data_source_google_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccContainerClusterDatasource_zonal(t *testing.T) {
Expand All @@ -19,7 +18,16 @@ func TestAccContainerClusterDatasource_zonal(t *testing.T) {
{
Config: testAccContainerClusterDatasource_zonal(),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceGoogleContainerClusterCheck("data.google_container_cluster.kubes", "google_container_cluster.kubes"),
checkDataSourceStateMatchesResourceStateWithIgnores(
"data.google_container_cluster.kubes",
"google_container_cluster.kubes",
// Remove once https://github.com/hashicorp/terraform/issues/21347 is fixed.
map[string]struct{}{
"enable_tpu": {},
"enable_binary_authorization": {},
"pod_security_policy_config.#": {},
},
),
),
},
},
Expand All @@ -36,76 +44,22 @@ func TestAccContainerClusterDatasource_regional(t *testing.T) {
{
Config: testAccContainerClusterDatasource_regional(),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceGoogleContainerClusterCheck("data.google_container_cluster.kubes", "google_container_cluster.kubes"),
checkDataSourceStateMatchesResourceStateWithIgnores(
"data.google_container_cluster.kubes",
"google_container_cluster.kubes",
// Remove once https://github.com/hashicorp/terraform/issues/21347 is fixed.
map[string]struct{}{
"enable_tpu": {},
"enable_binary_authorization": {},
"pod_security_policy_config.#": {},
},
),
),
},
},
})
}

func testAccDataSourceGoogleContainerClusterCheck(dataSourceName string, resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ds, ok := s.RootModule().Resources[dataSourceName]
if !ok {
return fmt.Errorf("root module has no resource called %s", dataSourceName)
}

rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("can't find %s in state", resourceName)
}

dsAttr := ds.Primary.Attributes
rsAttr := rs.Primary.Attributes

clusterAttrToCheck := []string{
"name",
"zone",
"additional_zones",
"addons_config",
"cluster_ipv4_cidr",
"description",
"enable_kubernetes_alpha",
"enable_tpu",
"enable_legacy_abac",
"endpoint",
"enable_legacy_abac",
"instance_group_urls",
"ip_allocation_policy",
"logging_service",
"maintenance_policy",
"master_auth",
"master_auth.0.password",
"master_auth.0.username",
"master_auth.0.client_certificate_config.0.issue_client_certificate",
"master_auth.0.client_certificate",
"master_auth.0.client_key",
"master_auth.0.cluster_ca_certificate",
"master_authorized_networks_config",
"master_version",
"min_master_version",
"monitoring_service",
"network",
"network_policy",
"node_version",
"subnetwork",
}

for _, attr := range clusterAttrToCheck {
if dsAttr[attr] != rsAttr[attr] {
return fmt.Errorf(
"%s is %s; want %s",
attr,
dsAttr[attr],
rsAttr[attr],
)
}
}

return nil
}
}

func testAccContainerClusterDatasource_zonal() string {
return fmt.Sprintf(`
resource "google_container_cluster" "kubes" {
Expand Down
48 changes: 4 additions & 44 deletions google-beta/data_source_google_folder_organization_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourceGoogleFolderOrganizationPolicy_basic(t *testing.T) {
Expand All @@ -19,54 +18,15 @@ func TestAccDataSourceGoogleFolderOrganizationPolicy_basic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleFolderOrganizationPolicy_basic(org, folder),
Check: testAccDataSourceGoogleOrganizationPolicyCheck(
Check: checkDataSourceStateMatchesResourceState(
"data.google_folder_organization_policy.data",
"google_folder_organization_policy.resource"),
"google_folder_organization_policy.resource",
),
},
},
})
}

func testAccDataSourceGoogleOrganizationPolicyCheck(dataSourceName string, resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ds, ok := s.RootModule().Resources[dataSourceName]
if !ok {
return fmt.Errorf("root module has no resource called %s", dataSourceName)
}

rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("can't find %s in state", resourceName)
}

dsAttr := ds.Primary.Attributes
rsAttr := rs.Primary.Attributes

cloudFuncAttrToCheck := []string{
"name",
"folder",
"constraint",
"version",
"list_policy",
"restore_policy",
"boolean_policy",
}

for _, attr := range cloudFuncAttrToCheck {
if dsAttr[attr] != rsAttr[attr] {
return fmt.Errorf(
"%s is %s; want %s",
attr,
dsAttr[attr],
rsAttr[attr],
)
}
}

return nil
}
}

func testAccDataSourceGoogleFolderOrganizationPolicy_basic(org, folder string) string {
return fmt.Sprintf(`
resource "google_folder" "orgpolicy" {
Expand All @@ -84,7 +44,7 @@ resource "google_folder_organization_policy" "resource" {
}
data "google_folder_organization_policy" "data" {
folder = "${google_folder.orgpolicy.name}"
folder = "${google_folder_organization_policy.resource.folder}"
constraint = "serviceuser.services"
}
`, folder, "organizations/"+org)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestAccDataSourceGoogleProjectOrganizationPolicy_basic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleProjectOrganizationPolicy_basic(project),
Check: testAccDataSourceGoogleOrganizationPolicyCheck(
Check: checkDataSourceStateMatchesResourceState(
"data.google_project_organization_policy.data",
"google_project_organization_policy.resource"),
},
Expand All @@ -40,8 +40,8 @@ resource "google_project_organization_policy" "resource" {
}
data "google_project_organization_policy" "data" {
project = "%s"
project = "${google_project_organization_policy.resource.project}"
constraint = "constraints/compute.trustedImageProjects"
}
`, project, project)
`, project)
}
Loading

0 comments on commit 3e46ad8

Please sign in to comment.