1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
|
Shindo.tests('AWS::Lambda | function requests', ['aws', 'lambda']) do
_lambda = Fog::AWS[:lambda]
account_id = _lambda.account_id
region = _lambda.region
function1 = IO.read(AWS::Lambda::Samples::FUNCTION_1)
function2 = IO.read(AWS::Lambda::Samples::FUNCTION_2)
zipped_function1 = Base64::encode64(AWS::Lambda::Formats.zip(function1))
zipped_function2 = Base64::encode64(AWS::Lambda::Formats.zip(function2))
function1_arn = nil
function1_name = 'function1'
function2_name = 'function2'
function1_handler = 'index.handler'
function_role = 'arn:aws:iam::647975416665:role/lambda_basic_execution'
sns_principal = 'sns.amazonaws.com'
sns_topic_sid = Fog::Mock.random_letters_and_numbers(32)
sns_allowed_action = 'lambda:invoke'
sns_topic_arn = Fog::AWS::Mock.arn('sns', account_id, 'mock_topic', region)
kinesis_stream_arn = Fog::AWS::Mock.arn('kinesis', account_id, 'mock_stream', region)
event_source_mapping1_id = nil
tests('success') do
tests('#list_functions').formats(AWS::Lambda::Formats::LIST_FUNCTIONS) do
result = _lambda.list_functions.body
functions = result['Functions']
returns(true) { functions.empty? }
result
end
tests('#create_function').formats(AWS::Lambda::Formats::CREATE_FUNCTION) do
description = 'a copy of my first function'
result = _lambda.create_function(
'FunctionName' => function1_name,
'Handler' => function1_handler,
'Role' => function_role,
'Description' => description,
'Code' => { 'ZipFile' => zipped_function1 }
).body
returns(true) { result.has_key?('FunctionArn') }
returns(true) { result['CodeSize'] > 0 }
returns(true) { result['MemorySize'] >= 128 }
returns(true) { result['FunctionName'].eql?(function1_name) }
returns(true) { result['Handler'].eql?(function1_handler) }
function1_arn = result['FunctionArn']
result
end
tests('#invoke') do
payload = { 'value1' => 2, 'value2' => 42 }
result = _lambda.invoke(
'FunctionName' => function1_name,
'Payload' => payload
).body
returns(false) { result.length.zero? }
returns(false) { result.match(/function:#{function1_name} was invoked/).nil? }
result
end
tests('#get_function').formats(AWS::Lambda::Formats::GET_FUNCTION) do
result = _lambda.get_function('FunctionName' => function1_name).body
func_config = result['Configuration']
returns(false) { result['Code']['Location'].match(/^https:\/\/awslambda-/).nil? }
returns(true) { func_config.has_key?('FunctionArn') }
returns(true) { func_config['CodeSize'] > 0 }
returns(true) { func_config['MemorySize'] >= 128 }
returns(true) { func_config['FunctionName'].eql?(function1_name) }
returns(true) { func_config['Handler'].eql?(function1_handler) }
returns(true) { func_config['FunctionArn'].eql?(function1_arn) }
result
end
tests('#get_function_configuration').formats(AWS::Lambda::Formats::GET_FUNCTION_CONFIGURATION) do
result = _lambda.get_function_configuration(
'FunctionName' => function1_name).body
returns(true) { result.has_key?('FunctionArn') }
returns(true) { result['CodeSize'] > 0 }
returns(true) { result['MemorySize'] >= 128 }
returns(true) { result['FunctionName'].eql?(function1_name) }
returns(true) { result['Handler'].eql?(function1_handler) }
returns(true) { result['FunctionArn'].eql?(function1_arn) }
result
end
tests('#update_function_configuration').formats(AWS::Lambda::Formats::UPDATE_FUNCTION_CONFIGURATION) do
new_memory_size = 256
new_description = "this function does nothing, just let's call it foobar"
new_timeout = 10
result = _lambda.update_function_configuration(
'FunctionName' => function1_name,
'MemorySize' => new_memory_size,
'Description' => new_description,
'Timeout' => new_timeout
).body
returns(true) { result['CodeSize'] > 0 }
returns(true) { result['MemorySize'].eql?(new_memory_size) }
returns(true) { result['FunctionArn'].eql?(function1_arn) }
returns(true) { result['Description'].eql?(new_description) }
returns(true) { result['Timeout'].eql?(new_timeout) }
result
end
tests('#update_function_code').formats(AWS::Lambda::Formats::UPDATE_FUNCTION_CODE) do
result = _lambda.update_function_code(
'FunctionName' => function1_name,
'ZipFile' => zipped_function2
).body
returns(true) { result.has_key?('FunctionArn') }
returns(true) { result['CodeSize'] > 0 }
returns(true) { result['MemorySize'] >= 128 }
returns(true) { result['FunctionName'].eql?(function1_name) }
returns(true) { result['Handler'].eql?(function1_handler) }
result
end
tests('#add_permission').formats(AWS::Lambda::Formats::ADD_PERMISSION) do
params = {
'FunctionName' => function1_name,
'Principal' => sns_principal,
'StatementId' => sns_topic_sid,
'Action' => sns_allowed_action,
'SourceArn' => sns_topic_arn
}
result = _lambda.add_permission(params).body
statement = result['Statement']
returns(true) { statement['Action'].include?(sns_allowed_action) }
returns(true) { statement['Principal']['Service'].eql?(sns_principal) }
returns(true) { statement['Sid'].eql?(sns_topic_sid) }
returns(true) { statement['Resource'].eql?(function1_arn) }
returns(true) { statement['Effect'].eql?('Allow') }
returns(false) { statement['Condition'].empty? }
result
end
tests('#get_policy').formats(AWS::Lambda::Formats::GET_POLICY) do
result = _lambda.get_policy('FunctionName' => function1_name).body
policy = result['Policy']
returns(false) { policy['Statement'].empty? }
statement = policy['Statement'].first
returns(true) { statement['Action'].include?(sns_allowed_action) }
returns(true) { statement['Principal']['Service'].eql?(sns_principal) }
returns(true) { statement['Sid'].eql?(sns_topic_sid) }
returns(true) { statement['Resource'].eql?(function1_arn) }
returns(true) { statement['Effect'].eql?('Allow') }
returns(false) { statement['Condition'].empty? }
result
end
tests('#remove_permission') do
params = {
'FunctionName' => function1_name,
'StatementId' => sns_topic_sid
}
result = _lambda.remove_permission(params).body
returns(true) { result.empty? }
raises(Fog::AWS::Lambda::Error) do
_lambda.get_policy('FunctionName' => function1_name)
end
result
end
tests('#create_event_source_mapping').formats(AWS::Lambda::Formats::CREATE_EVENT_SOURCE_MAPPING) do
params = {
'FunctionName' => function1_name,
'EventSourceArn' => kinesis_stream_arn,
'Enabled' => true,
'StartingPosition' => 'TRIM_HORIZON'
}
result = _lambda.create_event_source_mapping(params).body
returns(true) { result['BatchSize'] > 0 }
returns(true) { result['EventSourceArn'].eql?(kinesis_stream_arn) }
returns(true) { result['FunctionArn'].eql?(function1_arn) }
returns(true) { result['LastProcessingResult'].eql?('No records processed') }
returns(true) { result['State'].eql?('Creating') }
returns(true) { result['StateTransitionReason'].eql?('User action') }
event_source_mapping1_id = result['UUID']
result
end
tests('#list_event_source_mappings').formats(AWS::Lambda::Formats::LIST_EVENT_SOURCE_MAPPINGS) do
params = { 'FunctionName' => function1_name }
result = _lambda.list_event_source_mappings(params).body
event_source_mappings = result['EventSourceMappings']
returns(false) { event_source_mappings.empty? }
mapping = event_source_mappings.first
returns(true) { mapping['UUID'].eql?(event_source_mapping1_id) }
result
end
tests('#get_event_source_mapping').formats(AWS::Lambda::Formats::GET_EVENT_SOURCE_MAPPING) do
params = { 'UUID' => event_source_mapping1_id }
result = _lambda.get_event_source_mapping(params).body
returns(true) { result['BatchSize'] > 0 }
returns(true) { result['EventSourceArn'].eql?(kinesis_stream_arn) }
returns(true) { result['FunctionArn'].eql?(function1_arn) }
returns(true) { result['LastProcessingResult'].eql?('OK') }
returns(true) { result['State'].eql?('Enabled') }
returns(true) { result['StateTransitionReason'].eql?('User action') }
returns(true) { result['UUID'].eql?(event_source_mapping1_id) }
result
end
tests('#update_event_source_mapping').formats(AWS::Lambda::Formats::UPDATE_EVENT_SOURCE_MAPPING) do
new_batch_size = 500
enabled_mapping = false
params = {
'UUID' => event_source_mapping1_id,
'BatchSize' => new_batch_size,
'Enabled' => enabled_mapping
}
result = _lambda.update_event_source_mapping(params).body
returns(true) { result['BatchSize'].eql?(new_batch_size) }
returns(true) { result['EventSourceArn'].eql?(kinesis_stream_arn) }
returns(true) { result['FunctionArn'].eql?(function1_arn) }
returns(true) { result['LastProcessingResult'].eql?('OK') }
returns(true) { result['State'].eql?('Disabling') }
returns(true) { result['StateTransitionReason'].eql?('User action') }
returns(true) { result['UUID'].eql?(event_source_mapping1_id) }
result
end
tests('#delete_event_source_mapping').formats(AWS::Lambda::Formats::DELETE_EVENT_SOURCE_MAPPING) do
params = { 'UUID' => event_source_mapping1_id }
result = _lambda.delete_event_source_mapping(params).body
returns(true) { result['BatchSize'] > 0 }
returns(true) { result['EventSourceArn'].eql?(kinesis_stream_arn) }
returns(true) { result['FunctionArn'].eql?(function1_arn) }
returns(false) { result['LastProcessingResult'].empty? }
returns(true) { result['State'].eql?('Deleting') }
returns(true) { result['StateTransitionReason'].eql?('User action') }
returns(true) { result['UUID'].eql?(event_source_mapping1_id) }
result
end
tests('#list_event_source_mappings again').formats(AWS::Lambda::Formats::LIST_EVENT_SOURCE_MAPPINGS) do
params = { 'FunctionName' => function1_name }
result = _lambda.list_event_source_mappings(params).body
event_source_mappings = result['EventSourceMappings']
returns(true) { event_source_mappings.empty? }
result
end
tests('#delete_function') do
result = _lambda.delete_function('FunctionName' => function1_name).body
returns(true) { result.empty? }
raises(Fog::AWS::Lambda::Error) do
_lambda.get_function('FunctionName' => function1_name)
end
result
end
tests('#list_functions again').formats(AWS::Lambda::Formats::LIST_FUNCTIONS) do
result = _lambda.list_functions.body
functions = result['Functions']
returns(true) { functions.empty? }
result
end
tests('#create_function for failures tests').formats(AWS::Lambda::Formats::CREATE_FUNCTION) do
description = 'failure tests function'
result = _lambda.create_function(
'FunctionName' => function2_name,
'Handler' => function1_handler,
'Role' => function_role,
'Description' => description,
'Code' => { 'ZipFile' => zipped_function1 }
).body
returns(true) { result.has_key?('FunctionArn') }
returns(true) { result['CodeSize'] > 0 }
returns(true) { result['MemorySize'] >= 128 }
returns(true) { result['FunctionName'].eql?(function2_name) }
returns(true) { result['Handler'].eql?(function1_handler) }
result
end
end
tests('failures') do
tests("#invoke without function name").raises(Fog::AWS::Lambda::Error) do
response = _lambda.invoke.body
end
tests("#invoke nonexistent function").raises(Fog::AWS::Lambda::Error) do
response = Fog::AWS[:lambda].invoke('FunctionName' => 'nonexistent').body
end
tests("#get_function without function name").raises(Fog::AWS::Lambda::Error) do
response = _lambda.get_function.body
end
tests("#get_function on nonexistent function").raises(Fog::AWS::Lambda::Error) do
response = _lambda.get_function('FunctionName' => 'nonexistent').body
end
tests("#get_function_configuration without function name").raises(Fog::AWS::Lambda::Error) do
response = _lambda.get_function_configuration.body
end
tests("#get_function_configuration on nonexistent function").raises(Fog::AWS::Lambda::Error) do
response = _lambda.get_function_configuration('FunctionName' => 'nonexistent').body
end
tests("update_function_configuration without function name").raises(Fog::AWS::Lambda::Error) do
response = _lambda.update_function_configuration.body
end
tests("#update_function_configuration on nonexistent function").raises(Fog::AWS::Lambda::Error) do
response = _lambda.update_function_configuration('FunctionName' => 'nonexistent').body
end
tests("update_function_code without function name").raises(Fog::AWS::Lambda::Error) do
response = _lambda.update_function_code.body
end
tests("#update_function_code on nonexistent function").raises(Fog::AWS::Lambda::Error) do
response = _lambda.update_function_code(
'FunctionName' => 'nonexistent',
'ZipFile' => zipped_function2
).body
end
tests("#update_function_code on valid function without source").raises(Fog::AWS::Lambda::Error) do
response = _lambda.update_function_code('FunctionName' => 'foobar').body
end
tests("#delete_function without params").raises(Fog::AWS::Lambda::Error) do
response = _lambda.delete_function.body
end
tests("#delete_function on nonexistent function").raises(Fog::AWS::Lambda::Error) do
response = _lambda.delete_function('FunctionName' => 'nonexistent').body
end
tests('#get_policy without params').raises(Fog::AWS::Lambda::Error) do
response = _lambda.get_policy.body
end
tests('#get_policy on nonexistent function').raises(Fog::AWS::Lambda::Error) do
response = _lambda.get_policy('FunctionName' => 'nonexistent').body
end
tests('#get_policy on function without permissions').raises(Fog::AWS::Lambda::Error) do
response = _lambda.get_policy('FunctionName' => function2_name).body
end
tests('#add_permission without params').raises(Fog::AWS::Lambda::Error) do
response = _lambda.add_permission.body
end
tests('#add_permission on nonexistent function').raises(Fog::AWS::Lambda::Error) do
response = _lambda.add_permission('FunctionName' => 'nonexistent').body
end
tests('#add_permission with missing params').raises(Fog::AWS::Lambda::Error) do
response = _lambda.add_permission('FunctionName' => function2_name).body
end
tests('#remove_permission without params').raises(Fog::AWS::Lambda::Error) do
response = _lambda.remove_permission.body
end
tests('#remove_permission on nonexistent function').raises(Fog::AWS::Lambda::Error) do
response = _lambda.remove_permission('FunctionName' => 'nonexistent').body
end
tests('#remove_permission on function with missing sid param').raises(Fog::AWS::Lambda::Error) do
response = _lambda.get_policy('FunctionName' => function2_name).body
end
tests('#remove_permission on function with missing sid param').raises(Fog::AWS::Lambda::Error) do
response = _lambda.get_policy(
'FunctionName' => function2_name,
'StatementId' => 'nonexistent_statement_id'
).body
end
tests('#create_event_source_mapping without params').raises(Fog::AWS::Lambda::Error) do
response = _lambda.create_event_source_mapping.body
end
tests('#create_event_source_mapping on nonexistent function').raises(Fog::AWS::Lambda::Error) do
response = _lambda.create_event_source_mapping('FunctionName' => 'nonexistent').body
end
tests('#create_event_source_mapping with missing params').raises(Fog::AWS::Lambda::Error) do
response = _lambda.create_event_source_mapping('FunctionName' => function2_name).body
end
tests('#get_event_source_mapping without params').raises(Fog::AWS::Lambda::Error) do
response = _lambda.get_event_source_mapping.body
end
tests('#get_event_source_mapping nonexistent').raises(Fog::AWS::Lambda::Error) do
mapping_id = "deadbeef-caca-cafe-cafa-ffffdeadbeef"
response = _lambda.get_event_source_mapping('UUID' => mapping_id).body
end
tests('#update_event_source_mapping without params').raises(Fog::AWS::Lambda::Error) do
response = _lambda.update_event_source_mapping.body
end
tests('#update_event_source_mapping nonexistent').raises(Fog::AWS::Lambda::Error) do
mapping_id = "deadbeef-caca-cafe-cafa-ffffdeadbeef"
response = _lambda.update_event_source_mapping('UUID' => mapping_id).body
end
tests('#delete_event_source_mapping without params').raises(Fog::AWS::Lambda::Error) do
response = _lambda.delete_event_source_mapping.body
end
tests('#delete_event_source_mapping nonexistent').raises(Fog::AWS::Lambda::Error) do
mapping_id = "deadbeef-caca-cafe-cafa-ffffdeadbeef"
response = _lambda.create_event_source_mapping('UUID' => mapping_id).body
end
end
end
|