package aws import ( "fmt" "os" "regexp" "testing" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/codepipeline" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) func TestAccAWSCodePipeline_basic(t *testing.T) { if os.Getenv("GITHUB_TOKEN") == "" { t.Skip("Environment variable GITHUB_TOKEN is not set") } name := acctest.RandString(10) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAWSCodePipelineDestroy, Steps: []resource.TestStep{ { Config: testAccAWSCodePipelineConfig_basic(name), Check: resource.ComposeTestCheckFunc( testAccCheckAWSCodePipelineExists("aws_codepipeline.bar"), resource.TestCheckResourceAttr("aws_codepipeline.bar", "artifact_store.0.type", "S3"), resource.TestCheckResourceAttr("aws_codepipeline.bar", "artifact_store.0.encryption_key.0.id", "1234"), resource.TestCheckResourceAttr("aws_codepipeline.bar", "artifact_store.0.encryption_key.0.type", "KMS"), ), }, { Config: testAccAWSCodePipelineConfig_basicUpdated(name), Check: resource.ComposeTestCheckFunc( testAccCheckAWSCodePipelineExists("aws_codepipeline.bar"), resource.TestCheckResourceAttr("aws_codepipeline.bar", "artifact_store.0.type", "S3"), resource.TestCheckResourceAttr("aws_codepipeline.bar", "artifact_store.0.encryption_key.0.id", "4567"), resource.TestCheckResourceAttr("aws_codepipeline.bar", "artifact_store.0.encryption_key.0.type", "KMS"), ), }, }, }) } func TestAccAWSCodePipeline_deployWithServiceRole(t *testing.T) { if os.Getenv("GITHUB_TOKEN") == "" { t.Skip("Environment variable GITHUB_TOKEN is not set") } name := acctest.RandString(10) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAWSCodePipelineDestroy, Steps: []resource.TestStep{ { Config: testAccAWSCodePipelineConfig_deployWithServiceRole(name), Check: resource.ComposeTestCheckFunc( testAccCheckAWSCodePipelineExists("aws_codepipeline.bar"), resource.TestCheckResourceAttr("aws_codepipeline.bar", "stage.2.name", "Deploy"), resource.TestCheckResourceAttr("aws_codepipeline.bar", "stage.2.action.0.category", "Deploy"), resource.TestMatchResourceAttr( "aws_codepipeline.bar", "stage.2.action.0.role_arn", regexp.MustCompile("^arn:aws:iam::[0-9]{12}:role/codepipeline-action-role.*")), ), }, }, }) } func testAccCheckAWSCodePipelineExists(n string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Not found: %s", n) } if rs.Primary.ID == "" { return fmt.Errorf("No CodePipeline ID is set") } conn := testAccProvider.Meta().(*AWSClient).codepipelineconn _, err := conn.GetPipeline(&codepipeline.GetPipelineInput{ Name: aws.String(rs.Primary.ID), }) if err != nil { return err } return nil } } func testAccCheckAWSCodePipelineDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).codepipelineconn for _, rs := range s.RootModule().Resources { if rs.Type != "aws_codepipeline" { continue } _, err := conn.GetPipeline(&codepipeline.GetPipelineInput{ Name: aws.String(rs.Primary.ID), }) if err == nil { return fmt.Errorf("Expected AWS CodePipeline to be gone, but was still found") } return nil } return fmt.Errorf("Default error in CodePipeline Test") } func testAccAWSCodePipelineConfig_basic(rName string) string { return fmt.Sprintf(` resource "aws_s3_bucket" "foo" { bucket = "tf-test-pipeline-%s" acl = "private" } resource "aws_iam_role" "codepipeline_role" { name = "codepipeline-role-%s" assume_role_policy = <