github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/bitbucket/resource_hook_test.go (about)

     1  package bitbucket
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccBitbucketHook_basic(t *testing.T) {
    14  	var hook Hook
    15  
    16  	testUser := os.Getenv("BITBUCKET_USERNAME")
    17  	testAccBitbucketHookConfig := fmt.Sprintf(`
    18  		resource "bitbucket_repository" "test_repo" {
    19  			owner = "%s"
    20  			name = "test-repo-for-webhook-test"
    21  		}
    22  		resource "bitbucket_hook" "test_repo_hook" {
    23  			owner = "%s"
    24  			repository = "${bitbucket_repository.test_repo.name}"
    25  			description = "Test hook for terraform"
    26  			url = "https://httpbin.org"
    27  			events = [
    28  				"repo:push",
    29  			]
    30  		}
    31  	`, testUser, testUser)
    32  
    33  	resource.Test(t, resource.TestCase{
    34  		PreCheck:     func() { testAccPreCheck(t) },
    35  		Providers:    testAccProviders,
    36  		CheckDestroy: testAccCheckBitbucketHookDestroy,
    37  		Steps: []resource.TestStep{
    38  			resource.TestStep{
    39  				Config: testAccBitbucketHookConfig,
    40  				Check: resource.ComposeTestCheckFunc(
    41  					testAccCheckBitbucketHookExists("bitbucket_hook.test_repo_hook", &hook),
    42  				),
    43  			},
    44  		},
    45  	})
    46  }
    47  
    48  func testAccCheckBitbucketHookDestroy(s *terraform.State) error {
    49  	client := testAccProvider.Meta().(*BitbucketClient)
    50  	rs, ok := s.RootModule().Resources["bitbucket_hook.test_repo_hook"]
    51  	if !ok {
    52  		return fmt.Errorf("Not found %s", "bitbucket_hook.test_repo_hook")
    53  	}
    54  
    55  	response, err := client.Get(fmt.Sprintf("2.0/repositories/%s/%s/hooks/%s", rs.Primary.Attributes["owner"], rs.Primary.Attributes["repository"], url.PathEscape(rs.Primary.Attributes["uuid"])))
    56  
    57  	if err == nil {
    58  		return fmt.Errorf("The resource was found should have errored")
    59  	}
    60  
    61  	if response.StatusCode != 404 {
    62  		return fmt.Errorf("Hook still exists")
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  func testAccCheckBitbucketHookExists(n string, hook *Hook) resource.TestCheckFunc {
    69  	return func(s *terraform.State) error {
    70  		rs, ok := s.RootModule().Resources[n]
    71  		if !ok {
    72  			return fmt.Errorf("Not found %s", n)
    73  		}
    74  		if rs.Primary.ID == "" {
    75  			return fmt.Errorf("No Hook ID is set")
    76  		}
    77  		return nil
    78  	}
    79  }