Refactor PubSub import test (#1106)

* Refactor pubsub subscription import test

* Refactor pubsub topic import test
This commit is contained in:
Vincent Roseberry 2018-02-20 11:12:22 -08:00 committed by GitHub
parent 411017c35f
commit 297b0a80e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 128 deletions

View File

@ -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,
},
},
})
}

View File

@ -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"},
},
},
})
}

View File

@ -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)
}

View File

@ -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)
}