github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/bitbucket/resource_repository_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 TestAccBitbucketRepository_basic(t *testing.T) { 13 var repo Repository 14 15 testUser := os.Getenv("BITBUCKET_USERNAME") 16 testAccBitbucketRepositoryConfig := fmt.Sprintf(` 17 resource "bitbucket_repository" "test_repo" { 18 owner = "%s" 19 name = "test-repo-for-repository-test" 20 } 21 `, testUser) 22 23 resource.Test(t, resource.TestCase{ 24 PreCheck: func() { testAccPreCheck(t) }, 25 Providers: testAccProviders, 26 CheckDestroy: testAccCheckBitbucketRepositoryDestroy, 27 Steps: []resource.TestStep{ 28 resource.TestStep{ 29 Config: testAccBitbucketRepositoryConfig, 30 Check: resource.ComposeTestCheckFunc( 31 testAccCheckBitbucketRepositoryExists("bitbucket_repository.test_repo", &repo), 32 ), 33 }, 34 }, 35 }) 36 } 37 38 func testAccCheckBitbucketRepositoryDestroy(s *terraform.State) error { 39 client := testAccProvider.Meta().(*BitbucketClient) 40 rs, ok := s.RootModule().Resources["bitbucket_repository.test_repo"] 41 if !ok { 42 return fmt.Errorf("Not found %s", "bitbucket_repository.test_repo") 43 } 44 45 response, _ := client.Get(fmt.Sprintf("2.0/repositories/%s/%s", rs.Primary.Attributes["owner"], rs.Primary.Attributes["name"])) 46 47 if response.StatusCode != 404 { 48 return fmt.Errorf("Repository still exists") 49 } 50 51 return nil 52 } 53 54 func testAccCheckBitbucketRepositoryExists(n string, repository *Repository) resource.TestCheckFunc { 55 return func(s *terraform.State) error { 56 rs, ok := s.RootModule().Resources[n] 57 if !ok { 58 return fmt.Errorf("Not found %s", n) 59 } 60 if rs.Primary.ID == "" { 61 return fmt.Errorf("No repository ID is set") 62 } 63 return nil 64 } 65 }