From e5ff2b7bcd3defbc637fa1b2d4fbd905fbbef647 Mon Sep 17 00:00:00 2001 From: Riley Karson Date: Wed, 5 Jul 2017 09:59:57 -0700 Subject: [PATCH] Updated google_bigtable_table tests. --- google/resource_bigtable_table.go | 6 ++- google/resource_bigtable_table_test.go | 68 +++++++++++++++----------- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/google/resource_bigtable_table.go b/google/resource_bigtable_table.go index 550f00b8..edf49b7c 100644 --- a/google/resource_bigtable_table.go +++ b/google/resource_bigtable_table.go @@ -64,11 +64,15 @@ func resourceBigtableTableCreate(d *schema.ResourceData, meta interface{}) error name := d.Get("name").(string) if v, ok := d.GetOk("split_keys"); ok { splitKeys := convertSchemaArrayToStringArray(v.([]interface{})) + // This method may return before the table's creation is complete - we may need to wait until + // it exists in the future. err = c.CreatePresplitTable(ctx, name, splitKeys) if err != nil { return fmt.Errorf("Error creating presplit table. %s", err) } } else { + // This method may return before the table's creation is complete - we may need to wait until + // it exists in the future. err = c.CreateTable(ctx, name) if err != nil { return fmt.Errorf("Error creating table. %s", err) @@ -128,7 +132,7 @@ func resourceBigtableTableDestroy(d *schema.ResourceData, meta interface{}) erro name := d.Get("name").(string) err = c.DeleteTable(ctx, name) if err != nil { - return fmt.Errorf("Error deleting instance. %s", err) + return fmt.Errorf("Error deleting table. %s", err) } d.SetId("") diff --git a/google/resource_bigtable_table_test.go b/google/resource_bigtable_table_test.go index 216073e8..ff79ff98 100644 --- a/google/resource_bigtable_table_test.go +++ b/google/resource_bigtable_table_test.go @@ -30,6 +30,26 @@ func TestAccBigtableTable_basic(t *testing.T) { }) } +func TestAccBigtableTable_splitKeys(t *testing.T) { + instanceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) + tableName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBigtableTableDestroy, + Steps: []resource.TestStep{ + { + Config: testAccBigtableTable_splitKeys(instanceName, tableName), + Check: resource.ComposeTestCheckFunc( + testAccBigtableTableExists( + "google_bigtable_table.table"), + ), + }, + }, + }) +} + func testAccCheckBigtableTableDestroy(s *terraform.State) error { var ctx = context.Background() for _, rs := range s.RootModule().Resources { @@ -44,21 +64,8 @@ func testAccCheckBigtableTableDestroy(s *terraform.State) error { return nil } - tables, err := c.Tables(ctx) - if err != nil { - // The instance is already gone. - return nil - } - - found := false - for _, t := range tables { - if t == rs.Primary.Attributes["name"] { - found = true - break - } - } - - if found { + _, err = c.TableInfo(ctx, rs.Primary.Attributes["name"]) + if err == nil { return fmt.Errorf("Table still present. Found %s in %s.", rs.Primary.Attributes["name"], rs.Primary.Attributes["instance_name"]) } @@ -85,20 +92,8 @@ func testAccBigtableTableExists(n string) resource.TestCheckFunc { return fmt.Errorf("Error starting admin client. %s", err) } - tables, err := c.Tables(ctx) + _, err = c.TableInfo(ctx, rs.Primary.Attributes["name"]) if err != nil { - return fmt.Errorf("Error starting admin client. %s", err) - } - - found := false - for _, t := range tables { - if t == rs.Primary.Attributes["name"] { - found = true - break - } - } - - if !found { return fmt.Errorf("Error retrieving table. Could not find %s in %s.", rs.Primary.Attributes["name"], rs.Primary.Attributes["instance_name"]) } @@ -118,6 +113,23 @@ resource "google_bigtable_instance" "instance" { storage_type = "HDD" } +resource "google_bigtable_table" "table" { + name = "%s" + instance_name = "${google_bigtable_instance.instance.name}" +} +`, instanceName, instanceName, tableName) +} + +func testAccBigtableTable_splitKeys(instanceName, tableName string) string { + return fmt.Sprintf(` +resource "google_bigtable_instance" "instance" { + name = "%s" + cluster_id = "%s" + zone = "us-central1-b" + num_nodes = 3 + storage_type = "HDD" +} + resource "google_bigtable_table" "table" { name = "%s" instance_name = "${google_bigtable_instance.instance.name}"