summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/tests/compression.py
blob: 95af3f96d7d29ff5099252e4a4d475695d9e02a2 (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
#
# Copyright BitBake Contributors
#
# SPDX-License-Identifier: GPL-2.0-only
#

from pathlib import Path
import bb.compress.lz4
import bb.compress.zstd
import contextlib
import os
import shutil
import tempfile
import unittest
import subprocess


class CompressionTests(object):
    def setUp(self):
        self._t = tempfile.TemporaryDirectory()
        self.tmpdir = Path(self._t.name)
        self.addCleanup(self._t.cleanup)

    def _file_helper(self, mode_suffix, data):
        tmp_file = self.tmpdir / "compressed"

        with self.do_open(tmp_file, mode="w" + mode_suffix) as f:
            f.write(data)

        with self.do_open(tmp_file, mode="r" + mode_suffix) as f:
            read_data = f.read()

        self.assertEqual(read_data, data)

    def test_text_file(self):
        self._file_helper("t", "Hello")

    def test_binary_file(self):
        self._file_helper("b", "Hello".encode("utf-8"))

    def _pipe_helper(self, mode_suffix, data):
        rfd, wfd = os.pipe()
        with open(rfd, "rb") as r, open(wfd, "wb") as w:
            with self.do_open(r, mode="r" + mode_suffix) as decompress:
                with self.do_open(w, mode="w" + mode_suffix) as compress:
                    compress.write(data)
                read_data = decompress.read()

        self.assertEqual(read_data, data)

    def test_text_pipe(self):
        self._pipe_helper("t", "Hello")

    def test_binary_pipe(self):
        self._pipe_helper("b", "Hello".encode("utf-8"))

    def test_bad_decompress(self):
        tmp_file = self.tmpdir / "compressed"
        with tmp_file.open("wb") as f:
            f.write(b"\x00")

        with self.assertRaises(OSError):
            with self.do_open(tmp_file, mode="rb", stderr=subprocess.DEVNULL) as f:
                data = f.read()


class LZ4Tests(CompressionTests, unittest.TestCase):
    def setUp(self):
        if shutil.which("lz4c") is None:
            self.skipTest("'lz4c' not found")
        super().setUp()

    @contextlib.contextmanager
    def do_open(self, *args, **kwargs):
        with bb.compress.lz4.open(*args, **kwargs) as f:
            yield f


class ZStdTests(CompressionTests, unittest.TestCase):
    def setUp(self):
        if shutil.which("zstd") is None:
            self.skipTest("'zstd' not found")
        super().setUp()

    @contextlib.contextmanager
    def do_open(self, *args, **kwargs):
        with bb.compress.zstd.open(*args, **kwargs) as f:
            yield f


class PZStdTests(CompressionTests, unittest.TestCase):
    def setUp(self):
        if shutil.which("pzstd") is None:
            self.skipTest("'pzstd' not found")
        super().setUp()

    @contextlib.contextmanager
    def do_open(self, *args, **kwargs):
        with bb.compress.zstd.open(*args, num_threads=2, **kwargs) as f:
            yield f