2 from __future__ import absolute_import
3 import sys, os, stat, time, random, subprocess, glob
7 from bup import client, git
8 from bup.helpers import mkdirp
9 from buptest import no_lingering_errors, test_tempdir
15 s += chr(random.randrange(0,256))
19 top_dir = os.path.realpath('../../..')
20 bup_exe = top_dir + '/cmd/bup'
30 def test_server_split_with_indexes():
31 with no_lingering_errors():
32 with test_tempdir('bup-tclient-') as tmpdir:
33 os.environ['BUP_MAIN_EXE'] = bup_exe
34 os.environ['BUP_DIR'] = bupdir = tmpdir
37 c = client.Client(bupdir, create=True)
38 rw = c.new_packwriter()
50 def test_multiple_suggestions():
51 with no_lingering_errors():
52 with test_tempdir('bup-tclient-') as tmpdir:
53 os.environ['BUP_MAIN_EXE'] = bup_exe
54 os.environ['BUP_DIR'] = bupdir = tmpdir
63 WVPASSEQ(len(glob.glob(git.repo('objects/pack'+IDX_PAT))), 2)
65 c = client.Client(bupdir, create=True)
66 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 0)
67 rw = c.new_packwriter()
68 s1sha = rw.new_blob(s1)
69 WVPASS(rw.exists(s1sha))
70 s2sha = rw.new_blob(s2)
71 # This is a little hacky, but ensures that we test the
73 while (len(glob.glob(c.cachedir+IDX_PAT)) < 2 and
74 not c.conn.has_input()):
77 WVPASS(rw.objcache.exists(s1sha))
78 WVPASS(rw.objcache.exists(s2sha))
80 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 2)
82 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 3)
86 def test_dumb_client_server():
87 with no_lingering_errors():
88 with test_tempdir('bup-tclient-') as tmpdir:
89 os.environ['BUP_MAIN_EXE'] = bup_exe
90 os.environ['BUP_DIR'] = bupdir = tmpdir
92 open(git.repo('bup-dumb-server'), 'w').close()
98 c = client.Client(bupdir, create=True)
99 rw = c.new_packwriter()
100 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 1)
102 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 1)
105 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 2)
109 def test_midx_refreshing():
110 with no_lingering_errors():
111 with test_tempdir('bup-tclient-') as tmpdir:
112 os.environ['BUP_MAIN_EXE'] = bupmain = '../../../bup'
113 os.environ['BUP_DIR'] = bupdir = tmpdir
114 git.init_repo(bupdir)
115 c = client.Client(bupdir, create=True)
116 rw = c.new_packwriter()
118 p1base = rw.breakpoint()
119 p1name = os.path.join(c.cachedir, p1base)
120 s1sha = rw.new_blob(s1) # should not be written; it's already in p1
121 s2sha = rw.new_blob(s2)
123 p2name = os.path.join(c.cachedir, p2base)
126 pi = git.PackIdxList(bupdir + '/objects/pack')
127 WVPASSEQ(len(pi.packs), 2)
129 WVPASSEQ(len(pi.packs), 2)
130 WVPASSEQ(sorted([os.path.basename(i.name) for i in pi.packs]),
131 sorted([p1base, p2base]))
133 p1 = git.open_idx(p1name)
134 WVPASS(p1.exists(s1sha))
135 p2 = git.open_idx(p2name)
136 WVFAIL(p2.exists(s1sha))
137 WVPASS(p2.exists(s2sha))
139 subprocess.call([bupmain, 'midx', '-f'])
141 WVPASSEQ(len(pi.packs), 1)
142 pi.refresh(skip_midx=True)
143 WVPASSEQ(len(pi.packs), 2)
144 pi.refresh(skip_midx=False)
145 WVPASSEQ(len(pi.packs), 1)
149 def test_remote_parsing():
150 with no_lingering_errors():
152 (':/bup', ('file', None, None, '/bup')),
153 ('file:///bup', ('file', None, None, '/bup')),
154 ('192.168.1.1:/bup', ('ssh', '192.168.1.1', None, '/bup')),
155 ('ssh://192.168.1.1:2222/bup', ('ssh', '192.168.1.1', '2222', '/bup')),
156 ('ssh://[ff:fe::1]:2222/bup', ('ssh', 'ff:fe::1', '2222', '/bup')),
157 ('bup://foo.com:1950', ('bup', 'foo.com', '1950', None)),
158 ('bup://foo.com:1950/bup', ('bup', 'foo.com', '1950', '/bup')),
159 ('bup://[ff:fe::1]/bup', ('bup', 'ff:fe::1', None, '/bup')),)
160 for remote, values in tests:
161 WVPASSEQ(client.parse_remote(remote), values)
163 client.parse_remote('http://asdf.com/bup')
165 except client.ClientError: