From 297b0a80e03554d5ed7a45b53593c2b17c689075 Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Tue, 20 Feb 2018 11:12:22 -0800 Subject: [PATCH] Refactor PubSub import test (#1106) * Refactor pubsub subscription import test * Refactor pubsub topic import test --- google/import_pubsub_subscription_test.go | 33 --------------- google/import_pubsub_topic_test.go | 37 ----------------- google/resource_pubsub_subscription_test.go | 37 +++++------------ google/resource_pubsub_topic_test.go | 45 +++++++-------------- 4 files changed, 24 insertions(+), 128 deletions(-) delete mode 100644 google/import_pubsub_subscription_test.go delete mode 100644 google/import_pubsub_topic_test.go diff --git a/google/import_pubsub_subscription_test.go b/google/import_pubsub_subscription_test.go deleted file mode 100644 index 6a08d0d6..00000000 --- a/google/import_pubsub_subscription_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package google - -import ( - "fmt" - "testing" - - "github.com/hashicorp/terraform/helper/acctest" - "github.com/hashicorp/terraform/helper/resource" -) - -func TestAccPubsubSubscription_import(t *testing.T) { - t.Parallel() - - topic := fmt.Sprintf("tf-test-topic-%s", acctest.RandString(10)) - subscription := fmt.Sprintf("tf-test-sub-%s", acctest.RandString(10)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckPubsubTopicDestroy, - Steps: []resource.TestStep{ - resource.TestStep{ - Config: testAccPubsubSubscription_basic(topic, subscription), - }, - resource.TestStep{ - ResourceName: "google_pubsub_subscription.foobar_sub", - ImportStateId: subscription, - ImportState: true, - ImportStateVerify: true, - }, - }, - }) -} diff --git a/google/import_pubsub_topic_test.go b/google/import_pubsub_topic_test.go deleted file mode 100644 index 0b172f06..00000000 --- a/google/import_pubsub_topic_test.go +++ /dev/null @@ -1,37 +0,0 @@ -package google - -import ( - "fmt" - "testing" - - "github.com/hashicorp/terraform/helper/acctest" - "github.com/hashicorp/terraform/helper/resource" -) - -func TestAccPubsubTopic_import(t *testing.T) { - t.Parallel() - - topicName := fmt.Sprintf("tf-test-topic-%d", acctest.RandInt()) - conf := fmt.Sprintf(` - resource "google_pubsub_topic" "tf-test" { - name = "%s" - }`, topicName) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckPubsubTopicDestroy, - Steps: []resource.TestStep{ - resource.TestStep{ - Config: conf, - }, - resource.TestStep{ - ResourceName: "google_pubsub_topic.tf-test", - ImportStateId: topicName, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"force_destroy"}, - }, - }, - }) -} diff --git a/google/resource_pubsub_subscription_test.go b/google/resource_pubsub_subscription_test.go index 58c2255f..d813680b 100644 --- a/google/resource_pubsub_subscription_test.go +++ b/google/resource_pubsub_subscription_test.go @@ -22,11 +22,12 @@ func TestAccPubsubSubscription_basic(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccPubsubSubscription_basic(topic, subscription), - Check: resource.ComposeTestCheckFunc( - testAccPubsubSubscriptionExists( - "google_pubsub_subscription.foobar_sub"), - resource.TestCheckResourceAttrSet("google_pubsub_subscription.foobar_sub", "path"), - ), + }, + resource.TestStep{ + ResourceName: "google_pubsub_subscription.foo", + ImportStateId: subscription, + ImportState: true, + ImportStateVerify: true, }, }, }) @@ -60,35 +61,15 @@ func testAccCheckPubsubSubscriptionDestroy(s *terraform.State) error { return nil } -func testAccPubsubSubscriptionExists(n string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - config := testAccProvider.Meta().(*Config) - _, err := config.clientPubsub.Projects.Subscriptions.Get(rs.Primary.ID).Do() - if err != nil { - return fmt.Errorf("Subscription does not exist") - } - - return nil - } -} - func testAccPubsubSubscription_basic(topic, subscription string) string { return fmt.Sprintf(` -resource "google_pubsub_topic" "foobar_sub" { +resource "google_pubsub_topic" "foo" { name = "%s" } -resource "google_pubsub_subscription" "foobar_sub" { +resource "google_pubsub_subscription" "foo" { name = "%s" - topic = "${google_pubsub_topic.foobar_sub.name}" + topic = "${google_pubsub_topic.foo.name}" ack_deadline_seconds = 20 }`, topic, subscription) } diff --git a/google/resource_pubsub_topic_test.go b/google/resource_pubsub_topic_test.go index 72fd9f20..da3d86d1 100644 --- a/google/resource_pubsub_topic_test.go +++ b/google/resource_pubsub_topic_test.go @@ -9,20 +9,25 @@ import ( "github.com/hashicorp/terraform/terraform" ) -func TestAccPubsubTopicCreate(t *testing.T) { +func TestAccPubsubTopic_basic(t *testing.T) { t.Parallel() + topicName := acctest.RandomWithPrefix("tf-test-topic") + resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckPubsubTopicDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccPubsubTopic(), - Check: resource.ComposeTestCheckFunc( - testAccPubsubTopicExists( - "google_pubsub_topic.foobar"), - ), + Config: testAccPubsubTopic_basic(topicName), + }, + resource.TestStep{ + ResourceName: "google_pubsub_topic.foo", + ImportStateId: topicName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, }, }) @@ -44,29 +49,9 @@ func testAccCheckPubsubTopicDestroy(s *terraform.State) error { return nil } -func testAccPubsubTopicExists(n string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - config := testAccProvider.Meta().(*Config) - _, err := config.clientPubsub.Projects.Topics.Get(rs.Primary.ID).Do() - if err != nil { - return fmt.Errorf("Topic does not exist") - } - - return nil - } -} - -func testAccPubsubTopic() string { +func testAccPubsubTopic_basic(name string) string { return fmt.Sprintf(` -resource "google_pubsub_topic" "foobar" { - name = "pstopic-test-%s" -}`, acctest.RandString(10)) +resource "google_pubsub_topic" "foo" { + name = "%s" +}`, name) }