From c3eb9c5d2af64939ee206300de1ae47af0920a4b Mon Sep 17 00:00:00 2001 From: Chris Stephens Date: Wed, 13 Mar 2019 14:53:43 -0700 Subject: [PATCH] Add data source tests --- ..._google_folder_organization_policy_test.go | 91 +++++++++++++++++++ ...google_project_organization_policy_test.go | 47 ++++++++++ 2 files changed, 138 insertions(+) create mode 100644 third_party/terraform/tests/data_source_google_folder_organization_policy_test.go create mode 100644 third_party/terraform/tests/data_source_google_project_organization_policy_test.go diff --git a/third_party/terraform/tests/data_source_google_folder_organization_policy_test.go b/third_party/terraform/tests/data_source_google_folder_organization_policy_test.go new file mode 100644 index 000000000000..7a631d1d7f7c --- /dev/null +++ b/third_party/terraform/tests/data_source_google_folder_organization_policy_test.go @@ -0,0 +1,91 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccDataSourceGoogleFolderOrganizationPolicy_basic(t *testing.T) { + folder := acctest.RandomWithPrefix("tf-test") + org := getTestOrgFromEnv(t) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleFolderOrganizationPolicy_basic(org, folder), + Check: testAccDataSourceGoogleOrganizationPolicyCheck( + "data.google_folder_organization_policy.data", + "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" { + display_name = "%s" + parent = "%s" +} + +resource "google_folder_organization_policy" "resource" { + folder = "${google_folder.orgpolicy.name}" + constraint = "serviceuser.services" + + restore_policy { + default = true + } +} + +data "google_folder_organization_policy" "data" { + folder = "${google_folder.orgpolicy.name}" + constraint = "serviceuser.services" +} + `, folder, "organizations/"+org) +} diff --git a/third_party/terraform/tests/data_source_google_project_organization_policy_test.go b/third_party/terraform/tests/data_source_google_project_organization_policy_test.go new file mode 100644 index 000000000000..d4113b82bdc0 --- /dev/null +++ b/third_party/terraform/tests/data_source_google_project_organization_policy_test.go @@ -0,0 +1,47 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceGoogleProjectOrganizationPolicy_basic(t *testing.T) { + project := getTestProjectFromEnv() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleProjectOrganizationPolicy_basic(project), + Check: testAccDataSourceGoogleOrganizationPolicyCheck( + "data.google_project_organization_policy.data", + "google_project_organization_policy.resource"), + }, + }, + }) +} + +func testAccDataSourceGoogleProjectOrganizationPolicy_basic(project string) string { + return fmt.Sprintf(` + + +resource "google_project_organization_policy" "resource" { + project = "%s" + constraint = "constraints/compute.trustedImageProjects" + + list_policy { + allow { + all = true + } + } +} + +data "google_project_organization_policy" "data" { + project = "%s" + constraint = "constraints/compute.trustedImageProjects" +} + `, project, project) +}