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