github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_elastic_transcoder_pipeline_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"sort"
     7  	"testing"
     8  
     9  	"regexp"
    10  
    11  	"github.com/aws/aws-sdk-go/aws"
    12  	"github.com/aws/aws-sdk-go/aws/awserr"
    13  	"github.com/aws/aws-sdk-go/service/elastictranscoder"
    14  	"github.com/hashicorp/terraform/helper/acctest"
    15  	"github.com/hashicorp/terraform/helper/resource"
    16  	"github.com/hashicorp/terraform/terraform"
    17  )
    18  
    19  func TestAccAWSElasticTranscoderPipeline_basic(t *testing.T) {
    20  	pipeline := &elastictranscoder.Pipeline{}
    21  
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck:      func() { testAccPreCheck(t) },
    24  		IDRefreshName: "aws_elastictranscoder_pipeline.bar",
    25  		Providers:     testAccProviders,
    26  		CheckDestroy:  testAccCheckElasticTranscoderPipelineDestroy,
    27  		Steps: []resource.TestStep{
    28  			{
    29  				Config: awsElasticTranscoderPipelineConfigBasic,
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", pipeline),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func TestAccAWSElasticTranscoderPipeline_kmsKey(t *testing.T) {
    39  	pipeline := &elastictranscoder.Pipeline{}
    40  	ri := acctest.RandInt()
    41  	config := fmt.Sprintf(awsElasticTranscoderPipelineConfigKmsKey, ri, ri, ri)
    42  	keyRegex := regexp.MustCompile("^arn:aws:([a-zA-Z0-9\\-])+:([a-z]{2}-[a-z]+-\\d{1})?:(\\d{12})?:(.*)$")
    43  
    44  	resource.Test(t, resource.TestCase{
    45  		PreCheck:      func() { testAccPreCheck(t) },
    46  		IDRefreshName: "aws_elastictranscoder_pipeline.bar",
    47  		Providers:     testAccProviders,
    48  		CheckDestroy:  testAccCheckElasticTranscoderPipelineDestroy,
    49  		Steps: []resource.TestStep{
    50  			{
    51  				Config: config,
    52  				Check: resource.ComposeTestCheckFunc(
    53  					testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", pipeline),
    54  					resource.TestMatchResourceAttr("aws_elastictranscoder_pipeline.bar", "aws_kms_key_arn", keyRegex),
    55  				),
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func TestAccAWSElasticTranscoderPipeline_notifications(t *testing.T) {
    62  	pipeline := elastictranscoder.Pipeline{}
    63  
    64  	rInt := acctest.RandInt()
    65  
    66  	resource.Test(t, resource.TestCase{
    67  		PreCheck:      func() { testAccPreCheck(t) },
    68  		IDRefreshName: "aws_elastictranscoder_pipeline.bar",
    69  		Providers:     testAccProviders,
    70  		CheckDestroy:  testAccCheckElasticTranscoderPipelineDestroy,
    71  		Steps: []resource.TestStep{
    72  			{
    73  				Config: awsElasticTranscoderNotifications(rInt),
    74  				Check: resource.ComposeTestCheckFunc(
    75  					testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", &pipeline),
    76  					testAccCheckAWSElasticTranscoderPipeline_notifications(&pipeline, []string{"warning", "completed"}),
    77  				),
    78  			},
    79  
    80  			// update and check that we have 1 less notification
    81  			resource.TestStep{
    82  				Config: awsElasticTranscoderNotifications_update(rInt),
    83  				Check: resource.ComposeTestCheckFunc(
    84  					testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", &pipeline),
    85  					testAccCheckAWSElasticTranscoderPipeline_notifications(&pipeline, []string{"completed"}),
    86  				),
    87  			},
    88  		},
    89  	})
    90  }
    91  
    92  // testAccCheckTags can be used to check the tags on a resource.
    93  func testAccCheckAWSElasticTranscoderPipeline_notifications(
    94  	p *elastictranscoder.Pipeline, notifications []string) resource.TestCheckFunc {
    95  	return func(s *terraform.State) error {
    96  
    97  		var notes []string
    98  		if p.Notifications.Completed != nil && *p.Notifications.Completed != "" {
    99  			notes = append(notes, "completed")
   100  		}
   101  		if p.Notifications.Error != nil && *p.Notifications.Error != "" {
   102  			notes = append(notes, "error")
   103  		}
   104  		if p.Notifications.Progressing != nil && *p.Notifications.Progressing != "" {
   105  			notes = append(notes, "progressing")
   106  		}
   107  		if p.Notifications.Warning != nil && *p.Notifications.Warning != "" {
   108  			notes = append(notes, "warning")
   109  		}
   110  
   111  		if len(notes) != len(notifications) {
   112  			return fmt.Errorf("ETC notifications didn't match:\n\texpected: %#v\n\tgot: %#v\n\n", notifications, notes)
   113  		}
   114  
   115  		sort.Strings(notes)
   116  		sort.Strings(notifications)
   117  
   118  		if !reflect.DeepEqual(notes, notifications) {
   119  			return fmt.Errorf("ETC notifications were not equal:\n\texpected: %#v\n\tgot: %#v\n\n", notifications, notes)
   120  		}
   121  
   122  		return nil
   123  	}
   124  }
   125  
   126  func TestAccAWSElasticTranscoderPipeline_withContentConfig(t *testing.T) {
   127  	pipeline := &elastictranscoder.Pipeline{}
   128  
   129  	rInt := acctest.RandInt()
   130  
   131  	resource.Test(t, resource.TestCase{
   132  		PreCheck:      func() { testAccPreCheck(t) },
   133  		IDRefreshName: "aws_elastictranscoder_pipeline.bar",
   134  		Providers:     testAccProviders,
   135  		CheckDestroy:  testAccCheckElasticTranscoderPipelineDestroy,
   136  		Steps: []resource.TestStep{
   137  			{
   138  				Config: awsElasticTranscoderPipelineWithContentConfig(rInt),
   139  				Check: resource.ComposeTestCheckFunc(
   140  					testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", pipeline),
   141  				),
   142  			},
   143  			{
   144  				Config: awsElasticTranscoderPipelineWithContentConfigUpdate(rInt),
   145  				Check: resource.ComposeTestCheckFunc(
   146  					testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", pipeline),
   147  				),
   148  			},
   149  		},
   150  	})
   151  }
   152  
   153  func TestAccAWSElasticTranscoderPipeline_withPermissions(t *testing.T) {
   154  	pipeline := &elastictranscoder.Pipeline{}
   155  
   156  	rInt := acctest.RandInt()
   157  
   158  	resource.Test(t, resource.TestCase{
   159  		PreCheck:      func() { testAccPreCheck(t) },
   160  		IDRefreshName: "aws_elastictranscoder_pipeline.baz",
   161  		Providers:     testAccProviders,
   162  		CheckDestroy:  testAccCheckElasticTranscoderPipelineDestroy,
   163  		Steps: []resource.TestStep{
   164  			{
   165  				Config: awsElasticTranscoderPipelineWithPerms(rInt),
   166  				Check: resource.ComposeTestCheckFunc(
   167  					testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.baz", pipeline),
   168  				),
   169  			},
   170  		},
   171  	})
   172  }
   173  
   174  func testAccCheckAWSElasticTranscoderPipelineExists(n string, res *elastictranscoder.Pipeline) resource.TestCheckFunc {
   175  	return func(s *terraform.State) error {
   176  		rs, ok := s.RootModule().Resources[n]
   177  		if !ok {
   178  			return fmt.Errorf("Not found: %s", n)
   179  		}
   180  
   181  		if rs.Primary.ID == "" {
   182  			return fmt.Errorf("No Pipeline ID is set")
   183  		}
   184  
   185  		conn := testAccProvider.Meta().(*AWSClient).elastictranscoderconn
   186  
   187  		out, err := conn.ReadPipeline(&elastictranscoder.ReadPipelineInput{
   188  			Id: aws.String(rs.Primary.ID),
   189  		})
   190  
   191  		if err != nil {
   192  			return err
   193  		}
   194  
   195  		*res = *out.Pipeline
   196  
   197  		return nil
   198  	}
   199  }
   200  
   201  func testAccCheckElasticTranscoderPipelineDestroy(s *terraform.State) error {
   202  	conn := testAccProvider.Meta().(*AWSClient).elastictranscoderconn
   203  
   204  	for _, rs := range s.RootModule().Resources {
   205  		if rs.Type != "aws_elastictranscoder_pipline" {
   206  			continue
   207  		}
   208  
   209  		out, err := conn.ReadPipeline(&elastictranscoder.ReadPipelineInput{
   210  			Id: aws.String(rs.Primary.ID),
   211  		})
   212  
   213  		if err == nil {
   214  			if out.Pipeline != nil && *out.Pipeline.Id == rs.Primary.ID {
   215  				return fmt.Errorf("Elastic Transcoder Pipeline still exists")
   216  			}
   217  		}
   218  
   219  		awsErr, ok := err.(awserr.Error)
   220  		if !ok {
   221  			return err
   222  		}
   223  
   224  		if awsErr.Code() != "ResourceNotFoundException" {
   225  			return fmt.Errorf("unexpected error: %s", awsErr)
   226  		}
   227  
   228  	}
   229  	return nil
   230  }
   231  
   232  const awsElasticTranscoderPipelineConfigBasic = `
   233  resource "aws_elastictranscoder_pipeline" "bar" {
   234    input_bucket  = "${aws_s3_bucket.test_bucket.bucket}"
   235    output_bucket = "${aws_s3_bucket.test_bucket.bucket}"
   236    name          = "aws_elastictranscoder_pipeline_tf_test_"
   237    role          = "${aws_iam_role.test_role.arn}"
   238  }
   239  
   240  resource "aws_iam_role" "test_role" {
   241    name = "aws_elastictranscoder_pipeline_tf_test_role_"
   242  
   243    assume_role_policy = <<EOF
   244  {
   245    "Version": "2012-10-17",
   246    "Statement": [
   247      {
   248        "Action": "sts:AssumeRole",
   249        "Principal": {
   250          "Service": "ec2.amazonaws.com"
   251        },
   252        "Effect": "Allow",
   253        "Sid": ""
   254      }
   255    ]
   256  }
   257  EOF
   258  }
   259  
   260  resource "aws_s3_bucket" "test_bucket" {
   261    bucket = "aws-elasticencoder-pipeline-tf-test-bucket"
   262    acl    = "private"
   263  }
   264  `
   265  
   266  const awsElasticTranscoderPipelineConfigKmsKey = `
   267  resource "aws_kms_key" "foo" {
   268    description = "Terraform acc test %d"
   269    policy = <<POLICY
   270  {
   271    "Version": "2012-10-17",
   272    "Id": "kms-tf-1",
   273    "Statement": [
   274      {
   275        "Sid": "Enable IAM User Permissions",
   276        "Effect": "Allow",
   277        "Principal": {
   278          "AWS": "*"
   279        },
   280        "Action": "kms:*",
   281        "Resource": "*"
   282      }
   283    ]
   284  }
   285  POLICY
   286  }
   287  
   288  resource "aws_elastictranscoder_pipeline" "bar" {
   289    input_bucket    = "${aws_s3_bucket.test_bucket.bucket}"
   290    output_bucket   = "${aws_s3_bucket.test_bucket.bucket}"
   291    name            = "aws_elastictranscoder_pipeline_tf_test_"
   292    role            = "${aws_iam_role.test_role.arn}"
   293    aws_kms_key_arn = "${aws_kms_key.foo.arn}"
   294  }
   295  
   296  resource "aws_iam_role" "test_role" {
   297    name = "tf_test_elastictranscoder_pipeline_%d"
   298  
   299    assume_role_policy = <<EOF
   300  {
   301    "Version": "2012-10-17",
   302    "Statement": [
   303      {
   304        "Action": "sts:AssumeRole",
   305        "Principal": {
   306          "Service": "ec2.amazonaws.com"
   307        },
   308        "Effect": "Allow",
   309        "Sid": ""
   310      }
   311    ]
   312  }
   313  EOF
   314  }
   315  
   316  resource "aws_s3_bucket" "test_bucket" {
   317    bucket = "tf-test-aws-elasticencoder-pipeline-%d"
   318    acl    = "private"
   319  }
   320  `
   321  
   322  func awsElasticTranscoderPipelineWithContentConfig(rInt int) string {
   323  	return fmt.Sprintf(`
   324  resource "aws_elastictranscoder_pipeline" "bar" {
   325    input_bucket = "${aws_s3_bucket.content_bucket.bucket}"
   326    name         = "tf_test_pipeline_%d"
   327    role         = "${aws_iam_role.test_role.arn}"
   328  
   329    content_config {
   330      bucket        = "${aws_s3_bucket.content_bucket.bucket}"
   331      storage_class = "Standard"
   332    }
   333  
   334    thumbnail_config {
   335      bucket        = "${aws_s3_bucket.content_bucket.bucket}"
   336      storage_class = "Standard"
   337    }
   338  }
   339  
   340  resource "aws_iam_role" "test_role" {
   341    name = "tf_pipeline_role_%d"
   342  
   343    assume_role_policy = <<EOF
   344  {
   345    "Version": "2012-10-17",
   346    "Statement": [
   347      {
   348        "Action": "sts:AssumeRole",
   349        "Principal": {
   350          "Service": "ec2.amazonaws.com"
   351        },
   352        "Effect": "Allow",
   353        "Sid": ""
   354      }
   355    ]
   356  }
   357  EOF
   358  }
   359  
   360  resource "aws_s3_bucket" "content_bucket" {
   361    bucket = "tf-pipeline-content-%d"
   362    acl    = "private"
   363  }
   364  
   365  resource "aws_s3_bucket" "input_bucket" {
   366    bucket = "tf-pipeline-input-%d"
   367    acl    = "private"
   368  }
   369  
   370  resource "aws_s3_bucket" "thumb_bucket" {
   371    bucket = "tf-pipeline-thumb-%d"
   372    acl    = "private"
   373  }`, rInt, rInt, rInt, rInt, rInt)
   374  }
   375  
   376  func awsElasticTranscoderPipelineWithContentConfigUpdate(rInt int) string {
   377  	return fmt.Sprintf(`
   378  resource "aws_elastictranscoder_pipeline" "bar" {
   379    input_bucket = "${aws_s3_bucket.input_bucket.bucket}"
   380    name         = "tf_test_pipeline_%d"
   381    role         = "${aws_iam_role.test_role.arn}"
   382  
   383    content_config {
   384      bucket        = "${aws_s3_bucket.content_bucket.bucket}"
   385      storage_class = "Standard"
   386    }
   387  
   388    thumbnail_config {
   389      bucket        = "${aws_s3_bucket.thumb_bucket.bucket}"
   390      storage_class = "Standard"
   391    }
   392  }
   393  
   394  resource "aws_iam_role" "test_role" {
   395    name = "tf_pipeline_role_%d"
   396  
   397    assume_role_policy = <<EOF
   398  {
   399    "Version": "2012-10-17",
   400    "Statement": [
   401      {
   402        "Action": "sts:AssumeRole",
   403        "Principal": {
   404          "Service": "ec2.amazonaws.com"
   405        },
   406        "Effect": "Allow",
   407        "Sid": ""
   408      }
   409    ]
   410  }
   411  EOF
   412  }
   413  
   414  resource "aws_s3_bucket" "content_bucket" {
   415    bucket = "tf-pipeline-content-%d"
   416    acl    = "private"
   417  }
   418  
   419  resource "aws_s3_bucket" "input_bucket" {
   420    bucket = "tf-pipeline-input-%d"
   421    acl    = "private"
   422  }
   423  
   424  resource "aws_s3_bucket" "thumb_bucket" {
   425    bucket = "tf-pipeline-thumb-%d"
   426    acl    = "private"
   427  }`, rInt, rInt, rInt, rInt, rInt)
   428  }
   429  
   430  func awsElasticTranscoderPipelineWithPerms(rInt int) string {
   431  	return fmt.Sprintf(`
   432  resource "aws_elastictranscoder_pipeline" "baz" {
   433    input_bucket = "${aws_s3_bucket.content_bucket.bucket}"
   434    name         = "tf_test_pipeline_%d"
   435    role         = "${aws_iam_role.test_role.arn}"
   436  
   437    content_config {
   438      bucket        = "${aws_s3_bucket.content_bucket.bucket}"
   439      storage_class = "Standard"
   440    }
   441  
   442    content_config_permissions = {
   443      grantee_type = "Group"
   444      grantee      = "AuthenticatedUsers"
   445      access       = ["FullControl"]
   446    }
   447  
   448    thumbnail_config {
   449      bucket        = "${aws_s3_bucket.content_bucket.bucket}"
   450      storage_class = "Standard"
   451    }
   452  
   453    thumbnail_config_permissions = {
   454      grantee_type = "Group"
   455      grantee      = "AuthenticatedUsers"
   456      access       = ["FullControl"]
   457    }
   458  }
   459  
   460  resource "aws_iam_role" "test_role" {
   461    name = "tf_pipeline_role_%d"
   462  
   463    assume_role_policy = <<EOF
   464  {
   465    "Version": "2012-10-17",
   466    "Statement": [
   467      {
   468        "Action": "sts:AssumeRole",
   469        "Principal": {
   470          "Service": "ec2.amazonaws.com"
   471        },
   472        "Effect": "Allow",
   473        "Sid": ""
   474      }
   475    ]
   476  }
   477  EOF
   478  }
   479  
   480  resource "aws_s3_bucket" "content_bucket" {
   481    bucket = "tf-transcoding-pipe-%d"
   482    acl    = "private"
   483  }`, rInt, rInt, rInt)
   484  }
   485  
   486  func awsElasticTranscoderNotifications(r int) string {
   487  	return fmt.Sprintf(`
   488  resource "aws_elastictranscoder_pipeline" "bar" {
   489    input_bucket  = "${aws_s3_bucket.test_bucket.bucket}"
   490    output_bucket = "${aws_s3_bucket.test_bucket.bucket}"
   491    name          = "tf-transcoder-%d"
   492    role          = "${aws_iam_role.test_role.arn}"
   493  
   494    notifications {
   495      completed = "${aws_sns_topic.topic_example.arn}"
   496      warning   = "${aws_sns_topic.topic_example.arn}"
   497    }
   498  }
   499  
   500  resource "aws_iam_role" "test_role" {
   501    name = "tf-transcoder-%d"
   502  
   503    assume_role_policy = <<EOF
   504  {
   505    "Version": "2012-10-17",
   506    "Statement": [
   507      {
   508        "Action": "sts:AssumeRole",
   509        "Principal": {
   510          "Service": "ec2.amazonaws.com"
   511        },
   512        "Effect": "Allow",
   513        "Sid": ""
   514      }
   515    ]
   516  }
   517  EOF
   518  }
   519  
   520  resource "aws_s3_bucket" "test_bucket" {
   521    bucket = "tf-transcoder-%d"
   522    acl    = "private"
   523  }
   524  
   525  resource "aws_sns_topic" "topic_example" {
   526    name = "tf-transcoder-%d"
   527  
   528    policy = <<EOF
   529  {
   530    "Version": "2012-10-17",
   531    "Id": "AWSAccountTopicAccess",
   532    "Statement": [
   533      {
   534        "Sid": "*",
   535        "Effect": "Allow",
   536        "Principal": "*",
   537        "Action": "sns:Publish",
   538        "Resource": "*"
   539      }
   540    ]
   541  }
   542  EOF
   543  }`, r, r, r, r)
   544  }
   545  
   546  func awsElasticTranscoderNotifications_update(r int) string {
   547  	return fmt.Sprintf(`
   548  resource "aws_elastictranscoder_pipeline" "bar" {
   549    input_bucket  = "${aws_s3_bucket.test_bucket.bucket}"
   550    output_bucket = "${aws_s3_bucket.test_bucket.bucket}"
   551    name          = "tf-transcoder-%d"
   552    role          = "${aws_iam_role.test_role.arn}"
   553  
   554    notifications {
   555      completed = "${aws_sns_topic.topic_example.arn}"
   556    }
   557  }
   558  
   559  resource "aws_iam_role" "test_role" {
   560    name = "tf-transcoder-%d"
   561  
   562    assume_role_policy = <<EOF
   563  {
   564    "Version": "2012-10-17",
   565    "Statement": [
   566      {
   567        "Action": "sts:AssumeRole",
   568        "Principal": {
   569          "Service": "ec2.amazonaws.com"
   570        },
   571        "Effect": "Allow",
   572        "Sid": ""
   573      }
   574    ]
   575  }
   576  EOF
   577  }
   578  
   579  resource "aws_s3_bucket" "test_bucket" {
   580    bucket = "tf-transcoder-%d"
   581    acl    = "private"
   582  }
   583  
   584  resource "aws_sns_topic" "topic_example" {
   585    name = "tf-transcoder-%d"
   586  
   587    policy = <<EOF
   588  {
   589    "Version": "2012-10-17",
   590    "Id": "AWSAccountTopicAccess",
   591    "Statement": [
   592      {
   593        "Sid": "*",
   594        "Effect": "Allow",
   595        "Principal": "*",
   596        "Action": "sns:Publish",
   597        "Resource": "*"
   598      }
   599    ]
   600  }
   601  EOF
   602  }`, r, r, r, r)
   603  }