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