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))
27 def test_server_split_with_indexes():
28 with no_lingering_errors():
29 with test_tempdir('bup-tclient-') as tmpdir:
30 os.environ['BUP_MAIN_EXE'] = '../../../bup'
31 os.environ['BUP_DIR'] = bupdir = tmpdir
34 c = client.Client(bupdir, create=True)
35 rw = c.new_packwriter()
47 def test_multiple_suggestions():
48 with no_lingering_errors():
49 with test_tempdir('bup-tclient-') as tmpdir:
50 os.environ['BUP_MAIN_EXE'] = '../../../bup'
51 os.environ['BUP_DIR'] = bupdir = tmpdir
60 WVPASSEQ(len(glob.glob(git.repo('objects/pack'+IDX_PAT))), 2)
62 c = client.Client(bupdir, create=True)
63 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 0)
64 rw = c.new_packwriter()
65 s1sha = rw.new_blob(s1)
66 WVPASS(rw.exists(s1sha))
67 s2sha = rw.new_blob(s2)
68 # This is a little hacky, but ensures that we test the
70 while (len(glob.glob(c.cachedir+IDX_PAT)) < 2 and
71 not c.conn.has_input()):
74 WVPASS(rw.objcache.exists(s1sha))
75 WVPASS(rw.objcache.exists(s2sha))
77 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 2)
79 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 3)
83 def test_dumb_client_server():
84 with no_lingering_errors():
85 with test_tempdir('bup-tclient-') as tmpdir:
86 os.environ['BUP_MAIN_EXE'] = '../../../bup'
87 os.environ['BUP_DIR'] = bupdir = tmpdir
89 open(git.repo('bup-dumb-server'), 'w').close()
95 c = client.Client(bupdir, create=True)
96 rw = c.new_packwriter()
97 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 1)
99 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 1)
102 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 2)
106 def test_midx_refreshing():
107 with no_lingering_errors():
108 with test_tempdir('bup-tclient-') as tmpdir:
109 os.environ['BUP_MAIN_EXE'] = bupmain = '../../../bup'
110 os.environ['BUP_DIR'] = bupdir = tmpdir
111 git.init_repo(bupdir)
112 c = client.Client(bupdir, create=True)
113 rw = c.new_packwriter()
115 p1base = rw.breakpoint()
116 p1name = os.path.join(c.cachedir, p1base)
117 s1sha = rw.new_blob(s1) # should not be written; it's already in p1
118 s2sha = rw.new_blob(s2)
120 p2name = os.path.join(c.cachedir, p2base)
123 pi = git.PackIdxList(bupdir + '/objects/pack')
124 WVPASSEQ(len(pi.packs), 2)
126 WVPASSEQ(len(pi.packs), 2)
127 WVPASSEQ(sorted([os.path.basename(i.name) for i in pi.packs]),
128 sorted([p1base, p2base]))
130 p1 = git.open_idx(p1name)
131 WVPASS(p1.exists(s1sha))
132 p2 = git.open_idx(p2name)
133 WVFAIL(p2.exists(s1sha))
134 WVPASS(p2.exists(s2sha))
136 subprocess.call([bupmain, 'midx', '-f'])
138 WVPASSEQ(len(pi.packs), 1)
139 pi.refresh(skip_midx=True)
140 WVPASSEQ(len(pi.packs), 2)
141 pi.refresh(skip_midx=False)
142 WVPASSEQ(len(pi.packs), 1)
146 def test_remote_parsing():
147 with no_lingering_errors():
149 (':/bup', ('file', None, None, '/bup')),
150 ('file:///bup', ('file', None, None, '/bup')),
151 ('192.168.1.1:/bup', ('ssh', '192.168.1.1', None, '/bup')),
152 ('ssh://192.168.1.1:2222/bup', ('ssh', '192.168.1.1', '2222', '/bup')),
153 ('ssh://[ff:fe::1]:2222/bup', ('ssh', 'ff:fe::1', '2222', '/bup')),
154 ('bup://foo.com:1950', ('bup', 'foo.com', '1950', None)),
155 ('bup://foo.com:1950/bup', ('bup', 'foo.com', '1950', '/bup')),
156 ('bup://[ff:fe::1]/bup', ('bup', 'ff:fe::1', None, '/bup')),)
157 for remote, values in tests:
158 WVPASSEQ(client.parse_remote(remote), values)
160 client.parse_remote('http://asdf.com/bup')
162 except client.ClientError: