github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/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  			resource.TestStep{
    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  			RegistryId:      aws.String(rs.Primary.Attributes["registry_id"]),
    40  			RepositoryNames: []*string{aws.String(rs.Primary.Attributes["name"])},
    41  		}
    42  
    43  		out, err := conn.DescribeRepositories(&input)
    44  
    45  		if err != nil {
    46  			if ecrerr, ok := err.(awserr.Error); ok && ecrerr.Code() == "RepositoryNotFoundException" {
    47  				return nil
    48  			}
    49  			return err
    50  		}
    51  
    52  		for _, repository := range out.Repositories {
    53  			if repository.RepositoryName == aws.String(rs.Primary.Attributes["name"]) {
    54  				return fmt.Errorf("ECR repository still exists:\n%#v", repository)
    55  			}
    56  		}
    57  	}
    58  
    59  	return nil
    60  }
    61  
    62  func testAccCheckAWSEcrRepositoryExists(name string) resource.TestCheckFunc {
    63  	return func(s *terraform.State) error {
    64  		_, ok := s.RootModule().Resources[name]
    65  		if !ok {
    66  			return fmt.Errorf("Not found: %s", name)
    67  		}
    68  
    69  		return nil
    70  	}
    71  }
    72  
    73  var testAccAWSEcrRepository = `
    74  # ECR initially only available in us-east-1
    75  # https://aws.amazon.com/blogs/aws/ec2-container-registry-now-generally-available/
    76  provider "aws" {
    77  	region = "us-east-1"
    78  }
    79  resource "aws_ecr_repository" "default" {
    80  	name = "foo-repository-terraform"
    81  }
    82  `