Repository navigation
Expand file tree
/
Copy pathtest_error_conditions.py
More file actions
391 lines (310 loc) · 12.8 KB
/
Copy pathtest_error_conditions.py
File metadata and controls
391 lines (310 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import os
import shutil
import yaml
from random import randint
from runner import Runner
import util
import tempfile
import pytest
@pytest.fixture
def runner():
r = Runner()
r.login()
# username = r.username
# create repo (remote and local) and cd into directory
reponame = util.randrepo()
# repopath = f"{username}/{reponame}"
r.runcommand("gin", "create", reponame,
"Test repository for error output. Created with test scripts")
r.reponame = reponame
r.repositories[r.cmdloc] = reponame
r.cdrel(reponame)
r.runcommand("gin", "upload")
r.cdrel("..")
yield r
r.cleanup()
r.logout()
def test_errors(runner):
r = runner
norepoerr = "[error] this command must be run from inside a gin repository"
commands = [
"upload",
"lock",
"unlock",
"get-content",
"remove-content",
]
# Unable to run any git commands outside a git repository
for cmd in commands:
# On directory
out, err = r.runcommand("gin", cmd, ".", exit=False)
assert err == norepoerr
# On specific file
out, err = r.runcommand("gin", cmd, "foobar", exit=False)
assert err == norepoerr
out, err = r.runcommand("gin", "download", exit=False)
assert err == norepoerr
r.cdrel(r.reponame)
# Unable to run any command on file that does not exist
out, err = r.runcommand("gin", "upload", "foobar", exit=False)
assert out.endswith(" Nothing to do")
for cmd in commands[1:]:
out, err = r.runcommand("gin", cmd, "foobar", exit=False)
assert out.endswith("No files matched foobar")
assert err == "[error] 1 operation failed"
# make a few annex and git files
os.mkdir("smallfiles")
r.cdrel("smallfiles")
for idx in range(20):
util.mkrandfile(f"smallfile-{idx:03}", 20)
r.cdrel("..")
os.mkdir("datafiles")
r.cdrel("datafiles")
for idx in range(5):
util.mkrandfile(f"datafile-{idx:03}", 2000)
r.cdrel("..")
# Nothing to do on lock, unlock, getc, rmc on untracked file(s)
nothingmsg = " Nothing to do"
for cmd in commands[1:]:
out, err = r.runcommand("gin", cmd, "smallfiles", exit=False)
assert out.endswith(nothingmsg)
assert not err
out, err = r.runcommand("gin", cmd, "datafiles", exit=False)
assert out.endswith(nothingmsg)
assert not err
out, err = r.runcommand("gin", cmd, "datafiles/*", exit=False)
assert out.endswith(nothingmsg)
assert not err
out, err = r.runcommand("gin", "upload", "smallfiles")
assert out, "Expected output, got nothing"
assert not err
# Nothing to do on lock, unlock, getc, rmc on non-annexed file(s)
for cmd in commands[1:]:
out, err = r.runcommand("gin", cmd, "smallfiles", exit=False)
assert out.endswith(nothingmsg)
assert not err
out, err = r.runcommand("gin", "upload", "datafiles")
# unlock, commit, modify, and lock before commit
r.runcommand("gin", "unlock", "datafiles")
r.runcommand("gin", "annex", "sync")
# r.runcommand("gin", "commit", "datafiles")
r.runcommand("ls", "-l", "datafiles")
# modify the files
for idx in range(3):
util.mkrandfile(os.path.join("datafiles", f"datafile-{idx:03}"), 2000)
out, err = r.runcommand("gin", "lock", "datafiles", exit=False)
assert "Locking this file would discard" in out
assert err == "[error] 3 operations failed"
r.runcommand("gin", "upload", ".")
r.runcommand("gin", "lock", ".")
r.runcommand("gin", "rmc", "datafiles")
# change git repo remote address/port and test get-content failure
out, err = r.runcommand("git", "remote", "-v")
name, address, *_ = out.split()
badaddress = "ssh://git@bad-hostname:22"
r.runcommand("git", "remote", "set-url", name, badaddress)
out, err = r.runcommand("gin", "get-content", "datafiles", exit=False)
assert err, "Expected error, got nothing"
# count output lines with failed message
outlines = out.splitlines()
failmsg = "failed: authorisation failed or remote storage unavailable"
nfail = len([ol for ol in outlines if failmsg in ol])
assert nfail == 5
assert err.strip().endswith("[error] 5 operations failed")
# revert remote change
r.runcommand("git", "remote", "set-url", name, address)
# Change gin and git server address:port in config and test failures
goodconfdir = r.env["GIN_CONFIG_DIR"]
badconftemp = tempfile.TemporaryDirectory(prefix="badconf")
badconfdir = os.path.join(badconftemp.name, "conf")
r.env["GIN_CONFIG_DIR"] = badconfdir
shutil.copytree(goodconfdir, badconfdir)
with open(os.path.join(goodconfdir, "config.yml")) as conffile:
confdata = yaml.load(conffile.read(), Loader=yaml.SafeLoader)
confdata["servers"]["test"]["web"]["port"] = 1
confdata["servers"]["test"]["git"]["port"] = 1
with open(os.path.join(badconfdir, "config.yml"), "w") as conffile:
conffile.write(yaml.dump(confdata))
out, err = r.runcommand("gin", "create", "ThisShouldFail", exit=False)
assert err, "Expected error, got nothing"
errmsg = "[error] server refused connection"
assert err == errmsg
# TODO: simulate not-enough-free-space
# Recover good config
r.env["GIN_CONFIG_DIR"] = goodconfdir
# delete all keys (there might be leftovers from aborted tests)
err = ""
while len(err) == 0:
out, err = r.runcommand("gin", "keys", "--delete", "1", exit=False)
# get content should fail now
out, err = r.runcommand("gin", "get-content", "datafiles", exit=False)
assert err, "Expected error, got nothing"
# count output lines with failed message
outlines = out.splitlines()
failmsg = "failed: authorisation failed or remote storage unavailable"
nfail = len([ol for ol in outlines if failmsg in ol])
assert nfail == 5
assert err.strip().endswith("[error] 5 operations failed")
out, err = r.runcommand("gin", "download", exit=False)
assert err, "Expected error, got nothing"
assert err.strip() == "[error] download failed: permission denied"
out, err = r.runcommand("gin", "upload", exit=False)
assert err, "Expected error, got nothing"
assert err.strip() == "[error] 1 operation failed"
outlines = out.splitlines()
assert outlines[-1].strip() == "upload failed: permission denied"
# login to add key
r.login()
# set bad host key and check error
with open(os.path.join(goodconfdir, "known_hosts"), "r+") as hostkeyfile:
goodhostkey = hostkeyfile.read()
badhostkey = goodhostkey.replace("A", "B") # assumes at least one A
hostkeyfile.seek(0)
hostkeyfile.write(badhostkey)
# TODO: Check error messages
out, err = r.runcommand("gin", "download", "--content", exit=False)
assert err, "Expected error, got nothing"
assert err.strip() ==\
"[error] download failed: server key does not match known host key"
out, err = r.runcommand("gin", "get-content", "datafiles", exit=False)
assert err, "Expected error, got nothing"
# count output lines with failed message
outlines = out.splitlines()
failmsg = "failed: authorisation failed or remote storage unavailable"
nfail = len([ol for ol in outlines if failmsg in ol])
assert nfail == 5
assert err.strip().endswith("[error] 5 operations failed")
out, err = r.runcommand("gin", "upload", exit=False)
assert err, "Expected error, got nothing"
assert err.strip() == "[error] 1 operation failed"
outlines = out.splitlines()
assert outlines[-1].strip() ==\
"upload failed: server key does not match known host key"
# login to fix key
r.login()
# Creating repository that already exists
r.cdrel("..")
out, err = r.runcommand("gin", "create", r.reponame, exit=False)
assert err == ("[error] invalid repository name or "
"repository with the same name already exists")
# Creating repository but local directory already exists (non-empty)
anotherrepo = f"gin-test-{randint(0, 9999):04}"
print(f"Creating directory '{anotherrepo}'")
os.mkdir(anotherrepo)
util.mkrandfile(os.path.join(anotherrepo, f"hold"), 20)
out, err = r.runcommand("gin", "create", anotherrepo, exit=False)
assert ("Creating repository" in out
and "OK" in out), f"Failed to create {anotherrepo} on server"
assert out.endswith(
f"Repository download failed.\n'{anotherrepo}' already exists in the "
"current directory and is not empty."
)
r.repositories[r.cmdloc] = anotherrepo
assert err == "[error] 1 operation failed"
out, err = r.runcommand("gin", "repos")
assert anotherrepo in out
print("DONE!")
@pytest.fixture
def orunner():
remoteloc = tempfile.TemporaryDirectory(prefix="gintest-remote")
r = Runner(False)
reponame = util.randrepo()
os.mkdir(reponame)
r.cdrel(reponame)
r.runcommand("gin", "init")
r.runcommand("gin", "add-remote", "--create", "--default",
"origin", f"dir:{remoteloc.name}")
r.reponame = reponame
r.repositories[r.cmdloc] = None
r.runcommand("gin", "upload")
r.cdrel("..")
yield r
r.cleanup()
@pytest.mark.offline
def test_errors_offline(orunner):
r = orunner
norepoerr = "[error] this command must be run from inside a gin repository"
commands = [
"upload",
"lock",
"unlock",
"get-content",
"remove-content",
]
# Unable to run any git commands outside a git repository
for cmd in commands:
# On directory
out, err = r.runcommand("gin", cmd, ".", exit=False)
assert err == norepoerr
# On specific file
out, err = r.runcommand("gin", cmd, "foobar", exit=False)
assert err == norepoerr
out, err = r.runcommand("gin", "download", exit=False)
assert err == norepoerr
r.cdrel(r.reponame)
# Unable to run any command on file that does not exist
out, err = r.runcommand("gin", "upload", "foobar", exit=False)
assert out.endswith(" Nothing to do")
for cmd in commands[1:]:
out, err = r.runcommand("gin", cmd, "foobar", exit=False)
assert out.endswith("No files matched foobar")
assert err == "[error] 1 operation failed"
# make a few annex and git files
os.mkdir("smallfiles")
r.cdrel("smallfiles")
for idx in range(20):
util.mkrandfile(f"smallfile-{idx:03}", 20)
r.cdrel("..")
os.mkdir("datafiles")
r.cdrel("datafiles")
for idx in range(5):
util.mkrandfile(f"datafile-{idx:03}", 2000)
r.cdrel("..")
# Nothing to do on lock, unlock, getc, rmc on untracked file(s)
nothingmsg = " Nothing to do"
for cmd in commands[1:]:
out, err = r.runcommand("gin", cmd, "smallfiles", exit=False)
assert out.endswith(nothingmsg)
assert not err
out, err = r.runcommand("gin", cmd, "datafiles", exit=False)
assert out.endswith(nothingmsg)
assert not err
out, err = r.runcommand("gin", cmd, "datafiles/*", exit=False)
assert out.endswith(nothingmsg)
assert not err
out, err = r.runcommand("gin", "upload", "smallfiles")
assert out, "Expected output, got nothing"
assert not err
# Nothing to do on lock, unlock, getc, rmc on non-annexed file(s)
for cmd in commands[1:]:
out, err = r.runcommand("gin", cmd, "smallfiles", exit=False)
assert out.endswith(nothingmsg)
assert not err
out, err = r.runcommand("gin", "upload", "datafiles")
# unlock, commit, modify, and lock before commit
r.runcommand("gin", "unlock", "datafiles")
r.runcommand("gin", "annex", "sync")
# r.runcommand("gin", "commit", "datafiles")
r.runcommand("ls", "-l", "datafiles")
# modify the files
for idx in range(3):
util.mkrandfile(os.path.join("datafiles", f"datafile-{idx:03}"), 2000)
out, err = r.runcommand("gin", "lock", "datafiles", exit=False)
assert "Locking this file would discard" in out
assert err == "[error] 3 operations failed"
r.runcommand("gin", "upload", ".")
r.runcommand("gin", "lock", ".")
r.runcommand("gin", "rmc", "datafiles")
# change git repo remote address/port and test get-content failure
out, err = r.runcommand("git", "remote", "-v")
name, address, *_ = out.split()
r.runcommand("git", "remote", "set-url", name, "#_not_a_real_path")
out, err = r.runcommand("gin", "get-content", "datafiles", exit=False)
assert err, "Expected error, got nothing"
# count output lines with failed message
outlines = out.splitlines()
failmsg = "failed: authorisation failed or remote storage unavailable"
nfail = len([ol for ol in outlines if failmsg in ol])
assert nfail == 5
assert err.strip().endswith("[error] 5 operations failed")
print("DONE!")