##// END OF EJS Templates
deps: bumped certifi==2024.7.4 for security fix
deps: bumped certifi==2024.7.4 for security fix

File last commit:

r5608:6d33e504 default
r5629:9363094c default
Show More
test_archives.py
194 lines | 7.0 KiB | text/x-python | PythonLexer
core: updated copyright to 2024
r5608 # Copyright (C) 2010-2024 RhodeCode GmbH
project: added all source files and assets
r1 #
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
import datetime
import os
import shutil
import tarfile
import zipfile
python3: fixed various code issues...
r4973 import io
project: added all source files and assets
r1
import mock
import pytest
tests: fixed all tests for python3 BIG changes
r5087 import rhodecode
feat(archive-cache): implemented s3 based backend for filecaches
r5433 from rhodecode.lib.archive_cache import get_archival_config
tests: fixed all tests for python3 BIG changes
r5087 from rhodecode.lib.str_utils import ascii_bytes
project: added all source files and assets
r1 from rhodecode.lib.vcs.backends import base
from rhodecode.lib.vcs.exceptions import ImproperArchiveTypeError, VCSError
from rhodecode.lib.vcs.nodes import FileNode
tests: use gunicorn for testing. This is close to production testing...
r2453 from rhodecode.tests.vcs.conftest import BackendTestMixin
project: added all source files and assets
r1
tests: fixed all tests for python3 BIG changes
r5087 @pytest.fixture()
def d_cache_config():
return get_archival_config(config=rhodecode.CONFIG)
tests: use gunicorn for testing. This is close to production testing...
r2453 @pytest.mark.usefixtures("vcs_repository_support")
project: added all source files and assets
r1 class TestArchives(BackendTestMixin):
@classmethod
def _get_commits(cls):
start_date = datetime.datetime(2010, 1, 1, 20)
archives: optimize performance of repo archive option by delegating all logic to vcsserver....
r4536 yield {
tests: fixed test suite for celery adoption
r5607 "message": "Initial Commit",
"author": "Joe Doe <joe.doe@example.com>",
"date": start_date + datetime.timedelta(hours=12),
"added": [
FileNode(b"executable_0o100755", b"mode_755", mode=0o100755),
FileNode(b"executable_0o100500", b"mode_500", mode=0o100500),
FileNode(b"not_executable", b"mode_644", mode=0o100644),
archives: optimize performance of repo archive option by delegating all logic to vcsserver....
r4536 ],
}
tests: use gunicorn for testing. This is close to production testing...
r2453 for x in range(5):
project: added all source files and assets
r1 yield {
tests: fixed test suite for celery adoption
r5607 "message": "Commit %d" % x,
"author": "Joe Doe <joe.doe@example.com>",
"date": start_date + datetime.timedelta(hours=12 * x),
"added": [
FileNode(b"%d/file_%d.txt" % (x, x), content=b"Foobar %d" % x),
project: added all source files and assets
r1 ],
}
tests: fixed test suite for celery adoption
r5607 @pytest.mark.parametrize("compressor", ["gz", "bz2"])
tests: fixed all tests for python3 BIG changes
r5087 def test_archive_tar(self, compressor, tmpdir, tmp_path, d_cache_config):
tests: fixed test suite for celery adoption
r5607 archive_node = tmp_path / "archive-node"
tests: fixed all tests for python3 BIG changes
r5087 archive_node.touch()
archive_lnk = self.tip.archive_repo(
tests: fixed test suite for celery adoption
r5607 str(archive_node), kind=f"t{compressor}", archive_dir_name="repo", cache_config=d_cache_config
)
tests: fixed all tests for python3 BIG changes
r5087
out_dir = tmpdir
tests: fixed test suite for celery adoption
r5607 out_file = tarfile.open(str(archive_lnk), f"r|{compressor}")
project: added all source files and assets
r1 out_file.extractall(out_dir)
out_file.close()
tests: use gunicorn for testing. This is close to production testing...
r2453 for x in range(5):
tests: fixed test suite for celery adoption
r5607 node_path = "%d/file_%d.txt" % (x, x)
with open(os.path.join(out_dir, "repo/" + node_path), "rb") as f:
project: added all source files and assets
r1 file_content = f.read()
assert file_content == self.tip.get_node(node_path).content
shutil.rmtree(out_dir)
tests: fixed test suite for celery adoption
r5607 @pytest.mark.parametrize("compressor", ["gz", "bz2"])
archives: optimize performance of repo archive option by delegating all logic to vcsserver....
r4536 def test_archive_tar_symlink(self, compressor):
tests: fixed test suite for celery adoption
r5607 pytest.skip("Not supported")
archives: optimize performance of repo archive option by delegating all logic to vcsserver....
r4536
tests: fixed test suite for celery adoption
r5607 @pytest.mark.parametrize("compressor", ["gz", "bz2"])
tests: fixed all tests for python3 BIG changes
r5087 def test_archive_tar_file_modes(self, compressor, tmpdir, tmp_path, d_cache_config):
tests: fixed test suite for celery adoption
r5607 archive_node = tmp_path / "archive-node"
tests: fixed all tests for python3 BIG changes
r5087 archive_node.touch()
archive_lnk = self.tip.archive_repo(
tests: fixed test suite for celery adoption
r5607 str(archive_node), kind="t{}".format(compressor), archive_dir_name="repo", cache_config=d_cache_config
)
tests: fixed all tests for python3 BIG changes
r5087
out_dir = tmpdir
tests: fixed test suite for celery adoption
r5607 out_file = tarfile.open(str(archive_lnk), "r|{}".format(compressor))
archives: optimize performance of repo archive option by delegating all logic to vcsserver....
r4536 out_file.extractall(out_dir)
out_file.close()
tests: fixed all tests for python3 BIG changes
r5087 def dest(inp):
return os.path.join(out_dir, "repo/" + inp)
tests: fixed test suite for celery adoption
r5607 assert oct(os.stat(dest("not_executable")).st_mode) == "0o100644"
archives: optimize performance of repo archive option by delegating all logic to vcsserver....
r4536
tests: fixed all tests for python3 BIG changes
r5087 def test_archive_zip(self, tmp_path, d_cache_config):
tests: fixed test suite for celery adoption
r5607 archive_node = tmp_path / "archive-node"
tests: fixed all tests for python3 BIG changes
r5087 archive_node.touch()
project: added all source files and assets
r1
archival: added tests at subpath generation of archivals
r5112 archive_lnk = self.tip.archive_repo(
tests: fixed test suite for celery adoption
r5607 str(archive_node), kind="zip", archive_dir_name="repo", cache_config=d_cache_config
)
zip_file = zipfile.ZipFile(str(archive_lnk))
for x in range(5):
node_path = "%d/file_%d.txt" % (x, x)
data = zip_file.read(f"repo/{node_path}")
decompressed = io.BytesIO()
decompressed.write(data)
assert decompressed.getvalue() == self.tip.get_node(node_path).content
decompressed.close()
def test_archive_zip_with_metadata(self, tmp_path, d_cache_config):
archive_node = tmp_path / "archive-node"
archive_node.touch()
archive_lnk = self.tip.archive_repo(
str(archive_node), kind="zip", archive_dir_name="repo", write_metadata=True, cache_config=d_cache_config
)
project: added all source files and assets
r1
tests: fixed all tests for python3 BIG changes
r5087 zip_file = zipfile.ZipFile(str(archive_lnk))
tests: fixed test suite for celery adoption
r5607 metafile = zip_file.read("repo/.archival.txt")
tests: fixed all tests for python3 BIG changes
r5087
raw_id = ascii_bytes(self.tip.raw_id)
tests: fixed test suite for celery adoption
r5607 assert b"commit_id:%b" % raw_id in metafile
project: added all source files and assets
r1
tests: use gunicorn for testing. This is close to production testing...
r2453 for x in range(5):
tests: fixed test suite for celery adoption
r5607 node_path = "%d/file_%d.txt" % (x, x)
data = zip_file.read(f"repo/{node_path}")
tests: fixed all tests for python3 BIG changes
r5087 decompressed = io.BytesIO()
decompressed.write(data)
tests: fixed test suite for celery adoption
r5607 assert decompressed.getvalue() == self.tip.get_node(node_path).content
project: added all source files and assets
r1 decompressed.close()
tests: fixed all tests for python3 BIG changes
r5087 def test_archive_wrong_kind(self, tmp_path, d_cache_config):
tests: fixed test suite for celery adoption
r5607 archive_node = tmp_path / "archive-node"
tests: fixed all tests for python3 BIG changes
r5087 archive_node.touch()
project: added all source files and assets
r1 with pytest.raises(ImproperArchiveTypeError):
tests: fixed test suite for celery adoption
r5607 self.tip.archive_repo(str(archive_node), kind="wrong kind", cache_config=d_cache_config)
project: added all source files and assets
r1
pytest: use consistent way of creating a fixture by using pytest.fixture()
r3946 @pytest.fixture()
project: added all source files and assets
r1 def base_commit():
"""
Prepare a `base.BaseCommit` just enough for `_validate_archive_prefix`.
"""
commit = base.BaseCommit()
commit.repository = mock.Mock()
tests: fixed test suite for celery adoption
r5607 commit.repository.name = "fake_repo"
commit.short_id = "fake_id"
project: added all source files and assets
r1 return commit
tests: fixed all tests for python3 BIG changes
r5087 def test_validate_archive_prefix_enforces_non_ascii_as_prefix(base_commit):
with pytest.raises(VCSError):
base_commit._validate_archive_prefix("Ünïcödë")
project: added all source files and assets
r1
def test_validate_archive_prefix_empty_prefix(base_commit):
# TODO: johbo: Should raise a ValueError here.
with pytest.raises(VCSError):
tests: fixed test suite for celery adoption
r5607 base_commit._validate_archive_prefix("")
project: added all source files and assets
r1
def test_validate_archive_prefix_with_leading_slash(base_commit):
# TODO: johbo: Should raise a ValueError here.
with pytest.raises(VCSError):
tests: fixed test suite for celery adoption
r5607 base_commit._validate_archive_prefix("/any")
project: added all source files and assets
r1
def test_validate_archive_prefix_falls_back_to_repository_name(base_commit):
prefix = base_commit._validate_archive_prefix(None)
tests: fixed test suite for celery adoption
r5607 expected_prefix = base_commit._ARCHIVE_PREFIX_TEMPLATE.format(repo_name="fake_repo", short_id="fake_id")
project: added all source files and assets
r1 assert isinstance(prefix, str)
assert prefix == expected_prefix