blob: a6520952554d70bf50e62747b23c957409b4a301 [file] [log] [blame]
George Burgess IV757887f2021-09-22 15:58:35 -07001# Copyright 2021 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Unit tests for xz_auto.py."""
6
7import os
8from pathlib import Path
9import unittest
10from unittest import mock
11
12from chromite.lib import cros_build_lib
13from chromite.lib import cros_test_lib
14from chromite.lib import osutils
15from chromite.scripts import xz_auto
16
17
18DIR = Path(__file__).resolve().parent
19
20
21def FindXzAutoLocation():
22 """Figures out where the xz_auto binary is."""
23 return DIR / 'xz_auto'
24
25
26class XzAutoTests(cros_test_lib.MockTempDirTestCase):
27 """Various tests for xz_auto."""
28
29 def DisablePixzForCurrentTest(self):
30 """Disables the use of pixz for the current test."""
31 # This will be cleaned up by cros_test_lib, so no need to addCleanup.
32 os.environ[xz_auto.PIXZ_DISABLE_VAR] = '1'
33
34 def testPixzArgParsingSeemsToWork(self):
35 """Tests our detection of file names in pixz commandlines."""
36 self.assertEqual(
37 xz_auto.ParsePixzArgs(['to_compress.txt']),
38 ([], 'to_compress.txt', None),
39 )
40 self.assertEqual(
41 xz_auto.ParsePixzArgs(['to_compress.txt', 'compressed.txt']),
42 ([], 'to_compress.txt', 'compressed.txt'),
43 )
44 self.assertEqual(
45 xz_auto.ParsePixzArgs(['to_compress.txt', '-c', 'compressed.txt',
46 '-9']),
47 (['-c', '-9'], 'to_compress.txt', 'compressed.txt'),
48 )
49 self.assertEqual(
50 xz_auto.ParsePixzArgs(
51 ['-t', 'to_compress.txt', '-c', 'compressed.txt', '-p', '2']),
52 (['-t', '-c', '-p', '2'], 'to_compress.txt', 'compressed.txt'),
53 )
54 self.assertEqual(
55 xz_auto.ParsePixzArgs(['-tcp2', 'to_compress.txt', 'compressed.txt']),
56 (['-t', '-c', '-p', '2'], 'to_compress.txt', 'compressed.txt'),
57 )
58
59 @unittest.skipIf(not xz_auto.HasPixz(), 'need pixz for this test')
60 @mock.patch.object(xz_auto, 'Execvp')
61 def testPixzCommandCreationSelectsPixzIfAvailable(self, execvp_mock):
62 """Tests that we actually execute pixz when we intend to."""
63 class ExecvpStopError(Exception):
64 """Convenient way to halt execution."""
65
66 def execvp_side_effect(argv):
67 """Does testing of our execvp calls."""
68 self.assertEqual(argv[0], 'pixz')
69 raise ExecvpStopError()
70
71 execvp_mock.side_effect = execvp_side_effect
72 with self.assertRaises(ExecvpStopError):
73 xz_auto.ExecCompressCommand(stdout=False, argv=[])
74
75 with self.assertRaises(ExecvpStopError):
76 xz_auto.ExecDecompressCommand(stdout=False, argv=[])
77
78 def _TestFileCompressionImpl(self):
79 """Tests that compressing a file with xz_auto WAI."""
80 file_contents = b'some random file contents'
81 file_location = os.path.join(self.tempdir, 'file.txt')
82 osutils.WriteFile(file_location, file_contents, mode='wb')
83
84 xz_auto_script = str(FindXzAutoLocation())
85 cros_build_lib.run(
86 [
87 xz_auto_script,
88 file_location,
89 ],
90 check=True,
91 )
92
93 xz_location = file_location + '.xz'
94 self.assertExists(xz_location)
95 self.assertNotExists(file_location)
96 cros_build_lib.run([
97 xz_auto_script,
98 '--decompress',
99 xz_location,
100 ])
101 self.assertNotExists(xz_location)
102 self.assertExists(file_location)
103 self.assertEqual(
104 osutils.ReadFile(file_location, mode='rb'),
105 file_contents,
106 )
107
108 def _TestStdoutCompressionImpl(self):
109 """Tests that compressing stdstreams with xz_auto WAI."""
110 file_contents = b'some random file contents'
111 xz_auto_script = str(FindXzAutoLocation())
112
113 run_result = cros_build_lib.run(
114 [
115 xz_auto_script,
116 '-c',
117 ],
118 capture_output=True,
119 input=file_contents,
120 )
121
122 compressed_file = run_result.stdout
123 self.assertNotEqual(compressed_file, file_contents)
124
125 run_result = cros_build_lib.run(
126 [
127 xz_auto_script,
128 '--decompress',
129 '-c',
130 ],
131 input=compressed_file,
132 capture_output=True,
133 )
134 uncompressed_file = run_result.stdout
135 self.assertEqual(file_contents, uncompressed_file)
136
137 def _TestStdoutCompressionFromFileImpl(self):
138 """Tests that compression of a file & outputting to stdout works.
139
140 Pixz has some semi-weird behavior here (b/202735786).
141 """
142 file_contents = b'some random file contents'
143 xz_auto_script = str(FindXzAutoLocation())
144 file_location = os.path.join(self.tempdir, 'file.txt')
145 osutils.WriteFile(file_location, file_contents, mode='wb')
146
147 run_result = cros_build_lib.run(
148 [
149 xz_auto_script,
150 '-c',
151 file_location,
152 ],
153 capture_output=True,
154 )
155
156 compressed_file = run_result.stdout
157 self.assertExists(file_location)
158 self.assertNotEqual(compressed_file, file_contents)
159
160 run_result = cros_build_lib.run(
161 [
162 xz_auto_script,
163 '--decompress',
164 '-c',
165 ],
166 capture_output=True,
167 input=compressed_file,
168 )
169 uncompressed_file = run_result.stdout
170 self.assertEqual(file_contents, uncompressed_file)
171
172 @unittest.skipIf(not xz_auto.HasPixz(), 'need pixz for this test')
173 def testFileCompressionWithPixzWorks(self):
174 """Tests that compressing a file with pixz WAI."""
175 self._TestFileCompressionImpl()
176
177 @unittest.skipIf(not xz_auto.HasPixz(), 'need pixz for this test')
178 def testStdoutCompressionWithPixzWorks(self):
179 """Tests that compressing `stdout` with pixz WAI."""
180 self._TestStdoutCompressionImpl()
181
182 @unittest.skipIf(not xz_auto.HasPixz(), 'need pixz for this test')
183 def testStdoutCompressionFromFileWithPixzWorks(self):
184 """Tests that compressing from a file to stdout with pixz WAI."""
185 self._TestStdoutCompressionFromFileImpl()
186
187 def testFileCompressionWithXzWorks(self):
188 """Tests that compressing a file with pixz WAI."""
189 self.DisablePixzForCurrentTest()
190 self._TestFileCompressionImpl()
191
192 def testStdoutCompressionWithXzWorks(self):
193 """Tests that compressing `stdout` with pixz WAI."""
194 self.DisablePixzForCurrentTest()
195 self._TestStdoutCompressionImpl()
196
197 def testStdoutCompressionFromFileWithXzWorks(self):
198 """Tests that compressing from a file to stdout WAI."""
199 self.DisablePixzForCurrentTest()
200 self._TestStdoutCompressionFromFileImpl()