aboutsummaryrefslogtreecommitdiff
blob: fcc0581605e7a9138dd9e000daec82da05d9a081 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import os
from pathlib import Path

import pytest

from pkgcore.fs import contents, fs, livefs, ops
from snakeoil.data_source import local_source


def verify(obj, kwds):
    stat = os.stat(obj.location)
    for attr, keyword in (("st_mtime", "mtime"), ("st_gid", "gid"), ("st_uid", "uid")):
        if keyword in kwds:
            assert getattr(stat, attr) == kwds[keyword], f"testing {keyword}"
    if "mode" in kwds:
        assert (stat.st_mode & 0o4777) == kwds["mode"]


@pytest.mark.parametrize(
    ("creator_func", "kls"),
    (
        pytest.param(os.mkdir, fs.fsDir, id="dir"),
        pytest.param(lambda s: open(s, "w").close(), fs.fsFile, id="file"),
    ),
)
def test_default_ensure_perms(tmp_path, creator_func, kls):
    kwds = dict(
        mtime=0o1234, uid=os.getuid(), gid=os.getgid(), mode=0o775, dev=None, inode=None
    )
    o = kls(str(tmp_path / "blah"), **kwds)
    creator_func(o.location)
    assert ops.ensure_perms(o)
    verify(o, kwds)

    kwds["mode"] = 0o770
    o2 = kls(str(tmp_path / "blah"), **kwds)
    assert ops.ensure_perms(o2)
    verify(o2, kwds)

    with pytest.raises(OSError):
        ops.ensure_perms(kls(str(tmp_path / "asdf"), **kwds))


def test_default_mkdir(tmp_path):
    o = fs.fsDir(str(tmp_path / "mkdir_test"), strict=False)
    assert ops.mkdir(o)
    old_umask = os.umask(0)
    try:
        assert (os.stat(o.location).st_mode & 0o4777) == (0o777 & ~old_umask)
    finally:
        os.umask(old_umask)
    os.rmdir(o.location)

    o = fs.fsDir(str(tmp_path / "mkdir_test2"), strict=False, mode=0o750)
    assert ops.mkdir(o)
    assert (os.stat(o.location).st_mode & 0o4777) == 0o750


class TestCopyFile:
    def test_it(self, tmp_path):
        content = "\n".join("asdf" for _ in range(10))
        (src := tmp_path / "copy_test_src").write_text(content)
        dest = tmp_path / "copy_test_dest"

        kwds = {
            "mtime": 10321,
            "uid": os.getuid(),
            "gid": os.getgid(),
            "mode": 0o664,
            "data": local_source(str(src)),
            "dev": None,
            "inode": None,
        }
        o = fs.fsFile(str(dest), **kwds)
        assert ops.copyfile(o)
        assert dest.read_text() == content
        verify(o, kwds)

    def test_sym_perms(self, tmp_path):
        curgid = os.getgid()
        group = [x for x in os.getgroups() if x != curgid]
        if not group and os.getuid() != 0:
            pytest.skip(
                "requires root privs for this test, or for this user to"
                "belong to more then one group"
            )
        group = group[0]
        fp = str(tmp_path / "sym")
        o = fs.fsSymlink(
            fp, mtime=10321, uid=os.getuid(), gid=group, mode=0o664, target="target"
        )
        assert ops.copyfile(o)
        assert os.lstat(fp).st_gid == group
        assert os.lstat(fp).st_uid == os.getuid()

    def test_puke_on_dirs(self, tmp_path: Path):
        path = str(tmp_path / "puke_dir")
        with pytest.raises(TypeError):
            ops.copyfile(fs.fsDir(path, strict=False))
        os.mkdir(path)
        fp = str(tmp_path / "foon")
        open(fp, "w").close()
        f = livefs.gen_obj(fp)
        with pytest.raises(TypeError):
            livefs.gen_obj(fp).change_attributes(location=path)()

        # test sym over a directory.
        f = fs.fsSymlink(
            path, fp, mode=0o644, mtime=0, uid=os.getuid(), gid=os.getgid()
        )
        with pytest.raises(TypeError):
            ops.copyfile(f)
        os.unlink(fp)
        os.mkdir(fp)
        with pytest.raises(ops.CannotOverwrite):
            ops.copyfile(f)


class ContentsMixin:
    entries_norm1 = {
        "file1": ["reg"],
        "dir": ["dir"],
        "dir/subdir": ["dir"],
        "dir/file2": ["reg"],
        "ldir": ["sym", "dir/subdir"],
        "dir/lfile": ["sym", "../file1"],
    }

    entries_rec1 = {
        "dir": ["dir"],
        "dir/link": ["sym", "../dir"],
    }

    def generate_tree(self, base, entries):
        base.mkdir()
        s_ents = [(base / k, entries[k]) for k in sorted(entries)]
        for k, v in s_ents:
            if v[0] == "dir":
                k.mkdir()
        for k, v in s_ents:
            if v[0] == "dir":
                pass
            elif v[0] == "reg":
                k.touch()
            elif v[0] == "sym":
                k.symlink_to(v[1])
            else:
                pytest.fail(f"generate_tree doesn't support type {v!r} yet: k {k!r}")
        return str(base)


class TestMergeContents(ContentsMixin):
    @pytest.fixture
    def generic_merge_bits(self, request, tmp_path):
        entries = getattr(self, request.param)
        assert isinstance(entries, dict)
        src = self.generate_tree(tmp_path / "src", entries)
        cset = livefs.scan(src, offset=src)
        (dest := tmp_path / "dest").mkdir()
        dest = str(dest)
        assert ops.merge_contents(cset, offset=dest)
        assert livefs.scan(src, offset=src) == livefs.scan(dest, offset=dest)
        return src, dest, cset

    @pytest.mark.parametrize(
        "generic_merge_bits", ("entries_norm1", "entries_rec1"), indirect=True
    )
    def test_callback(self, generic_merge_bits):
        src, dest, cset = generic_merge_bits
        new_cset = contents.contentsSet(contents.offset_rewriter(dest, cset))
        s = set(new_cset)
        ops.merge_contents(cset, offset=dest, callback=s.remove)
        assert not s

    def test_dangling_symlink(self, tmp_path):
        src = self.generate_tree(tmp_path / "src", {"dir": ["dir"]})
        cset = livefs.scan(src, offset=src)
        (dest := tmp_path / "dest").mkdir()
        (dest / "dir").symlink_to(dest / "dest")
        assert ops.merge_contents(cset, offset=str(dest))
        assert cset == livefs.scan(src, offset=str(dest))

    @pytest.mark.parametrize("generic_merge_bits", ("entries_norm1",), indirect=True)
    def test_exact_overwrite(self, generic_merge_bits):
        src, dest, cset = generic_merge_bits
        assert ops.merge_contents(cset, offset=dest)

    def test_sym_over_dir(self, tmp_path):
        (path := tmp_path / "sym").mkdir()
        fp = tmp_path / "trg"
        # test sym over a directory.
        f = fs.fsSymlink(
            str(path), str(fp), mode=0o644, mtime=0, uid=os.getuid(), gid=os.getgid()
        )
        cset = contents.contentsSet([f])
        with pytest.raises(ops.FailedCopy):
            ops.merge_contents(cset)
        assert fs.isdir(livefs.gen_obj(str(path)))
        fp.mkdir()
        ops.merge_contents(cset)

    def test_dir_over_file(self, tmp_path):
        # according to the spec, dirs can't be merged over files that
        # aren't dirs or symlinks to dirs
        (path := tmp_path / "file2dir").touch()
        d = fs.fsDir(str(path), mode=0o755, mtime=0, uid=os.getuid(), gid=os.getgid())
        cset = contents.contentsSet([d])
        with pytest.raises(ops.CannotOverwrite):
            ops.merge_contents(cset)


class TestUnmergeContents(ContentsMixin):
    @pytest.fixture
    def generic_unmerge_bits(self, request, tmp_path):
        entries = getattr(self, request.param)
        assert isinstance(entries, dict)
        img = self.generate_tree(tmp_path / "img", entries)
        cset = livefs.scan(img, offset=img)
        return img, cset

    @pytest.mark.parametrize(
        "generic_unmerge_bits", ("entries_norm1", "entries_rec1"), indirect=True
    )
    def test_callback(self, generic_unmerge_bits):
        img, cset = generic_unmerge_bits
        s = set(contents.offset_rewriter(img, cset))
        ops.unmerge_contents(cset, offset=img, callback=s.remove)
        assert not s

    @pytest.mark.parametrize("generic_unmerge_bits", ("entries_norm1",), indirect=True)
    def test_empty_removal(self, tmp_path, generic_unmerge_bits):
        img, cset = generic_unmerge_bits
        assert ops.unmerge_contents(cset, offset=str(tmp_path / "dest"))

    @pytest.mark.parametrize("generic_unmerge_bits", ("entries_norm1",), indirect=True)
    def test_exact_removal(self, generic_unmerge_bits):
        img, cset = generic_unmerge_bits
        assert ops.unmerge_contents(cset, offset=img)
        assert not livefs.scan(img, offset=img)

    @pytest.mark.parametrize("generic_unmerge_bits", ("entries_norm1",), indirect=True)
    def test_lingering_file(self, generic_unmerge_bits):
        img, cset = generic_unmerge_bits
        dirs = [k for k, v in self.entries_norm1.items() if v[0] == "dir"]
        (fp := Path(img) / dirs[0] / "linger").touch()
        assert ops.unmerge_contents(cset, offset=img)
        assert fp.exists()