def test_when_config_given_then_do_not_read_from_disk( self, get_files_in_directory_mock: Mock, build_requirements_mock: Mock, read_gitignore_mock: Mock, create_from_config_file_mock: Mock, zip_package_mock: Mock, find_paths_mock: Mock, rmtree_mock: Mock, ): create_from_config_file_mock.return_value = Configuration() find_paths_mock.return_value = ([Path("mypaths")], "") package(configuration=Configuration()) create_from_config_file_mock.assert_not_called()
def test_when_requirements_given_and_use_docker_false_then_local_pip_is_called( self, from_env_mock: Mock, generate_temp_task_dir_mock: Mock, copy_mock: Mock, unlink_mock, mkdir_mock, subprocess_run_mock, ): generate_temp_task_dir_mock.return_value = "my_temp_dir" expected_temp_dir = Path(TempDir).joinpath("my_temp_dir").absolute() build_requirements( Configuration(requirements="my_requirements", use_docker=False, python_version="5.6")) subprocess_run_mock.assert_called_once_with([ "pip5.6", "install", "-t", str(expected_temp_dir), "-r", "my_requirements", "--cache-dir", f"{TempDir}/{CacheDirName}/local_5.6", ])
def test_when_requirements_given_and_use_docker_true_then_docker_is_called( self, from_env_mock: Mock, generate_temp_task_dir_mock: Mock, copy_mock: Mock, unlink_mock, mkdir_mock, subprocess_run_mock, ): generate_temp_task_dir_mock.return_value = "my_temp_dir" run_mock = Mock() from_env_mock.return_value = Mock() from_env_mock.return_value.containers.run = run_mock expected_temp_dir = Path(TempDir).joinpath("my_temp_dir").absolute() build_requirements( Configuration(requirements="my_requirements", use_docker=True, python_version="5.6")) run_mock.assert_called_once_with( "lambci/lambda:build-python5.6", f"pip install -t /var/task/ -r /var/task/my_requirements --cache-dir {TempDir}/{CacheDirName}/docker_5.6", volumes={ str(expected_temp_dir): { "bind": "/var/task", "mode": "z" } }, )
def validate_configuration(configuration: Configuration) -> Configuration: """ Validates the configuration. If configuration is `None`, it will be read from disk. """ if not configuration: configuration = Configuration.create_from_config_file() if not configuration.exclude: configuration.exclude = read_gitignore() if configuration.layer_output and not configuration.requirements: raise ValueError( "Layer output parameter cannot be given without requirements parameter" ) return configuration
def test_when_no_output_specified_then_zip_package_not_called( self, get_files_in_directory_mock: Mock, build_requirements_mock: Mock, read_gitignore_mock: Mock, create_from_config_file_mock: Mock, zip_package_mock: Mock, find_paths_mock: Mock, rmtree_mock: Mock, ): find_paths_mock.return_value = ("", "") package(configuration=Configuration()) zip_package_mock.assert_not_called()
def test_when_output_specified_then_zip_package_called( self, get_files_in_directory_mock: Mock, build_requirements_mock: Mock, read_gitignore_mock: Mock, create_from_config_file_mock: Mock, zip_package_mock: Mock, find_paths_mock: Mock, rmtree_mock: Mock, ): find_paths_mock.return_value = ([Path("mypaths")], "") package(configuration=Configuration(output="myoutput")) zip_package_mock.assert_called_once_with(paths=[(Path("mypaths"), Path("mypaths"))], fp="myoutput")
def test_when_requirements_given_but_not_layer_or_output_then_build_requirements_not_called( self, get_files_in_directory_mock: Mock, build_requirements_mock: Mock, read_gitignore_mock: Mock, create_from_config_file_mock: Mock, zip_package_mock: Mock, find_paths_mock: Mock, rmtree_mock: Mock, ): read_gitignore_mock.return_value = [] find_paths_mock.return_value = ([Path("mypaths")], "") package(configuration=Configuration(requirements="my_requirements.txt", output=None, layer_output=None)) build_requirements_mock.assert_not_called()
def test_when_config_has_no_excludes_then_read_gitignore( self, get_files_in_directory_mock: Mock, build_requirements_mock: Mock, read_gitignore_mock: Mock, create_from_config_file_mock: Mock, zip_package_mock: Mock, find_paths_mock: Mock, rmtree_mock: Mock, ): read_gitignore_mock.return_value = ["gitignoreex"] find_paths_mock.return_value = ([Path("mypaths")], "") package(configuration=Configuration()) read_gitignore_mock.assert_called_once() find_paths_mock.assert_called_once_with(root_path=ANY, excludes=["gitignoreex"])
def test_when_requirements_not_given_layer_output_given_then_raise_exception( self, get_files_in_directory_mock: Mock, build_requirements_mock: Mock, read_gitignore_mock: Mock, create_from_config_file_mock: Mock, zip_package_mock: Mock, find_paths_mock: Mock, rmtree_mock: Mock, ): read_gitignore_mock.return_value = [] find_paths_mock.return_value = ([Path("mypath1")], "") self.assertRaisesRegex( ValueError, "Layer output parameter cannot be given without requirements parameter", package, configuration=Configuration(requirements=None, layer_output="my_layer_output"), )
def test_when_requirements_given_then_call_build_requirements( self, get_files_in_directory_mock: Mock, build_requirements_mock: Mock, read_gitignore_mock: Mock, create_from_config_file_mock: Mock, zip_package_mock: Mock, find_paths_mock: Mock, rmtree_mock: Mock, ): read_gitignore_mock.return_value = [] find_paths_mock.return_value = ([Path("mypath")], "") build_requirements_mock.return_value = Path("my_temp_dir") get_files_in_directory_mock.return_value = [ Path("my_temp_dir/myreqfile") ] package(configuration=Configuration(requirements="my_requirements", output="my_output")) build_requirements_mock.assert_called_once()
def test_when_build_requirements_is_called_then_returns_requirements_directory( self, from_env_mock: Mock, generate_temp_task_dir_mock: Mock, copy_mock: Mock, unlink_mock, mkdir_mock, subprocess_run_mock, ): generate_temp_task_dir_mock.return_value = "my_temp_dir" run_mock = Mock() from_env_mock.return_value = Mock() from_env_mock.return_value.containers.run = run_mock expected_temp_dir = Path(TempDir).joinpath("my_temp_dir").absolute() res = build_requirements( Configuration(requirements="my_requirements", use_docker=True)) self.assertEqual(res, expected_temp_dir)
def test_when_requirements_given_and_layer_output_given_then_seperate_zip_created( self, get_files_in_directory_mock: Mock, build_requirements_mock: Mock, read_gitignore_mock: Mock, create_from_config_file_mock: Mock, zip_package_mock: Mock, find_paths_mock: Mock, rmtree_mock: Mock, ): read_gitignore_mock.return_value = [] find_paths_mock.return_value = ([Path("mypath1")], "") build_requirements_mock.return_value = Path("my_temp_dir") get_files_in_directory_mock.return_value = [ Path("my_temp_dir/req_file_1"), Path("my_temp_dir/req_file_2"), Path("my_temp_dir/req_file_3"), ] package(configuration=Configuration( requirements="my_requirements", layer_output="layer_out", output="my_output", )) get_files_in_directory_mock.assert_called_once_with( Path("my_temp_dir")) zip_package_mock.assert_any_call( paths=[ (Path("my_temp_dir/req_file_1"), Path("req_file_1")), (Path("my_temp_dir/req_file_2"), Path("req_file_2")), (Path("my_temp_dir/req_file_3"), Path("req_file_3")), ], fp="layer_out", ) zip_package_mock.assert_any_call( paths=[(Path("mypath1"), Path("mypath1"))], fp="my_output", )
def main(): """ Main entry point of the Command """ parser = argparse.ArgumentParser("lambda_package") add_arguments(parser) args = parser.parse_args() configuration = Configuration.create_from_config_file() configuration.output = args.output if args.output else configuration.output (_, tree) = package(root_path=args.path, configuration=configuration) if not configuration.output and not configuration.layer_output: print_tree(tree) else: if configuration.output: print(f"Successfully created package {configuration.output}") if configuration.requirements and configuration.layer_output: print( f"Successfully created layer package {configuration.layer_output}" )