def test_extract_file(mocker): # FIXME: rm out of tests tar file example # FIXME: generate a test tarfile tar_file_mock = mocker.MagicMock(spec_set=tarfile.TarFile) # TODO: replace with tmpdir fixture tmp_dir = tempfile.mkdtemp(prefix=TMP_PREFIX) files_to_extract = [] # for pathname in tar_example1: pathname = 'roles/test-role-d/handlers/main.yml' top_dir = 'ansible-content-archive' dest_dir = os.path.join(tmp_dir, 'extracted_stuff') member = tarfile.TarInfo(os.path.join(top_dir, pathname)) item = { 'archive_member': member, 'dest_dir': dest_dir, 'dest_filename': pathname, 'force_overwrite': True } log.debug('files_to_extract: %s', pprint.pformat(files_to_extract)) res = archive.extract_file(tar_file_mock, file_to_extract=item) log.debug('tar_file_mock: %s', tar_file_mock) log.debug('tar_file_mock.call_args_list: %s', tar_file_mock.call_args_list) log.debug('tar_file_mock.method_calls: %s', tar_file_mock.method_calls) log.debug('res: %s', res) assert pathname in res # log.debug('%s contents: %s', tmp_dir, glob.glob(dest_dir, '**', recursive=True)) shutil.rmtree(tmp_dir)
def test_extract_file_exists(mocker): tar_file_mock = mocker.MagicMock(spec_set=tarfile.TarFile) # TODO: replace with tmpdir fixture tmp_dir = tempfile.mkdtemp(prefix=TMP_PREFIX) files_to_extract = [] # for pathname in tar_example1: pathname = 'roles/test-role-d/handlers/main.yml' top_dir = 'ansible-content-archive' dest_dir = os.path.join(tmp_dir, 'extracted_stuff') def faux_exists(path): if path.startswith(dest_dir): return True return False mocker.patch('ansible_galaxy.archive.os.path.exists', side_effect=faux_exists) member = tarfile.TarInfo(os.path.join(top_dir, pathname)) item = { 'archive_member': member, 'dest_dir': dest_dir, 'dest_filename': pathname, 'force_overwrite': False } log.debug('files_to_extract: %s', pprint.pformat(files_to_extract)) try: archive.extract_file(tar_file_mock, file_to_extract=item) except exceptions.GalaxyClientError as e: log.exception(e) return finally: shutil.rmtree(tmp_dir) assert False, 'Expected a GalaxyClientError because the file to extract already exists, but that did not happen'
def test_extract_file_foo(): tmp_tar_fo = tempfile.NamedTemporaryFile(delete=False, prefix='dddddddddddddd') tmp_member_fo = tempfile.NamedTemporaryFile(delete=False, prefix='cccccccccc') # TODO: replace with tmpdir fixture tmp_dir = tempfile.mkdtemp(prefix='fffffcccc') tar_file = tarfile.TarFile.open(mode='w', fileobj=tmp_tar_fo) # fileobj=tmp_tar) files_to_extract = [] # for pathname in tar_example1: pathname = 'foo_blip.yml' member = tarfile.TarInfo(pathname) tar_file.addfile(member, tmp_member_fo) log.debug('tar_file2 members: %s', tar_file.getmembers()) tar_file.close() item = { 'archive_member': member, 'dest_dir': tmp_dir, 'dest_filename': pathname, 'force_overwrite': False } files_to_extract.append(item) log.debug('files_to_extract: %s', pprint.pformat(files_to_extract)) read_tar_file = tarfile.TarFile.open(name=tmp_tar_fo.name, mode='r') res = archive.extract_file(read_tar_file, file_to_extract=item) log.debug('res: %s', res) log.debug('listdir: %s', os.listdir(tmp_dir)) assert pathname in os.listdir(tmp_dir) os.unlink(tmp_tar_fo.name) read_tar_file.close() tmp_tar_fo.close() tmp_member_fo.close() os.unlink(tmp_member_fo.name) shutil.rmtree(tmp_dir)
def test_extract_file_empty(): tmp_tar = tempfile.TemporaryFile(prefix=TMP_PREFIX) # TODO: replace with tmpdir fixture tmp_dir = tempfile.mkdtemp(prefix=TMP_PREFIX) tar_file = tarfile.TarFile( # name='some-top-level', mode='w', fileobj=tmp_tar) log.debug('tar_file.members: %s', tar_file.members) tar_file.close() # for pathname in tar_example1: pathname = 'this_should_not_be_in_empty_tar.yml' file_to_extract = { 'archive_member': tarfile.TarInfo(name='foo'), 'dest_dir': tmp_dir, 'dest_filename': pathname, 'force_overwrite': True } log.debug('files_to_extract: %s', pprint.pformat(file_to_extract)) read_tar_file = tarfile.TarFile.open(fileobj=tmp_tar, mode='r') try: res = archive.extract_file(read_tar_file, file_to_extract=file_to_extract) log.debug('res: %s', res) log.debug('listdir(%s): %s', tmp_dir, os.listdir(tmp_dir)) except exceptions.GalaxyArchiveError as e: log.exception(e) return except tarfile.ReadError as e: log.exception(e) return finally: tmp_tar.close() read_tar_file.close() shutil.rmtree(tmp_dir)