def test_extract_invalid_install_dir(test_package): """ Test extract_package with invalid install directory :param test_package: fixture that creates the fake agent directory and returns the directory name, distribution name, version of the fake/test package, and package name """ install_dir = "" try: tmpdir, distribution_name, version, package = test_package wheel_name = '-'.join( [distribution_name, version, 'py2-none-any.whl']) wheel_file = os.path.join(tmpdir, 'dist', wheel_name) install_dir = tempfile.mkdtemp() os.chmod(install_dir, stat.S_IREAD) extract_package(wheel_file, install_dir, include_uuid=True, specific_uuid="123456789") except Exception as e: print e assert str(e).find("Permission denied") != -1 finally: if install_dir: shutil.rmtree(install_dir)
def test_extract_invalid_install_dir(test_package): """ Test extract_package with invalid install directory :param test_package: fixture that creates the fake agent directory and returns the directory name, distribution name, version of the fake/test package, and package name """ install_dir = "" try: tmpdir, distribution_name, version, package = test_package wheel_name = '-'.join([distribution_name, version, 'py2-none-any.whl']) wheel_file = os.path.join(tmpdir, 'dist', wheel_name) install_dir = tempfile.mkdtemp() os.chmod(install_dir, stat.S_IREAD) extract_package(wheel_file, install_dir, include_uuid=True, specific_uuid="123456789") except Exception as e: print e assert str(e).find("Permission denied") != -1 finally: if install_dir: shutil.rmtree(install_dir)
def test_can_extract_package(self): wheelhouse = os.path.join(self.tmpdir, 'extract_package') expected_install_at = os.path.join(wheelhouse, 'listeneragent-0.1') test_wheel_name = 'listeneragent-0.1-py2-none-any.whl' wheel_file = os.path.join(self.fixtureDir, test_wheel_name) installed_at = extract_package(wheel_file, wheelhouse) try: self.assertIsNotNone(installed_at) self.assertTrue(os.path.isdir(installed_at)) self.assertEqual(expected_install_at, installed_at) # use the wheel file to verify that everything was extracted # properly. wf = WheelFile(wheel_file) self.assertIsNone(wf.verify()) for o in wf.zipfile.infolist(): self.assertTrue( os.path.exists(os.path.join(expected_install_at, o.filename))) wf.zipfile.close() finally: shutil.rmtree(installed_at) shutil.rmtree(wheelhouse)
def test_create_package_with_id(test_package): """ Test if we can create a wheel file given a agent directory and vip id Expected result: 1. wheel file with the name <distribution_name>-<version>-py2-none-any.whl 2. Wheel file should contains the identity passed in a file called 'IDENTITY_TEMPLATE' in <distribution_name>-<version>.dist-info folder :param test_package: fixture that creates the fake agent directory and returns the directory name, distribution name, version of the fake/test package, and package name """ tmpdir, distribution_name, version, package_name = test_package wheel_dir = os.path.join(tmpdir, "wheel_dir") result = create_package(tmpdir, wheel_dir, "test_vip_id") assert result == os.path.join( wheel_dir, '-'.join([distribution_name, version, 'py2-none-any.whl'])) extract_dir = tempfile.mkdtemp() result2 = extract_package(result, extract_dir) dist_info_dir = os.path.join( result2, distribution_name + "-" + version + ".dist-info") files = os.listdir(dist_info_dir) assert 'IDENTITY_TEMPLATE' in files with open(os.path.join(dist_info_dir, 'IDENTITY_TEMPLATE'), 'r') as f: data = f.read().replace('\n', '') assert data == "test_vip_id"
def test_can_extract_package(self): wheelhouse = os.path.join(self.tmpdir, 'extract_package') expected_install_at = os.path.join(wheelhouse, 'listeneragent-0.1') test_wheel_name = 'listeneragent-0.1-py2-none-any.whl' wheel_file = os.path.join(self.fixtureDir, test_wheel_name) installed_at = extract_package(wheel_file, wheelhouse) try: self.assertIsNotNone(installed_at) self.assertTrue(os.path.isdir(installed_at)) self.assertEqual(expected_install_at, installed_at) # use the wheel file to verify that everything was extracted # properly. wf = WheelFile(wheel_file) self.assertIsNone(wf.verify()) for o in wf.zipfile.infolist(): self.assertTrue( os.path.exists( os.path.join(expected_install_at, o.filename))) wf.zipfile.close() finally: shutil.rmtree(installed_at) shutil.rmtree(wheelhouse)
def test_create_package_with_id(test_package): """ Test if we can create a wheel file given a agent directory and vip id Expected result: 1. wheel file with the name <distribution_name>-<version>-py2-none-any.whl 2. Wheel file should contains the identity passed in a file called 'IDENTITY_TEMPLATE' in <distribution_name>-<version>.dist-info folder :param test_package: fixture that creates the fake agent directory and returns the directory name, distribution name, version of the fake/test package, and package name """ tmpdir, distribution_name, version, package_name = test_package wheel_dir = os.path.join(tmpdir, "wheel_dir") result = create_package(tmpdir, wheel_dir, "test_vip_id") assert result == os.path.join(wheel_dir, '-'.join( [distribution_name, version, 'py2-none-any.whl'])) extract_dir = tempfile.mkdtemp() result2 = extract_package(result, extract_dir) dist_info_dir = os.path.join(result2, distribution_name + "-" + version + ".dist-info") files = os.listdir(dist_info_dir) assert 'IDENTITY_TEMPLATE' in files with open(os.path.join(dist_info_dir, 'IDENTITY_TEMPLATE'), 'r') as f: data = f.read().replace('\n', '') assert data == "test_vip_id"
def test_extract_invalid_wheel(): """ Test extract_package with invalid wheel file name. """ install_dir = "" f = None try: f = tempfile.NamedTemporaryFile(suffix=".whl") install_dir = tempfile.mkdtemp() extract_package(f.name, install_dir, include_uuid=True, specific_uuid="123456789") except Exception as e: assert e.message == "Bad filename '{}'".format(f.name) finally: if install_dir: shutil.rmtree(install_dir)
def test_extract_invalid_wheel(): """ Test extract_package with invalid wheel file name. """ install_dir = "" f = None try: f = tempfile.NamedTemporaryFile(suffix=".whl") install_dir = tempfile.mkdtemp() extract_package(f.name, install_dir, include_uuid=True, specific_uuid="123456789") except Exception as e: assert e.message == "Bad filename '{}'".format(f.name) finally: if install_dir: shutil.rmtree(install_dir)
def test_extract_new_install_dir(test_package): """ Test extract_package with invalid install directory :param test_package: fixture that creates the fake agent directory and returns the directory name, distribution name, version of the fake/test package, and package name """ install_dir = "" try: tmpdir, distribution_name, version, package = test_package wheel_name = '-'.join([distribution_name, version, 'py2-none-any.whl']) wheel_file = os.path.join(tmpdir, 'dist', wheel_name) install_dir = tempfile.mkdtemp() install_dir = os.path.join(install_dir, 'newdir') destination = extract_package(wheel_file, install_dir, include_uuid=True, specific_uuid="123456789") print("destination {}".format(destination)) name_version = distribution_name + "-" + version assert os.path.basename(destination) == name_version assert os.path.dirname(destination) == os.path.join( install_dir, "123456789") assert Counter(os.listdir(destination)) == Counter( [name_version + '.dist-info', package]) dist = os.path.join(destination, name_version + '.dist-info') assert Counter(os.listdir(dist)) == Counter([ 'DESCRIPTION.rst', 'METADATA', 'metadata.json', 'RECORD', 'top_level.txt', 'WHEEL' ]) finally: if install_dir: shutil.rmtree(install_dir)
def test_extract_include_uuid(test_package): """ Test if we can extract a wheel file, a specific install directory. Specify include_uuid as True and verify that the extraction happens within given install_dir/uuid directory :param test_package: fixture that creates the fake agent directory and returns the directory name, distribution name, version of the fake/test package, and package name """ install_dir = "" try: tmpdir, distribution_name, version, package = test_package wheel_name = '-'.join([distribution_name, version, 'py3-none-any.whl']) wheel_file = os.path.join(tmpdir, 'dist', wheel_name) install_dir = tempfile.mkdtemp() destination = extract_package(wheel_file, install_dir, include_uuid=True, specific_uuid=None) print("destination {}".format(destination)) name_version = distribution_name + "-" + version assert os.path.basename(destination) == name_version assert os.path.dirname(os.path.dirname(destination)) == install_dir assert Counter(os.listdir(destination)) == Counter( [name_version + '.dist-info', package]) dist = os.path.join(destination, name_version + '.dist-info') assert Counter(os.listdir(dist)) == Counter([ 'DESCRIPTION.rst', 'METADATA', 'metadata.json', 'RECORD', 'top_level.txt', 'WHEEL' ]) finally: if install_dir: shutil.rmtree(install_dir)
def test_extract_new_install_dir(test_package): """ Test extract_package with invalid install directory :param test_package: fixture that creates the fake agent directory and returns the directory name, distribution name, version of the fake/test package, and package name """ install_dir = "" try: tmpdir, distribution_name, version, package = test_package wheel_name = '-'.join( [distribution_name, version, 'py2-none-any.whl']) wheel_file = os.path.join(tmpdir, 'dist', wheel_name) install_dir = tempfile.mkdtemp() install_dir = os.path.join(install_dir, 'newdir') destination = extract_package(wheel_file, install_dir, include_uuid=True, specific_uuid="123456789") print ("destination {}".format(destination)) name_version = distribution_name + "-" + version assert os.path.basename(destination) == name_version assert os.path.dirname(destination) == os.path.join(install_dir, "123456789") assert Counter(os.listdir(destination)) == Counter( [name_version + '.dist-info', package]) dist = os.path.join(destination, name_version + '.dist-info') assert Counter(os.listdir(dist)) == Counter( ['DESCRIPTION.rst', 'METADATA', 'metadata.json', 'RECORD', 'top_level.txt', 'WHEEL']) finally: if install_dir: shutil.rmtree(install_dir)