1+ import base64
12import json
2- import os
3+ import re
34import unittest
45
56import boto3
@@ -28,7 +29,7 @@ def test_script(self):
2829 )
2930 raw_payload = response ['Payload' ].read ().decode ('utf-8' )
3031 result = json .loads (raw_payload )
31- self .assertEqual (result , 2 )
32+ self .assertEqual (2 , result )
3233
3334 def test_lowercase_extension (self ):
3435 lambda_client = self .get_client ()
@@ -37,7 +38,7 @@ def test_lowercase_extension(self):
3738 )
3839 raw_payload = response ['Payload' ].read ().decode ('utf-8' )
3940 result = json .loads (raw_payload )
40- self .assertEqual (result , 2 )
41+ self .assertEqual (2 , result )
4142
4243 def test_multiple_arguments (self ):
4344 lambda_client = self .get_client ()
@@ -47,7 +48,39 @@ def test_multiple_arguments(self):
4748 )
4849 raw_payload = response ['Payload' ].read ().decode ('utf-8' )
4950 result = json .loads (raw_payload )
50- self .assertDictEqual (result , payload )
51+ self .assertDictEqual (payload , result )
52+
53+ @unittest .skipIf (is_local (), 'Lambda local does not support log retrieval' )
54+ def test_debug_logging (self ):
55+ lambda_client = self .get_client ()
56+ response = lambda_client .invoke (FunctionName = get_function_name ("LoggingFunction" ),
57+ LogType = 'Tail' ,
58+ Payload = json .dumps ({'x' : 1 }),
59+ )
60+ raw_payload = response ['Payload' ].read ().decode ('utf-8' )
61+ result = json .loads (raw_payload )
62+ self .assertEqual (1 , result )
63+ log = base64 .b64decode (response ['LogResult' ]).decode ('utf-8' )
64+ self .assertIn ("runtime:Sourcing 'script.R'" , log )
65+ self .assertIn ("runtime:Invoking function 'handler_with_debug_logging' with parameters:\n $x\n [1] 1" , log )
66+ self .assertIn ("runtime:Function returned:\n [1] 1" , log )
67+ self .assertIn ("runtime:Posted result:\n " , log )
68+
69+ @unittest .skipIf (is_local (), 'Lambda local does not support log retrieval' )
70+ def test_no_debug_logging (self ):
71+ lambda_client = self .get_client ()
72+ response = lambda_client .invoke (FunctionName = get_function_name ("ExampleFunction" ),
73+ LogType = 'Tail' ,
74+ Payload = json .dumps ({'x' : 1 }),
75+ )
76+ raw_payload = response ['Payload' ].read ().decode ('utf-8' )
77+ result = json .loads (raw_payload )
78+ self .assertEqual (2 , result )
79+ log = base64 .b64decode (response ['LogResult' ]).decode ('utf-8' )
80+ self .assertNotIn ("Sourcing " , log )
81+ self .assertNotIn ("Invoking function " , log )
82+ self .assertNotIn ("Function returned:" , log )
83+ self .assertNotIn ("Posted result:" , log )
5184
5285 @unittest .skipIf (is_local (), 'Lambda local does not pass errors properly' )
5386 def test_missing_source_file (self ):
@@ -58,7 +91,7 @@ def test_missing_source_file(self):
5891 raw_payload = response ['Payload' ].read ().decode ('utf-8' )
5992 json_payload = json .loads (raw_payload )
6093 self .assertIn ('Source file does not exist: missing.[R|r]' , json_payload ['errorMessage' ])
61- self .assertEqual (json_payload [ 'errorType' ], 'simpleError' )
94+ self .assertEqual ('simpleError' , json_payload [ 'errorType' ] )
6295
6396 @unittest .skipIf (is_local (), 'Lambda local does not pass errors properly' )
6497 def test_missing_function (self ):
@@ -69,7 +102,7 @@ def test_missing_function(self):
69102 raw_payload = response ['Payload' ].read ().decode ('utf-8' )
70103 json_payload = json .loads (raw_payload )
71104 self .assertIn ('Function "handler_missing" does not exist' , json_payload ['errorMessage' ])
72- self .assertEqual (json_payload [ 'errorType' ], 'simpleError' )
105+ self .assertEqual ('simpleError' , json_payload [ 'errorType' ] )
73106
74107 @unittest .skipIf (is_local (), 'Lambda local does not pass errors properly' )
75108 def test_function_as_variable (self ):
@@ -80,7 +113,7 @@ def test_function_as_variable(self):
80113 raw_payload = response ['Payload' ].read ().decode ('utf-8' )
81114 json_payload = json .loads (raw_payload )
82115 self .assertIn ('Function "handler_as_variable" does not exist' , json_payload ['errorMessage' ])
83- self .assertEqual (json_payload [ 'errorType' ], 'simpleError' )
116+ self .assertEqual ('simpleError' , json_payload [ 'errorType' ] )
84117
85118 @unittest .skipIf (is_local (), 'Lambda local does not pass errors properly' )
86119 def test_missing_argument (self ):
@@ -89,7 +122,7 @@ def test_missing_argument(self):
89122 raw_payload = response ['Payload' ].read ().decode ('utf-8' )
90123 json_payload = json .loads (raw_payload )
91124 self .assertIn ('argument "x" is missing, with no default' , json_payload ['errorMessage' ])
92- self .assertEqual (json_payload [ 'errorType' ], 'simpleError' )
125+ self .assertEqual ('simpleError' , json_payload [ 'errorType' ] )
93126
94127 @unittest .skipIf (is_local (), 'Lambda local does not pass errors properly' )
95128 def test_unused_argument (self ):
@@ -100,18 +133,18 @@ def test_unused_argument(self):
100133 raw_payload = response ['Payload' ].read ().decode ('utf-8' )
101134 json_payload = json .loads (raw_payload )
102135 self .assertIn ('unused argument (y = 1)' , json_payload ['errorMessage' ])
103- self .assertEqual (json_payload [ 'errorType' ], 'simpleError' )
136+ self .assertEqual ('simpleError' , json_payload [ 'errorType' ] )
104137
105- @unittest .skip ( 'Lambda local does not pass errors properly ' )
138+ @unittest .skipIf ( is_local (), 'Fails locally with "argument list too long" ' )
106139 def test_long_argument (self ):
107140 lambda_client = self .get_client ()
108141 payload = {x : x for x in range (0 , 100000 )}
109142 response = lambda_client .invoke (FunctionName = get_function_name ("VariableArgumentsFunction" ),
110143 Payload = json .dumps (payload ),
111144 )
112145 raw_payload = response ['Payload' ].read ().decode ('utf-8' )
113- json_payload = json .loads (raw_payload )
114- self .assertEqual (json_payload [ 'errorType' ], 'Runtime.ExitError' )
146+ result = json .loads (raw_payload )
147+ self .assertEqual (1 , result )
115148
116149 @unittest .skipIf (is_local (), 'Lambda local does not pass errors properly' )
117150 def test_missing_library (self ):
@@ -123,7 +156,7 @@ def test_missing_library(self):
123156 json_payload = json .loads (raw_payload )
124157 self .assertIn ('there is no package called ‘Matrix’' , json_payload ['errorMessage' ])
125158 error_type = 'packageNotFoundError' if get_version () == '3_6_0' else 'simpleError'
126- self .assertEqual (json_payload ['errorType' ], error_type )
159+ self .assertEqual (error_type , json_payload ['errorType' ])
127160
128161 @classmethod
129162 def tearDownClass (cls ):
0 commit comments