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.compat import bytes_from_uint, environ, range
9 from bup.helpers import mkdirp
10 from buptest import no_lingering_errors, test_tempdir
16 s += bytes_from_uint(random.randrange(0,256))
28 def test_server_split_with_indexes():
29 with no_lingering_errors():
30 with test_tempdir(b'bup-tclient-') as tmpdir:
31 environ[b'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(b'bup-tclient-') as tmpdir:
50 environ[b'BUP_DIR'] = bupdir = tmpdir
59 WVPASSEQ(len(glob.glob(git.repo(b'objects/pack'+IDX_PAT))), 2)
61 c = client.Client(bupdir, create=True)
62 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 0)
63 rw = c.new_packwriter()
64 s1sha = rw.new_blob(s1)
65 WVPASS(rw.exists(s1sha))
66 s2sha = rw.new_blob(s2)
67 # This is a little hacky, but ensures that we test the
69 while (len(glob.glob(c.cachedir+IDX_PAT)) < 2 and
70 not c.conn.has_input()):
73 WVPASS(rw.objcache.exists(s1sha))
74 WVPASS(rw.objcache.exists(s2sha))
76 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 2)
78 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 3)
82 def test_dumb_client_server():
83 with no_lingering_errors():
84 with test_tempdir(b'bup-tclient-') as tmpdir:
85 environ[b'BUP_DIR'] = bupdir = tmpdir
87 open(git.repo(b'bup-dumb-server'), 'w').close()
93 c = client.Client(bupdir, create=True)
94 rw = c.new_packwriter()
95 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 1)
97 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 1)
100 WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 2)
104 def test_midx_refreshing():
105 with no_lingering_errors():
106 with test_tempdir(b'bup-tclient-') as tmpdir:
107 environ[b'BUP_DIR'] = bupdir = tmpdir
108 git.init_repo(bupdir)
109 c = client.Client(bupdir, create=True)
110 rw = c.new_packwriter()
112 p1base = rw.breakpoint()
113 p1name = os.path.join(c.cachedir, p1base)
114 s1sha = rw.new_blob(s1) # should not be written; it's already in p1
115 s2sha = rw.new_blob(s2)
117 p2name = os.path.join(c.cachedir, p2base)
120 pi = git.PackIdxList(bupdir + b'/objects/pack')
121 WVPASSEQ(len(pi.packs), 2)
123 WVPASSEQ(len(pi.packs), 2)
124 WVPASSEQ(sorted([os.path.basename(i.name) for i in pi.packs]),
125 sorted([p1base, p2base]))
127 p1 = git.open_idx(p1name)
128 WVPASS(p1.exists(s1sha))
129 p2 = git.open_idx(p2name)
130 WVFAIL(p2.exists(s1sha))
131 WVPASS(p2.exists(s2sha))
133 subprocess.call([path.exe(), b'midx', b'-f'])
135 WVPASSEQ(len(pi.packs), 1)
136 pi.refresh(skip_midx=True)
137 WVPASSEQ(len(pi.packs), 2)
138 pi.refresh(skip_midx=False)
139 WVPASSEQ(len(pi.packs), 1)
143 def test_remote_parsing():
144 with no_lingering_errors():
146 (b':/bup', (b'file', None, None, b'/bup')),
147 (b'file:///bup', (b'file', None, None, b'/bup')),
148 (b'192.168.1.1:/bup', (b'ssh', b'192.168.1.1', None, b'/bup')),
149 (b'ssh://192.168.1.1:2222/bup', (b'ssh', b'192.168.1.1', b'2222', b'/bup')),
150 (b'ssh://[ff:fe::1]:2222/bup', (b'ssh', b'ff:fe::1', b'2222', b'/bup')),
151 (b'bup://foo.com:1950', (b'bup', b'foo.com', b'1950', None)),
152 (b'bup://foo.com:1950/bup', (b'bup', b'foo.com', b'1950', b'/bup')),
153 (b'bup://[ff:fe::1]/bup', (b'bup', b'ff:fe::1', None, b'/bup')),)
154 for remote, values in tests:
155 WVPASSEQ(client.parse_remote(remote), values)
157 client.parse_remote(b'http://asdf.com/bup')
159 except client.ClientError: