mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-10-04 17:51:11 +00:00
Refactor PubSub import test (#1106)
* Refactor pubsub subscription import test * Refactor pubsub topic import test
This commit is contained in:
parent
411017c35f
commit
297b0a80e0
@ -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,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
@ -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"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
@ -22,11 +22,12 @@ func TestAccPubsubSubscription_basic(t *testing.T) {
|
|||||||
Steps: []resource.TestStep{
|
Steps: []resource.TestStep{
|
||||||
{
|
{
|
||||||
Config: testAccPubsubSubscription_basic(topic, subscription),
|
Config: testAccPubsubSubscription_basic(topic, subscription),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
},
|
||||||
testAccPubsubSubscriptionExists(
|
resource.TestStep{
|
||||||
"google_pubsub_subscription.foobar_sub"),
|
ResourceName: "google_pubsub_subscription.foo",
|
||||||
resource.TestCheckResourceAttrSet("google_pubsub_subscription.foobar_sub", "path"),
|
ImportStateId: subscription,
|
||||||
),
|
ImportState: true,
|
||||||
|
ImportStateVerify: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@ -60,35 +61,15 @@ func testAccCheckPubsubSubscriptionDestroy(s *terraform.State) error {
|
|||||||
return nil
|
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 {
|
func testAccPubsubSubscription_basic(topic, subscription string) string {
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
resource "google_pubsub_topic" "foobar_sub" {
|
resource "google_pubsub_topic" "foo" {
|
||||||
name = "%s"
|
name = "%s"
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "google_pubsub_subscription" "foobar_sub" {
|
resource "google_pubsub_subscription" "foo" {
|
||||||
name = "%s"
|
name = "%s"
|
||||||
topic = "${google_pubsub_topic.foobar_sub.name}"
|
topic = "${google_pubsub_topic.foo.name}"
|
||||||
ack_deadline_seconds = 20
|
ack_deadline_seconds = 20
|
||||||
}`, topic, subscription)
|
}`, topic, subscription)
|
||||||
}
|
}
|
||||||
|
@ -9,20 +9,25 @@ import (
|
|||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAccPubsubTopicCreate(t *testing.T) {
|
func TestAccPubsubTopic_basic(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
topicName := acctest.RandomWithPrefix("tf-test-topic")
|
||||||
|
|
||||||
resource.Test(t, resource.TestCase{
|
resource.Test(t, resource.TestCase{
|
||||||
PreCheck: func() { testAccPreCheck(t) },
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
Providers: testAccProviders,
|
Providers: testAccProviders,
|
||||||
CheckDestroy: testAccCheckPubsubTopicDestroy,
|
CheckDestroy: testAccCheckPubsubTopicDestroy,
|
||||||
Steps: []resource.TestStep{
|
Steps: []resource.TestStep{
|
||||||
resource.TestStep{
|
resource.TestStep{
|
||||||
Config: testAccPubsubTopic(),
|
Config: testAccPubsubTopic_basic(topicName),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
},
|
||||||
testAccPubsubTopicExists(
|
resource.TestStep{
|
||||||
"google_pubsub_topic.foobar"),
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAccPubsubTopicExists(n string) resource.TestCheckFunc {
|
func testAccPubsubTopic_basic(name string) string {
|
||||||
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 {
|
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
resource "google_pubsub_topic" "foobar" {
|
resource "google_pubsub_topic" "foo" {
|
||||||
name = "pstopic-test-%s"
|
name = "%s"
|
||||||
}`, acctest.RandString(10))
|
}`, name)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user