2 # Copyright (c) 2015-2016, 2019 Intel Corporation
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 """Tests for compression in file backends."""
30 from unittest
import mock
34 from framework
import core
35 from framework
.backends
import abstract
36 from framework
.backends
import compression
40 # pylint: disable=no-self-use,redefined-outer-name
46 """Try to import backports.lzma, and return True if it exists, False if it
50 import backports
.lzma
# pylint: disable=unused-variable
57 """Check for an xz binary."""
59 subprocess
.check_call(['xz', '--help'])
60 except subprocess
.CalledProcessError
:
62 except OSError as e
: # pylint: disable=undefined-variable
71 with mock
.patch
.dict('os.environ'):
76 with mock
.patch('framework.core.PIGLIT_CONFIG',
77 new_callable
=core
.PiglitConfig
) as conf
:
78 conf
.add_section('core')
83 """Workaround for yield_fixture not working with classes."""
84 class Compressor(object):
85 """Fixture to control the compressor/decompressor objects."""
88 self
._mock
_c
= mock
.patch
.dict(
89 'framework.backends.compression.COMPRESSORS', clear
=False)
90 self
._mock
_d
= mock
.patch
.dict(
91 'framework.backends.compression.DECOMPRESSORS', clear
=False)
98 def __exit__(self
, type_
, value
, traceback
):
102 def add(self
, name
, func
):
103 assert name
not in compression
.COMPRESSORS
104 assert name
not in compression
.DECOMPRESSORS
105 compression
.COMPRESSORS
[name
] = func
106 compression
.DECOMPRESSORS
[name
] = func
108 def rm(self
, name
): # pylint: disable=invalid-name
109 assert name
in compression
.COMPRESSORS
110 assert name
in compression
.DECOMPRESSORS
111 del compression
.COMPRESSORS
[name
]
112 del compression
.DECOMPRESSORS
[name
]
114 with
Compressor() as c
:
121 @pytest.mark
.parametrize("mode", ['none', 'bz2', 'gz', 'xz'])
122 def test_compress(mode
, tmpdir
):
123 """Test that each compressor that we want works.
125 These only check using modules, that means on python2 this test will skip
126 unless backports.lzma is available, and it will not test the the xz binary
129 func
= compression
.COMPRESSORS
[mode
]
130 testfile
= tmpdir
.join('test')
131 with
func(str(testfile
)) as f
:
135 @pytest.mark
.parametrize("mode", ['none', 'bz2', 'gz', 'xz'])
136 def test_decompress(mode
, tmpdir
):
137 """Test that each supported decompressor works.
139 See the docstring in test_compress.
141 comp
= compression
.COMPRESSORS
[mode
]
142 dec
= compression
.DECOMPRESSORS
[mode
]
143 testfile
= tmpdir
.join('test')
145 with
comp(str(testfile
)) as f
:
148 with
dec(str(testfile
)) as f
:
151 assert actual
== 'foo'
155 class TestXZBin(object):
156 """Tests for the xz bin path on python2.x."""
158 def test_compress_xz_bin(self
, tmpdir
):
159 """Test python2 xz compression using the xz binary."""
160 func
= compression
.COMPRESSORS
['xz']
161 testfile
= tmpdir
.join('test')
162 with
func(str(testfile
)) as f
:
165 def test_decompress_xz_bin(self
, tmpdir
):
166 """Test python2 xz decompression using the xz binary."""
167 comp
= compression
.COMPRESSORS
['xz']
168 dec
= compression
.DECOMPRESSORS
['xz']
169 testfile
= tmpdir
.join('test')
171 with
comp(str(testfile
)) as f
:
174 with
dec(str(testfile
)) as f
:
177 assert actual
== 'foo'
180 class TestGetMode(object):
181 """Tests for the compression.get_mode function."""
183 def test_default(self
, env
, config
): # pylint: disable=unused-argument
184 """When neither the config file nor the environment sets a value for
185 compression the default value should be used.
189 assert compression
.get_mode() == compression
.DEFAULT
191 def test_env(self
, env
, config
, compressor
):
192 """Test that when env doesn't have a PIGLIT_COMPRESSION environment
193 variable set, but the configuration has a compression method set that
196 compressor
.add('foo', None)
197 compressor
.add('bar', None)
198 config
.set('core', 'compression', 'foo')
199 env
['PIGLIT_COMPRESSION'] = 'bar'
201 assert compression
.get_mode() == 'bar'
203 def test_piglit_conf(self
, env
, config
, compressor
):
204 """Test that when env doesn't have a PIGLIT_COMPRESSION environment
205 variable set, but the configuration has a compression method set that
208 compressor
.add('foobar', None)
209 config
.set('core', 'compression', 'foobar')
212 assert compression
.get_mode() == 'foobar'
215 @pytest.mark
.parametrize("extension", ['bz2', 'gz', 'xz'])
216 def test_duplicate_extensions(extension
, tmpdir
, config
):
217 """Tests that exercises a bug that caused the compressed extension to be
218 duplicated in some cases.
221 config
.set('core', 'compression', extension
)
222 expected
= 'results.txt.' + extension
224 with abstract
.write_compressed(expected
) as f
:
227 assert expected
in os
.listdir('.')
230 @pytest.mark
.parametrize("orig,new", itertools
.permutations(
231 ['bz2', 'gz', 'xz'], 2))
232 def test_changed_extension(orig
, new
, tmpdir
, config
):
233 """Tests that exercises a bug that caused two extensions to be present if
234 the compression method changed.
237 config
.set('core', 'compression', str(new
))
239 with abstract
.write_compressed('results.txt.' + orig
) as f
:
242 assert 'results.txt.' + new
in os
.listdir('.')
243 assert 'results.txt.' + orig
not in os
.listdir('.')
244 assert 'results.txt.{}.{}'.format(orig
, new
) not in os
.listdir('.')