github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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/service/codecommit"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSCodeCommitRepository_basic(t *testing.T) {
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckCodeCommitRepositoryDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccCodeCommitRepository_basic,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func TestAccAWSCodeCommitRepository_withChanges(t *testing.T) {
    30  	resource.Test(t, resource.TestCase{
    31  		PreCheck:     func() { testAccPreCheck(t) },
    32  		Providers:    testAccProviders,
    33  		CheckDestroy: testAccCheckCodeCommitRepositoryDestroy,
    34  		Steps: []resource.TestStep{
    35  			resource.TestStep{
    36  				Config: testAccCodeCommitRepository_basic,
    37  				Check: resource.ComposeTestCheckFunc(
    38  					testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"),
    39  					resource.TestCheckResourceAttr(
    40  						"aws_codecommit_repository.test", "description", "This is a test description"),
    41  				),
    42  			},
    43  			resource.TestStep{
    44  				Config: testAccCodeCommitRepository_withChanges,
    45  				Check: resource.ComposeTestCheckFunc(
    46  					testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"),
    47  					resource.TestCheckResourceAttr(
    48  						"aws_codecommit_repository.test", "description", "This is a test description - with changes"),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func testAccCheckCodeCommitRepositoryExists(name string) resource.TestCheckFunc {
    56  	return func(s *terraform.State) error {
    57  		rs, ok := s.RootModule().Resources[name]
    58  		if !ok {
    59  			return fmt.Errorf("Not found: %s", name)
    60  		}
    61  
    62  		if rs.Primary.ID == "" {
    63  			return fmt.Errorf("No ID is set")
    64  		}
    65  
    66  		codecommitconn := testAccProvider.Meta().(*AWSClient).codecommitconn
    67  		out, err := codecommitconn.GetRepository(&codecommit.GetRepositoryInput{
    68  			RepositoryName: aws.String(rs.Primary.ID),
    69  		})
    70  
    71  		if err != nil {
    72  			return err
    73  		}
    74  
    75  		if out.RepositoryMetadata.Arn == nil {
    76  			return fmt.Errorf("No CodeCommit Repository Vault Found")
    77  		}
    78  
    79  		if *out.RepositoryMetadata.RepositoryName != rs.Primary.ID {
    80  			return fmt.Errorf("CodeCommit Repository Mismatch - existing: %q, state: %q",
    81  				*out.RepositoryMetadata.RepositoryName, rs.Primary.ID)
    82  		}
    83  
    84  		return nil
    85  	}
    86  }
    87  
    88  func testAccCheckCodeCommitRepositoryDestroy(s *terraform.State) error {
    89  	if len(s.RootModule().Resources) > 0 {
    90  		return fmt.Errorf("Expected all resources to be gone, but found: %#v",
    91  			s.RootModule().Resources)
    92  	}
    93  
    94  	return nil
    95  }
    96  
    97  const testAccCodeCommitRepository_basic = `
    98  resource "aws_codecommit_repository" "test" {
    99    repository_name = "my_test_repository"
   100    description = "This is a test description"
   101  }
   102  `
   103  
   104  const testAccCodeCommitRepository_withChanges = `
   105  resource "aws_codecommit_repository" "test" {
   106    repository_name = "my_test_repository"
   107    description = "This is a test description - with changes"
   108  }
   109  `