From b82949bdcd9c69fbea076fb343a53d6097f796d8 Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Tue, 19 Dec 2017 14:40:24 -0800 Subject: [PATCH] Reduce number of new project creation (#880) --- google/resource_google_project_test.go | 175 ++----------------------- 1 file changed, 14 insertions(+), 161 deletions(-) diff --git a/google/resource_google_project_test.go b/google/resource_google_project_test.go index 502f28b8..8eab7276 100644 --- a/google/resource_google_project_test.go +++ b/google/resource_google_project_test.go @@ -69,10 +69,11 @@ func TestAccGoogleProject_create(t *testing.T) { // Test that a Project resource can be created with an associated // billing account -func TestAccGoogleProject_createBilling(t *testing.T) { +func TestAccGoogleProject_billing(t *testing.T) { t.Parallel() - org := getTestOrgFromEnv(t) + skipIfEnvNotSet(t, "GOOGLE_BILLING_ACCOUNT_2") + billingId2 := os.Getenv("GOOGLE_BILLING_ACCOUNT_2") billingId := getTestBillingAccountFromEnv(t) pid := "terraform-" + acctest.RandString(10) resource.Test(t, resource.TestCase{ @@ -86,69 +87,12 @@ func TestAccGoogleProject_createBilling(t *testing.T) { testAccCheckGoogleProjectHasBillingAccount("google_project.acceptance", pid, billingId), ), }, + // Make sure import supports billing account resource.TestStep{ ResourceName: "google_project.acceptance", ImportState: true, ImportStateVerify: true, }, - }, - }) -} - -// Test that a Project resource can be created with labels -func TestAccGoogleProject_createLabels(t *testing.T) { - t.Parallel() - - org := getTestOrgFromEnv(t) - pid := "terraform-" + acctest.RandString(10) - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccGoogleProject_createLabels(pid, pname, org, "test", "that"), - Check: resource.ComposeTestCheckFunc( - testAccCheckGoogleProjectHasLabels("google_project.acceptance", pid, map[string]string{"test": "that"}), - ), - }, - resource.TestStep{ - ResourceName: "google_project.acceptance", - ImportState: true, - ImportStateVerify: true, - }, - }, - }) -} - -// Test that a Project resource can be created and updated -// with billing account information -func TestAccGoogleProject_updateBilling(t *testing.T) { - t.Parallel() - - skipIfEnvNotSet(t, "GOOGLE_BILLING_ACCOUNT_2") - - org := getTestOrgFromEnv(t) - billingId := getTestBillingAccountFromEnv(t) - billingId2 := os.Getenv("GOOGLE_BILLING_ACCOUNT_2") - pid := "terraform-" + acctest.RandString(10) - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - // This step creates a new project without a billing account - resource.TestStep{ - Config: testAccGoogleProject_create(pid, pname, org), - Check: resource.ComposeTestCheckFunc( - testAccCheckGoogleProjectExists("google_project.acceptance", pid), - ), - }, - // Update to include a billing account - resource.TestStep{ - Config: testAccGoogleProject_createBilling(pid, pname, org, billingId), - Check: resource.ComposeTestCheckFunc( - testAccCheckGoogleProjectHasBillingAccount("google_project.acceptance", pid, billingId), - ), - }, // Update to a different billing account resource.TestStep{ Config: testAccGoogleProject_createBilling(pid, pname, org, billingId2), @@ -167,9 +111,8 @@ func TestAccGoogleProject_updateBilling(t *testing.T) { }) } -// Test that a Project resource merges the IAM policies that already -// exist, and won't lock people out. -func TestAccGoogleProject_merge(t *testing.T) { +// Test that a Project resource can be created with labels +func TestAccGoogleProject_labels(t *testing.T) { t.Parallel() org := getTestOrgFromEnv(t) @@ -178,59 +121,26 @@ func TestAccGoogleProject_merge(t *testing.T) { PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ - // when policy_data is set, merge { - Config: testAccGoogleProject_toMerge(pid, pname, org), + Config: testAccGoogleProject_labels(pid, pname, org, map[string]string{"test": "that"}), Check: resource.ComposeTestCheckFunc( - testAccCheckGoogleProjectExists("google_project.acceptance", pid), - testAccCheckGoogleProjectHasMoreBindingsThan(pid, 1), + testAccCheckGoogleProjectHasLabels("google_project.acceptance", pid, map[string]string{"test": "that"}), ), }, - // when policy_data is unset, restore to what it was + // Make sure import supports labels { - Config: testAccGoogleProject_mergeEmpty(pid, pname, org), - Check: resource.ComposeTestCheckFunc( - testAccCheckGoogleProjectExists("google_project.acceptance", pid), - testAccCheckGoogleProjectHasMoreBindingsThan(pid, 0), - ), - }, - }, - }) -} - -// Test that a Project resource can be updated with labels -func TestAccGoogleProject_updateLabels(t *testing.T) { - t.Parallel() - - org := getTestOrgFromEnv(t) - pid := "terraform-" + acctest.RandString(10) - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - // create project without labels - { - Config: testAccGoogleProject_create(pid, pname, org), - Check: resource.ComposeTestCheckFunc( - testAccCheckGoogleProjectExists("google_project.acceptance", pid), - ), + ResourceName: "google_project.acceptance", + ImportState: true, + ImportStateVerify: true, }, // update project with labels { - Config: testAccGoogleProject_updateLabels(pid, pname, org, map[string]string{"label": "label-value"}), + Config: testAccGoogleProject_labels(pid, pname, org, map[string]string{"label": "label-value"}), Check: resource.ComposeTestCheckFunc( testAccCheckGoogleProjectExists("google_project.acceptance", pid), testAccCheckGoogleProjectHasLabels("google_project.acceptance", pid, map[string]string{"label": "label-value"}), ), }, - // update project with other labels - { - Config: testAccGoogleProject_updateLabels(pid, pname, org, map[string]string{"empty-label": ""}), - Check: resource.ComposeTestCheckFunc( - testAccCheckGoogleProjectExists("google_project.acceptance", pid), - testAccCheckGoogleProjectHasLabels("google_project.acceptance", pid, map[string]string{"empty-label": ""}), - ), - }, // update project delete labels { Config: testAccGoogleProject_create(pid, pname, org), @@ -288,19 +198,6 @@ func testAccCheckGoogleProjectHasBillingAccount(r, pid, billingId string) resour } } -func testAccCheckGoogleProjectHasMoreBindingsThan(pid string, count int) resource.TestCheckFunc { - return func(s *terraform.State) error { - policy, err := getProjectIamPolicy(pid, testAccProvider.Meta().(*Config)) - if err != nil { - return err - } - if len(policy.Bindings) <= count { - return fmt.Errorf("Expected more than %d bindings, got %d: %#v", count, len(policy.Bindings), policy.Bindings) - } - return nil - } -} - func testAccCheckGoogleProjectHasLabels(r, pid string, expected map[string]string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[r] @@ -372,51 +269,7 @@ func testAccCheckGoogleProjectHasNoLabels(r, pid string) resource.TestCheckFunc } } -func testAccGoogleProject_toMerge(pid, name, org string) string { - return fmt.Sprintf(` -resource "google_project" "acceptance" { - project_id = "%s" - name = "%s" - org_id = "%s" -} - -resource "google_project_iam_policy" "acceptance" { - project = "${google_project.acceptance.project_id}" - policy_data = "${data.google_iam_policy.acceptance.policy_data}" -} - -data "google_iam_policy" "acceptance" { - binding { - role = "roles/storage.objectViewer" - members = [ - "user:evanbrown@google.com", - ] - } -}`, pid, name, org) -} - -func testAccGoogleProject_mergeEmpty(pid, name, org string) string { - return fmt.Sprintf(` -resource "google_project" "acceptance" { - project_id = "%s" - name = "%s" - org_id = "%s" -}`, pid, name, org) -} - -func testAccGoogleProject_createLabels(pid, name, org, key, value string) string { - return fmt.Sprintf(` -resource "google_project" "acceptance" { - project_id = "%s" - name = "%s" - org_id = "%s" - labels { - "%s" = "%s" - } -}`, pid, name, org, key, value) -} - -func testAccGoogleProject_updateLabels(pid, name, org string, labels map[string]string) string { +func testAccGoogleProject_labels(pid, name, org string, labels map[string]string) string { r := fmt.Sprintf(` resource "google_project" "acceptance" { project_id = "%s"