github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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 TestAccAWSCodeCommitRepository_create_default_branch(t *testing.T) {
    57  	resource.Test(t, resource.TestCase{
    58  		PreCheck:     func() { testAccPreCheck(t) },
    59  		Providers:    testAccProviders,
    60  		CheckDestroy: testAccCheckCodeCommitRepositoryDestroy,
    61  		Steps: []resource.TestStep{
    62  			resource.TestStep{
    63  				Config: testAccCodeCommitRepository_with_default_branch,
    64  				Check: resource.ComposeTestCheckFunc(
    65  					testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"),
    66  					resource.TestCheckResourceAttr(
    67  						"aws_codecommit_repository.test", "default_branch", "master"),
    68  				),
    69  			},
    70  		},
    71  	})
    72  }
    73  
    74  func TestAccAWSCodeCommitRepository_create_and_update_default_branch(t *testing.T) {
    75  	resource.Test(t, resource.TestCase{
    76  		PreCheck:     func() { testAccPreCheck(t) },
    77  		Providers:    testAccProviders,
    78  		CheckDestroy: testAccCheckCodeCommitRepositoryDestroy,
    79  		Steps: []resource.TestStep{
    80  			resource.TestStep{
    81  				Config: testAccCodeCommitRepository_basic,
    82  				Check: resource.ComposeTestCheckFunc(
    83  					testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"),
    84  					resource.TestCheckResourceAttr(
    85  						"aws_codecommit_repository.test", "default_branch", ""),
    86  				),
    87  			},
    88  			resource.TestStep{
    89  				Config: testAccCodeCommitRepository_with_default_branch,
    90  				Check: resource.ComposeTestCheckFunc(
    91  					testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"),
    92  					resource.TestCheckResourceAttr(
    93  						"aws_codecommit_repository.test", "default_branch", "master"),
    94  				),
    95  			},
    96  		},
    97  	})
    98  }
    99  
   100  func testAccCheckCodeCommitRepositoryExists(name string) resource.TestCheckFunc {
   101  	return func(s *terraform.State) error {
   102  		rs, ok := s.RootModule().Resources[name]
   103  		if !ok {
   104  			return fmt.Errorf("Not found: %s", name)
   105  		}
   106  
   107  		if rs.Primary.ID == "" {
   108  			return fmt.Errorf("No ID is set")
   109  		}
   110  
   111  		codecommitconn := testAccProvider.Meta().(*AWSClient).codecommitconn
   112  		out, err := codecommitconn.GetRepository(&codecommit.GetRepositoryInput{
   113  			RepositoryName: aws.String(rs.Primary.ID),
   114  		})
   115  
   116  		if err != nil {
   117  			return err
   118  		}
   119  
   120  		if out.RepositoryMetadata.Arn == nil {
   121  			return fmt.Errorf("No CodeCommit Repository Vault Found")
   122  		}
   123  
   124  		if *out.RepositoryMetadata.RepositoryName != rs.Primary.ID {
   125  			return fmt.Errorf("CodeCommit Repository Mismatch - existing: %q, state: %q",
   126  				*out.RepositoryMetadata.RepositoryName, rs.Primary.ID)
   127  		}
   128  
   129  		return nil
   130  	}
   131  }
   132  
   133  func testAccCheckCodeCommitRepositoryDestroy(s *terraform.State) error {
   134  	conn := testAccProvider.Meta().(*AWSClient).codecommitconn
   135  
   136  	for _, rs := range s.RootModule().Resources {
   137  		if rs.Type != "aws_codecommit_repository" {
   138  			continue
   139  		}
   140  
   141  		_, err := conn.GetRepository(&codecommit.GetRepositoryInput{
   142  			RepositoryName: aws.String(rs.Primary.ID),
   143  		})
   144  
   145  		if ae, ok := err.(awserr.Error); ok && ae.Code() == "RepositoryDoesNotExistException" {
   146  			continue
   147  		}
   148  		if err == nil {
   149  			return fmt.Errorf("Repository still exists: %s", rs.Primary.ID)
   150  		}
   151  		return err
   152  	}
   153  
   154  	return nil
   155  }
   156  
   157  const testAccCodeCommitRepository_basic = `
   158  provider "aws" {
   159    region = "us-east-1"
   160  }
   161  resource "aws_codecommit_repository" "test" {
   162    repository_name = "my_test_repository"
   163    description = "This is a test description"
   164  }
   165  `
   166  
   167  const testAccCodeCommitRepository_withChanges = `
   168  provider "aws" {
   169    region = "us-east-1"
   170  }
   171  resource "aws_codecommit_repository" "test" {
   172    repository_name = "my_test_repository"
   173    description = "This is a test description - with changes"
   174  }
   175  `
   176  
   177  const testAccCodeCommitRepository_with_default_branch = `
   178  provider "aws" {
   179    region = "us-east-1"
   180  }
   181  resource "aws_codecommit_repository" "test" {
   182    repository_name = "my_test_repository"
   183    description = "This is a test description"
   184    default_branch = "master"
   185  }
   186  `