github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_ecr_repository_policy_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 TestAccAWSEcrRepositoryPolicy_basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAWSEcrRepositoryPolicyDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAWSEcrRepositoryPolicy,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckAWSEcrRepositoryPolicyExists("aws_ecr_repository_policy.default"),
    24  				),
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func testAccCheckAWSEcrRepositoryPolicyDestroy(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_policy" {
    35  			continue
    36  		}
    37  
    38  		_, err := conn.GetRepositoryPolicy(&ecr.GetRepositoryPolicyInput{
    39  			RegistryId:     aws.String(rs.Primary.Attributes["registry_id"]),
    40  			RepositoryName: aws.String(rs.Primary.Attributes["repository"]),
    41  		})
    42  		if err != nil {
    43  			if ecrerr, ok := err.(awserr.Error); ok && ecrerr.Code() == "RepositoryNotFoundException" {
    44  				return nil
    45  			}
    46  			return err
    47  		}
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  func testAccCheckAWSEcrRepositoryPolicyExists(name string) resource.TestCheckFunc {
    54  	return func(s *terraform.State) error {
    55  		_, ok := s.RootModule().Resources[name]
    56  		if !ok {
    57  			return fmt.Errorf("Not found: %s", name)
    58  		}
    59  
    60  		return nil
    61  	}
    62  }
    63  
    64  var testAccAWSEcrRepositoryPolicy = `
    65  # ECR initially only available in us-east-1
    66  # https://aws.amazon.com/blogs/aws/ec2-container-registry-now-generally-available/
    67  provider "aws" {
    68  	region = "us-east-1"
    69  }
    70  resource "aws_ecr_repository" "foo" {
    71  	name = "bar"
    72  }
    73  
    74  resource "aws_ecr_repository_policy" "default" {
    75  	repository = "${aws_ecr_repository.foo.name}"
    76  	policy = <<EOF
    77  {
    78      "Version": "2008-10-17",
    79      "Statement": [
    80          {
    81              "Sid": "testpolicy",
    82              "Effect": "Allow",
    83              "Principal": "*",
    84              "Action": [
    85                  "ecr:ListImages"
    86              ]
    87          }
    88      ]
    89  }
    90  EOF
    91  }
    92  `