github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSCodeCommitRepository_basic(t *testing.T) {
    16  	rInt := acctest.RandInt()
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckCodeCommitRepositoryDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccCodeCommitRepository_basic(rInt),
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAWSCodeCommitRepository_withChanges(t *testing.T) {
    33  	rInt := acctest.RandInt()
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckCodeCommitRepositoryDestroy,
    38  		Steps: []resource.TestStep{
    39  			resource.TestStep{
    40  				Config: testAccCodeCommitRepository_basic(rInt),
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"),
    43  					resource.TestCheckResourceAttr(
    44  						"aws_codecommit_repository.test", "description", "This is a test description"),
    45  				),
    46  			},
    47  			resource.TestStep{
    48  				Config: testAccCodeCommitRepository_withChanges(rInt),
    49  				Check: resource.ComposeTestCheckFunc(
    50  					testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"),
    51  					resource.TestCheckResourceAttr(
    52  						"aws_codecommit_repository.test", "description", "This is a test description - with changes"),
    53  				),
    54  			},
    55  		},
    56  	})
    57  }
    58  
    59  func TestAccAWSCodeCommitRepository_create_default_branch(t *testing.T) {
    60  	rInt := acctest.RandInt()
    61  	resource.Test(t, resource.TestCase{
    62  		PreCheck:     func() { testAccPreCheck(t) },
    63  		Providers:    testAccProviders,
    64  		CheckDestroy: testAccCheckCodeCommitRepositoryDestroy,
    65  		Steps: []resource.TestStep{
    66  			resource.TestStep{
    67  				Config: testAccCodeCommitRepository_with_default_branch(rInt),
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"),
    70  					resource.TestCheckResourceAttr(
    71  						"aws_codecommit_repository.test", "default_branch", "master"),
    72  				),
    73  			},
    74  		},
    75  	})
    76  }
    77  
    78  func TestAccAWSCodeCommitRepository_create_and_update_default_branch(t *testing.T) {
    79  	rInt := acctest.RandInt()
    80  	resource.Test(t, resource.TestCase{
    81  		PreCheck:     func() { testAccPreCheck(t) },
    82  		Providers:    testAccProviders,
    83  		CheckDestroy: testAccCheckCodeCommitRepositoryDestroy,
    84  		Steps: []resource.TestStep{
    85  			resource.TestStep{
    86  				Config: testAccCodeCommitRepository_basic(rInt),
    87  				Check: resource.ComposeTestCheckFunc(
    88  					testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"),
    89  					resource.TestCheckNoResourceAttr(
    90  						"aws_codecommit_repository.test", "default_branch"),
    91  				),
    92  			},
    93  			resource.TestStep{
    94  				Config: testAccCodeCommitRepository_with_default_branch(rInt),
    95  				Check: resource.ComposeTestCheckFunc(
    96  					testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"),
    97  					resource.TestCheckResourceAttr(
    98  						"aws_codecommit_repository.test", "default_branch", "master"),
    99  				),
   100  			},
   101  		},
   102  	})
   103  }
   104  
   105  func testAccCheckCodeCommitRepositoryExists(name string) resource.TestCheckFunc {
   106  	return func(s *terraform.State) error {
   107  		rs, ok := s.RootModule().Resources[name]
   108  		if !ok {
   109  			return fmt.Errorf("Not found: %s", name)
   110  		}
   111  
   112  		if rs.Primary.ID == "" {
   113  			return fmt.Errorf("No ID is set")
   114  		}
   115  
   116  		codecommitconn := testAccProvider.Meta().(*AWSClient).codecommitconn
   117  		out, err := codecommitconn.GetRepository(&codecommit.GetRepositoryInput{
   118  			RepositoryName: aws.String(rs.Primary.ID),
   119  		})
   120  
   121  		if err != nil {
   122  			return err
   123  		}
   124  
   125  		if out.RepositoryMetadata.Arn == nil {
   126  			return fmt.Errorf("No CodeCommit Repository Vault Found")
   127  		}
   128  
   129  		if *out.RepositoryMetadata.RepositoryName != rs.Primary.ID {
   130  			return fmt.Errorf("CodeCommit Repository Mismatch - existing: %q, state: %q",
   131  				*out.RepositoryMetadata.RepositoryName, rs.Primary.ID)
   132  		}
   133  
   134  		return nil
   135  	}
   136  }
   137  
   138  func testAccCheckCodeCommitRepositoryDestroy(s *terraform.State) error {
   139  	conn := testAccProvider.Meta().(*AWSClient).codecommitconn
   140  
   141  	for _, rs := range s.RootModule().Resources {
   142  		if rs.Type != "aws_codecommit_repository" {
   143  			continue
   144  		}
   145  
   146  		_, err := conn.GetRepository(&codecommit.GetRepositoryInput{
   147  			RepositoryName: aws.String(rs.Primary.ID),
   148  		})
   149  
   150  		if ae, ok := err.(awserr.Error); ok && ae.Code() == "RepositoryDoesNotExistException" {
   151  			continue
   152  		}
   153  		if err == nil {
   154  			return fmt.Errorf("Repository still exists: %s", rs.Primary.ID)
   155  		}
   156  		return err
   157  	}
   158  
   159  	return nil
   160  }
   161  
   162  func testAccCodeCommitRepository_basic(rInt int) string {
   163  	return fmt.Sprintf(`
   164  resource "aws_codecommit_repository" "test" {
   165    repository_name = "test_repository_%d"
   166    description = "This is a test description"
   167  }
   168  `, rInt)
   169  }
   170  
   171  func testAccCodeCommitRepository_withChanges(rInt int) string {
   172  	return fmt.Sprintf(`
   173  resource "aws_codecommit_repository" "test" {
   174    repository_name = "test_repository_%d"
   175    description = "This is a test description - with changes"
   176  }
   177  `, rInt)
   178  }
   179  
   180  func testAccCodeCommitRepository_with_default_branch(rInt int) string {
   181  	return fmt.Sprintf(`
   182  resource "aws_codecommit_repository" "test" {
   183    repository_name = "test_repository_%d"
   184    description = "This is a test description"
   185    default_branch = "master"
   186  }
   187  `, rInt)
   188  }