github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_lambda_event_source_mapping_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 6 "strconv" 7 "testing" 8 "time" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/aws/awserr" 12 "github.com/aws/aws-sdk-go/service/lambda" 13 "github.com/hashicorp/terraform/helper/resource" 14 "github.com/hashicorp/terraform/terraform" 15 ) 16 17 func TestAccAWSLambdaEventSourceMapping_basic(t *testing.T) { 18 var conf lambda.EventSourceMappingConfiguration 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccCheckLambdaEventSourceMappingDestroy, 24 Steps: []resource.TestStep{ 25 resource.TestStep{ 26 Config: testAccAWSLambdaEventSourceMappingConfig, 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf), 29 testAccCheckAWSLambdaEventSourceMappingAttributes(&conf), 30 ), 31 }, 32 resource.TestStep{ 33 Config: testAccAWSLambdaEventSourceMappingConfigUpdate, 34 Check: resource.ComposeTestCheckFunc( 35 testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf), 36 resource.TestCheckResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", 37 "batch_size", 38 strconv.Itoa(200)), 39 resource.TestCheckResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", 40 "enabled", 41 strconv.FormatBool(false)), 42 resource.TestMatchResourceAttr( 43 "aws_lambda_event_source_mapping.lambda_event_source_mapping_test", 44 "function_arn", 45 regexp.MustCompile("example_lambda_name_update$"), 46 ), 47 ), 48 }, 49 }, 50 }) 51 } 52 53 func TestAccAWSLambdaEventSourceMapping_disappears(t *testing.T) { 54 var conf lambda.EventSourceMappingConfiguration 55 56 resource.Test(t, resource.TestCase{ 57 PreCheck: func() { testAccPreCheck(t) }, 58 Providers: testAccProviders, 59 CheckDestroy: testAccCheckLambdaEventSourceMappingDestroy, 60 Steps: []resource.TestStep{ 61 resource.TestStep{ 62 Config: testAccAWSLambdaEventSourceMappingConfig, 63 Check: resource.ComposeTestCheckFunc( 64 testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf), 65 testAccCheckAWSLambdaEventSourceMappingDisappears(&conf), 66 ), 67 ExpectNonEmptyPlan: true, 68 }, 69 }, 70 }) 71 } 72 73 func testAccCheckAWSLambdaEventSourceMappingDisappears(conf *lambda.EventSourceMappingConfiguration) resource.TestCheckFunc { 74 return func(s *terraform.State) error { 75 conn := testAccProvider.Meta().(*AWSClient).lambdaconn 76 77 params := &lambda.DeleteEventSourceMappingInput{ 78 UUID: conf.UUID, 79 } 80 81 _, err := conn.DeleteEventSourceMapping(params) 82 if err != nil { 83 if err != nil { 84 return err 85 } 86 } 87 88 return resource.Retry(10*time.Minute, func() *resource.RetryError { 89 params := &lambda.GetEventSourceMappingInput{ 90 UUID: conf.UUID, 91 } 92 _, err := conn.GetEventSourceMapping(params) 93 if err != nil { 94 cgw, ok := err.(awserr.Error) 95 if ok && cgw.Code() == "ResourceNotFoundException" { 96 return nil 97 } 98 return resource.NonRetryableError( 99 fmt.Errorf("Error retrieving Lambda Event Source Mapping: %s", err)) 100 } 101 return resource.RetryableError(fmt.Errorf( 102 "Waiting for Lambda Event Source Mapping: %v", conf.UUID)) 103 }) 104 } 105 } 106 107 func testAccCheckLambdaEventSourceMappingDestroy(s *terraform.State) error { 108 conn := testAccProvider.Meta().(*AWSClient).lambdaconn 109 110 for _, rs := range s.RootModule().Resources { 111 if rs.Type != "aws_lambda_event_source_mapping" { 112 continue 113 } 114 115 _, err := conn.GetEventSourceMapping(&lambda.GetEventSourceMappingInput{ 116 UUID: aws.String(rs.Primary.ID), 117 }) 118 119 if err == nil { 120 return fmt.Errorf("Lambda event source mapping was not deleted") 121 } 122 123 } 124 125 return nil 126 127 } 128 129 func testAccCheckAwsLambdaEventSourceMappingExists(n string, mapping *lambda.EventSourceMappingConfiguration) resource.TestCheckFunc { 130 // Wait for IAM role 131 return func(s *terraform.State) error { 132 rs, ok := s.RootModule().Resources[n] 133 if !ok { 134 return fmt.Errorf("Lambda event source mapping not found: %s", n) 135 } 136 137 if rs.Primary.ID == "" { 138 return fmt.Errorf("Lambda event source mapping ID not set") 139 } 140 141 conn := testAccProvider.Meta().(*AWSClient).lambdaconn 142 143 params := &lambda.GetEventSourceMappingInput{ 144 UUID: aws.String(rs.Primary.ID), 145 } 146 147 getSourceMappingConfiguration, err := conn.GetEventSourceMapping(params) 148 if err != nil { 149 return err 150 } 151 152 *mapping = *getSourceMappingConfiguration 153 154 return nil 155 } 156 } 157 158 func testAccCheckAWSLambdaEventSourceMappingAttributes(mapping *lambda.EventSourceMappingConfiguration) resource.TestCheckFunc { 159 return func(s *terraform.State) error { 160 uuid := *mapping.UUID 161 if uuid == "" { 162 return fmt.Errorf("Could not read Lambda event source mapping's UUID") 163 } 164 165 return nil 166 } 167 } 168 169 const testAccAWSLambdaEventSourceMappingConfig = ` 170 resource "aws_iam_role" "iam_for_lambda" { 171 name = "iam_for_lambda" 172 assume_role_policy = <<EOF 173 { 174 "Version": "2012-10-17", 175 "Statement": [ 176 { 177 "Action": "sts:AssumeRole", 178 "Principal": { 179 "Service": "lambda.amazonaws.com" 180 }, 181 "Effect": "Allow", 182 "Sid": "" 183 } 184 ] 185 } 186 EOF 187 } 188 189 resource "aws_iam_policy" "policy_for_role" { 190 name = "policy_for_role" 191 path = "/" 192 description = "IAM policy for for Lamda event mapping testing" 193 policy = <<EOF 194 { 195 "Version": "2012-10-17", 196 "Statement": [ 197 { 198 "Effect": "Allow", 199 "Action": [ 200 "kinesis:GetRecords", 201 "kinesis:GetShardIterator", 202 "kinesis:DescribeStream" 203 ], 204 "Resource": "*" 205 }, 206 { 207 "Effect": "Allow", 208 "Action": [ 209 "kinesis:ListStreams" 210 ], 211 "Resource": "*" 212 } 213 ] 214 } 215 EOF 216 } 217 218 resource "aws_iam_policy_attachment" "policy_attachment_for_role" { 219 name = "policy_attachment_for_role" 220 roles = ["${aws_iam_role.iam_for_lambda.name}"] 221 policy_arn = "${aws_iam_policy.policy_for_role.arn}" 222 } 223 224 resource "aws_kinesis_stream" "kinesis_stream_test" { 225 name = "kinesis_stream_test" 226 shard_count = 1 227 } 228 229 resource "aws_lambda_function" "lambda_function_test_create" { 230 filename = "test-fixtures/lambdatest.zip" 231 function_name = "example_lambda_name_create" 232 role = "${aws_iam_role.iam_for_lambda.arn}" 233 handler = "exports.example" 234 } 235 236 resource "aws_lambda_function" "lambda_function_test_update" { 237 filename = "test-fixtures/lambdatest.zip" 238 function_name = "example_lambda_name_update" 239 role = "${aws_iam_role.iam_for_lambda.arn}" 240 handler = "exports.example" 241 } 242 243 resource "aws_lambda_event_source_mapping" "lambda_event_source_mapping_test" { 244 batch_size = 100 245 event_source_arn = "${aws_kinesis_stream.kinesis_stream_test.arn}" 246 enabled = true 247 depends_on = ["aws_iam_policy_attachment.policy_attachment_for_role"] 248 function_name = "${aws_lambda_function.lambda_function_test_create.arn}" 249 starting_position = "TRIM_HORIZON" 250 } 251 ` 252 253 const testAccAWSLambdaEventSourceMappingConfigUpdate = ` 254 resource "aws_iam_role" "iam_for_lambda" { 255 name = "iam_for_lambda" 256 assume_role_policy = <<EOF 257 { 258 "Version": "2012-10-17", 259 "Statement": [ 260 { 261 "Action": "sts:AssumeRole", 262 "Principal": { 263 "Service": "lambda.amazonaws.com" 264 }, 265 "Effect": "Allow", 266 "Sid": "" 267 } 268 ] 269 } 270 EOF 271 } 272 273 resource "aws_iam_policy" "policy_for_role" { 274 name = "policy_for_role" 275 path = "/" 276 description = "IAM policy for for Lamda event mapping testing" 277 policy = <<EOF 278 { 279 "Version": "2012-10-17", 280 "Statement": [ 281 { 282 "Effect": "Allow", 283 "Action": [ 284 "kinesis:GetRecords", 285 "kinesis:GetShardIterator", 286 "kinesis:DescribeStream" 287 ], 288 "Resource": "*" 289 }, 290 { 291 "Effect": "Allow", 292 "Action": [ 293 "kinesis:ListStreams" 294 ], 295 "Resource": "*" 296 } 297 ] 298 } 299 EOF 300 } 301 302 resource "aws_iam_policy_attachment" "policy_attachment_for_role" { 303 name = "policy_attachment_for_role" 304 roles = ["${aws_iam_role.iam_for_lambda.name}"] 305 policy_arn = "${aws_iam_policy.policy_for_role.arn}" 306 } 307 308 resource "aws_kinesis_stream" "kinesis_stream_test" { 309 name = "kinesis_stream_test" 310 shard_count = 1 311 } 312 313 resource "aws_lambda_function" "lambda_function_test_create" { 314 filename = "test-fixtures/lambdatest.zip" 315 function_name = "example_lambda_name_create" 316 role = "${aws_iam_role.iam_for_lambda.arn}" 317 handler = "exports.example" 318 } 319 320 resource "aws_lambda_function" "lambda_function_test_update" { 321 filename = "test-fixtures/lambdatest.zip" 322 function_name = "example_lambda_name_update" 323 role = "${aws_iam_role.iam_for_lambda.arn}" 324 handler = "exports.example" 325 } 326 327 resource "aws_lambda_event_source_mapping" "lambda_event_source_mapping_test" { 328 batch_size = 200 329 event_source_arn = "${aws_kinesis_stream.kinesis_stream_test.arn}" 330 enabled = false 331 depends_on = ["aws_iam_policy_attachment.policy_attachment_for_role"] 332 function_name = "${aws_lambda_function.lambda_function_test_update.arn}" 333 starting_position = "TRIM_HORIZON" 334 } 335 `