File: test_transmute.py

package info (click to toggle)
conda-package-streaming 0.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 456 kB
  • sloc: python: 1,390; makefile: 14; sh: 12
file content (209 lines) | stat: -rw-r--r-- 6,874 bytes parent folder | download
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
import contextlib
import io
import itertools
import os
import tarfile
import time
from pathlib import Path
from zipfile import ZipFile

import pytest
import zstandard
from conda_package_handling.validate import validate_converted_files_match_streaming

from conda_package_streaming.create import anonymize
from conda_package_streaming.package_streaming import (
    CondaComponent,
    stream_conda_component,
)
from conda_package_streaming.transmute import (
    transmute,
    transmute_stream,
    transmute_tar_bz2,
)


@pytest.fixture
def testtar_bytes():
    buffer = io.BytesIO()
    with tarfile.open("test.tar.bz2", "w:bz2", fileobj=buffer) as tar:
        symlink = tarfile.TarInfo(name="symlink")
        symlink.type = tarfile.LNKTYPE
        symlink.linkname = "target"
        tar.addfile(symlink)

        expected = tarfile.TarInfo(name="info/expected")
        tar.addfile(expected, io.BytesIO())
        unexpected = tarfile.TarInfo(name="info/unexpected")
        tar.addfile(unexpected, io.BytesIO())
    return buffer.getbuffer()


@contextlib.contextmanager
def timeme(message: str = ""):
    begin = time.time()
    yield
    end = time.time()
    print(f"{message}{end - begin:0.2f}s")


@pytest.mark.skip(reason="Conda is not yet available for Debian")
def test_transmute(conda_paths: list[Path], tmpdir):
    tarbz_packages = []
    for path in conda_paths:
        path = str(path)
        if path.endswith(".tar.bz2") and (1 << 20 < os.stat(path).st_size < 1 << 22):
            tarbz_packages = [path]
    conda_packages = []  # not supported

    assert tarbz_packages, "no medium-sized .tar.bz2 packages found"

    metadata_checks = 0

    for packages in (conda_packages, tarbz_packages):
        for package in packages:
            with timeme(f"{package} took "):
                out = transmute(package, tmpdir)
                _, missing, mismatched = validate_converted_files_match_streaming(
                    out, package, strict=True
                )
                assert missing == mismatched == []
                if out.name.endswith(".conda"):
                    with ZipFile(out) as zf:
                        metadata_checks += 1
                        assert "metadata.json" in zf.namelist()

    assert metadata_checks > 0


def test_transmute_symlink(tmpdir, testtar_bytes):
    testtar = Path(tmpdir, "test.tar.bz2")
    testtar.write_bytes(testtar_bytes)

    out = transmute(str(testtar), tmpdir)
    _, missing, mismatched = validate_converted_files_match_streaming(
        out, testtar, strict=True
    )
    assert missing == mismatched == []


def test_transmute_info_filter(tmpdir, testtar_bytes):
    testtar = Path(tmpdir, "test.tar.bz2")
    testtar.write_bytes(testtar_bytes)

    transmute(
        str(testtar), tmpdir, is_info=lambda filename: filename == "info/expected"
    )

    with open(Path(tmpdir, "test.conda"), "rb") as fileobj:
        for component, expected in (
            (CondaComponent.info, {"info/expected"}),
            (
                CondaComponent.pkg,
                {
                    "info/unexpected",
                    "symlink",
                },
            ),
        ):
            items = stream_conda_component("test.conda", fileobj, component)
            assert {member.name for tar, member in items} == expected, items

@pytest.mark.skip(reason="Conda is not yet available for Debian")
def test_transmute_backwards(tmpdir, conda_paths):
    tarbz_packages = []
    for path in conda_paths:
        path = str(path)
        if path.endswith(".conda") and (1 << 20 < os.stat(path).st_size < 1 << 22):
            tarbz_packages = [path]
    conda_packages = []  # not supported

    assert tarbz_packages, "no medium-sized .conda packages found"

    for packages in (conda_packages, tarbz_packages):
        for package in packages:
            with timeme(f"{package} took "):
                out = transmute_tar_bz2(package, tmpdir)
                _, missing, mismatched = validate_converted_files_match_streaming(
                    out, package, strict=True
                )
                assert missing == mismatched == []


def test_transmute_tarbz2_to_tarbz2(tmpdir, testtar_bytes):
    testtar = Path(tmpdir, "test.tar.bz2")
    testtar.write_bytes(testtar_bytes)
    outdir = Path(tmpdir, "output")
    outdir.mkdir()
    out = transmute_tar_bz2(str(testtar), outdir)
    _, missing, mismatched = validate_converted_files_match_streaming(
        out, testtar, strict=True
    )
    assert missing == mismatched == []


def test_transmute_conditional_zip64(tmp_path, mocker):
    """
    Test that zip64 is used in transmute after a threshold.
    """

    LIMIT = 16384

    for test_size, extra_expected in (LIMIT // 2, False), (LIMIT * 2, True):
        mocker.patch("conda_package_streaming.create.CONDA_ZIP64_LIMIT", new=LIMIT)
        mocker.patch("zipfile.ZIP64_LIMIT", new=LIMIT)

        tmp_tar = tmp_path / f"{test_size}.tar.bz2"
        with tarfile.open(tmp_tar, "w:bz2") as tar:
            pkg = tarfile.TarInfo(name="packagedata")
            data = io.BytesIO(os.urandom(test_size))
            pkg.size = len(data.getbuffer())
            tar.addfile(pkg, data)

            info = tarfile.TarInfo(name="info/data")
            data = io.BytesIO(os.urandom(test_size))
            info.size = len(data.getbuffer())
            tar.addfile(info, data)

        out = transmute(str(tmp_tar), tmp_path)

        with ZipFile(out) as e:
            assert e.filelist[0].extra == b""
            # when zip64 extension is used, extra contains zip64 headers
            assert bool(e.filelist[1].extra) == extra_expected
            assert bool(e.filelist[2].extra) == extra_expected


@pytest.mark.skip(reason="Conda is not yet available for Debian")
def test_transmute_stream(tmpdir, conda_paths):
    """
    Test example from transmute_stream documentation. Recompress .conda using
    transmute_stream()
    """
    conda_packages = []
    for path in conda_paths:
        if path.name.endswith(".conda") and (1 << 20 < os.stat(path).st_size < 1 << 22):
            conda_packages.append(path)

    for package in conda_packages[:3]:
        file_id = package.name

        transmute_stream(
            file_id,
            tmpdir,
            compressor=lambda: zstandard.ZstdCompressor(),
            package_stream=itertools.chain(
                stream_conda_component(package, component=CondaComponent.pkg),
                stream_conda_component(package, component=CondaComponent.info),
            ),
        )


def test_anonymize_helper():
    ti = tarfile.TarInfo(name="info")
    ti.uid = ti.gid = 500
    ti.uname = ti.gname = "somebody"
    anon = anonymize(ti)
    assert anon.name == ti.name  # they are also the same object
    assert anon.uid == anon.gid == 0
    assert anon.uname == anon.gname == ""