2015-10-28 17:55:50 +00:00
package google
import (
"fmt"
"testing"
2016-01-05 21:47:10 +00:00
"github.com/hashicorp/terraform/helper/acctest"
2015-10-28 17:55:50 +00:00
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
2017-09-26 20:44:13 +00:00
func TestAccPubsubSubscription_basic ( t * testing . T ) {
topic := fmt . Sprintf ( "tf-test-topic-%s" , acctest . RandString ( 10 ) )
subscription := fmt . Sprintf ( "tf-test-sub-%s" , acctest . RandString ( 10 ) )
2015-10-28 17:55:50 +00:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckPubsubSubscriptionDestroy ,
Steps : [ ] resource . TestStep {
2017-05-05 13:57:24 +00:00
{
2017-09-26 20:44:13 +00:00
Config : testAccPubsubSubscription_basic ( topic , subscription ) ,
2015-10-28 17:55:50 +00:00
Check : resource . ComposeTestCheckFunc (
testAccPubsubSubscriptionExists (
"google_pubsub_subscription.foobar_sub" ) ,
2017-05-05 13:57:24 +00:00
resource . TestCheckResourceAttrSet ( "google_pubsub_subscription.foobar_sub" , "path" ) ,
2015-10-28 17:55:50 +00:00
) ,
} ,
} ,
} )
}
2017-09-26 20:44:13 +00:00
// TODO: Add acceptance test for push delivery.
//
// Testing push endpoints is tricky for the following reason:
// - You need a publicly accessible HTTPS server to handle POST requests in order to receive push messages.
// - The server must present a valid SSL certificate signed by a certificate authority
// - The server must be routable by DNS.
// - You also need to validate that you own the domain (or have equivalent access to the endpoint).
// - Finally, you must register the endpoint domain with the GCP project.
//
// An easy way to test this would be to create an App Engine Hello World app. With AppEngine, SSL certificate, DNS and domain registry is handled for us.
// App Engine is not yet supported by Terraform but once it is, it will provide an easy path to testing push configs.
// Another option would be to use Cloud Functions once Terraform support is added.
2015-10-28 17:55:50 +00:00
func testAccCheckPubsubSubscriptionDestroy ( s * terraform . State ) error {
for _ , rs := range s . RootModule ( ) . Resources {
if rs . Type != "google_pubsub_subscription" {
continue
}
config := testAccProvider . Meta ( ) . ( * Config )
2016-03-02 23:36:32 +00:00
sub , _ := config . clientPubsub . Projects . Subscriptions . Get ( rs . Primary . ID ) . Do ( )
if sub != nil {
2016-02-13 06:39:23 +00:00
return fmt . Errorf ( "Subscription still present" )
2015-10-28 17:55:50 +00:00
}
}
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 {
2016-03-02 23:36:32 +00:00
return fmt . Errorf ( "Subscription does not exist" )
2015-10-28 17:55:50 +00:00
}
return nil
}
}
2017-09-26 20:44:13 +00:00
func testAccPubsubSubscription_basic ( topic , subscription string ) string {
return fmt . Sprintf ( `
2015-10-28 17:55:50 +00:00
resource "google_pubsub_topic" "foobar_sub" {
2017-09-26 20:44:13 +00:00
name = "%s"
2015-10-28 17:55:50 +00:00
}
resource "google_pubsub_subscription" "foobar_sub" {
2017-09-26 20:44:13 +00:00
name = "%s"
2016-02-23 00:55:22 +00:00
topic = "${google_pubsub_topic.foobar_sub.name}"
ack_deadline_seconds = 20
2017-09-26 20:44:13 +00:00
} ` , topic , subscription )
}