github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/bitbucket/resource_hook_test.go (about)

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