Skip to content

Commit

Permalink
Add data source tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisst committed Mar 13, 2019
1 parent 904e8ec commit c603081
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit c603081

Please sign in to comment.