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"
)
2019-02-19 23:10:44 +00:00
func TestAccPubsubSubscription_fullName ( t * testing . T ) {
2017-10-12 22:07:29 +00:00
t . Parallel ( )
2017-09-26 20:44:13 +00:00
topic := fmt . Sprintf ( "tf-test-topic-%s" , acctest . RandString ( 10 ) )
2019-02-19 23:10:44 +00:00
subscription := fmt . Sprintf ( "projects/%s/subscriptions/tf-test-sub-%s" , getTestProjectFromEnv ( ) , 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
{
2019-02-19 23:10:44 +00:00
Config : testAccPubsubSubscription_fullName ( topic , subscription , "bar" , 20 ) ,
2018-02-20 19:12:22 +00:00
} ,
2018-12-14 20:51:11 +00:00
{
2018-02-20 19:12:22 +00:00
ResourceName : "google_pubsub_subscription.foo" ,
ImportStateId : subscription ,
ImportState : true ,
ImportStateVerify : true ,
2015-10-28 17:55:50 +00:00
} ,
} ,
} )
}
2019-02-19 23:10:44 +00:00
func TestAccPubsubSubscription_update ( t * testing . T ) {
t . Parallel ( )
topic := fmt . Sprintf ( "tf-test-topic-%s" , acctest . RandString ( 10 ) )
subscriptionShort := fmt . Sprintf ( "tf-test-sub-%s" , acctest . RandString ( 10 ) )
subscriptionLong := fmt . Sprintf ( "projects/%s/subscriptions/%s" , getTestProjectFromEnv ( ) , subscriptionShort )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckPubsubSubscriptionDestroy ,
Steps : [ ] resource . TestStep {
{
Config : testAccPubsubSubscription_fullName ( topic , subscriptionLong , "bar" , 20 ) ,
} ,
{
ResourceName : "google_pubsub_subscription.foo" ,
ImportStateId : subscriptionLong ,
ImportState : true ,
ImportStateVerify : true ,
} ,
{
Config : testAccPubsubSubscription_fullName ( topic , subscriptionLong , "baz" , 30 ) ,
Check : resource . TestCheckResourceAttr (
"google_pubsub_subscription.foo" , "path" , subscriptionLong ,
) ,
} ,
{
ResourceName : "google_pubsub_subscription.foo" ,
ImportStateId : subscriptionLong ,
ImportState : true ,
ImportStateVerify : true ,
} ,
{
Config : testAccPubsubSubscription_fullName ( topic , subscriptionShort , "baz" , 30 ) ,
Check : resource . TestCheckResourceAttr (
"google_pubsub_subscription.foo" , "path" , subscriptionLong ,
) ,
} ,
{
ResourceName : "google_pubsub_subscription.foo" ,
ImportStateId : subscriptionShort ,
ImportState : true ,
ImportStateVerify : true ,
} ,
} ,
} )
}
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
2019-02-19 23:10:44 +00:00
func testAccPubsubSubscription_fullName ( topic , subscription , label string , deadline int ) string {
2017-09-26 20:44:13 +00:00
return fmt . Sprintf ( `
2018-02-20 19:12:22 +00:00
resource "google_pubsub_topic" "foo" {
2017-09-26 20:44:13 +00:00
name = "%s"
2015-10-28 17:55:50 +00:00
}
2018-02-20 19:12:22 +00:00
resource "google_pubsub_subscription" "foo" {
2017-09-26 20:44:13 +00:00
name = "%s"
2019-02-19 23:10:44 +00:00
topic = "${google_pubsub_topic.foo.id}"
labels = {
foo = "%s"
}
ack_deadline_seconds = % d
} ` , topic , subscription , label , deadline )
2017-09-26 20:44:13 +00:00
}
2017-10-30 20:12:14 +00:00
func TestGetComputedTopicName ( t * testing . T ) {
type testData struct {
project string
topic string
expected string
}
var testCases = [ ] testData {
2018-12-14 20:51:11 +00:00
{
2017-10-30 20:12:14 +00:00
project : "my-project" ,
topic : "my-topic" ,
expected : "projects/my-project/topics/my-topic" ,
} ,
2018-12-14 20:51:11 +00:00
{
2017-10-30 20:12:14 +00:00
project : "my-project" ,
topic : "projects/another-project/topics/my-topic" ,
expected : "projects/another-project/topics/my-topic" ,
} ,
}
for _ , testCase := range testCases {
computedTopicName := getComputedTopicName ( testCase . project , testCase . topic )
if computedTopicName != testCase . expected {
t . Fatalf ( "bad computed topic name: %s' => expected %s" , computedTopicName , testCase . expected )
}
}
}