def test_package_clean_workspace(): temp_workspace = path.join(TESTING_TEMP_DIR, package.TEMP_WORKSPACE_NAME) os.mkdir(temp_workspace) pkg = package.Package(TESTING_TEMP_DIR) pkg.clean_workspace() assert path.isdir(temp_workspace) is False
def test_omit_virtualenv(): pkg = package.Package(TESTING_TEMP_DIR, False) pkg.prepare_virtualenv() assert pkg._pkg_venv is False with pytest.raises(Exception): pkg.build_new_virtualenv()
def test_default_virtualenv(): temp_workspace = path.join(TESTING_TEMP_DIR, package.TEMP_WORKSPACE_NAME) reqs = ['pytest'] pkg = package.Package(TESTING_TEMP_DIR) pkg.requirements = reqs pkg._build_new_virtualenv() # ensure we picked a real venv path if using default behavior assert pkg._pkg_venv == ("%s/venv" % temp_workspace)
def test_package_with_ignores(): pkg = package.Package(TESTING_TEMP_DIR) pkg.extra_file(path.join('tests', 'extra')) pkg.package(ignore=[DOTFILE_REGEX]) # test ignored file is *not* there dotfile = path.join(PACKAGE_TEMP_DIR, 'extra', '.dotfile') assert not path.exists(dotfile)
def test_existing_virtualenv(): venv_dir = "virtualenv_test" temp_virtualenv = path.join(TESTING_TEMP_DIR, venv_dir) os.mkdir(temp_virtualenv) pkg = package.Package(TESTING_TEMP_DIR, temp_virtualenv) pkg.prepare_virtualenv() assert pkg._pkg_venv == temp_virtualenv
def build_package(lambda_zip_filename): """Given this project, package it using lambda_uploader""" pkg = lu_package.Package('.', lambda_zip_filename) pkg.clean_zipfile() # lambda-uploader step to build zip file pkg.extra_file('ebs_snapper/lambdas.py') pkg.requirements('requirements.txt') pkg.build(IGNORED_UPLOADER_FILES) pkg.clean_workspace()
def test_existing_virtualenv(): venv_dir = "virtualenv_test" temp_virtualenv = path.join(TESTING_TEMP_DIR, venv_dir) os.mkdir(temp_virtualenv) pkg = package.Package(TESTING_TEMP_DIR, temp_virtualenv) pkg.virtualenv(temp_virtualenv) pkg.install_dependencies() assert pkg._pkg_venv == temp_virtualenv
def test_prepare_workspace(): temp_workspace = path.join(TESTING_TEMP_DIR, package.TEMP_WORKSPACE_NAME) pkg = package.Package(TESTING_TEMP_DIR) pkg.requirements(['pytest']) pkg.install_dependencies() assert path.isdir(temp_workspace) assert path.isdir(path.join(temp_workspace, 'venv')) if sys.platform == 'win32' or sys.platform == 'cygwin': assert path.isfile(path.join(temp_workspace, "venv\\Scripts\\pip.exe")) else: assert path.isfile(path.join(temp_workspace, 'venv/bin/pip'))
def test_prepare_workspace(): temp_workspace = path.join(TESTING_TEMP_DIR, package.TEMP_WORKSPACE_NAME) pkg = package.Package(TESTING_TEMP_DIR) pkg.prepare_workspace() pkg.prepare_virtualenv() assert path.isdir(temp_workspace) assert path.isdir(path.join(temp_workspace, 'venv')) if sys.platform == 'win32' or sys.platform == 'cygwin': assert path.isfile(path.join(temp_workspace, "venv\\Scripts\\pip.exe")) else: assert path.isfile(path.join(temp_workspace, 'venv/bin/pip'))
def test_install_requirements(): reqs = ['pytest'] temp_workspace = path.join(TESTING_TEMP_DIR, package.TEMP_WORKSPACE_NAME) pkg = package.Package(TESTING_TEMP_DIR, requirements=reqs) pkg.prepare_virtualenv() site_packages = path.join(temp_workspace, 'venv/lib/python2.7/site-packages') if sys.platform == 'win32' or sys.platform == 'cygwin': site_packages = path.join(temp_workspace, "venv\\lib\\site-packages") assert path.isdir(path.join(site_packages, '_pytest'))
def test_package_with_extras(): pkg = package.Package(TESTING_TEMP_DIR) pkg.extra_file(path.join('test', 'extra')) pkg.extra_file(path.join('test', 'dummyfile')) pkg.package() # test a single file expected_extra_file1 = path.join(PACKAGE_TEMP_DIR, 'dummyfile') assert path.isfile(expected_extra_file1) # test a recursive directory expected_extra_file2 = path.join(PACKAGE_TEMP_DIR, 'extra/foo/__init__.py') assert path.isfile(expected_extra_file2)
def test_install_requirements(): temp_workspace = path.join(TESTING_TEMP_DIR, package.TEMP_WORKSPACE_NAME) pkg = package.Package(TESTING_TEMP_DIR) pkg.requirements(['pytest']) pkg.install_dependencies() site_packages = path.join(temp_workspace, 'venv/lib/python2.7/site-packages') if sys.platform == 'win32' or sys.platform == 'cygwin': site_packages = path.join(temp_workspace, "venv\\lib\\site-packages") assert path.isdir(path.join(site_packages, '_pytest'))
def test_ignores_using_all_items_and_regex(): pkg = package.Package(TESTING_TEMP_DIR) pyc_path = path.join(TESTING_TEMP_DIR, 'fake.pyc') py_path = path.join(TESTING_TEMP_DIR, 'real.py') open(py_path, 'w').close() open(pyc_path, 'w').close() pkg.package([r"dummy.*", r'[a-z]+\.pyc']) os.remove(py_path) os.remove(pyc_path) # test the ignores has excluded the .pyc expected_extra_file = path.join(PACKAGE_TEMP_DIR, 'fake.pyc') assert not path.exists(expected_extra_file) # ...but the path not affected by either ignore entry remains assert path.exists(path.join(PACKAGE_TEMP_DIR, 'real.py'))
def test_package(): pkg = package.Package(TESTING_TEMP_DIR) pkg.package() assert path.isfile(path.join(TESTING_TEMP_DIR, 'lambda_function.zip'))
def test_default_virtualenv(): temp_workspace = path.join(TESTING_TEMP_DIR, package.TEMP_WORKSPACE_NAME) pkg = package.Package(TESTING_TEMP_DIR) pkg.prepare_virtualenv() # ensure we picked a real venv path if using default behavior assert pkg._pkg_venv == ("%s/venv" % temp_workspace)
def test_bad_existing_virtualenv(): pkg = package.Package(TESTING_TEMP_DIR) with pytest.raises(Exception): pkg.virtualenv('abc')
def test_package_zip_location(): pkg = package.Package(TESTING_TEMP_DIR) assert pkg.zip_file == '.testing_temp/lambda_function.zip'
def test_package_name(): pkg = package.Package(TESTING_TEMP_DIR, zipfile_name='test.zip') pkg.package() assert path.isfile(path.join(TESTING_TEMP_DIR, 'test.zip'))
def test_omit_virtualenv(): pkg = package.Package(TESTING_TEMP_DIR) pkg.virtualenv(False) pkg.install_dependencies() assert pkg._pkg_venv is False
def build_lambda_package(self, skip_if_exists=False): LOG.warning("Building Lambda package ...") pkg = package.Package(self.lambda_path) pkg._requirements_file = None if os.path.isfile(pkg.zip_file): if skip_if_exists: # Package already built, don't do anything else LOG.warning( "Lambda package already built, using existing file.") return pkg else: # Otherwise we should delete the existing file, otherwise it # will be packaged up. LOG.warning("Removing existing Lambda package.") os.remove(pkg.zip_file) dependency_config = self.config['Lambda'].get('dependencies') if dependency_config is not None: build_enabled = dependency_config.get('build', False) if build_enabled: LOG.warning("Building dependencies enabled ...") runtime = self.config['Lambda']['config'].get( 'runtime', 'python2.7') if not runtime.startswith('python'): raise Exception( "Building dependencies only supported on Python " "runtimes.") service_name = self.config['Lambda']['config']['name'] wheelhouse_path = dependency_config.get('wheelhouse') if wheelhouse_path is not None: wheelhouse_path = os.path.abspath( os.path.join( wheelhouse_path, service_name, ), ) else: wheelhouse_path = os.path.abspath( os.path.join( self.project_dir, '../../wheelhouse', service_name, ), ) install_dir = dependency_config.get('install_dir') or './lib' builder = PythonDependencyBuilder( runtime=runtime, project_path=self.project_dir, wheelhouse_path=wheelhouse_path, lambda_path=self.lambda_path, install_dir=install_dir, service_name=service_name, extra_packages=dependency_config.get('packages'), ) builder.build() if self.extra_files: for _file in self.extra_files: pkg.extra_file(_file) pkg._prepare_workspace() pkg.package() pkg.clean_workspace() return pkg