github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_ecr_repository_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/ecr" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSEcrRepository_basic(t *testing.T) { 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckAWSEcrRepositoryDestroy, 19 Steps: []resource.TestStep{ 20 { 21 Config: testAccAWSEcrRepository, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckAWSEcrRepositoryExists("aws_ecr_repository.default"), 24 ), 25 }, 26 }, 27 }) 28 } 29 30 func testAccCheckAWSEcrRepositoryDestroy(s *terraform.State) error { 31 conn := testAccProvider.Meta().(*AWSClient).ecrconn 32 33 for _, rs := range s.RootModule().Resources { 34 if rs.Type != "aws_ecr_repository" { 35 continue 36 } 37 38 input := ecr.DescribeRepositoriesInput{ 39 RepositoryNames: []*string{aws.String(rs.Primary.Attributes["name"])}, 40 } 41 42 out, err := conn.DescribeRepositories(&input) 43 44 if err != nil { 45 if ecrerr, ok := err.(awserr.Error); ok && ecrerr.Code() == "RepositoryNotFoundException" { 46 return nil 47 } 48 return err 49 } 50 51 for _, repository := range out.Repositories { 52 if repository.RepositoryName == aws.String(rs.Primary.Attributes["name"]) { 53 return fmt.Errorf("ECR repository still exists:\n%#v", repository) 54 } 55 } 56 } 57 58 return nil 59 } 60 61 func testAccCheckAWSEcrRepositoryExists(name string) resource.TestCheckFunc { 62 return func(s *terraform.State) error { 63 _, ok := s.RootModule().Resources[name] 64 if !ok { 65 return fmt.Errorf("Not found: %s", name) 66 } 67 68 return nil 69 } 70 } 71 72 var testAccAWSEcrRepository = ` 73 resource "aws_ecr_repository" "default" { 74 name = "foo-repository-terraform" 75 } 76 `