def test_java_runtime_with_lib(self): java_jar_with_lib = load_file(TEST_LAMBDA_JAVA_WITH_LIB, mode='rb') # create ZIP file from JAR file jar_dir = new_tmp_dir() zip_dir = new_tmp_dir() unzip(TEST_LAMBDA_JAVA_WITH_LIB, jar_dir) shutil.move(os.path.join(jar_dir, 'lib'), os.path.join(zip_dir, 'lib')) jar_without_libs_file = testutil.create_zip_file(jar_dir) shutil.copy(jar_without_libs_file, os.path.join(zip_dir, 'lib', 'lambda.jar')) java_zip_with_lib = testutil.create_zip_file(zip_dir, get_content=True) for archive in [java_jar_with_lib, java_zip_with_lib]: lambda_name = 'test-%s' % short_uid() testutil.create_lambda_function( func_name=lambda_name, zip_file=archive, runtime=LAMBDA_RUNTIME_JAVA8, handler='cloud.localstack.sample.LambdaHandlerWithLib') result = self.lambda_client.invoke(FunctionName=lambda_name, Payload=b'{"echo":"echo"}') result_data = result['Payload'].read() self.assertEqual(result['StatusCode'], 200) self.assertIn('echo', to_str(result_data)) # clean up testutil.delete_lambda_function(lambda_name)
def test_create_archive(self): # create archive from empty directory tmp_dir = new_tmp_dir() content = create_zip_file(tmp_dir, get_content=True) zip_obj = zipfile.ZipFile(io.BytesIO(content)) assert zip_obj.infolist() == [] rm_rf(tmp_dir) # create archive from non-empty directory tmp_dir = new_tmp_dir() save_file(os.path.join(tmp_dir, "testfile"), "content 123") content = create_zip_file(tmp_dir, get_content=True) zip_obj = zipfile.ZipFile(io.BytesIO(content)) assert len(zip_obj.infolist()) == 1 assert zip_obj.infolist()[0].filename == "testfile" rm_rf(tmp_dir)
def get_lambda_code_param(params, _include_arch=False, **kwargs): code = params.get("Code", {}) zip_file = code.get("ZipFile") if zip_file and not is_base64(zip_file) and not is_zip_file( to_bytes(zip_file)): tmp_dir = new_tmp_dir() handler_file = get_handler_file_from_name( params["Handler"], runtime=params["Runtime"]) tmp_file = os.path.join(tmp_dir, handler_file) save_file(tmp_file, zip_file) # add 'cfn-response' module to archive - see: # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-lambda-function-code-cfnresponsemodule.html cfn_response_tmp_file = get_cfn_response_mod_file() cfn_response_mod_dir = os.path.join(tmp_dir, "node_modules", "cfn-response") mkdir(cfn_response_mod_dir) cp_r( cfn_response_tmp_file, os.path.join(cfn_response_mod_dir, "index.js"), ) # create zip file zip_file = create_zip_file(tmp_dir, get_content=True) code["ZipFile"] = zip_file rm_rf(tmp_dir) if _include_arch and "Architectures" in params: code["Architectures"] = params.get("Architectures") return code
def test_get_java_lib_folder_classpath_no_directories(self): base_dir = new_tmp_dir() jar_file = os.path.join(base_dir, 'foo.jar') save_file(jar_file, '') lib_file = os.path.join(base_dir, 'lib', 'lib.jar') mkdir(os.path.dirname(lib_file)) save_file(lib_file, '') self.assertEquals('.:foo.jar:lib/lib.jar', lambda_executors.Util.get_java_classpath(jar_file))
def test_get_java_lib_folder_classpath_no_directories(self): base_dir = new_tmp_dir() jar_file = os.path.join(base_dir, "foo.jar") save_file(jar_file, "") lib_file = os.path.join(base_dir, "lib", "lib.jar") mkdir(os.path.dirname(lib_file)) save_file(lib_file, "") classpath = lambda_executors.Util.get_java_classpath(jar_file) self.assertIn(":foo.jar", classpath) self.assertIn("lib/lib.jar:", classpath) self.assertIn(":*.jar", classpath)
def get_lambda_code_param(params, **kwargs): code = params.get('Code', {}) zip_file = code.get('ZipFile') if zip_file and not common.is_base64(zip_file): tmp_dir = common.new_tmp_dir() handler_file = get_handler_file_from_name(params['Handler'], runtime=params['Runtime']) tmp_file = os.path.join(tmp_dir, handler_file) common.save_file(tmp_file, zip_file) zip_file = create_zip_file(tmp_file, get_content=True) code['ZipFile'] = zip_file common.rm_rf(tmp_dir) return code
def test_is_dir_empty(self): tmp_dir = new_tmp_dir() assert is_empty_dir(tmp_dir) def _check(fname, is_dir): test_entry = os.path.join(tmp_dir, fname) mkdir(test_entry) if is_dir else save_file(test_entry, "test content") assert not is_empty_dir(tmp_dir) assert is_empty_dir(tmp_dir, ignore_hidden=True) == (fname == ".hidden") rm_rf(test_entry) assert is_empty_dir(tmp_dir) for name in ["regular", ".hidden"]: for is_dir in [True, False]: _check(name, is_dir)
def test_unzip_bad_crc(self): """Test unzipping of files with incorrect CRC codes - usually works with native `unzip` command, but seems to fail with zipfile module under certain Python versions (extracts 0-bytes files)""" # base64-encoded zip file with a single entry with incorrect CRC (created by Node.js 18 / Serverless) zip_base64 = """ UEsDBBQAAAAIAAAAIQAAAAAAJwAAAAAAAAAjAAAAbm9kZWpzL25vZGVfbW9kdWxlcy9sb2Rhc2gvaW5k ZXguanPLzU8pzUnVS60oyC8qKVawVShKLSzNLErVUNfTz8lPSSzOUNe0BgBQSwECLQMUAAAACAAAACEA AAAAACcAAAAAAAAAIwAAAAAAAAAAACAApIEAAAAAbm9kZWpzL25vZGVfbW9kdWxlcy9sb2Rhc2gvaW5k ZXguanNQSwUGAAAAAAEAAQBRAAAAaAAAAAAA """ tmp_dir = new_tmp_dir() zip_file = os.path.join(tmp_dir, "test.zip") save_file(zip_file, base64.b64decode(zip_base64)) unzip(zip_file, tmp_dir) content = load_file( os.path.join(tmp_dir, "nodejs", "node_modules", "lodash", "index.js")) assert content.strip() == "module.exports = require('./lodash');" rm_rf(tmp_dir)
def test_get_java_lib_folder_classpath(self): jar_file = os.path.join(new_tmp_dir(), 'foo.jar') save_file(jar_file, '') classpath = lambda_executors.Util.get_java_classpath(jar_file) self.assertIn('.:foo.jar', classpath) self.assertIn('*.jar', classpath)
def test_get_java_lib_folder_classpath(self): jar_file = os.path.join(new_tmp_dir(), "foo.jar") save_file(jar_file, "") classpath = lambda_executors.Util.get_java_classpath(jar_file) self.assertIn(".:foo.jar", classpath) self.assertIn("*.jar", classpath)