github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/google/resource_pubsub_topic_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 TestAccPubsubTopicCreate(t *testing.T) { 13 14 resource.Test(t, resource.TestCase{ 15 PreCheck: func() { testAccPreCheck(t) }, 16 Providers: testAccProviders, 17 CheckDestroy: testAccCheckPubsubTopicDestroy, 18 Steps: []resource.TestStep{ 19 resource.TestStep{ 20 Config: testAccPubsubTopic, 21 Check: resource.ComposeTestCheckFunc( 22 testAccPubsubTopicExists( 23 "google_pubsub_topic.foobar"), 24 ), 25 }, 26 }, 27 }) 28 } 29 30 func testAccCheckPubsubTopicDestroy(s *terraform.State) error { 31 for _, rs := range s.RootModule().Resources { 32 if rs.Type != "google_pubsub_topic" { 33 continue 34 } 35 36 config := testAccProvider.Meta().(*Config) 37 topic, _ := config.clientPubsub.Projects.Topics.Get(rs.Primary.ID).Do() 38 if topic != nil { 39 return fmt.Errorf("Topic still present") 40 } 41 } 42 43 return nil 44 } 45 46 func testAccPubsubTopicExists(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.Topics.Get(rs.Primary.ID).Do() 58 if err != nil { 59 return fmt.Errorf("Topic does not exist") 60 } 61 62 return nil 63 } 64 } 65 66 var testAccPubsubTopic = fmt.Sprintf(` 67 resource "google_pubsub_topic" "foobar" { 68 name = "pstopic-test-%s" 69 }`, acctest.RandString(10))