2 from __future__ import absolute_import
4 import helpers, math, os, os.path, stat, subprocess
8 from bup.compat import environ
9 from bup.helpers import (atomically_replaced_file, batchpipe, detect_fakeroot,
10 grafted_path_components, mkdirp, parse_num,
11 path_components, readpipe, stripped_path_components,
13 from buptest import no_lingering_errors, test_tempdir
14 import bup._helpers as _helpers
17 bup_tmp = os.path.realpath('../../../t/tmp')
23 with no_lingering_errors():
27 WVPASSEQ(pn('1.5k'), 1536)
28 WVPASSEQ(pn('2 gb'), 2*1024*1024*1024)
29 WVPASSEQ(pn('1e+9 k'), 1000000000 * 1024)
30 WVPASSEQ(pn('-3e-3mb'), int(-0.003 * 1024 * 1024))
33 def test_detect_fakeroot():
34 with no_lingering_errors():
35 if os.getenv('FAKEROOTKEY'):
36 WVPASS(detect_fakeroot())
38 WVPASS(not detect_fakeroot())
41 def test_path_components():
42 with no_lingering_errors():
43 WVPASSEQ(path_components('/'), [('', '/')])
44 WVPASSEQ(path_components('/foo'), [('', '/'), ('foo', '/foo')])
45 WVPASSEQ(path_components('/foo/'), [('', '/'), ('foo', '/foo')])
46 WVPASSEQ(path_components('/foo/bar'),
47 [('', '/'), ('foo', '/foo'), ('bar', '/foo/bar')])
48 WVEXCEPT(Exception, path_components, 'foo')
52 def test_stripped_path_components():
53 with no_lingering_errors():
54 WVPASSEQ(stripped_path_components('/', []), [('', '/')])
55 WVPASSEQ(stripped_path_components('/', ['']), [('', '/')])
56 WVPASSEQ(stripped_path_components('/', ['/']), [('', '/')])
57 WVPASSEQ(stripped_path_components('/foo', ['/']),
58 [('', '/'), ('foo', '/foo')])
59 WVPASSEQ(stripped_path_components('/', ['/foo']), [('', '/')])
60 WVPASSEQ(stripped_path_components('/foo', ['/bar']),
61 [('', '/'), ('foo', '/foo')])
62 WVPASSEQ(stripped_path_components('/foo', ['/foo']), [('', '/foo')])
63 WVPASSEQ(stripped_path_components('/foo/bar', ['/foo']),
64 [('', '/foo'), ('bar', '/foo/bar')])
65 WVPASSEQ(stripped_path_components('/foo/bar', ['/bar', '/foo', '/baz']),
66 [('', '/foo'), ('bar', '/foo/bar')])
67 WVPASSEQ(stripped_path_components('/foo/bar/baz', ['/foo/bar/baz']),
68 [('', '/foo/bar/baz')])
69 WVEXCEPT(Exception, stripped_path_components, 'foo', [])
73 def test_grafted_path_components():
74 with no_lingering_errors():
75 WVPASSEQ(grafted_path_components([('/chroot', '/')], '/foo'),
76 [('', '/'), ('foo', '/foo')])
77 WVPASSEQ(grafted_path_components([('/foo/bar', '/')],
80 ('baz', '/foo/bar/baz'),
81 ('bax', '/foo/bar/baz/bax')])
82 WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/bax')],
85 ('bax', '/foo/bar/baz'),
86 ('1', '/foo/bar/baz/1'),
87 ('2', '/foo/bar/baz/1/2')])
88 WVPASSEQ(grafted_path_components([('/foo', '/bar/baz/bax')],
95 WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/a/b/c')],
97 [('', None), ('a', None), ('b', None), ('c', '/foo/bar/baz')])
98 WVPASSEQ(grafted_path_components([('/', '/a/b/c/')], '/foo/bar'),
99 [('', None), ('a', None), ('b', None), ('c', '/'),
100 ('foo', '/foo'), ('bar', '/foo/bar')])
101 WVEXCEPT(Exception, grafted_path_components, 'foo', [])
106 with no_lingering_errors():
107 x = readpipe(['echo', '42'])
110 readpipe(['bash', '-c', 'exit 42'])
111 except Exception as ex:
113 "subprocess 'bash -c exit 42' failed with status 42")
117 def test_batchpipe():
118 with no_lingering_errors():
119 for chunk in batchpipe(['echo'], []):
122 for chunk in batchpipe(['echo'], ['42']):
124 WVPASSEQ(out, b'42\n')
126 batchpipe(['bash', '-c'], ['exit 42'])
127 except Exception as ex:
129 "subprocess 'bash -c exit 42' failed with status 42")
130 args = [str(x) for x in range(6)]
131 # Force batchpipe to break the args into batches of 3. This
132 # approach assumes all args are the same length.
134 helpers._argmax_base(['echo']) + helpers._argmax_args_size(args[:3])
135 batches = batchpipe(['echo'], args, arg_max=arg_max)
136 WVPASSEQ(next(batches), b'0 1 2\n')
137 WVPASSEQ(next(batches), b'3 4 5\n')
138 WVPASSEQ(next(batches, None), None)
139 batches = batchpipe(['echo'], [str(x) for x in range(5)], arg_max=arg_max)
140 WVPASSEQ(next(batches), b'0 1 2\n')
141 WVPASSEQ(next(batches), b'3 4\n')
142 WVPASSEQ(next(batches, None), None)
146 def test_atomically_replaced_file():
147 with no_lingering_errors():
148 with test_tempdir(b'bup-thelper-') as tmpdir:
149 target_file = os.path.join(tmpdir, b'test-atomic-write')
151 with atomically_replaced_file(target_file, mode='w') as f:
153 WVPASSEQ(f.mode, 'w')
154 f = open(target_file, 'r')
155 WVPASSEQ(f.read(), 'asdf')
158 with atomically_replaced_file(target_file, mode='w') as f:
163 with open(target_file) as f:
164 WVPASSEQ(f.read(), 'asdf')
166 with atomically_replaced_file(target_file, mode='wb') as f:
167 f.write(os.urandom(20))
168 WVPASSEQ(f.mode, 'wb')
180 def test_utc_offset_str():
181 with no_lingering_errors():
182 tz = environ.get(b'TZ')
186 WVPASSEQ(utc_offset_str(0), b'+0000')
188 WVPASSEQ(utc_offset_str(0), b'-0100')
190 WVPASSEQ(utc_offset_str(0), b'+0100')
192 WVPASSEQ(utc_offset_str(0), b'-0303')
194 WVPASSEQ(utc_offset_str(0), b'+0303')
195 # Offset is not an integer number of minutes
197 WVPASSEQ(utc_offset_str(1), b'-0303')
199 WVPASSEQ(utc_offset_str(1), b'+0303')
200 WVPASSEQ(utc_offset_str(314159), b'+0303')
211 def test_valid_save_name():
212 with no_lingering_errors():
213 valid = helpers.valid_save_name
218 WVFAIL(valid('/foo'))
219 WVFAIL(valid('foo/'))
220 WVFAIL(valid('/foo/'))
221 WVFAIL(valid('foo//bar'))
223 WVFAIL(valid('bar.'))
224 WVFAIL(valid('foo@{'))
225 for x in ' ~^:?*[\\':
226 WVFAIL(valid('foo' + x))
228 WVFAIL(valid('foo' + chr(i)))
229 WVFAIL(valid('foo' + chr(0x7f)))
230 WVFAIL(valid('foo..bar'))
231 WVFAIL(valid('bar.lock/baz'))
232 WVFAIL(valid('foo/bar.lock/baz'))
233 WVFAIL(valid('.bar/baz'))
234 WVFAIL(valid('foo/.bar/baz'))