forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_install.py
353 lines (289 loc) · 12.7 KB
/
test_install.py
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from contextlib import contextmanager
import random
import shutil
import stat
import tempfile
import unittest
from os.path import join
from conda import install
from conda.install import PaddingError, binary_replace, update_prefix
from .decorators import skip_if_no_mock
from .helpers import mock
patch = mock.patch if mock else None
def generate_random_path():
return '/some/path/to/file%s' % random.randint(100, 200)
class TestBinaryReplace(unittest.TestCase):
def test_simple(self):
self.assertEqual(
binary_replace(b'xxxaaaaaxyz\x00zz', b'aaaaa', b'bbbbb'),
b'xxxbbbbbxyz\x00zz')
def test_shorter(self):
self.assertEqual(
binary_replace(b'xxxaaaaaxyz\x00zz', b'aaaaa', b'bbbb'),
b'xxxbbbbxyz\x00\x00zz')
def test_too_long(self):
self.assertRaises(PaddingError, binary_replace,
b'xxxaaaaaxyz\x00zz', b'aaaaa', b'bbbbbbbb')
def test_no_extra(self):
self.assertEqual(binary_replace(b'aaaaa\x00', b'aaaaa', b'bbbbb'),
b'bbbbb\x00')
def test_two(self):
self.assertEqual(
binary_replace(b'aaaaa\x001234aaaaacc\x00\x00', b'aaaaa',
b'bbbbb'),
b'bbbbb\x001234bbbbbcc\x00\x00')
def test_spaces(self):
self.assertEqual(
binary_replace(b' aaaa \x00', b'aaaa', b'bbbb'),
b' bbbb \x00')
def test_multiple(self):
self.assertEqual(
binary_replace(b'aaaacaaaa\x00', b'aaaa', b'bbbb'),
b'bbbbcbbbb\x00')
self.assertEqual(
binary_replace(b'aaaacaaaa\x00', b'aaaa', b'bbb'),
b'bbbcbbb\x00\x00\x00')
self.assertRaises(PaddingError, binary_replace,
b'aaaacaaaa\x00', b'aaaa', b'bbbbb')
class FileTests(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
self.tmpfname = join(self.tmpdir, 'testfile')
def tearDown(self):
shutil.rmtree(self.tmpdir)
def test_default_text(self):
with open(self.tmpfname, 'w') as fo:
fo.write('#!/opt/anaconda1anaconda2anaconda3/bin/python\n'
'echo "Hello"\n')
update_prefix(self.tmpfname, '/usr/local')
with open(self.tmpfname, 'r') as fi:
data = fi.read()
self.assertEqual(data, '#!/usr/local/bin/python\n'
'echo "Hello"\n')
def test_binary(self):
with open(self.tmpfname, 'wb') as fo:
fo.write(b'\x7fELF.../some-placeholder/lib/libfoo.so\0')
update_prefix(self.tmpfname, '/usr/local',
placeholder='/some-placeholder', mode='binary')
with open(self.tmpfname, 'rb') as fi:
data = fi.read()
self.assertEqual(
data,
b'\x7fELF.../usr/local/lib/libfoo.so\0\0\0\0\0\0\0\0'
)
class remove_readonly_TestCase(unittest.TestCase):
def test_takes_three_args(self):
with self.assertRaises(TypeError):
install._remove_readonly()
with self.assertRaises(TypeError):
install._remove_readonly(True)
with self.assertRaises(TypeError):
install._remove_readonly(True, True)
with self.assertRaises(TypeError):
install._remove_readonly(True, True, True, True)
@skip_if_no_mock
def test_calls_os_chmod(self):
some_path = generate_random_path()
with patch.object(install.os, 'chmod') as chmod:
install._remove_readonly(mock.Mock(), some_path, {})
chmod.assert_called_with(some_path, stat.S_IWRITE)
@skip_if_no_mock
def test_calls_func(self):
some_path = generate_random_path()
func = mock.Mock()
with patch.object(install.os, 'chmod'):
install._remove_readonly(func, some_path, {})
func.assert_called_with(some_path)
class rm_rf_file_and_link_TestCase(unittest.TestCase):
@contextmanager
def generate_mock_islink(self, value):
with patch.object(install, 'islink', return_value=value) as islink:
yield islink
@contextmanager
def generate_mock_isdir(self, value):
with patch.object(install, 'isdir', return_value=value) as isdir:
yield isdir
@contextmanager
def generate_mock_isfile(self, value):
with patch.object(install, 'isfile', return_value=value) as isfile:
yield isfile
@contextmanager
def generate_mock_unlink(self):
with patch.object(install.os, 'unlink') as unlink:
yield unlink
@contextmanager
def generate_mock_rmtree(self):
with patch.object(install.shutil, 'rmtree') as rmtree:
yield rmtree
@contextmanager
def generate_mock_sleep(self):
with patch.object(install.time, 'sleep') as sleep:
yield sleep
@contextmanager
def generate_mock_log(self):
with patch.object(install, 'log') as log:
yield log
@contextmanager
def generate_mock_on_win(self, value):
original = install.on_win
install.on_win = value
yield
install.on_win = original
@contextmanager
def generate_mock_check_call(self):
with patch.object(install.subprocess, 'check_call') as check_call:
yield check_call
@contextmanager
def generate_mocks(self, islink=True, isfile=True, isdir=True, on_win=False):
with self.generate_mock_islink(islink) as mock_islink:
with self.generate_mock_isfile(isfile) as mock_isfile:
with self.generate_mock_isdir(isdir) as mock_isdir:
with self.generate_mock_unlink() as mock_unlink:
with self.generate_mock_rmtree() as mock_rmtree:
with self.generate_mock_sleep() as mock_sleep:
with self.generate_mock_log() as mock_log:
with self.generate_mock_on_win(on_win):
with self.generate_mock_check_call() as check_call:
yield {
'islink': mock_islink,
'isfile': mock_isfile,
'isdir': mock_isdir,
'unlink': mock_unlink,
'rmtree': mock_rmtree,
'sleep': mock_sleep,
'log': mock_log,
'check_call': check_call,
}
def generate_directory_mocks(self, on_win=False):
return self.generate_mocks(islink=False, isfile=False, isdir=True,
on_win=on_win)
def generate_all_false_mocks(self):
return self.generate_mocks(False, False, False)
@property
def generate_random_path(self):
return generate_random_path()
@skip_if_no_mock
def test_calls_islink(self):
with self.generate_mocks() as mocks:
some_path = self.generate_random_path
install.rm_rf(some_path)
mocks['islink'].assert_called_with(some_path)
@skip_if_no_mock
def test_calls_unlink_on_true_islink(self):
with self.generate_mocks() as mocks:
some_path = self.generate_random_path
install.rm_rf(some_path)
mocks['unlink'].assert_called_with(some_path)
@skip_if_no_mock
def test_does_not_call_isfile_if_islink_is_true(self):
with self.generate_mocks() as mocks:
some_path = self.generate_random_path
install.rm_rf(some_path)
self.assertFalse(mocks['isfile'].called)
@skip_if_no_mock
def test_calls_isfile_with_path(self):
with self.generate_mocks(islink=False, isfile=True) as mocks:
some_path = self.generate_random_path
install.rm_rf(some_path)
mocks['isfile'].assert_called_with(some_path)
@skip_if_no_mock
def test_calls_unlink_on_false_islink_and_true_isfile(self):
with self.generate_mocks(islink=False, isfile=True) as mocks:
some_path = self.generate_random_path
install.rm_rf(some_path)
mocks['unlink'].assert_called_with(some_path)
@skip_if_no_mock
def test_does_not_call_unlink_on_false_values(self):
with self.generate_mocks(islink=False, isfile=False) as mocks:
some_path = self.generate_random_path
install.rm_rf(some_path)
self.assertFalse(mocks['unlink'].called)
@skip_if_no_mock
def test_does_not_call_shutil_on_false_isdir(self):
with self.generate_all_false_mocks() as mocks:
some_path = self.generate_random_path
install.rm_rf(some_path)
self.assertFalse(mocks['rmtree'].called)
@skip_if_no_mock
def test_calls_rmtree_at_least_once_on_isdir_true(self):
with self.generate_directory_mocks() as mocks:
some_path = self.generate_random_path
install.rm_rf(some_path)
mocks['rmtree'].assert_called_with(some_path)
@skip_if_no_mock
def test_calls_rmtree_only_once_on_success(self):
with self.generate_directory_mocks() as mocks:
some_path = self.generate_random_path
install.rm_rf(some_path)
self.assertEqual(1, mocks['rmtree'].call_count)
@skip_if_no_mock
def test_raises_final_exception_if_it_cant_remove(self):
with self.generate_directory_mocks() as mocks:
mocks['rmtree'].side_effect = OSError
some_path = self.generate_random_path
with self.assertRaises(OSError):
install.rm_rf(some_path)
@skip_if_no_mock
def test_retries_six_times_to_ensure_it_cant_really_remove(self):
with self.generate_directory_mocks() as mocks:
mocks['rmtree'].side_effect = OSError
some_path = self.generate_random_path
with self.assertRaises(OSError):
install.rm_rf(some_path)
self.assertEqual(6, mocks['rmtree'].call_count)
@skip_if_no_mock
def test_retries_as_many_as_max_retries_plus_one(self):
max_retries = random.randint(7, 10)
with self.generate_directory_mocks() as mocks:
mocks['rmtree'].side_effect = OSError
some_path = self.generate_random_path
with self.assertRaises(OSError):
install.rm_rf(some_path, max_retries=max_retries)
self.assertEqual(max_retries + 1, mocks['rmtree'].call_count)
@skip_if_no_mock
def test_stops_retrying_after_success(self):
with self.generate_directory_mocks() as mocks:
mocks['rmtree'].side_effect = [OSError, OSError, None]
some_path = self.generate_random_path
install.rm_rf(some_path)
self.assertEqual(3, mocks['rmtree'].call_count)
@skip_if_no_mock
def test_pauses_for_same_number_of_seconds_as_max_retries(self):
with self.generate_directory_mocks() as mocks:
mocks['rmtree'].side_effect = OSError
max_retries = random.randint(1, 10)
with self.assertRaises(OSError):
install.rm_rf(self.generate_random_path,
max_retries=max_retries)
expected = [mock.call(i) for i in range(max_retries)]
mocks['sleep'].assert_has_calls(expected)
@skip_if_no_mock
def test_logs_messages_generated_for_each_retry(self):
with self.generate_directory_mocks() as mocks:
random_path = self.generate_random_path
mocks['rmtree'].side_effect = OSError(random_path)
max_retries = random.randint(1, 10)
with self.assertRaises(OSError):
install.rm_rf(random_path, max_retries=max_retries)
log_template = "\n".join([
"Unable to delete %s" % random_path,
"%s" % OSError(random_path),
"Retrying after %d seconds...",
])
expected_call_list = [mock.call(log_template % i)
for i in range(max_retries)]
mocks['log'].debug.assert_has_calls(expected_call_list)
@skip_if_no_mock
def test_tries_extra_kwarg_on_windows(self):
with self.generate_directory_mocks(on_win=True) as mocks:
random_path = self.generate_random_path
mocks['rmtree'].side_effect = [OSError, None]
install.rm_rf(random_path)
expected_call_list = [
mock.call(random_path),
mock.call(random_path, onerror=install._remove_readonly)
]
mocks['rmtree'].assert_has_calls(expected_call_list)
self.assertEqual(2, mocks['rmtree'].call_count)
if __name__ == '__main__':
unittest.main()