]> arthur.barton.de Git - bup.git/blob - lib/bup/t/tclient.py
Adjust server and client to accommodate python 3
[bup.git] / lib / bup / t / tclient.py
1
2 from __future__ import absolute_import
3 import sys, os, stat, time, random, subprocess, glob
4
5 from wvtest import *
6
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
11
12
13 def randbytes(sz):
14     s = b''
15     for i in range(sz):
16         s += bytes_from_uint(random.randrange(0,256))
17     return s
18
19
20 s1 = randbytes(10000)
21 s2 = randbytes(10000)
22 s3 = randbytes(10000)
23
24 IDX_PAT = b'/*.idx'
25     
26
27 @wvtest
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
32             git.init_repo(bupdir)
33             lw = git.PackWriter()
34             c = client.Client(bupdir, create=True)
35             rw = c.new_packwriter()
36
37             lw.new_blob(s1)
38             lw.close()
39
40             rw.new_blob(s2)
41             rw.breakpoint()
42             rw.new_blob(s1)
43             rw.close()
44     
45
46 @wvtest
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
51             git.init_repo(bupdir)
52
53             lw = git.PackWriter()
54             lw.new_blob(s1)
55             lw.close()
56             lw = git.PackWriter()
57             lw.new_blob(s2)
58             lw.close()
59             WVPASSEQ(len(glob.glob(git.repo(b'objects/pack'+IDX_PAT))), 2)
60
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
68             # code under test
69             while (len(glob.glob(c.cachedir+IDX_PAT)) < 2 and
70                    not c.conn.has_input()):
71                 pass
72             rw.new_blob(s2)
73             WVPASS(rw.objcache.exists(s1sha))
74             WVPASS(rw.objcache.exists(s2sha))
75             rw.new_blob(s3)
76             WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 2)
77             rw.close()
78             WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 3)
79
80
81 @wvtest
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
86             git.init_repo(bupdir)
87             open(git.repo(b'bup-dumb-server'), 'w').close()
88
89             lw = git.PackWriter()
90             lw.new_blob(s1)
91             lw.close()
92
93             c = client.Client(bupdir, create=True)
94             rw = c.new_packwriter()
95             WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 1)
96             rw.new_blob(s1)
97             WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 1)
98             rw.new_blob(s2)
99             rw.close()
100             WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 2)
101
102
103 @wvtest
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()
111             rw.new_blob(s1)
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)
116             p2base = rw.close()
117             p2name = os.path.join(c.cachedir, p2base)
118             del rw
119
120             pi = git.PackIdxList(bupdir + b'/objects/pack')
121             WVPASSEQ(len(pi.packs), 2)
122             pi.refresh()
123             WVPASSEQ(len(pi.packs), 2)
124             WVPASSEQ(sorted([os.path.basename(i.name) for i in pi.packs]),
125                      sorted([p1base, p2base]))
126
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))
132
133             subprocess.call([path.exe(), b'midx', b'-f'])
134             pi.refresh()
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)
140
141
142 @wvtest
143 def test_remote_parsing():
144     with no_lingering_errors():
145         tests = (
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)
156         try:
157             client.parse_remote(b'http://asdf.com/bup')
158             WVFAIL()
159         except client.ClientError:
160             WVPASS()