2 from __future__ import absolute_import
3 import sys, os, stat, time, random, subprocess, glob
7 from bup import client, git, path
8 from bup.helpers import mkdirp
9 from buptest import no_lingering_errors, test_tempdir
15 s += chr(random.randrange(0,256))
27 def test_server_split_with_indexes():
28 with no_lingering_errors():
29 with test_tempdir('bup-tclient-') as tmpdir:
30 os.environ['BUP_DIR'] = bupdir = tmpdir
33 c = client.Client(bupdir, create=True)
34 rw = c.new_packwriter()
46 def test_multiple_suggestions():
47 with no_lingering_errors():
48 with test_tempdir('bup-tclient-') as tmpdir:
49 os.environ['BUP_DIR'] = bupdir = tmpdir
58 WVPASSEQ(len(glob.glob(git.repo('objects/pack'+IDX_PAT))), 2)
60 c = client.Client(bupdir, create=True)
61 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 0)
62 rw = c.new_packwriter()
63 s1sha = rw.new_blob(s1)
64 WVPASS(rw.exists(s1sha))
65 s2sha = rw.new_blob(s2)
66 # This is a little hacky, but ensures that we test the
68 while (len(glob.glob(c.cachedir+IDX_PAT)) < 2 and
69 not c.conn.has_input()):
72 WVPASS(rw.objcache.exists(s1sha))
73 WVPASS(rw.objcache.exists(s2sha))
75 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 2)
77 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 3)
81 def test_dumb_client_server():
82 with no_lingering_errors():
83 with test_tempdir('bup-tclient-') as tmpdir:
84 os.environ['BUP_DIR'] = bupdir = tmpdir
86 open(git.repo('bup-dumb-server'), 'w').close()
92 c = client.Client(bupdir, create=True)
93 rw = c.new_packwriter()
94 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 1)
96 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 1)
99 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 2)
103 def test_midx_refreshing():
104 with no_lingering_errors():
105 with test_tempdir('bup-tclient-') as tmpdir:
106 os.environ['BUP_DIR'] = bupdir = tmpdir
107 git.init_repo(bupdir)
108 c = client.Client(bupdir, create=True)
109 rw = c.new_packwriter()
111 p1base = rw.breakpoint()
112 p1name = os.path.join(c.cachedir, p1base)
113 s1sha = rw.new_blob(s1) # should not be written; it's already in p1
114 s2sha = rw.new_blob(s2)
116 p2name = os.path.join(c.cachedir, p2base)
119 pi = git.PackIdxList(bupdir + '/objects/pack')
120 WVPASSEQ(len(pi.packs), 2)
122 WVPASSEQ(len(pi.packs), 2)
123 WVPASSEQ(sorted([os.path.basename(i.name) for i in pi.packs]),
124 sorted([p1base, p2base]))
126 p1 = git.open_idx(p1name)
127 WVPASS(p1.exists(s1sha))
128 p2 = git.open_idx(p2name)
129 WVFAIL(p2.exists(s1sha))
130 WVPASS(p2.exists(s2sha))
132 subprocess.call([path.exe(), 'midx', '-f'])
134 WVPASSEQ(len(pi.packs), 1)
135 pi.refresh(skip_midx=True)
136 WVPASSEQ(len(pi.packs), 2)
137 pi.refresh(skip_midx=False)
138 WVPASSEQ(len(pi.packs), 1)
142 def test_remote_parsing():
143 with no_lingering_errors():
145 (':/bup', ('file', None, None, '/bup')),
146 ('file:///bup', ('file', None, None, '/bup')),
147 ('192.168.1.1:/bup', ('ssh', '192.168.1.1', None, '/bup')),
148 ('ssh://192.168.1.1:2222/bup', ('ssh', '192.168.1.1', '2222', '/bup')),
149 ('ssh://[ff:fe::1]:2222/bup', ('ssh', 'ff:fe::1', '2222', '/bup')),
150 ('bup://foo.com:1950', ('bup', 'foo.com', '1950', None)),
151 ('bup://foo.com:1950/bup', ('bup', 'foo.com', '1950', '/bup')),
152 ('bup://[ff:fe::1]/bup', ('bup', 'ff:fe::1', None, '/bup')),)
153 for remote, values in tests:
154 WVPASSEQ(client.parse_remote(remote), values)
156 client.parse_remote('http://asdf.com/bup')
158 except client.ClientError: