github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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  			{
    20  				Config: testAccPubsubSubscription,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccPubsubSubscriptionExists(
    23  						"google_pubsub_subscription.foobar_sub"),
    24  					resource.TestCheckResourceAttrSet("google_pubsub_subscription.foobar_sub", "path"),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccCheckPubsubSubscriptionDestroy(s *terraform.State) error {
    32  	for _, rs := range s.RootModule().Resources {
    33  		if rs.Type != "google_pubsub_subscription" {
    34  			continue
    35  		}
    36  
    37  		config := testAccProvider.Meta().(*Config)
    38  		sub, _ := config.clientPubsub.Projects.Subscriptions.Get(rs.Primary.ID).Do()
    39  		if sub != nil {
    40  			return fmt.Errorf("Subscription still present")
    41  		}
    42  	}
    43  
    44  	return nil
    45  }
    46  
    47  func testAccPubsubSubscriptionExists(n string) resource.TestCheckFunc {
    48  	return func(s *terraform.State) error {
    49  		rs, ok := s.RootModule().Resources[n]
    50  		if !ok {
    51  			return fmt.Errorf("Not found: %s", n)
    52  		}
    53  
    54  		if rs.Primary.ID == "" {
    55  			return fmt.Errorf("No ID is set")
    56  		}
    57  		config := testAccProvider.Meta().(*Config)
    58  		_, err := config.clientPubsub.Projects.Subscriptions.Get(rs.Primary.ID).Do()
    59  		if err != nil {
    60  			return fmt.Errorf("Subscription does not exist")
    61  		}
    62  
    63  		return nil
    64  	}
    65  }
    66  
    67  var testAccPubsubSubscription = fmt.Sprintf(`
    68  resource "google_pubsub_topic" "foobar_sub" {
    69  	name = "pssub-test-%s"
    70  }
    71  
    72  resource "google_pubsub_subscription" "foobar_sub" {
    73  	name                 = "pssub-test-%s"
    74  	topic                = "${google_pubsub_topic.foobar_sub.name}"
    75  	ack_deadline_seconds = 20
    76  }`, acctest.RandString(10), acctest.RandString(10))