github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/builtin/providers/aws/resource_aws_codecommit_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/codecommit"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSCodeCommitRepository_basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckCodeCommitRepositoryDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCodeCommitRepository_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"),
    24  				),
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func TestAccAWSCodeCommitRepository_withChanges(t *testing.T) {
    31  	resource.Test(t, resource.TestCase{
    32  		PreCheck:     func() { testAccPreCheck(t) },
    33  		Providers:    testAccProviders,
    34  		CheckDestroy: testAccCheckCodeCommitRepositoryDestroy,
    35  		Steps: []resource.TestStep{
    36  			resource.TestStep{
    37  				Config: testAccCodeCommitRepository_basic,
    38  				Check: resource.ComposeTestCheckFunc(
    39  					testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"),
    40  					resource.TestCheckResourceAttr(
    41  						"aws_codecommit_repository.test", "description", "This is a test description"),
    42  				),
    43  			},
    44  			resource.TestStep{
    45  				Config: testAccCodeCommitRepository_withChanges,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"),
    48  					resource.TestCheckResourceAttr(
    49  						"aws_codecommit_repository.test", "description", "This is a test description - with changes"),
    50  				),
    51  			},
    52  		},
    53  	})
    54  }
    55  
    56  func testAccCheckCodeCommitRepositoryExists(name string) resource.TestCheckFunc {
    57  	return func(s *terraform.State) error {
    58  		rs, ok := s.RootModule().Resources[name]
    59  		if !ok {
    60  			return fmt.Errorf("Not found: %s", name)
    61  		}
    62  
    63  		if rs.Primary.ID == "" {
    64  			return fmt.Errorf("No ID is set")
    65  		}
    66  
    67  		codecommitconn := testAccProvider.Meta().(*AWSClient).codecommitconn
    68  		out, err := codecommitconn.GetRepository(&codecommit.GetRepositoryInput{
    69  			RepositoryName: aws.String(rs.Primary.ID),
    70  		})
    71  
    72  		if err != nil {
    73  			return err
    74  		}
    75  
    76  		if out.RepositoryMetadata.Arn == nil {
    77  			return fmt.Errorf("No CodeCommit Repository Vault Found")
    78  		}
    79  
    80  		if *out.RepositoryMetadata.RepositoryName != rs.Primary.ID {
    81  			return fmt.Errorf("CodeCommit Repository Mismatch - existing: %q, state: %q",
    82  				*out.RepositoryMetadata.RepositoryName, rs.Primary.ID)
    83  		}
    84  
    85  		return nil
    86  	}
    87  }
    88  
    89  func testAccCheckCodeCommitRepositoryDestroy(s *terraform.State) error {
    90  	conn := testAccProvider.Meta().(*AWSClient).codecommitconn
    91  
    92  	for _, rs := range s.RootModule().Resources {
    93  		if rs.Type != "aws_codecommit_repository" {
    94  			continue
    95  		}
    96  
    97  		_, err := conn.GetRepository(&codecommit.GetRepositoryInput{
    98  			RepositoryName: aws.String(rs.Primary.ID),
    99  		})
   100  
   101  		if ae, ok := err.(awserr.Error); ok && ae.Code() == "RepositoryDoesNotExistException" {
   102  			continue
   103  		}
   104  		if err == nil {
   105  			return fmt.Errorf("Repository still exists: %s", rs.Primary.ID)
   106  		}
   107  		return err
   108  	}
   109  
   110  	return nil
   111  }
   112  
   113  const testAccCodeCommitRepository_basic = `
   114  provider "aws" {
   115    region = "us-east-1"
   116  }
   117  resource "aws_codecommit_repository" "test" {
   118    repository_name = "my_test_repository"
   119    description = "This is a test description"
   120  }
   121  `
   122  
   123  const testAccCodeCommitRepository_withChanges = `
   124  provider "aws" {
   125    region = "us-east-1"
   126  }
   127  resource "aws_codecommit_repository" "test" {
   128    repository_name = "my_test_repository"
   129    description = "This is a test description - with changes"
   130  }
   131  `