Changeset View
Changeset View
Standalone View
Standalone View
swh/vault/tests/test_cookers.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import contextlib | import contextlib | ||||
import datetime | import datetime | ||||
import glob | |||||
import gzip | import gzip | ||||
import io | import io | ||||
import os | import os | ||||
import pathlib | import pathlib | ||||
import subprocess | import subprocess | ||||
import tarfile | import tarfile | ||||
import tempfile | import tempfile | ||||
import unittest | import unittest | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | class TestRepo: | ||||
def commit(self, message="Commit test\n", ref=b"HEAD"): | def commit(self, message="Commit test\n", ref=b"HEAD"): | ||||
"""Commit the current working tree in a new commit with message on | """Commit the current working tree in a new commit with message on | ||||
the branch 'ref'. | the branch 'ref'. | ||||
At the end of the commit, the reference should stay the same | At the end of the commit, the reference should stay the same | ||||
and the index should be clean. | and the index should be clean. | ||||
""" | """ | ||||
self.git_shell("add", ".") | paths = [ | ||||
os.path.relpath(path, self.repo_dir) | |||||
for path in glob.glob(self.repo_dir + "/**/*", recursive=True) | |||||
] | |||||
self.repo.stage(paths) | |||||
message = message.encode() + b"\n" | message = message.encode() + b"\n" | ||||
ret = self.repo.do_commit( | ret = self.repo.do_commit( | ||||
message=message, | message=message, | ||||
committer=self.author, | committer=self.author, | ||||
commit_timestamp=self.base_date + self.counter, | commit_timestamp=self.base_date + self.counter, | ||||
commit_timezone=0, | commit_timezone=0, | ||||
ref=ref, | ref=ref, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 171 Lines • ▼ Show 20 Lines | def test_directory_bogus_perms(self, git_loader): | ||||
repo = TestRepo() | repo = TestRepo() | ||||
with repo as rp: | with repo as rp: | ||||
(rp / "file").write_text(TEST_CONTENT) | (rp / "file").write_text(TEST_CONTENT) | ||||
(rp / "file").chmod(0o664) | (rp / "file").chmod(0o664) | ||||
(rp / "executable").write_bytes(TEST_EXECUTABLE) | (rp / "executable").write_bytes(TEST_EXECUTABLE) | ||||
(rp / "executable").chmod(0o775) | (rp / "executable").chmod(0o775) | ||||
(rp / "wat").write_text(TEST_CONTENT) | (rp / "wat").write_text(TEST_CONTENT) | ||||
(rp / "wat").chmod(0o604) | (rp / "wat").chmod(0o604) | ||||
# Disable mode cleanup | |||||
with unittest.mock.patch("dulwich.index.cleanup_mode", lambda mode: mode): | |||||
c = repo.commit() | c = repo.commit() | ||||
# Make sure Dulwich didn't normalize the permissions itself. | |||||
# (if it did, then the test can't check the cooker normalized them) | |||||
tree_id = repo.repo[c].tree | |||||
assert {entry.mode for entry in repo.repo[tree_id].items()} == { | |||||
0o100775, | |||||
0o100664, | |||||
0o100604, | |||||
} | |||||
# Disable mode checks | |||||
with unittest.mock.patch("dulwich.objects.Tree.check", lambda self: None): | |||||
loader = git_loader(str(rp)) | loader = git_loader(str(rp)) | ||||
loader.load() | loader.load() | ||||
# Make sure swh-loader didn't normalize them either | |||||
dir_entries = loader.storage.directory_ls(hashutil.bytehex_to_hash(tree_id)) | |||||
assert {entry["perms"] for entry in dir_entries} == { | |||||
0o100664, | |||||
0o100775, | |||||
0o100604, | |||||
} | |||||
obj_id_hex = repo.repo[c].tree.decode() | obj_id_hex = repo.repo[c].tree.decode() | ||||
obj_id = hashutil.hash_to_bytes(obj_id_hex) | obj_id = hashutil.hash_to_bytes(obj_id_hex) | ||||
with cook_extract_directory(loader.storage, obj_id) as p: | with cook_extract_directory(loader.storage, obj_id) as p: | ||||
assert (p / "file").stat().st_mode == 0o100644 | assert (p / "file").stat().st_mode == 0o100644 | ||||
assert (p / "executable").stat().st_mode == 0o100755 | assert (p / "executable").stat().st_mode == 0o100755 | ||||
assert (p / "wat").stat().st_mode == 0o100644 | assert (p / "wat").stat().st_mode == 0o100644 | ||||
▲ Show 20 Lines • Show All 192 Lines • ▼ Show 20 Lines | def test_revision_bogus_perms(self, git_loader): | ||||
repo = TestRepo() | repo = TestRepo() | ||||
with repo as rp: | with repo as rp: | ||||
(rp / "file").write_text(TEST_CONTENT) | (rp / "file").write_text(TEST_CONTENT) | ||||
(rp / "file").chmod(0o664) | (rp / "file").chmod(0o664) | ||||
(rp / "executable").write_bytes(TEST_EXECUTABLE) | (rp / "executable").write_bytes(TEST_EXECUTABLE) | ||||
(rp / "executable").chmod(0o775) | (rp / "executable").chmod(0o775) | ||||
(rp / "wat").write_text(TEST_CONTENT) | (rp / "wat").write_text(TEST_CONTENT) | ||||
(rp / "wat").chmod(0o604) | (rp / "wat").chmod(0o604) | ||||
repo.commit("initial commit") | |||||
# Disable mode cleanup | |||||
with unittest.mock.patch("dulwich.index.cleanup_mode", lambda mode: mode): | |||||
c = repo.commit("initial commit") | |||||
# Make sure Dulwich didn't normalize the permissions itself. | |||||
# (if it did, then the test can't check the cooker normalized them) | |||||
tree_id = repo.repo[c].tree | |||||
assert {entry.mode for entry in repo.repo[tree_id].items()} == { | |||||
0o100775, | |||||
0o100664, | |||||
0o100604, | |||||
} | |||||
# Disable mode checks | |||||
with unittest.mock.patch("dulwich.objects.Tree.check", lambda self: None): | |||||
loader = git_loader(str(rp)) | loader = git_loader(str(rp)) | ||||
loader.load() | loader.load() | ||||
# Make sure swh-loader didn't normalize them either | |||||
dir_entries = loader.storage.directory_ls(hashutil.bytehex_to_hash(tree_id)) | |||||
assert {entry["perms"] for entry in dir_entries} == { | |||||
0o100664, | |||||
0o100775, | |||||
0o100604, | |||||
} | |||||
obj_id_hex = repo.repo.refs[b"HEAD"].decode() | obj_id_hex = repo.repo.refs[b"HEAD"].decode() | ||||
obj_id = hashutil.hash_to_bytes(obj_id_hex) | obj_id = hashutil.hash_to_bytes(obj_id_hex) | ||||
with cook_extract_revision_gitfast(loader.storage, obj_id) as (ert, p): | with cook_extract_revision_gitfast(loader.storage, obj_id) as (ert, p): | ||||
ert.checkout(b"HEAD") | ert.checkout(b"HEAD") | ||||
assert (p / "file").stat().st_mode == 0o100644 | assert (p / "file").stat().st_mode == 0o100644 | ||||
assert (p / "executable").stat().st_mode == 0o100755 | assert (p / "executable").stat().st_mode == 0o100755 | ||||
assert (p / "wat").stat().st_mode == 0o100644 | assert (p / "wat").stat().st_mode == 0o100644 | ||||
▲ Show 20 Lines • Show All 66 Lines • Show Last 20 Lines |