package aws import ( "fmt" "reflect" "sort" "testing" "regexp" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/elastictranscoder" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) func TestAccAWSElasticTranscoderPipeline_basic(t *testing.T) { pipeline := &elastictranscoder.Pipeline{} resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, IDRefreshName: "aws_elastictranscoder_pipeline.bar", Providers: testAccProviders, CheckDestroy: testAccCheckElasticTranscoderPipelineDestroy, Steps: []resource.TestStep{ { Config: awsElasticTranscoderPipelineConfigBasic, Check: resource.ComposeTestCheckFunc( testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", pipeline), ), }, }, }) } func TestAccAWSElasticTranscoderPipeline_kmsKey(t *testing.T) { pipeline := &elastictranscoder.Pipeline{} ri := acctest.RandInt() config := fmt.Sprintf(awsElasticTranscoderPipelineConfigKmsKey, ri, ri, ri) keyRegex := regexp.MustCompile("^arn:aws:([a-zA-Z0-9\\-])+:([a-z]{2}-[a-z]+-\\d{1})?:(\\d{12})?:(.*)$") resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, IDRefreshName: "aws_elastictranscoder_pipeline.bar", Providers: testAccProviders, CheckDestroy: testAccCheckElasticTranscoderPipelineDestroy, Steps: []resource.TestStep{ { Config: config, Check: resource.ComposeTestCheckFunc( testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", pipeline), resource.TestMatchResourceAttr("aws_elastictranscoder_pipeline.bar", "aws_kms_key_arn", keyRegex), ), }, }, }) } func TestAccAWSElasticTranscoderPipeline_notifications(t *testing.T) { pipeline := elastictranscoder.Pipeline{} rInt := acctest.RandInt() resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, IDRefreshName: "aws_elastictranscoder_pipeline.bar", Providers: testAccProviders, CheckDestroy: testAccCheckElasticTranscoderPipelineDestroy, Steps: []resource.TestStep{ { Config: awsElasticTranscoderNotifications(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", &pipeline), testAccCheckAWSElasticTranscoderPipeline_notifications(&pipeline, []string{"warning", "completed"}), ), }, // update and check that we have 1 less notification resource.TestStep{ Config: awsElasticTranscoderNotifications_update(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", &pipeline), testAccCheckAWSElasticTranscoderPipeline_notifications(&pipeline, []string{"completed"}), ), }, }, }) } // testAccCheckTags can be used to check the tags on a resource. func testAccCheckAWSElasticTranscoderPipeline_notifications( p *elastictranscoder.Pipeline, notifications []string) resource.TestCheckFunc { return func(s *terraform.State) error { var notes []string if p.Notifications.Completed != nil && *p.Notifications.Completed != "" { notes = append(notes, "completed") } if p.Notifications.Error != nil && *p.Notifications.Error != "" { notes = append(notes, "error") } if p.Notifications.Progressing != nil && *p.Notifications.Progressing != "" { notes = append(notes, "progressing") } if p.Notifications.Warning != nil && *p.Notifications.Warning != "" { notes = append(notes, "warning") } if len(notes) != len(notifications) { return fmt.Errorf("ETC notifications didn't match:\n\texpected: %#v\n\tgot: %#v\n\n", notifications, notes) } sort.Strings(notes) sort.Strings(notifications) if !reflect.DeepEqual(notes, notifications) { return fmt.Errorf("ETC notifications were not equal:\n\texpected: %#v\n\tgot: %#v\n\n", notifications, notes) } return nil } } func TestAccAWSElasticTranscoderPipeline_withContentConfig(t *testing.T) { pipeline := &elastictranscoder.Pipeline{} rInt := acctest.RandInt() resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, IDRefreshName: "aws_elastictranscoder_pipeline.bar", Providers: testAccProviders, CheckDestroy: testAccCheckElasticTranscoderPipelineDestroy, Steps: []resource.TestStep{ { Config: awsElasticTranscoderPipelineWithContentConfig(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", pipeline), ), }, { Config: awsElasticTranscoderPipelineWithContentConfigUpdate(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", pipeline), ), }, }, }) } func TestAccAWSElasticTranscoderPipeline_withPermissions(t *testing.T) { pipeline := &elastictranscoder.Pipeline{} rInt := acctest.RandInt() resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, IDRefreshName: "aws_elastictranscoder_pipeline.baz", Providers: testAccProviders, CheckDestroy: testAccCheckElasticTranscoderPipelineDestroy, Steps: []resource.TestStep{ { Config: awsElasticTranscoderPipelineWithPerms(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.baz", pipeline), ), }, }, }) } func testAccCheckAWSElasticTranscoderPipelineExists(n string, res *elastictranscoder.Pipeline) 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 Pipeline ID is set") } conn := testAccProvider.Meta().(*AWSClient).elastictranscoderconn out, err := conn.ReadPipeline(&elastictranscoder.ReadPipelineInput{ Id: aws.String(rs.Primary.ID), }) if err != nil { return err } *res = *out.Pipeline return nil } } func testAccCheckElasticTranscoderPipelineDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).elastictranscoderconn for _, rs := range s.RootModule().Resources { if rs.Type != "aws_elastictranscoder_pipline" { continue } out, err := conn.ReadPipeline(&elastictranscoder.ReadPipelineInput{ Id: aws.String(rs.Primary.ID), }) if err == nil { if out.Pipeline != nil && *out.Pipeline.Id == rs.Primary.ID { return fmt.Errorf("Elastic Transcoder Pipeline still exists") } } awsErr, ok := err.(awserr.Error) if !ok { return err } if awsErr.Code() != "ResourceNotFoundException" { return fmt.Errorf("unexpected error: %s", awsErr) } } return nil } const awsElasticTranscoderPipelineConfigBasic = ` resource "aws_elastictranscoder_pipeline" "bar" { input_bucket = "${aws_s3_bucket.test_bucket.bucket}" output_bucket = "${aws_s3_bucket.test_bucket.bucket}" name = "aws_elastictranscoder_pipeline_tf_test_" role = "${aws_iam_role.test_role.arn}" } resource "aws_iam_role" "test_role" { name = "aws_elastictranscoder_pipeline_tf_test_role_" assume_role_policy = <