]> arthur.ath.cx Git - bup.git/blob - test/ext/test_ftp.py
ftp: clean up error handling
[bup.git] / test / ext / test_ftp.py
1
2 from os import chdir, mkdir, symlink, unlink
3 from subprocess import PIPE
4 from time import localtime, strftime, tzset
5 import re
6
7 from bup.compat import environ
8 from bup.helpers import unlink as unlink_if_exists
9 from buptest import ex, exo
10 from wvpytest import wvfail, wvpass, wvpasseq, wvpassne, wvstart
11 import bup.path
12
13 bup_cmd = bup.path.exe()
14
15 def bup(*args, **kwargs):
16     if 'stdout' not in kwargs:
17         return exo((bup_cmd,) + args, **kwargs)
18     return ex((bup_cmd,) + args, **kwargs)
19
20 def jl(*lines):
21     return b''.join(line + b'\n' for line in lines)
22
23 def match_rx_grp(rx, expected, src):
24     match = re.fullmatch(rx, src)
25     wvpass(match, 're.fullmatch(%r, %r)' % (rx, src))
26     if not match:
27         return
28     wvpasseq(expected, match.groups())
29
30 environ[b'GIT_AUTHOR_NAME'] = b'bup test'
31 environ[b'GIT_COMMITTER_NAME'] = b'bup test'
32 environ[b'GIT_AUTHOR_EMAIL'] = b'bup@a425bc70a02811e49bdf73ee56450e6f'
33 environ[b'GIT_COMMITTER_EMAIL'] = b'bup@a425bc70a02811e49bdf73ee56450e6f'
34
35 def test_ftp(tmpdir):
36     environ[b'BUP_DIR'] = tmpdir + b'/repo'
37     environ[b'GIT_DIR'] = tmpdir + b'/repo'
38     environ[b'TZ'] = b'UTC'
39     tzset()
40
41     chdir(tmpdir)
42     mkdir(b'src')
43     chdir(b'src')
44     mkdir(b'dir')
45     with open(b'file-1', 'wb') as f:
46         f.write(b'excitement!\n')
47     with open(b'dir/file-2', 'wb') as f:
48         f.write(b'more excitement!\n')
49     symlink(b'file-1', b'file-symlink')
50     symlink(b'dir', b'dir-symlink')
51     symlink(b'not-there', b'bad-symlink')
52
53     chdir(tmpdir)    
54     bup(b'init')
55     bup(b'index', b'src')
56     bup(b'save', b'-n', b'src', b'--strip', b'src')
57     save_utc = int(exo((b'git', b'show',
58                         b'-s', b'--format=%at', b'src')).out.strip())
59     save_name = strftime('%Y-%m-%d-%H%M%S', localtime(save_utc)).encode('ascii')
60     
61     wvstart('help')
62     wvpasseq(b'Commands: ls cd pwd cat get mget help quit\n',
63              exo((bup_cmd, b'ftp'), input=b'help\n', stderr=PIPE).out)
64
65     wvstart('pwd/cd')
66     wvpasseq(b'/\n', bup(b'ftp', input=b'pwd\n').out)
67     wvpasseq(b'', bup(b'ftp', input=b'cd src\n').out)
68     wvpasseq(b'/src\n', bup(b'ftp', input=jl(b'cd src', b'pwd')).out)
69     wvpasseq(b'/src\n/\n', bup(b'ftp', input=jl(b'cd src', b'pwd',
70                                                 b'cd ..', b'pwd')).out)
71     wvpasseq(b'/src\n/\n', bup(b'ftp', input=jl(b'cd src', b'pwd',
72                                                 b'cd ..', b'cd ..',
73                                                 b'pwd')).out)
74     wvpasseq(b'/src/%s/dir\n' % save_name,
75              bup(b'ftp', input=jl(b'cd src/latest/dir-symlink', b'pwd')).out)
76     wvpasseq(b'/src/%s/dir\n' % save_name,
77              bup(b'ftp', input=jl(b'cd src latest dir-symlink', b'pwd')).out)
78
79     match_rx_grp(br'(error: path does not exist: /src/)[0-9-]+(/not-there\n/\n)',
80                  (b'error: path does not exist: /src/', b'/not-there\n/\n'),
81                  bup(b'ftp', input=jl(b'cd src/latest/bad-symlink', b'pwd')).out)
82
83     match_rx_grp(br'(error: path does not exist: /src/)[0-9-]+(/not-there\n/\n)',
84                  (b'error: path does not exist: /src/', b'/not-there\n/\n'),
85                  bup(b'ftp', input=jl(b'cd src/latest/not-there', b'pwd')).out)
86
87     wvstart('ls')
88     # FIXME: elaborate
89     wvpasseq(b'src\n', bup(b'ftp', input=b'ls\n').out)
90     wvpasseq(save_name + b'\nlatest\n',
91              bup(b'ftp', input=b'ls src\n').out)
92
93     wvstart('cat')
94     wvpasseq(b'excitement!\n',
95              bup(b'ftp', input=b'cat src/latest/file-1\n').out)
96     wvpasseq(b'excitement!\nmore excitement!\n',
97              bup(b'ftp',
98                  input=b'cat src/latest/file-1 src/latest/dir/file-2\n').out)
99     
100     wvstart('get')
101     bup(b'ftp', input=jl(b'get src/latest/file-1 dest'))
102     with open(b'dest', 'rb') as f:
103         wvpasseq(b'excitement!\n', f.read())
104     unlink(b'dest')
105     bup(b'ftp', input=jl(b'get src/latest/file-symlink dest'))
106     with open(b'dest', 'rb') as f:
107         wvpasseq(b'excitement!\n', f.read())
108     unlink(b'dest')
109
110     match_rx_grp(br'(error: path does not exist: /src/)[0-9-]+(/not-there\n)',
111                  (b'error: path does not exist: /src/', b'/not-there\n'),
112                  bup(b'ftp', input=jl(b'get src/latest/bad-symlink dest')).out)
113
114     match_rx_grp(br'(error: path does not exist: /src/)[0-9-]+(/not-there\n)',
115                  (b'error: path does not exist: /src/', b'/not-there\n'),
116                  bup(b'ftp', input=jl(b'get src/latest/not-there dest')).out)
117
118     wvstart('mget')
119     unlink_if_exists(b'file-1')
120     bup(b'ftp', input=jl(b'mget src/latest/file-1'))
121     with open(b'file-1', 'rb') as f:
122         wvpasseq(b'excitement!\n', f.read())
123     unlink_if_exists(b'file-1')
124     unlink_if_exists(b'file-2')
125     bup(b'ftp', input=jl(b'mget src/latest/file-1 src/latest/dir/file-2'))
126     with open(b'file-1', 'rb') as f:
127         wvpasseq(b'excitement!\n', f.read())
128     with open(b'file-2', 'rb') as f:
129         wvpasseq(b'more excitement!\n', f.read())
130     unlink_if_exists(b'file-symlink')
131     bup(b'ftp', input=jl(b'mget src/latest/file-symlink'))
132     with open(b'file-symlink', 'rb') as f:
133         wvpasseq(b'excitement!\n', f.read())
134     # bup mget currently always does pattern matching
135     bup(b'ftp', input=b'mget src/latest/not-there\n')