def test_create_dir_structure(): project_test_name = 'lambda_test_dir_structure' create_dir_structure(project_test_name) dir_level_one_returned = os.listdir(os.path.join('.', project_test_name)) dir_level_one_expected = [project_test_name] dir_level_two_returned = os.listdir( os.path.join('.', project_test_name, project_test_name)) dir_level_two_expected = ['src', 'tests'] dir_level_two_returned.sort() dir_level_two_expected.sort() dir_level_three_returned = os.listdir( os.path.join('.', project_test_name, project_test_name, 'tests')) dir_level_three_expected = ['integration', 'unit', 'utils'] dir_level_three_returned.sort() dir_level_three_expected.sort() dir_level_four_returned = os.listdir( os.path.join('.', project_test_name, project_test_name, 'tests', 'utils')) dir_level_four_expected = ['files', 'mocks'] dir_level_four_returned.sort() dir_level_four_expected.sort() rmdir(os.path.join('.', project_test_name)) assert( dir_level_one_returned == dir_level_one_expected and \ dir_level_two_returned == dir_level_two_expected and \ dir_level_three_returned == dir_level_three_expected and \ dir_level_four_returned == dir_level_four_expected )
def test_create_main_files(): project_test_name = 'lambda_test_main_files' create_dir_structure(project_test_name) create_main_files(project_test_name, python_version) files_level_one_returned = [ f for f in os.listdir(os.path.join('.', project_test_name)) if '.' in f ] files_level_one_expected = [ 'requirements.txt', 'docker-compose.yml', 'pyproject.toml', 'README.md', '.gitignore' ] files_level_one_returned.sort() files_level_one_expected.sort() files_level_two_returned = [ f for f in os.listdir( os.path.join('.', project_test_name, project_test_name)) if '.' in f ] files_level_two_expected = ['__init__.py'] files_level_two_returned.sort() files_level_two_expected.sort() rmdir(os.path.join('.', project_test_name)) assert( files_level_one_returned == files_level_one_expected and \ files_level_two_returned == files_level_two_expected )
def update_local(service_name, function_name): project_dir = os.getcwd() path_code_from = os.path.join(function_name, 'src') path_code_to = os.path.join('.', 'src_tmp') os.mkdir(path_code_to) copy_folder(path_code_from, path_code_to) os.system('pip3 install -r requirements.txt -t '+path_code_to) os.chdir(path_code_to) zip_files('.', 'lambda_function') os.chdir(project_dir) config_client = { 'service_name': 'lambda', 'aws_access_key_id': '123', 'aws_secret_access_key': '123', 'region_name': 'us-east-1', 'endpoint_url': 'http://localhost:4566' } client = boto3.client(**config_client) function_name_in_localstack = service_name+'-dev-'+function_name with open(os.path.join(path_code_to, 'lambda_function.zip'), 'rb') as f: response = client.update_function_code( FunctionName=function_name_in_localstack, ZipFile=f.read(), ) rmdir(path_code_to) print('Stack update finished')
def create_layer(layer_name, python_version): if 'requirements.txt' in os.listdir('.'): os.system('pip3 install -r requirements.txt -t '+os.path.join('.','python','lib','python'+python_version,'site-packages')) zip_files(os.path.join('.', 'python'), layer_name) rmdir(os.path.join('.', 'python')) else: print('requirements.txt don\'t exist')
def deploy_local(function_name): path_code_from = os.path.join(function_name, 'src') path_code_to = os.path.join('.', 'src_tmp') os.mkdir(path_code_to) copy_folder(path_code_from, path_code_to) os.system('cd ' + path_code_to + ' && npm install --save-dev serverless-localstack') os.system('pip3 install -r requirements.txt -t ' + path_code_to) os.system('cd ' + path_code_to + ' && sls deploy') rmdir(path_code_to)
def test_create_layer_no_req(): lambda_name = 'lambda_test_layer_no_req' layer_test_name = 'numpy_no_req' create_lambda(lambda_name, python_version) create_layer(layer_test_name, python_version) files_list = os.listdir('.') rmdir(os.path.join('.', lambda_name)) assert (layer_test_name + '.zip' not in files_list)
def test_create_src_files(): project_test_name = 'lambda_test_src_files' create_dir_structure(project_test_name) create_src_files(project_test_name, python_version) files_returned = [ f for f in os.listdir( os.path.join('.', project_test_name, project_test_name, 'src')) if '.' in f ] files_expected = ['lambda_function.py', 'serverless.yml'] files_returned.sort() files_expected.sort() rmdir(os.path.join('.', project_test_name)) assert (files_returned == files_expected)
def test_create_layer_req(): lambda_name = 'lambda_test_layer_req' layer_test_name = 'numpy_req' create_lambda(lambda_name, python_version) with open(os.path.join('.', 'requirements.txt'), 'w') as requirements: requirements.write('numpy') create_layer(layer_test_name, python_version) files_list = os.listdir('.') os.remove(os.path.join('.', layer_test_name + '.zip')) os.remove(os.path.join('.', 'requirements.txt')) rmdir(os.path.join('.', lambda_name)) assert (layer_test_name + '.zip' in files_list)