github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/resource_pubsub_subscription_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccPubsubSubscriptionCreate(t *testing.T) {
    13  
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckPubsubSubscriptionDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccPubsubSubscription,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccPubsubSubscriptionExists(
    23  						"google_pubsub_subscription.foobar_sub"),
    24  				),
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func testAccCheckPubsubSubscriptionDestroy(s *terraform.State) error {
    31  	for _, rs := range s.RootModule().Resources {
    32  		if rs.Type != "google_pubsub_subscription" {
    33  			continue
    34  		}
    35  
    36  		config := testAccProvider.Meta().(*Config)
    37  		sub, _ := config.clientPubsub.Projects.Subscriptions.Get(rs.Primary.ID).Do()
    38  		if sub != nil {
    39  			return fmt.Errorf("Subscription still present")
    40  		}
    41  	}
    42  
    43  	return nil
    44  }
    45  
    46  func testAccPubsubSubscriptionExists(n string) resource.TestCheckFunc {
    47  	return func(s *terraform.State) error {
    48  		rs, ok := s.RootModule().Resources[n]
    49  		if !ok {
    50  			return fmt.Errorf("Not found: %s", n)
    51  		}
    52  
    53  		if rs.Primary.ID == "" {
    54  			return fmt.Errorf("No ID is set")
    55  		}
    56  		config := testAccProvider.Meta().(*Config)
    57  		_, err := config.clientPubsub.Projects.Subscriptions.Get(rs.Primary.ID).Do()
    58  		if err != nil {
    59  			return fmt.Errorf("Subscription does not exist")
    60  		}
    61  
    62  		return nil
    63  	}
    64  }
    65  
    66  var testAccPubsubSubscription = fmt.Sprintf(`
    67  resource "google_pubsub_topic" "foobar_sub" {
    68  	name = "pssub-test-%s"
    69  }
    70  
    71  resource "google_pubsub_subscription" "foobar_sub" {
    72  	name                 = "pssub-test-%s"
    73  	topic                = "${google_pubsub_topic.foobar_sub.name}"
    74  	ack_deadline_seconds = 20
    75  }`, acctest.RandString(10), acctest.RandString(10))